1 package org.apache.archiva.repository;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.configuration.ArchivaConfiguration;
23 import org.apache.archiva.configuration.ConfigurationNames;
24 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
26 import org.apache.archiva.redback.components.registry.Registry;
27 import org.apache.archiva.redback.components.registry.RegistryListener;
28 import org.springframework.context.ApplicationContext;
29 import org.springframework.stereotype.Service;
31 import javax.annotation.PostConstruct;
32 import javax.inject.Inject;
33 import java.util.List;
35 import java.util.concurrent.ConcurrentHashMap;
38 * RepositoryContentRequest
40 @Service( "repositoryContentFactory#default" )
41 public class RepositoryContentFactory
42 implements RegistryListener
48 private ArchivaConfiguration archivaConfiguration;
51 private ApplicationContext applicationContext;
54 private List<RepositoryContentProvider> repositoryContentProviders;
56 private final Map<String, ManagedRepositoryContent> managedContentMap;
58 private final Map<String, RemoteRepositoryContent> remoteContentMap;
61 public RepositoryContentFactory( )
63 managedContentMap = new ConcurrentHashMap<String, ManagedRepositoryContent>( );
64 remoteContentMap = new ConcurrentHashMap<String, RemoteRepositoryContent>( );
68 * Get the ManagedRepositoryContent object for the repository Id specified.
70 * @param repoId the repository id to fetch.
71 * @return the ManagedRepositoryContent object associated with the repository id.
72 * @throws RepositoryNotFoundException if the repository id does not exist within the configuration.
73 * @throws RepositoryException the repository content object cannot be loaded due to configuration issue.
75 public ManagedRepositoryContent getManagedRepositoryContent( String repoId )
76 throws RepositoryException
78 ManagedRepositoryContent repo = managedContentMap.get( repoId );
86 throw new RepositoryNotFoundException(
87 "Unable to find managed repository configuration for id " + repoId );
92 private RepositoryContentProvider getProvider(final String layout, final RepositoryType repoType) throws RepositoryException
94 return repositoryContentProviders.stream().filter(p->p.supports( repoType ) && p.supportsLayout( layout )).
95 findFirst().orElseThrow( ( ) -> new RepositoryException( "Could not find content provider for repository type "+repoType+" and layout "+layout ) );
98 public ManagedRepositoryContent getManagedRepositoryContent( org.apache.archiva.repository.ManagedRepository mRepo )
99 throws RepositoryException
101 final String id = mRepo.getId();
102 ManagedRepositoryContent content = managedContentMap.get( id );
104 if ( content != null && content.getRepository()==mRepo)
109 RepositoryContentProvider contentProvider = getProvider( mRepo.getLayout( ), mRepo.getType( ) );
110 content = contentProvider.createManagedContent( mRepo );
112 throw new RepositoryException( "Could not create repository content instance for "+mRepo.getId() );
114 ManagedRepositoryContent previousContent = managedContentMap.put( id, content );
115 if (previousContent!=null) {
116 previousContent.setRepository( null );
122 public RemoteRepositoryContent getRemoteRepositoryContent( String repoId )
123 throws RepositoryException
125 RemoteRepositoryContent repo = remoteContentMap.get( repoId );
133 throw new RepositoryNotFoundException(
134 "Unable to find remote repository configuration for id:" + repoId );
139 public RemoteRepositoryContent getRemoteRepositoryContent( RemoteRepository mRepo )
140 throws RepositoryException
142 final String id = mRepo.getId();
143 RemoteRepositoryContent content = remoteContentMap.get( id );
145 if ( content != null && content.getRepository()==mRepo)
150 RepositoryContentProvider contentProvider = getProvider( mRepo.getLayout( ), mRepo.getType( ) );
151 content = contentProvider.createRemoteContent( mRepo );
153 throw new RepositoryException( "Could not create repository content instance for "+mRepo.getId() );
155 RemoteRepositoryContent previousContent = remoteContentMap.put( id, content );
156 if (previousContent!=null) {
157 previousContent.setRepository( null );
164 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
166 if ( ConfigurationNames.isManagedRepositories( propertyName ) || ConfigurationNames.isRemoteRepositories(
174 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
180 public void initialize( )
182 archivaConfiguration.addChangeListener( this );
185 private void initMaps( )
187 // olamy we use concurent so no need of synchronize
188 //synchronized ( managedContentMap )
190 managedContentMap.clear( );
193 //synchronized ( remoteContentMap )
195 remoteContentMap.clear( );
199 public ArchivaConfiguration getArchivaConfiguration( )
201 return archivaConfiguration;
204 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
206 this.archivaConfiguration = archivaConfiguration;
209 public void setRepositoryContentProviders(List<RepositoryContentProvider> providers) {
210 this.repositoryContentProviders = providers;
213 public List<RepositoryContentProvider> getRepositoryContentProviders() {
214 return repositoryContentProviders;