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.ReadMetadataRequest;
28 import org.apache.archiva.metadata.repository.storage.RepositoryStorage;
29 import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException;
30 import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException;
31 import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException;
32 import org.apache.archiva.redback.components.cache.Cache;
33 import org.apache.archiva.repository.events.RepositoryListener;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.stereotype.Service;
38 import javax.inject.Inject;
39 import javax.inject.Named;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.List;
45 * Default implementation of the metadata resolver API. At present it will handle updating the content repository
46 * from new or changed information in the model and artifacts from the repository storage.
48 * This is a singleton component to allow an alternate implementation to be provided. It is intended to be the same
49 * system-wide for the whole content repository instead of on a per-managed-repository basis. Therefore, the session is
50 * passed in as an argument to obtain any necessary resources, rather than the class being instantiated within the
51 * session in the context of a single managed repository's resolution needs.
53 * Note that the caller is responsible for the session, such as closing and saving (which is implied by the resolver
54 * being obtained from within the session). The {@link RepositorySession#markDirty()} method is used as a hint to ensure
55 * that the session knows we've made changes at close. We cannot ensure the changes will be persisted if the caller
56 * chooses to revert first. This is preferable to storing the metadata immediately - a separate session would require
57 * having a bi-directional link with the session factory, and saving the existing session might save other changes
58 * unknowingly by the caller.
61 @Service("metadataResolver#default")
62 public class DefaultMetadataResolver
63 implements MetadataResolver
66 private Logger log = LoggerFactory.getLogger( DefaultMetadataResolver.class );
69 * FIXME: this needs to be configurable based on storage type - and could also be instantiated per repo. Change to a
70 * factory, and perhaps retrieve from the session. We should avoid creating one per request, however.
72 * TODO: Also need to accommodate availability of proxy module
73 * ... could be a different type since we need methods to modify the storage metadata, which would also allow more
74 * appropriate methods to pass in the already determined repository configuration, for example, instead of the ID
77 @Named(value = "repositoryStorage#maven2")
78 private RepositoryStorage repositoryStorage;
81 private List<RepositoryListener> listeners;
84 * Cache used for namespaces
87 @Named( value = "cache#namespaces" )
88 private Cache<String, Collection<String>> namespacesCache;
91 public ProjectVersionMetadata resolveProjectVersion( RepositorySession session, String repoId, String namespace,
92 String projectId, String projectVersion )
93 throws MetadataResolutionException
95 MetadataRepository metadataRepository = session.getRepository();
97 ProjectVersionMetadata metadata =
98 metadataRepository.getProjectVersion( repoId, namespace, projectId, projectVersion );
99 // TODO: do we want to detect changes as well by comparing timestamps? isProjectVersionNewerThan(updated)
100 // in such cases we might also remove/update stale metadata, including adjusting plugin-based facets
101 // This would also be better than checking for completeness - we can then refresh only when fixed (though
102 // sometimes this has an additional dependency - such as a parent - requesting the user to force an update
103 // may then work here and be more efficient than always trying again)
104 if ( metadata == null || metadata.isIncomplete() )
108 ReadMetadataRequest readMetadataRequest =
109 new ReadMetadataRequest().repositoryId( repoId ).namespace( namespace ).projectId(
110 projectId ).projectVersion( projectVersion ).browsingRequest( true );
111 metadata = repositoryStorage.readProjectVersionMetadata( readMetadataRequest );
113 log.debug( "Resolved project version metadata from storage: {}", metadata );
115 // FIXME: make this a more generic post-processing that plugins can take advantage of
116 // eg. maven projects should be able to process parent here
117 if ( !metadata.getDependencies().isEmpty() )
119 ProjectVersionReference ref = new ProjectVersionReference();
120 ref.setNamespace( namespace );
121 ref.setProjectId( projectId );
122 ref.setProjectVersion( projectVersion );
123 ref.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
127 for ( RepositoryListener listener : listeners )
129 listener.addArtifact( session, repoId, namespace, projectId, metadata );
131 metadataRepository.updateProjectVersion( repoId, namespace, projectId, metadata );
133 catch ( MetadataRepositoryException e )
135 log.warn( "Unable to persist resolved information: {}", e.getMessage(), e );
140 catch ( RepositoryStorageMetadataInvalidException e )
142 for ( RepositoryListener listener : listeners )
144 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
146 throw new MetadataResolutionException( e.getMessage(), e );
148 catch ( RepositoryStorageMetadataNotFoundException e )
150 for ( RepositoryListener listener : listeners )
152 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
154 // no need to rethrow - return null
156 catch ( RepositoryStorageRuntimeException e )
158 for ( RepositoryListener listener : listeners )
160 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
162 throw new MetadataResolutionException( e.getMessage(), e );
170 public Collection<ProjectVersionReference> resolveProjectReferences( RepositorySession session, String repoId,
171 String namespace, String projectId,
172 String projectVersion )
173 throws MetadataResolutionException
175 // TODO: is this assumption correct? could a storage mech. actually know all references in a non-Maven scenario?
176 // not passed to the storage mechanism as resolving references would require iterating all artifacts
177 MetadataRepository metadataRepository = session.getRepository();
178 return metadataRepository.getProjectReferences( repoId, namespace, projectId, projectVersion );
182 public Collection<String> resolveRootNamespaces( RepositorySession session, String repoId )
183 throws MetadataResolutionException
188 Collection<String> namespaces = namespacesCache.get( repoId );
189 if ( namespaces != null )
194 MetadataRepository metadataRepository = session.getRepository();
195 namespaces = metadataRepository.getRootNamespaces( repoId );
196 Collection<String> storageNamespaces =
197 repositoryStorage.listRootNamespaces( repoId, new ExcludesFilter<String>( namespaces ) );
198 if ( storageNamespaces != null && !storageNamespaces.isEmpty() )
201 log.debug( "Resolved root namespaces from storage: {}", storageNamespaces );
203 for ( String n : storageNamespaces )
207 metadataRepository.updateNamespace( repoId, n );
208 // just invalidate cache entry
209 String cacheKey = repoId + "-" + n;
210 namespacesCache.remove( cacheKey );
212 catch ( MetadataRepositoryException e )
214 log.warn( "Unable to persist resolved information: {}", e.getMessage(), e );
219 namespaces = new ArrayList<>( namespaces );
220 namespaces.addAll( storageNamespaces );
223 namespacesCache.put( repoId, namespaces );
227 catch ( RepositoryStorageRuntimeException e )
229 throw new MetadataResolutionException( e.getMessage(), e );
234 public Collection<String> resolveNamespaces( RepositorySession session, String repoId, String namespace )
235 throws MetadataResolutionException
239 MetadataRepository metadataRepository = session.getRepository();
240 String cacheKey = repoId + "-" + namespace;
241 Collection<String> namespaces = namespacesCache.get( cacheKey );
242 if ( namespaces == null )
244 namespaces = metadataRepository.getNamespaces( repoId, namespace );
245 namespacesCache.put( cacheKey, namespaces );
247 Collection<String> exclusions = new ArrayList<>( namespaces );
248 exclusions.addAll( metadataRepository.getProjects( repoId, namespace ) );
249 Collection<String> storageNamespaces =
250 repositoryStorage.listNamespaces( repoId, namespace, new ExcludesFilter<String>( exclusions ) );
251 if ( storageNamespaces != null && !storageNamespaces.isEmpty() )
254 log.debug( "Resolved namespaces from storage: {}", storageNamespaces );
256 for ( String n : storageNamespaces )
260 metadataRepository.updateNamespace( repoId, namespace + "." + n );
261 // just invalidate cache entry
262 cacheKey = repoId + "-" + namespace + "." + n;
263 namespacesCache.remove( cacheKey );
265 catch ( MetadataRepositoryException e )
267 log.warn( "Unable to persist resolved information: {}", e.getMessage(), e );
272 namespaces = new ArrayList<>( namespaces );
273 namespaces.addAll( storageNamespaces );
277 catch ( RepositoryStorageRuntimeException e )
279 throw new MetadataResolutionException( e.getMessage(), e );
284 public Collection<String> resolveProjects( RepositorySession session, String repoId, String namespace )
285 throws MetadataResolutionException
289 MetadataRepository metadataRepository = session.getRepository();
290 Collection<String> projects = metadataRepository.getProjects( repoId, namespace );
291 Collection<String> exclusions = new ArrayList<>( projects );
293 String cacheKey = repoId + "-" + namespace;
294 Collection<String> namespaces = namespacesCache.get( cacheKey );
295 if ( namespaces == null )
297 namespaces = metadataRepository.getNamespaces( repoId, namespace );
298 namespacesCache.put( cacheKey, namespaces );
301 exclusions.addAll( namespaces );
303 Collection<String> storageProjects =
304 repositoryStorage.listProjects( repoId, namespace, new ExcludesFilter<String>( exclusions ) );
305 if ( storageProjects != null && !storageProjects.isEmpty() )
308 log.debug( "Resolved projects from storage: {}", storageProjects );
309 for ( String projectId : storageProjects )
311 ProjectMetadata projectMetadata =
312 repositoryStorage.readProjectMetadata( repoId, namespace, projectId );
313 if ( projectMetadata != null )
317 metadataRepository.updateProject( repoId, projectMetadata );
319 catch ( MetadataRepositoryException e )
321 log.warn( "Unable to persist resolved information: {}", e.getMessage(), e );
327 projects = new ArrayList<>( projects );
328 projects.addAll( storageProjects );
332 catch ( RepositoryStorageRuntimeException e )
334 throw new MetadataResolutionException( e.getMessage(), e );
339 public Collection<String> resolveProjectVersions( RepositorySession session, String repoId, String namespace,
341 throws MetadataResolutionException
345 MetadataRepository metadataRepository = session.getRepository();
347 Collection<String> projectVersions = metadataRepository.getProjectVersions( repoId, namespace, projectId );
348 Collection<String> storageProjectVersions =
349 repositoryStorage.listProjectVersions( repoId, namespace, projectId,
350 new ExcludesFilter<String>( projectVersions ) );
351 if ( storageProjectVersions != null && !storageProjectVersions.isEmpty() )
353 log.debug( "Resolved project versions from storage: {}", storageProjectVersions );
355 for ( String projectVersion : storageProjectVersions )
359 ReadMetadataRequest readMetadataRequest =
360 new ReadMetadataRequest().repositoryId( repoId ).namespace( namespace ).projectId(
361 projectId ).projectVersion( projectVersion );
362 ProjectVersionMetadata versionMetadata =
363 repositoryStorage.readProjectVersionMetadata( readMetadataRequest );
364 for ( RepositoryListener listener : listeners )
366 listener.addArtifact( session, repoId, namespace, projectId, versionMetadata );
369 metadataRepository.updateProjectVersion( repoId, namespace, projectId, versionMetadata );
371 catch ( MetadataRepositoryException e )
373 log.warn( "Unable to persist resolved information: {}", e.getMessage(), e );
375 catch ( RepositoryStorageMetadataInvalidException e )
378 "Not update project in metadata repository due to an error resolving it from storage: {}",
381 for ( RepositoryListener listener : listeners )
383 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
386 catch ( RepositoryStorageMetadataNotFoundException e )
388 for ( RepositoryListener listener : listeners )
390 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
396 projectVersions = new ArrayList<>( projectVersions );
397 projectVersions.addAll( storageProjectVersions );
399 return projectVersions;
401 catch ( RepositoryStorageRuntimeException e )
403 throw new MetadataResolutionException( e.getMessage(), e );
408 public Collection<ArtifactMetadata> resolveArtifacts( RepositorySession session, String repoId, String namespace,
409 String projectId, String projectVersion )
410 throws MetadataResolutionException
414 MetadataRepository metadataRepository = session.getRepository();
415 Collection<ArtifactMetadata> artifacts =
416 metadataRepository.getArtifacts( repoId, namespace, projectId, projectVersion );
417 ExcludesFilter<String> filter = new ExcludesFilter<String>( createArtifactIdList( artifacts ) );
419 ReadMetadataRequest readMetadataRequest =
420 new ReadMetadataRequest().repositoryId( repoId ).namespace( namespace ).projectId(
421 projectId ).projectVersion( projectVersion ).filter( filter );
423 Collection<ArtifactMetadata> storageArtifacts =
424 repositoryStorage.readArtifactsMetadata( readMetadataRequest );
425 if ( storageArtifacts != null && !storageArtifacts.isEmpty() )
428 log.debug( "Resolved artifacts from storage: {}", storageArtifacts );
430 for ( ArtifactMetadata artifact : storageArtifacts )
434 metadataRepository.updateArtifact( repoId, namespace, projectId, projectVersion, artifact );
436 catch ( MetadataRepositoryException e )
438 log.warn( "Unable to persist resolved information: {}", e.getMessage(), e );
443 artifacts = new ArrayList<>( artifacts );
444 artifacts.addAll( storageArtifacts );
448 catch ( RepositoryStorageRuntimeException e )
450 for ( RepositoryListener listener : listeners )
452 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
454 throw new MetadataResolutionException( e.getMessage(), e );
458 private Collection<String> createArtifactIdList( Collection<ArtifactMetadata> artifacts )
460 Collection<String> artifactIds = new ArrayList<>();
461 for ( ArtifactMetadata artifact : artifacts )
463 artifactIds.add( artifact.getId() );