1 package org.apache.maven.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.maven.archiva.configuration.ArchivaConfiguration;
23 import org.apache.maven.archiva.configuration.ConfigurationNames;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
26 import org.codehaus.plexus.PlexusContainer;
27 import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
28 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
29 import org.codehaus.plexus.context.Context;
30 import org.codehaus.plexus.context.ContextException;
31 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
32 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
33 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
34 import org.codehaus.plexus.registry.Registry;
35 import org.codehaus.plexus.registry.RegistryListener;
37 import java.util.HashMap;
41 * RepositoryContentRequest
46 * role="org.apache.maven.archiva.repository.RepositoryContentFactory"
48 public class RepositoryContentFactory
49 implements Contextualizable, RegistryListener, Initializable
54 private ArchivaConfiguration archivaConfiguration;
56 private Map<String, ManagedRepositoryContent> managedContentMap;
58 private Map<String, RemoteRepositoryContent> remoteContentMap;
60 private PlexusContainer container;
63 * Get the ManagedRepositoryContent object for the repository Id specified.
65 * @param repoId the repository id to fetch.
66 * @return the ManagedRepositoryContent object associated with the repository id.
67 * @throws RepositoryNotFoundException if the repository id does not exist within the configuration.
68 * @throws RepositoryException the repository content object cannot be loaded due to configuration issue.
70 public ManagedRepositoryContent getManagedRepositoryContent( String repoId )
71 throws RepositoryNotFoundException, RepositoryException
73 ManagedRepositoryContent repo = managedContentMap.get( repoId );
80 ManagedRepositoryConfiguration repoConfig = archivaConfiguration.getConfiguration()
81 .findManagedRepositoryById( repoId );
82 if ( repoConfig == null )
84 throw new RepositoryNotFoundException( "Unable to find managed repository configuration for id:" + repoId );
89 repo = (ManagedRepositoryContent) container.lookup( ManagedRepositoryContent.class, repoConfig.getLayout() );
90 repo.setRepository( repoConfig );
91 managedContentMap.put( repoId, repo );
93 catch ( ComponentLookupException e )
95 throw new RepositoryException( "Specified layout [" + repoConfig.getLayout()
96 + "] on managed repository id [" + repoId + "] is not valid.", e );
102 public RemoteRepositoryContent getRemoteRepositoryContent( String repoId )
103 throws RepositoryNotFoundException, RepositoryException
105 RemoteRepositoryContent repo = remoteContentMap.get( repoId );
112 RemoteRepositoryConfiguration repoConfig = archivaConfiguration.getConfiguration()
113 .findRemoteRepositoryById( repoId );
114 if ( repoConfig == null )
116 throw new RepositoryNotFoundException( "Unable to find remote repository configuration for id:" + repoId );
121 repo = (RemoteRepositoryContent) container.lookup( RemoteRepositoryContent.class, repoConfig.getLayout() );
122 repo.setRepository( repoConfig );
123 remoteContentMap.put( repoId, repo );
125 catch ( ComponentLookupException e )
127 throw new RepositoryException( "Specified layout [" + repoConfig.getLayout()
128 + "] on remote repository id [" + repoId + "] is not valid.", e );
134 public void contextualize( Context context )
135 throws ContextException
137 container = (PlexusContainer) context.get( "plexus" );
140 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
142 if ( ConfigurationNames.isManagedRepositories( propertyName )
143 || ConfigurationNames.isRemoteRepositories( propertyName ) )
149 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
154 public void initialize()
155 throws InitializationException
157 managedContentMap = new HashMap<String, ManagedRepositoryContent>();
158 remoteContentMap = new HashMap<String, RemoteRepositoryContent>();
160 archivaConfiguration.addChangeListener( this );
163 private void initMaps()
165 synchronized ( managedContentMap )
167 // First, return any references to the container.
168 for ( ManagedRepositoryContent repo : managedContentMap.values() )
172 container.release( repo );
174 catch ( ComponentLifecycleException e )
180 // Next clear the map.
181 managedContentMap.clear();
184 synchronized ( remoteContentMap )
186 // First, return any references to the container.
187 for ( RemoteRepositoryContent repo : remoteContentMap.values() )
191 container.release( repo );
193 catch ( ComponentLifecycleException e )
199 // Next clear the map.
200 remoteContentMap.clear();