1 package org.apache.archiva.metadata.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.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.model.Dependency;
24 import org.apache.archiva.metadata.model.ProjectMetadata;
25 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
26 import org.apache.archiva.metadata.model.ProjectVersionReference;
27 import org.apache.archiva.metadata.repository.filter.ExcludesFilter;
28 import org.apache.archiva.metadata.repository.storage.RepositoryStorage;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import java.util.ArrayList;
33 import java.util.Collection;
36 * @plexus.component role="org.apache.archiva.metadata.repository.MetadataResolver"
38 public class DefaultMetadataResolver
39 implements MetadataResolver
44 private MetadataRepository metadataRepository;
47 * FIXME: this needs to be configurable based on storage type - and could also be instantiated per repo. Change to a
50 * TODO: Also need to accommodate availability of proxy module
51 * ... could be a different type since we need methods to modify the storage metadata, which would also allow more
52 * appropriate methods to pass in the already determined repository configuration, for example, instead of the ID
54 * @plexus.requirement role-hint="maven2"
56 private RepositoryStorage repositoryStorage;
58 private static final Logger log = LoggerFactory.getLogger( DefaultMetadataResolver.class );
60 public ProjectVersionMetadata resolveProjectVersion( String repoId, String namespace, String projectId,
61 String projectVersion )
62 throws MetadataResolutionException
64 ProjectVersionMetadata metadata = metadataRepository.getProjectVersion( repoId, namespace, projectId,
66 // TODO: do we want to detect changes as well by comparing timestamps? isProjectVersionNewerThan(updated)
67 // in such cases we might also remove/update stale metadata, including adjusting plugin-based facets
68 // This would also be better than checking for completeness - we can then refresh only when fixed (though
69 // sometimes this has an additional dependency - such as a parent - requesting the user to force an update
70 // may then work here and be more efficient than always trying again)
71 if ( metadata == null || metadata.isIncomplete() )
73 metadata = repositoryStorage.readProjectVersionMetadata( repoId, namespace, projectId, projectVersion );
74 if ( metadata != null )
76 if ( log.isDebugEnabled() )
78 log.debug( "Resolved project version metadata from storage: " + metadata );
80 // FIXME: make this a more generic post-processing that plugins can take advantage of
81 // eg. maven projects should be able to process parent here
82 if ( !metadata.getDependencies().isEmpty() )
84 ProjectVersionReference ref = new ProjectVersionReference();
85 ref.setNamespace( namespace );
86 ref.setProjectId( projectId );
87 ref.setProjectVersion( projectVersion );
88 ref.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
89 for ( Dependency dependency : metadata.getDependencies() )
93 metadataRepository.updateProjectReference( repoId, dependency.getGroupId(),
94 dependency.getArtifactId(),
95 dependency.getVersion(), ref );
97 catch ( MetadataRepositoryException e )
99 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
105 metadataRepository.updateProjectVersion( repoId, namespace, projectId, metadata );
107 catch ( MetadataRepositoryException e )
109 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
116 public Collection<ProjectVersionReference> resolveProjectReferences( String repoId, String namespace,
117 String projectId, String projectVersion )
118 throws MetadataResolutionException
120 // TODO: is this assumption correct? could a storage mech. actually know all references in a non-Maven scenario?
121 // not passed to the storage mechanism as resolving references would require iterating all artifacts
122 return metadataRepository.getProjectReferences( repoId, namespace, projectId, projectVersion );
125 public Collection<String> resolveRootNamespaces( String repoId )
126 throws MetadataResolutionException
128 Collection<String> namespaces = metadataRepository.getRootNamespaces( repoId );
129 Collection<String> storageNamespaces = repositoryStorage.listRootNamespaces( repoId, new ExcludesFilter<String>(
131 if ( storageNamespaces != null && !storageNamespaces.isEmpty() )
133 if ( log.isDebugEnabled() )
135 log.debug( "Resolved root namespaces from storage: " + storageNamespaces );
137 for ( String n : storageNamespaces )
141 metadataRepository.updateNamespace( repoId, n );
143 catch ( MetadataRepositoryException e )
145 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
148 namespaces = new ArrayList<String>( namespaces );
149 namespaces.addAll( storageNamespaces );
154 public Collection<String> resolveNamespaces( String repoId, String namespace )
155 throws MetadataResolutionException
157 Collection<String> namespaces = metadataRepository.getNamespaces( repoId, namespace );
158 Collection<String> exclusions = new ArrayList<String>( namespaces );
159 exclusions.addAll( metadataRepository.getProjects( repoId, namespace ) );
160 Collection<String> storageNamespaces = repositoryStorage.listNamespaces( repoId, namespace,
161 new ExcludesFilter<String>(
163 if ( storageNamespaces != null && !storageNamespaces.isEmpty() )
165 if ( log.isDebugEnabled() )
167 log.debug( "Resolved namespaces from storage: " + storageNamespaces );
169 for ( String n : storageNamespaces )
173 metadataRepository.updateNamespace( repoId, namespace + "." + n );
175 catch ( MetadataRepositoryException e )
177 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
180 namespaces = new ArrayList<String>( namespaces );
181 namespaces.addAll( storageNamespaces );
186 public Collection<String> resolveProjects( String repoId, String namespace )
187 throws MetadataResolutionException
189 Collection<String> projects = metadataRepository.getProjects( repoId, namespace );
190 Collection<String> exclusions = new ArrayList<String>( projects );
191 exclusions.addAll( metadataRepository.getNamespaces( repoId, namespace ) );
192 Collection<String> storageProjects = repositoryStorage.listProjects( repoId, namespace,
193 new ExcludesFilter<String>( exclusions ) );
194 if ( storageProjects != null && !storageProjects.isEmpty() )
196 if ( log.isDebugEnabled() )
198 log.debug( "Resolved projects from storage: " + storageProjects );
200 for ( String projectId : storageProjects )
202 ProjectMetadata projectMetadata = repositoryStorage.readProjectMetadata( repoId, namespace, projectId );
203 if ( projectMetadata != null )
207 metadataRepository.updateProject( repoId, projectMetadata );
209 catch ( MetadataRepositoryException e )
211 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
215 projects = new ArrayList<String>( projects );
216 projects.addAll( storageProjects );
221 public Collection<String> resolveProjectVersions( String repoId, String namespace, String projectId )
222 throws MetadataResolutionException
224 Collection<String> projectVersions = metadataRepository.getProjectVersions( repoId, namespace, projectId );
225 Collection<String> storageProjectVersions = repositoryStorage.listProjectVersions( repoId, namespace, projectId,
226 new ExcludesFilter<String>(
228 if ( storageProjectVersions != null && !storageProjectVersions.isEmpty() )
230 if ( log.isDebugEnabled() )
232 log.debug( "Resolved project versions from storage: " + storageProjectVersions );
234 for ( String projectVersion : storageProjectVersions )
238 ProjectVersionMetadata versionMetadata = repositoryStorage.readProjectVersionMetadata( repoId,
242 if ( versionMetadata != null )
244 metadataRepository.updateProjectVersion( repoId, namespace, projectId, versionMetadata );
247 catch ( MetadataResolutionException e )
249 log.warn( "Not update project in metadata repository due to an error resolving it from storage: " +
252 catch ( MetadataRepositoryException e )
254 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
257 projectVersions = new ArrayList<String>( projectVersions );
258 projectVersions.addAll( storageProjectVersions );
260 return projectVersions;
263 public Collection<ArtifactMetadata> resolveArtifacts( String repoId, String namespace, String projectId,
264 String projectVersion )
265 throws MetadataResolutionException
267 Collection<ArtifactMetadata> artifacts = metadataRepository.getArtifacts( repoId, namespace, projectId,
269 Collection<ArtifactMetadata> storageArtifacts = repositoryStorage.readArtifactsMetadata( repoId, namespace,
272 new ExcludesFilter<String>(
273 createArtifactIdList(
275 if ( storageArtifacts != null && !storageArtifacts.isEmpty() )
277 if ( log.isDebugEnabled() )
279 log.debug( "Resolved artifacts from storage: " + storageArtifacts );
281 for ( ArtifactMetadata artifact : storageArtifacts )
285 metadataRepository.updateArtifact( repoId, namespace, projectId, projectVersion, artifact );
287 catch ( MetadataRepositoryException e )
289 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
292 artifacts = new ArrayList<ArtifactMetadata>( artifacts );
293 artifacts.addAll( storageArtifacts );
298 private Collection<String> createArtifactIdList( Collection<ArtifactMetadata> artifacts )
300 Collection<String> artifactIds = new ArrayList<String>();
301 for ( ArtifactMetadata artifact : artifacts )
303 artifactIds.add( artifact.getId() );