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.personality.plexus.lifecycle.phase.Initializable;
40 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
41 import org.codehaus.plexus.registry.Registry;
42 import org.codehaus.plexus.registry.RegistryListener;
45 import java.util.Iterator;
46 import java.util.List;
49 * DefaultActiveManagedRepositories
51 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
53 * @plexus.component role="org.apache.maven.archiva.repositories.ActiveManagedRepositories"
55 public class DefaultActiveManagedRepositories
56 implements ActiveManagedRepositories, Initializable, RegistryListener
59 * @plexus.requirement role-hint="artifactCache"
61 private Cache artifactCache;
66 private ArtifactFactory artifactFactory;
71 private ArchivaConfiguration archivaConfiguration;
76 private MavenProjectBuilder projectBuilder;
79 * @plexus.requirement role-hint="projectCache"
81 private Cache projectCache;
86 private ConfiguredRepositoryFactory repositoryFactory;
88 private Configuration configuration;
90 private ArtifactRepository localRepository;
92 private List repositories;
94 public Artifact createRelatedArtifact( Artifact artifact, String classifier, String type )
96 String groupId = artifact.getGroupId();
97 String artifactId = artifact.getArtifactId();
98 String version = artifact.getVersion();
99 String reltype = StringUtils.defaultIfEmpty( type, artifact.getType() );
100 return artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, reltype, classifier );
103 public ManagedArtifact findArtifact( Artifact artifact )
105 ManagedArtifact managedArtifact = (ManagedArtifact) artifactCache.get( toKey( artifact ) );
107 if ( managedArtifact != null )
109 return managedArtifact;
112 boolean snapshot = artifact.isSnapshot();
114 for ( Iterator i = repositories.iterator(); i.hasNext(); )
116 ArtifactRepository repository = (ArtifactRepository) i.next();
117 if ( snapshot && !repository.getSnapshots().isEnabled() )
123 String path = repository.pathOf( artifact );
124 File f = new File( repository.getBasedir(), path );
128 managedArtifact = createManagedArtifact( repository, artifact, f );
130 artifactCache.put( toKey( artifact ), managedArtifact );
132 return managedArtifact;
139 public ManagedArtifact findArtifact( String groupId, String artifactId, String version )
140 throws ProjectBuildingException
142 MavenProject project = findProject( groupId, artifactId, version );
143 Model model = project.getModel();
145 return findArtifact( model.getGroupId(), model.getArtifactId(), model.getVersion(), model.getPackaging() );
148 public ManagedArtifact findArtifact( String groupId, String artifactId, String version, String type )
150 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, type );
151 return findArtifact( artifact );
154 public MavenProject findProject( String groupId, String artifactId, String version )
155 throws ProjectBuildingException
157 MavenProject project = (MavenProject) projectCache.get( toKey( groupId, artifactId, version ) );
159 if ( project != null )
164 Artifact projectArtifact = artifactFactory.createProjectArtifact( groupId, artifactId, version );
166 project = projectBuilder.buildFromRepository( projectArtifact, repositories, localRepository );
168 projectCache.put( toKey( groupId, artifactId, version ), project );
173 public ArtifactRepository getArtifactRepository( String id )
175 RepositoryConfiguration repoConfig = getRepositoryConfiguration( id );
176 if ( repoConfig == null )
181 return repositoryFactory.createRepository( repoConfig );
184 public RepositoryConfiguration getRepositoryConfiguration( String id )
186 return this.configuration.getRepositoryById( id );
189 public void initialize()
190 throws InitializationException
192 Configuration config = archivaConfiguration.getConfiguration();
193 archivaConfiguration.addChangeListener( this );
194 configureSelf( config );
197 private String toKey( Artifact artifact )
199 if ( artifact == null )
204 return toKey( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
207 private String toKey( String groupId, String artifactId, String version )
209 return groupId + ":" + artifactId + ":" + version;
212 private void configureSelf( Configuration config )
214 this.configuration = config;
215 this.artifactCache.clear();
216 this.projectCache.clear();
218 repositories = repositoryFactory.createRepositories( this.configuration );
219 localRepository = repositoryFactory.createLocalRepository( this.configuration );
222 private ManagedArtifact createManagedArtifact( ArtifactRepository repository, Artifact artifact, File f )
224 artifact.isSnapshot();
225 String path = repository.pathOf( artifact );
227 switch ( ManagedArtifactTypes.whichType( artifact.getType() ) )
229 case ManagedArtifactTypes.EJB:
230 ManagedEjbArtifact managedEjbArtifact = new ManagedEjbArtifact( repository.getId(), artifact, path );
232 managedEjbArtifact.setJavadocPath( pathToRelatedArtifact( repository, artifact, "javadoc", "jar" ) );
233 managedEjbArtifact.setSourcesPath( pathToRelatedArtifact( repository, artifact, "sources", "jar" ) );
234 managedEjbArtifact.setClientPath( pathToRelatedArtifact( repository, artifact, "client", "jar" ) );
236 return managedEjbArtifact;
238 case ManagedArtifactTypes.JAVA:
239 ManagedJavaArtifact managedJavaArtifact = new ManagedJavaArtifact( repository.getId(), artifact, path );
241 managedJavaArtifact.setJavadocPath( pathToRelatedArtifact( repository, artifact, "javadoc", "jar" ) );
242 managedJavaArtifact.setSourcesPath( pathToRelatedArtifact( repository, artifact, "sources", "jar" ) );
244 return managedJavaArtifact;
246 case ManagedArtifactTypes.GENERIC:
248 return new ManagedArtifact( repository.getId(), artifact, path );
252 private String pathToRelatedArtifact( ArtifactRepository repository, Artifact artifact, String classifier,
255 Artifact relatedArtifact = createRelatedArtifact( artifact, classifier, type );
257 relatedArtifact.isSnapshot();
258 String path = repository.pathOf( relatedArtifact );
260 File relatedFile = new File( repository.getBasedir(), path );
261 if ( !relatedFile.exists() )
263 // Return null to set the ManagedArtifact related path to empty.
270 public void notifyOfConfigurationChange( Registry registry )
272 configureSelf( archivaConfiguration.getConfiguration() );