You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RepositoryContentFactory.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package org.apache.archiva.repository;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.configuration.ArchivaConfiguration;
  21. import org.apache.archiva.configuration.ConfigurationNames;
  22. import org.apache.archiva.redback.components.registry.Registry;
  23. import org.apache.archiva.redback.components.registry.RegistryListener;
  24. import org.springframework.context.ApplicationContext;
  25. import org.springframework.stereotype.Service;
  26. import javax.annotation.PostConstruct;
  27. import javax.inject.Inject;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.concurrent.ConcurrentHashMap;
  31. /**
  32. * RepositoryContentRequest
  33. */
  34. @Service( "repositoryContentFactory#default" )
  35. public class RepositoryContentFactory
  36. implements RegistryListener
  37. {
  38. /**
  39. *
  40. */
  41. @Inject
  42. private ArchivaConfiguration archivaConfiguration;
  43. @Inject
  44. private ApplicationContext applicationContext;
  45. @Inject
  46. private List<RepositoryContentProvider> repositoryContentProviders;
  47. private final Map<String, ManagedRepositoryContent> managedContentMap;
  48. private final Map<String, RemoteRepositoryContent> remoteContentMap;
  49. public RepositoryContentFactory( )
  50. {
  51. managedContentMap = new ConcurrentHashMap<String, ManagedRepositoryContent>( );
  52. remoteContentMap = new ConcurrentHashMap<String, RemoteRepositoryContent>( );
  53. }
  54. /**
  55. * Get the ManagedRepositoryContent object for the repository Id specified.
  56. *
  57. * @param repoId the repository id to fetch.
  58. * @return the ManagedRepositoryContent object associated with the repository id.
  59. * @throws RepositoryNotFoundException if the repository id does not exist within the configuration.
  60. * @throws RepositoryException the repository content object cannot be loaded due to configuration issue.
  61. */
  62. public ManagedRepositoryContent getManagedRepositoryContent( String repoId )
  63. throws RepositoryException
  64. {
  65. ManagedRepositoryContent repo = managedContentMap.get( repoId );
  66. if ( repo != null )
  67. {
  68. return repo;
  69. }
  70. else
  71. {
  72. throw new RepositoryNotFoundException(
  73. "Unable to find managed repository configuration for id " + repoId );
  74. }
  75. }
  76. private RepositoryContentProvider getProvider(final String layout, final RepositoryType repoType) throws RepositoryException
  77. {
  78. return repositoryContentProviders.stream().filter(p->p.supports( repoType ) && p.supportsLayout( layout )).
  79. findFirst().orElseThrow( ( ) -> new RepositoryException( "Could not find content provider for repository type "+repoType+" and layout "+layout ) );
  80. }
  81. public ManagedRepositoryContent getManagedRepositoryContent( org.apache.archiva.repository.ManagedRepository mRepo )
  82. throws RepositoryException
  83. {
  84. final String id = mRepo.getId();
  85. ManagedRepositoryContent content = managedContentMap.get( id );
  86. if ( content != null && content.getRepository()==mRepo)
  87. {
  88. return content;
  89. }
  90. RepositoryContentProvider contentProvider = getProvider( mRepo.getLayout( ), mRepo.getType( ) );
  91. content = contentProvider.createManagedContent( mRepo );
  92. if (content==null) {
  93. throw new RepositoryException( "Could not create repository content instance for "+mRepo.getId() );
  94. }
  95. ManagedRepositoryContent previousContent = managedContentMap.put( id, content );
  96. if (previousContent!=null) {
  97. previousContent.setRepository( null );
  98. }
  99. return content;
  100. }
  101. public RemoteRepositoryContent getRemoteRepositoryContent( String repoId )
  102. throws RepositoryException
  103. {
  104. RemoteRepositoryContent repo = remoteContentMap.get( repoId );
  105. if ( repo != null )
  106. {
  107. return repo;
  108. }
  109. else
  110. {
  111. throw new RepositoryNotFoundException(
  112. "Unable to find remote repository configuration for id:" + repoId );
  113. }
  114. }
  115. public RemoteRepositoryContent getRemoteRepositoryContent( RemoteRepository mRepo )
  116. throws RepositoryException
  117. {
  118. final String id = mRepo.getId();
  119. RemoteRepositoryContent content = remoteContentMap.get( id );
  120. if ( content != null && content.getRepository()==mRepo)
  121. {
  122. return content;
  123. }
  124. RepositoryContentProvider contentProvider = getProvider( mRepo.getLayout( ), mRepo.getType( ) );
  125. content = contentProvider.createRemoteContent( mRepo );
  126. if (content==null) {
  127. throw new RepositoryException( "Could not create repository content instance for "+mRepo.getId() );
  128. }
  129. RemoteRepositoryContent previousContent = remoteContentMap.put( id, content );
  130. if (previousContent!=null) {
  131. previousContent.setRepository( null );
  132. }
  133. return content;
  134. }
  135. @Override
  136. public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  137. {
  138. if ( ConfigurationNames.isManagedRepositories( propertyName ) || ConfigurationNames.isRemoteRepositories(
  139. propertyName ) )
  140. {
  141. initMaps( );
  142. }
  143. }
  144. @Override
  145. public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  146. {
  147. /* do nothing */
  148. }
  149. @PostConstruct
  150. public void initialize( )
  151. {
  152. archivaConfiguration.addChangeListener( this );
  153. }
  154. private void initMaps( )
  155. {
  156. // olamy we use concurent so no need of synchronize
  157. //synchronized ( managedContentMap )
  158. //{
  159. managedContentMap.clear( );
  160. //}
  161. //synchronized ( remoteContentMap )
  162. //{
  163. remoteContentMap.clear( );
  164. //}
  165. }
  166. public ArchivaConfiguration getArchivaConfiguration( )
  167. {
  168. return archivaConfiguration;
  169. }
  170. public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
  171. {
  172. this.archivaConfiguration = archivaConfiguration;
  173. }
  174. public void setRepositoryContentProviders(List<RepositoryContentProvider> providers) {
  175. this.repositoryContentProviders = providers;
  176. }
  177. public List<RepositoryContentProvider> getRepositoryContentProviders() {
  178. return repositoryContentProviders;
  179. }
  180. }