1 package org.apache.maven.archiva.repositories;
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.commons.lang.StringUtils;
23 import org.apache.maven.archiva.artifact.ManagedArtifact;
24 import org.apache.maven.archiva.artifact.ManagedArtifactTypes;
25 import org.apache.maven.archiva.artifact.ManagedEjbArtifact;
26 import org.apache.maven.archiva.artifact.ManagedJavaArtifact;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.Configuration;
29 import org.apache.maven.archiva.configuration.ConfiguredRepositoryFactory;
30 import org.apache.maven.archiva.configuration.RepositoryConfiguration;
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.artifact.factory.ArtifactFactory;
33 import org.apache.maven.artifact.repository.ArtifactRepository;
34 import org.apache.maven.model.Model;
35 import org.apache.maven.project.MavenProject;
36 import org.apache.maven.project.MavenProjectBuilder;
37 import org.apache.maven.project.ProjectBuildingException;
38 import org.codehaus.plexus.cache.Cache;
39 import org.codehaus.plexus.logging.AbstractLogEnabled;
40 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
41 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
42 import org.codehaus.plexus.registry.Registry;
43 import org.codehaus.plexus.registry.RegistryListener;
46 import java.util.Iterator;
47 import java.util.List;
50 * DefaultActiveManagedRepositories
52 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
54 * @plexus.component role="org.apache.maven.archiva.repositories.ActiveManagedRepositories"
56 public class DefaultActiveManagedRepositories
57 extends AbstractLogEnabled
58 implements ActiveManagedRepositories, Initializable, RegistryListener
61 * @plexus.requirement role-hint="artifactCache"
63 private Cache artifactCache;
68 private ArtifactFactory artifactFactory;
73 private ArchivaConfiguration archivaConfiguration;
78 private MavenProjectBuilder projectBuilder;
81 * @plexus.requirement role-hint="projectCache"
83 private Cache projectCache;
88 private ConfiguredRepositoryFactory repositoryFactory;
90 private Configuration configuration;
92 private ArtifactRepository localRepository;
94 private List repositories;
96 public Artifact createRelatedArtifact( Artifact artifact, String classifier, String type )
98 String groupId = artifact.getGroupId();
99 String artifactId = artifact.getArtifactId();
100 String version = artifact.getVersion();
101 String reltype = StringUtils.defaultIfEmpty( type, artifact.getType() );
102 return artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, reltype, classifier );
105 public ManagedArtifact findArtifact( Artifact artifact )
107 ManagedArtifact managedArtifact = (ManagedArtifact) artifactCache.get( toKey( artifact ) );
109 if ( managedArtifact != null )
111 return managedArtifact;
114 boolean snapshot = artifact.isSnapshot();
116 for ( Iterator i = repositories.iterator(); i.hasNext(); )
118 ArtifactRepository repository = (ArtifactRepository) i.next();
119 if ( snapshot && !repository.getSnapshots().isEnabled() )
125 String path = repository.pathOf( artifact );
126 File f = new File( repository.getBasedir(), path );
130 managedArtifact = createManagedArtifact( repository, artifact, f );
132 artifactCache.put( toKey( artifact ), managedArtifact );
134 return managedArtifact;
141 public ManagedArtifact findArtifact( String groupId, String artifactId, String version )
142 throws ProjectBuildingException
144 MavenProject project = findProject( groupId, artifactId, version );
145 Model model = project.getModel();
147 return findArtifact( model.getGroupId(), model.getArtifactId(), model.getVersion(), model.getPackaging() );
150 public ManagedArtifact findArtifact( String groupId, String artifactId, String version, String type )
152 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, type );
153 return findArtifact( artifact );
156 public MavenProject findProject( String groupId, String artifactId, String version )
157 throws ProjectBuildingException
159 MavenProject project = (MavenProject) projectCache.get( toKey( groupId, artifactId, version ) );
161 if ( project != null )
166 Artifact projectArtifact = artifactFactory.createProjectArtifact( groupId, artifactId, version );
168 project = projectBuilder.buildFromRepository( projectArtifact, repositories, localRepository );
170 projectCache.put( toKey( groupId, artifactId, version ), project );
175 public ArtifactRepository getArtifactRepository( String id )
177 RepositoryConfiguration repoConfig = getRepositoryConfiguration( id );
178 if ( repoConfig == null )
183 return repositoryFactory.createRepository( repoConfig );
186 public List getAllArtifactRepositories()
188 return repositoryFactory.createRepositories( configuration );
191 public RepositoryConfiguration getRepositoryConfiguration( String id )
193 return this.configuration.getRepositoryById( id );
196 public void initialize()
197 throws InitializationException
199 Configuration config = archivaConfiguration.getConfiguration();
200 archivaConfiguration.addChangeListener( this );
201 configureSelf( config );
204 private String toKey( Artifact artifact )
206 if ( artifact == null )
211 return toKey( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
214 private String toKey( String groupId, String artifactId, String version )
216 return groupId + ":" + artifactId + ":" + version;
219 private void configureSelf( Configuration config )
221 this.configuration = config;
222 this.artifactCache.clear();
223 this.projectCache.clear();
225 repositories = repositoryFactory.createRepositories( this.configuration );
226 localRepository = repositoryFactory.createLocalRepository( this.configuration );
229 private ManagedArtifact createManagedArtifact( ArtifactRepository repository, Artifact artifact, File f )
231 artifact.isSnapshot();
232 String path = repository.pathOf( artifact );
234 switch ( ManagedArtifactTypes.whichType( artifact.getType() ) )
236 case ManagedArtifactTypes.EJB:
237 ManagedEjbArtifact managedEjbArtifact = new ManagedEjbArtifact( repository.getId(), artifact, path );
239 managedEjbArtifact.setJavadocPath( pathToRelatedArtifact( repository, artifact, "javadoc", "jar" ) );
240 managedEjbArtifact.setSourcesPath( pathToRelatedArtifact( repository, artifact, "sources", "jar" ) );
241 managedEjbArtifact.setClientPath( pathToRelatedArtifact( repository, artifact, "client", "jar" ) );
243 return managedEjbArtifact;
245 case ManagedArtifactTypes.JAVA:
246 ManagedJavaArtifact managedJavaArtifact = new ManagedJavaArtifact( repository.getId(), artifact, path );
248 managedJavaArtifact.setJavadocPath( pathToRelatedArtifact( repository, artifact, "javadoc", "jar" ) );
249 managedJavaArtifact.setSourcesPath( pathToRelatedArtifact( repository, artifact, "sources", "jar" ) );
251 return managedJavaArtifact;
253 case ManagedArtifactTypes.GENERIC:
255 return new ManagedArtifact( repository.getId(), artifact, path );
259 private String pathToRelatedArtifact( ArtifactRepository repository, Artifact artifact, String classifier,
262 Artifact relatedArtifact = createRelatedArtifact( artifact, classifier, type );
264 relatedArtifact.isSnapshot();
265 String path = repository.pathOf( relatedArtifact );
267 File relatedFile = new File( repository.getBasedir(), path );
268 if ( !relatedFile.exists() )
270 // Return null to set the ManagedArtifact related path to empty.
277 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
282 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
284 if ( propertyName.startsWith( "repositories" ) || propertyName.startsWith( "localRepository" ) )
286 getLogger().debug( "Triggering managed repository configuration change with " + propertyName + " set to " +
288 configureSelf( archivaConfiguration.getConfiguration() );
292 getLogger().debug( "Not triggering managed repository configuration change with " + propertyName );