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.common.artifact.managed.ManagedArtifact;
24 import org.apache.maven.archiva.common.artifact.managed.ManagedArtifactTypes;
25 import org.apache.maven.archiva.common.artifact.managed.ManagedEjbArtifact;
26 import org.apache.maven.archiva.common.artifact.managed.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.archiva.discoverer.DiscovererStatistics;
32 import org.apache.maven.archiva.scheduler.executors.DataRefreshExecutor;
33 import org.apache.maven.artifact.Artifact;
34 import org.apache.maven.artifact.factory.ArtifactFactory;
35 import org.apache.maven.artifact.repository.ArtifactRepository;
36 import org.apache.maven.model.Model;
37 import org.apache.maven.project.MavenProject;
38 import org.apache.maven.project.MavenProjectBuilder;
39 import org.apache.maven.project.ProjectBuildingException;
40 import org.codehaus.plexus.cache.Cache;
41 import org.codehaus.plexus.logging.AbstractLogEnabled;
42 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
43 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
44 import org.codehaus.plexus.registry.Registry;
45 import org.codehaus.plexus.registry.RegistryListener;
48 import java.io.FileNotFoundException;
49 import java.io.IOException;
50 import java.util.Iterator;
51 import java.util.List;
54 * DefaultActiveManagedRepositories
56 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
58 * @plexus.component role="org.apache.maven.archiva.repositories.ActiveManagedRepositories"
60 public class DefaultActiveManagedRepositories
61 extends AbstractLogEnabled
62 implements ActiveManagedRepositories, Initializable, RegistryListener
65 * @plexus.requirement role-hint="artifactCache"
67 private Cache artifactCache;
72 private ArtifactFactory artifactFactory;
77 private ArchivaConfiguration archivaConfiguration;
82 private MavenProjectBuilder projectBuilder;
85 * @plexus.requirement role-hint="projectCache"
87 private Cache projectCache;
92 private ConfiguredRepositoryFactory repositoryFactory;
94 private Configuration configuration;
96 private ArtifactRepository localRepository;
98 private List repositories;
100 public Artifact createRelatedArtifact( Artifact artifact, String classifier, String type )
102 String groupId = artifact.getGroupId();
103 String artifactId = artifact.getArtifactId();
104 String version = artifact.getVersion();
105 String reltype = StringUtils.defaultIfEmpty( type, artifact.getType() );
106 return artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, reltype, classifier );
109 public ManagedArtifact findArtifact( Artifact artifact )
111 ManagedArtifact managedArtifact = (ManagedArtifact) artifactCache.get( toKey( artifact ) );
113 if ( managedArtifact != null )
115 return managedArtifact;
118 boolean snapshot = artifact.isSnapshot();
120 for ( Iterator i = repositories.iterator(); i.hasNext(); )
122 ArtifactRepository repository = (ArtifactRepository) i.next();
123 if ( snapshot && !repository.getSnapshots().isEnabled() )
129 String path = repository.pathOf( artifact );
130 File f = new File( repository.getBasedir(), path );
134 managedArtifact = createManagedArtifact( repository, artifact, f );
136 artifactCache.put( toKey( artifact ), managedArtifact );
138 return managedArtifact;
145 public ManagedArtifact findArtifact( String groupId, String artifactId, String version )
146 throws ProjectBuildingException
148 MavenProject project = findProject( groupId, artifactId, version );
149 Model model = project.getModel();
151 return findArtifact( model.getGroupId(), model.getArtifactId(), model.getVersion(), model.getPackaging() );
154 public ManagedArtifact findArtifact( String groupId, String artifactId, String version, String type )
156 Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, type );
157 return findArtifact( artifact );
160 public MavenProject findProject( String groupId, String artifactId, String version )
161 throws ProjectBuildingException
163 MavenProject project = (MavenProject) projectCache.get( toKey( groupId, artifactId, version ) );
165 if ( project != null )
170 Artifact projectArtifact = artifactFactory.createProjectArtifact( groupId, artifactId, version );
172 project = projectBuilder.buildFromRepository( projectArtifact, repositories, localRepository );
174 projectCache.put( toKey( groupId, artifactId, version ), project );
179 public ArtifactRepository getArtifactRepository( String id )
181 RepositoryConfiguration repoConfig = getRepositoryConfiguration( id );
182 if ( repoConfig == null )
187 return repositoryFactory.createRepository( repoConfig );
190 public List getAllArtifactRepositories()
192 return repositoryFactory.createRepositories( configuration );
195 public RepositoryConfiguration getRepositoryConfiguration( String id )
197 return this.configuration.getRepositoryById( id );
200 public void initialize()
201 throws InitializationException
203 Configuration config = archivaConfiguration.getConfiguration();
204 archivaConfiguration.addChangeListener( this );
205 configureSelf( config );
208 private String toKey( Artifact artifact )
210 if ( artifact == null )
215 return toKey( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
218 private String toKey( String groupId, String artifactId, String version )
220 return groupId + ":" + artifactId + ":" + version;
223 private void configureSelf( Configuration config )
225 this.configuration = config;
226 this.artifactCache.clear();
227 this.projectCache.clear();
229 repositories = repositoryFactory.createRepositories( this.configuration );
230 localRepository = repositoryFactory.createLocalRepository( this.configuration );
234 private ManagedArtifact createManagedArtifact( ArtifactRepository repository, Artifact artifact, File f )
236 artifact.isSnapshot();
237 String path = repository.pathOf( artifact );
239 switch ( ManagedArtifactTypes.whichType( artifact.getType() ) )
241 case ManagedArtifactTypes.EJB:
242 ManagedEjbArtifact managedEjbArtifact = new ManagedEjbArtifact( repository.getId(), artifact, path );
244 managedEjbArtifact.setJavadocPath( pathToRelatedArtifact( repository, artifact, "javadoc", "jar" ) );
245 managedEjbArtifact.setSourcesPath( pathToRelatedArtifact( repository, artifact, "sources", "jar" ) );
246 managedEjbArtifact.setClientPath( pathToRelatedArtifact( repository, artifact, "client", "jar" ) );
248 return managedEjbArtifact;
250 case ManagedArtifactTypes.JAVA:
251 ManagedJavaArtifact managedJavaArtifact = new ManagedJavaArtifact( repository.getId(), artifact, path );
253 managedJavaArtifact.setJavadocPath( pathToRelatedArtifact( repository, artifact, "javadoc", "jar" ) );
254 managedJavaArtifact.setSourcesPath( pathToRelatedArtifact( repository, artifact, "sources", "jar" ) );
256 return managedJavaArtifact;
258 case ManagedArtifactTypes.GENERIC:
260 return new ManagedArtifact( repository.getId(), artifact, path );
264 private String pathToRelatedArtifact( ArtifactRepository repository, Artifact artifact, String classifier,
267 Artifact relatedArtifact = createRelatedArtifact( artifact, classifier, type );
269 relatedArtifact.isSnapshot();
270 String path = repository.pathOf( relatedArtifact );
272 File relatedFile = new File( repository.getBasedir(), path );
273 if ( !relatedFile.exists() )
275 // Return null to set the ManagedArtifact related path to empty.
282 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
287 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
289 if ( propertyName.startsWith( "repositories" ) || propertyName.startsWith( "localRepository" ) )
292 "Triggering managed repository configuration change with " + propertyName + " set to "
294 configureSelf( archivaConfiguration.getConfiguration() );
298 getLogger().debug( "Not triggering managed repository configuration change with " + propertyName );
302 public long getLastDataRefreshTime()
304 long lastDataRefreshTime = 0;
306 for ( Iterator i = getAllArtifactRepositories().iterator(); i.hasNext(); )
308 ArtifactRepository repository = (ArtifactRepository) i.next();
310 DiscovererStatistics stats = new DiscovererStatistics( repository );
313 stats.load( DataRefreshExecutor.DATAREFRESH_FILE );
314 if ( stats.getTimestampFinished() > lastDataRefreshTime )
316 lastDataRefreshTime = stats.getTimestampFinished();
319 catch ( FileNotFoundException e)
322 "No previous datarefresh timestamp available, as "
323 + DataRefreshExecutor.DATAREFRESH_FILE + " has never been generated." );
325 catch ( IOException e )
328 "Unable to load " + DataRefreshExecutor.DATAREFRESH_FILE
329 + " to determine last refresh timestamp: " + e.getMessage(), e );
333 return lastDataRefreshTime;
336 public boolean needsDataRefresh()
338 for ( Iterator i = getAllArtifactRepositories().iterator(); i.hasNext(); )
340 ArtifactRepository repository = (ArtifactRepository) i.next();
342 DiscovererStatistics stats = new DiscovererStatistics( repository );
343 if ( stats.getTimestampFinished() <= 0 )
345 // Found a repository that has NEVER had it's data walked.