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;
34 import java.util.concurrent.ConcurrentHashMap;
37 * RepositoryContentRequest
39 @Service( "repositoryContentFactory#default" )
40 public class RepositoryContentFactory
41 implements RegistryListener
47 private ArchivaConfiguration archivaConfiguration;
50 private ApplicationContext applicationContext;
52 private final Map<String, ManagedRepositoryContent> managedContentMap;
54 private final Map<String, RemoteRepositoryContent> remoteContentMap;
57 public RepositoryContentFactory( )
59 managedContentMap = new ConcurrentHashMap<String, ManagedRepositoryContent>( );
60 remoteContentMap = new ConcurrentHashMap<String, RemoteRepositoryContent>( );
64 * Get the ManagedRepositoryContent object for the repository Id specified.
66 * @param repoId the repository id to fetch.
67 * @return the ManagedRepositoryContent object associated with the repository id.
68 * @throws RepositoryNotFoundException if the repository id does not exist within the configuration.
69 * @throws RepositoryException the repository content object cannot be loaded due to configuration issue.
71 public ManagedRepositoryContent getManagedRepositoryContent( String repoId )
72 throws RepositoryNotFoundException, RepositoryException
74 ManagedRepositoryContent repo = managedContentMap.get( repoId );
82 throw new RepositoryNotFoundException(
83 "Unable to find managed repository configuration for id " + repoId );
88 public ManagedRepositoryContent getManagedRepositoryContent( ManagedRepositoryConfiguration repoConfig, org.apache.archiva.repository.ManagedRepository mRepo )
89 throws RepositoryNotFoundException, RepositoryException
91 ManagedRepositoryContent repo = managedContentMap.get( repoConfig.getId( ) );
93 if ( repo != null && repo.getRepository()==mRepo)
98 repo = applicationContext.getBean( "managedRepositoryContent#" + repoConfig.getLayout( ),
99 ManagedRepositoryContent.class );
100 repo.setRepository( mRepo );
101 ManagedRepositoryContent previousRepo = managedContentMap.put( repoConfig.getId( ), repo );
102 if (previousRepo!=null) {
103 ManagedRepository previousMRepo = previousRepo.getRepository( );
104 if (previousMRepo!=null && previousMRepo instanceof EditableManagedRepository) {
105 ((EditableManagedRepository)previousMRepo).setContent( null );
107 previousRepo.setRepository( null );
113 public RemoteRepositoryContent getRemoteRepositoryContent( String repoId )
114 throws RepositoryNotFoundException, RepositoryException
116 RemoteRepositoryContent repo = remoteContentMap.get( repoId );
124 throw new RepositoryNotFoundException(
125 "Unable to find remote repository configuration for id:" + repoId );
130 public RemoteRepositoryContent getRemoteRepositoryContent( RemoteRepositoryConfiguration repoConfig, RemoteRepository mRepo )
131 throws RepositoryNotFoundException, RepositoryException
133 RemoteRepositoryContent repo = remoteContentMap.get( repoConfig.getId( ) );
135 if ( repo != null && repo.getRepository()==mRepo)
140 repo = applicationContext.getBean( "remoteRepositoryContent#" + repoConfig.getLayout( ),
141 RemoteRepositoryContent.class );
142 repo.setRepository( mRepo );
143 RemoteRepositoryContent previousRepo = remoteContentMap.put( repoConfig.getId( ), repo );
144 if (previousRepo!=null) {
145 RemoteRepository previousMRepo = previousRepo.getRepository( );
146 if (previousMRepo!=null && previousMRepo instanceof EditableRemoteRepository) {
147 ((EditableRemoteRepository)previousMRepo).setContent( null );
149 previousRepo.setRepository( null );
158 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
160 if ( ConfigurationNames.isManagedRepositories( propertyName ) || ConfigurationNames.isRemoteRepositories(
168 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
174 public void initialize( )
176 archivaConfiguration.addChangeListener( this );
179 private void initMaps( )
181 // olamy we use concurent so no need of synchronize
182 //synchronized ( managedContentMap )
184 managedContentMap.clear( );
187 //synchronized ( remoteContentMap )
189 remoteContentMap.clear( );
193 public ArchivaConfiguration getArchivaConfiguration( )
195 return archivaConfiguration;
198 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
200 this.archivaConfiguration = archivaConfiguration;