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.ProjectMetadata;
24 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
25 import org.apache.archiva.metadata.model.ProjectVersionReference;
26 import org.apache.archiva.metadata.repository.filter.ExcludesFilter;
27 import org.apache.archiva.metadata.repository.storage.RepositoryStorage;
28 import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException;
29 import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException;
30 import org.apache.archiva.repository.events.RepositoryListener;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.stereotype.Service;
36 import javax.annotation.PostConstruct;
37 import javax.inject.Inject;
38 import javax.inject.Named;
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.List;
44 * Default implementation of the metadata resolver API. At present it will handle updating the content repository
45 * from new or changed information in the model and artifacts from the repository storage.
47 * This is a singleton component to allow an alternate implementation to be provided. It is intended to be the same
48 * system-wide for the whole content repository instead of on a per-managed-repository basis. Therefore, the session is
49 * passed in as an argument to obtain any necessary resources, rather than the class being instantiated within the
50 * session in the context of a single managed repository's resolution needs.
52 * Note that the caller is responsible for the session, such as closing and saving (which is implied by the resolver
53 * being obtained from within the session). The {@link RepositorySession#markDirty()} method is used as a hint to ensure
54 * that the session knows we've made changes at close. We cannot ensure the changes will be persisted if the caller
55 * chooses to revert first. This is preferable to storing the metadata immediately - a separate session would require
56 * having a bi-directional link with the session factory, and saving the existing session might save other changes
57 * unknowingly by the caller.
60 @Service( "metadataResolver#default" )
61 public class DefaultMetadataResolver
62 implements MetadataResolver
65 private Logger log = LoggerFactory.getLogger( DefaultMetadataResolver.class );
68 * FIXME: this needs to be configurable based on storage type - and could also be instantiated per repo. Change to a
69 * factory, and perhaps retrieve from the session. We should avoid creating one per request, however.
71 * TODO: Also need to accommodate availability of proxy module
72 * ... could be a different type since we need methods to modify the storage metadata, which would also allow more
73 * appropriate methods to pass in the already determined repository configuration, for example, instead of the ID
75 * plexus.requirement role-hint="maven2"
78 @Named( value = "repositoryStorage#maven2" )
79 private RepositoryStorage repositoryStorage;
82 * plexus.requirement role="org.apache.archiva.repository.events.RepositoryListener"
85 private List<RepositoryListener> listeners;
89 //private ApplicationContext applicationContext;
92 private void initialize()
95 // new ArrayList<RepositoryListener>( applicationContext.getBeansOfType( RepositoryListener.class ).values() );
98 public ProjectVersionMetadata resolveProjectVersion( RepositorySession session, String repoId, String namespace,
99 String projectId, String projectVersion )
100 throws MetadataResolutionException
102 MetadataRepository metadataRepository = session.getRepository();
104 ProjectVersionMetadata metadata =
105 metadataRepository.getProjectVersion( repoId, namespace, projectId, projectVersion );
106 // TODO: do we want to detect changes as well by comparing timestamps? isProjectVersionNewerThan(updated)
107 // in such cases we might also remove/update stale metadata, including adjusting plugin-based facets
108 // This would also be better than checking for completeness - we can then refresh only when fixed (though
109 // sometimes this has an additional dependency - such as a parent - requesting the user to force an update
110 // may then work here and be more efficient than always trying again)
111 if ( metadata == null || metadata.isIncomplete() )
115 metadata = repositoryStorage.readProjectVersionMetadata( repoId, namespace, projectId, projectVersion );
117 if ( log.isDebugEnabled() )
119 log.debug( "Resolved project version metadata from storage: " + metadata );
121 // FIXME: make this a more generic post-processing that plugins can take advantage of
122 // eg. maven projects should be able to process parent here
123 if ( !metadata.getDependencies().isEmpty() )
125 ProjectVersionReference ref = new ProjectVersionReference();
126 ref.setNamespace( namespace );
127 ref.setProjectId( projectId );
128 ref.setProjectVersion( projectVersion );
129 ref.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
133 for ( RepositoryListener listener : listeners )
135 listener.addArtifact( session, repoId, namespace, projectId, metadata );
137 metadataRepository.updateProjectVersion( repoId, namespace, projectId, metadata );
139 catch ( MetadataRepositoryException e )
141 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
146 catch ( RepositoryStorageMetadataInvalidException e )
148 for ( RepositoryListener listener : listeners )
150 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
152 throw new MetadataResolutionException( e.getMessage(), e );
154 catch ( RepositoryStorageMetadataNotFoundException e )
156 for ( RepositoryListener listener : listeners )
158 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
160 // no need to rethrow - return null
166 public Collection<ProjectVersionReference> resolveProjectReferences( RepositorySession session, String repoId,
167 String namespace, String projectId,
168 String projectVersion )
169 throws MetadataResolutionException
171 // TODO: is this assumption correct? could a storage mech. actually know all references in a non-Maven scenario?
172 // not passed to the storage mechanism as resolving references would require iterating all artifacts
173 MetadataRepository metadataRepository = session.getRepository();
174 return metadataRepository.getProjectReferences( repoId, namespace, projectId, projectVersion );
177 public Collection<String> resolveRootNamespaces( RepositorySession session, String repoId )
178 throws MetadataResolutionException
180 MetadataRepository metadataRepository = session.getRepository();
181 Collection<String> namespaces = metadataRepository.getRootNamespaces( repoId );
182 Collection<String> storageNamespaces =
183 repositoryStorage.listRootNamespaces( repoId, new ExcludesFilter<String>( namespaces ) );
184 if ( storageNamespaces != null && !storageNamespaces.isEmpty() )
186 if ( log.isDebugEnabled() )
188 log.debug( "Resolved root namespaces from storage: " + storageNamespaces );
190 for ( String n : storageNamespaces )
194 metadataRepository.updateNamespace( repoId, n );
196 catch ( MetadataRepositoryException e )
198 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
203 namespaces = new ArrayList<String>( namespaces );
204 namespaces.addAll( storageNamespaces );
209 public Collection<String> resolveNamespaces( RepositorySession session, String repoId, String namespace )
210 throws MetadataResolutionException
212 MetadataRepository metadataRepository = session.getRepository();
213 Collection<String> namespaces = metadataRepository.getNamespaces( repoId, namespace );
214 Collection<String> exclusions = new ArrayList<String>( namespaces );
215 exclusions.addAll( metadataRepository.getProjects( repoId, namespace ) );
216 Collection<String> storageNamespaces =
217 repositoryStorage.listNamespaces( repoId, namespace, new ExcludesFilter<String>( exclusions ) );
218 if ( storageNamespaces != null && !storageNamespaces.isEmpty() )
220 if ( log.isDebugEnabled() )
222 log.debug( "Resolved namespaces from storage: " + storageNamespaces );
224 for ( String n : storageNamespaces )
228 metadataRepository.updateNamespace( repoId, namespace + "." + n );
230 catch ( MetadataRepositoryException e )
232 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
237 namespaces = new ArrayList<String>( namespaces );
238 namespaces.addAll( storageNamespaces );
243 public Collection<String> resolveProjects( RepositorySession session, String repoId, String namespace )
244 throws MetadataResolutionException
246 MetadataRepository metadataRepository = session.getRepository();
247 Collection<String> projects = metadataRepository.getProjects( repoId, namespace );
248 Collection<String> exclusions = new ArrayList<String>( projects );
249 exclusions.addAll( metadataRepository.getNamespaces( repoId, namespace ) );
250 Collection<String> storageProjects =
251 repositoryStorage.listProjects( repoId, namespace, new ExcludesFilter<String>( exclusions ) );
252 if ( storageProjects != null && !storageProjects.isEmpty() )
254 if ( log.isDebugEnabled() )
256 log.debug( "Resolved projects from storage: " + storageProjects );
258 for ( String projectId : storageProjects )
260 ProjectMetadata projectMetadata = repositoryStorage.readProjectMetadata( repoId, namespace, projectId );
261 if ( projectMetadata != null )
265 metadataRepository.updateProject( repoId, projectMetadata );
267 catch ( MetadataRepositoryException e )
269 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
275 projects = new ArrayList<String>( projects );
276 projects.addAll( storageProjects );
281 public Collection<String> resolveProjectVersions( RepositorySession session, String repoId, String namespace,
283 throws MetadataResolutionException
285 MetadataRepository metadataRepository = session.getRepository();
286 Collection<String> projectVersions = metadataRepository.getProjectVersions( repoId, namespace, projectId );
287 Collection<String> storageProjectVersions = repositoryStorage.listProjectVersions( repoId, namespace, projectId,
288 new ExcludesFilter<String>(
290 if ( storageProjectVersions != null && !storageProjectVersions.isEmpty() )
292 if ( log.isDebugEnabled() )
294 log.debug( "Resolved project versions from storage: " + storageProjectVersions );
296 for ( String projectVersion : storageProjectVersions )
300 ProjectVersionMetadata versionMetadata =
301 repositoryStorage.readProjectVersionMetadata( repoId, namespace, projectId, projectVersion );
302 for ( RepositoryListener listener : listeners )
304 listener.addArtifact( session, repoId, namespace, projectId, versionMetadata );
307 metadataRepository.updateProjectVersion( repoId, namespace, projectId, versionMetadata );
309 catch ( MetadataRepositoryException e )
311 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
313 catch ( RepositoryStorageMetadataInvalidException e )
315 log.warn( "Not update project in metadata repository due to an error resolving it from storage: "
318 for ( RepositoryListener listener : listeners )
320 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
323 catch ( RepositoryStorageMetadataNotFoundException e )
325 for ( RepositoryListener listener : listeners )
327 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
333 projectVersions = new ArrayList<String>( projectVersions );
334 projectVersions.addAll( storageProjectVersions );
336 return projectVersions;
339 public Collection<ArtifactMetadata> resolveArtifacts( RepositorySession session, String repoId, String namespace,
340 String projectId, String projectVersion )
341 throws MetadataResolutionException
343 MetadataRepository metadataRepository = session.getRepository();
344 Collection<ArtifactMetadata> artifacts =
345 metadataRepository.getArtifacts( repoId, namespace, projectId, projectVersion );
346 ExcludesFilter<String> filter = new ExcludesFilter<String>( createArtifactIdList( artifacts ) );
347 Collection<ArtifactMetadata> storageArtifacts =
348 repositoryStorage.readArtifactsMetadata( repoId, namespace, projectId, projectVersion, filter );
349 if ( storageArtifacts != null && !storageArtifacts.isEmpty() )
351 if ( log.isDebugEnabled() )
353 log.debug( "Resolved artifacts from storage: " + storageArtifacts );
355 for ( ArtifactMetadata artifact : storageArtifacts )
359 metadataRepository.updateArtifact( repoId, namespace, projectId, projectVersion, artifact );
361 catch ( MetadataRepositoryException e )
363 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
368 artifacts = new ArrayList<ArtifactMetadata>( artifacts );
369 artifacts.addAll( storageArtifacts );
374 private Collection<String> createArtifactIdList( Collection<ArtifactMetadata> artifacts )
376 Collection<String> artifactIds = new ArrayList<String>();
377 for ( ArtifactMetadata artifact : artifacts )
379 artifactIds.add( artifact.getId() );