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.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Service;
39 import javax.inject.Inject;
40 import javax.inject.Named;
41 import java.util.ArrayList;
42 import java.util.Collection;
43 import java.util.List;
46 * Default implementation of the metadata resolver API. At present it will handle updating the content repository
47 * from new or changed information in the model and artifacts from the repository storage.
49 * This is a singleton component to allow an alternate implementation to be provided. It is intended to be the same
50 * system-wide for the whole content repository instead of on a per-managed-repository basis. Therefore, the session is
51 * passed in as an argument to obtain any necessary resources, rather than the class being instantiated within the
52 * session in the context of a single managed repository's resolution needs.
54 * Note that the caller is responsible for the session, such as closing and saving (which is implied by the resolver
55 * being obtained from within the session). The {@link RepositorySession#markDirty()} method is used as a hint to ensure
56 * that the session knows we've made changes at close. We cannot ensure the changes will be persisted if the caller
57 * chooses to revert first. This is preferable to storing the metadata immediately - a separate session would require
58 * having a bi-directional link with the session factory, and saving the existing session might save other changes
59 * unknowingly by the caller.
62 @Service("metadataResolver#default")
63 public class DefaultMetadataResolver
64 implements MetadataResolver
67 private Logger log = LoggerFactory.getLogger( DefaultMetadataResolver.class );
70 * FIXME: this needs to be configurable based on storage type - and could also be instantiated per repo. Change to a
71 * factory, and perhaps retrieve from the session. We should avoid creating one per request, however.
73 * TODO: Also need to accommodate availability of proxy module
74 * ... could be a different type since we need methods to modify the storage metadata, which would also allow more
75 * appropriate methods to pass in the already determined repository configuration, for example, instead of the ID
78 @Named(value = "repositoryStorage#maven2")
79 private RepositoryStorage repositoryStorage;
82 @Autowired(required = false)
83 private List<RepositoryListener> listeners = new ArrayList<>();
86 * Cache used for namespaces
89 @Named( value = "cache#namespaces" )
90 private Cache<String, Collection<String>> namespacesCache;
93 public ProjectVersionMetadata resolveProjectVersion( RepositorySession session, String repoId, String namespace,
94 String projectId, String projectVersion )
95 throws MetadataResolutionException
97 MetadataRepository metadataRepository = session.getRepository();
99 ProjectVersionMetadata metadata =
100 metadataRepository.getProjectVersion( repoId, namespace, projectId, projectVersion );
101 // TODO: do we want to detect changes as well by comparing timestamps? isProjectVersionNewerThan(updated)
102 // in such cases we might also remove/update stale metadata, including adjusting plugin-based facets
103 // This would also be better than checking for completeness - we can then refresh only when fixed (though
104 // sometimes this has an additional dependency - such as a parent - requesting the user to force an update
105 // may then work here and be more efficient than always trying again)
106 if ( metadata == null || metadata.isIncomplete() )
110 ReadMetadataRequest readMetadataRequest =
111 new ReadMetadataRequest().repositoryId( repoId ).namespace( namespace ).projectId(
112 projectId ).projectVersion( projectVersion ).browsingRequest( true );
113 metadata = repositoryStorage.readProjectVersionMetadata( readMetadataRequest );
115 log.debug( "Resolved project version metadata from storage: {}", metadata );
117 // FIXME: make this a more generic post-processing that plugins can take advantage of
118 // eg. maven projects should be able to process parent here
119 if ( !metadata.getDependencies().isEmpty() )
121 ProjectVersionReference ref = new ProjectVersionReference();
122 ref.setNamespace( namespace );
123 ref.setProjectId( projectId );
124 ref.setProjectVersion( projectVersion );
125 ref.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
129 for ( RepositoryListener listener : listeners )
131 listener.addArtifact( session, repoId, namespace, projectId, metadata );
133 metadataRepository.updateProjectVersion( repoId, namespace, projectId, metadata );
135 catch ( MetadataRepositoryException e )
137 log.warn( "Unable to persist resolved information: {}", e.getMessage(), e );
142 catch ( RepositoryStorageMetadataInvalidException e )
144 for ( RepositoryListener listener : listeners )
146 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
148 throw new MetadataResolutionException( e.getMessage(), e );
150 catch ( RepositoryStorageMetadataNotFoundException e )
152 for ( RepositoryListener listener : listeners )
154 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
156 // no need to rethrow - return null
158 catch ( RepositoryStorageRuntimeException e )
160 for ( RepositoryListener listener : listeners )
162 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
164 throw new MetadataResolutionException( e.getMessage(), e );
172 public Collection<ProjectVersionReference> resolveProjectReferences( RepositorySession session, String repoId,
173 String namespace, String projectId,
174 String projectVersion )
175 throws MetadataResolutionException
177 // TODO: is this assumption correct? could a storage mech. actually know all references in a non-Maven scenario?
178 // not passed to the storage mechanism as resolving references would require iterating all artifacts
179 MetadataRepository metadataRepository = session.getRepository();
180 return metadataRepository.getProjectReferences( repoId, namespace, projectId, projectVersion );
184 public Collection<String> resolveRootNamespaces( RepositorySession session, String repoId )
185 throws MetadataResolutionException
190 Collection<String> namespaces = namespacesCache.get( repoId );
191 if ( namespaces != null )
196 MetadataRepository metadataRepository = session.getRepository();
197 namespaces = metadataRepository.getRootNamespaces( repoId );
198 Collection<String> storageNamespaces =
199 repositoryStorage.listRootNamespaces( repoId, new ExcludesFilter<String>( namespaces ) );
200 if ( storageNamespaces != null && !storageNamespaces.isEmpty() )
203 log.debug( "Resolved root namespaces from storage: {}", storageNamespaces );
205 for ( String n : storageNamespaces )
209 metadataRepository.updateNamespace( repoId, n );
210 // just invalidate cache entry
211 String cacheKey = repoId + "-" + n;
212 namespacesCache.remove( cacheKey );
214 catch ( MetadataRepositoryException e )
216 log.warn( "Unable to persist resolved information: {}", e.getMessage(), e );
221 namespaces = new ArrayList<>( namespaces );
222 namespaces.addAll( storageNamespaces );
225 namespacesCache.put( repoId, namespaces );
229 catch ( RepositoryStorageRuntimeException e )
231 throw new MetadataResolutionException( e.getMessage(), e );
236 public Collection<String> resolveNamespaces( RepositorySession session, String repoId, String namespace )
237 throws MetadataResolutionException
241 MetadataRepository metadataRepository = session.getRepository();
242 String cacheKey = repoId + "-" + namespace;
243 Collection<String> namespaces = namespacesCache.get( cacheKey );
244 if ( namespaces == null )
246 namespaces = metadataRepository.getNamespaces( repoId, namespace );
247 namespacesCache.put( cacheKey, namespaces );
249 Collection<String> exclusions = new ArrayList<>( namespaces );
250 exclusions.addAll( metadataRepository.getProjects( repoId, namespace ) );
251 Collection<String> storageNamespaces =
252 repositoryStorage.listNamespaces( repoId, namespace, new ExcludesFilter<String>( exclusions ) );
253 if ( storageNamespaces != null && !storageNamespaces.isEmpty() )
256 log.debug( "Resolved namespaces from storage: {}", storageNamespaces );
258 for ( String n : storageNamespaces )
262 metadataRepository.updateNamespace( repoId, namespace + "." + n );
263 // just invalidate cache entry
264 cacheKey = repoId + "-" + namespace + "." + n;
265 namespacesCache.remove( cacheKey );
267 catch ( MetadataRepositoryException e )
269 log.warn( "Unable to persist resolved information: {}", e.getMessage(), e );
274 namespaces = new ArrayList<>( namespaces );
275 namespaces.addAll( storageNamespaces );
279 catch ( RepositoryStorageRuntimeException e )
281 throw new MetadataResolutionException( e.getMessage(), e );
286 public Collection<String> resolveProjects( RepositorySession session, String repoId, String namespace )
287 throws MetadataResolutionException
291 MetadataRepository metadataRepository = session.getRepository();
292 Collection<String> projects = metadataRepository.getProjects( repoId, namespace );
293 Collection<String> exclusions = new ArrayList<>( projects );
295 String cacheKey = repoId + "-" + namespace;
296 Collection<String> namespaces = namespacesCache.get( cacheKey );
297 if ( namespaces == null )
299 namespaces = metadataRepository.getNamespaces( repoId, namespace );
300 namespacesCache.put( cacheKey, namespaces );
303 exclusions.addAll( namespaces );
305 Collection<String> storageProjects =
306 repositoryStorage.listProjects( repoId, namespace, new ExcludesFilter<>( exclusions ) );
307 if ( storageProjects != null && !storageProjects.isEmpty() )
310 log.debug( "Resolved projects from storage: {}", storageProjects );
311 for ( String projectId : storageProjects )
313 ProjectMetadata projectMetadata =
314 repositoryStorage.readProjectMetadata( repoId, namespace, projectId );
315 if ( projectMetadata != null )
319 metadataRepository.updateProject( repoId, projectMetadata );
321 catch ( MetadataRepositoryException e )
323 log.warn( "Unable to persist resolved information: {}", e.getMessage(), e );
329 projects = new ArrayList<>( projects );
330 projects.addAll( storageProjects );
334 catch ( RepositoryStorageRuntimeException e )
336 throw new MetadataResolutionException( e.getMessage(), e );
341 public Collection<String> resolveProjectVersions( RepositorySession session, String repoId, String namespace,
343 throws MetadataResolutionException
347 MetadataRepository metadataRepository = session.getRepository();
349 Collection<String> projectVersions = metadataRepository.getProjectVersions( repoId, namespace, projectId );
350 Collection<String> storageProjectVersions =
351 repositoryStorage.listProjectVersions( repoId, namespace, projectId,
352 new ExcludesFilter<String>( projectVersions ) );
353 if ( storageProjectVersions != null && !storageProjectVersions.isEmpty() )
355 log.debug( "Resolved project versions from storage: {}", storageProjectVersions );
357 for ( String projectVersion : storageProjectVersions )
361 ReadMetadataRequest readMetadataRequest =
362 new ReadMetadataRequest().repositoryId( repoId ).namespace( namespace ).projectId(
363 projectId ).projectVersion( projectVersion );
364 ProjectVersionMetadata versionMetadata =
365 repositoryStorage.readProjectVersionMetadata( readMetadataRequest );
366 for ( RepositoryListener listener : listeners )
368 listener.addArtifact( session, repoId, namespace, projectId, versionMetadata );
371 metadataRepository.updateProjectVersion( repoId, namespace, projectId, versionMetadata );
373 catch ( MetadataRepositoryException e )
375 log.warn( "Unable to persist resolved information: {}", e.getMessage(), e );
377 catch ( RepositoryStorageMetadataInvalidException e )
380 "Not update project in metadata repository due to an error resolving it from storage: {}",
383 for ( RepositoryListener listener : listeners )
385 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
388 catch ( RepositoryStorageMetadataNotFoundException e )
390 for ( RepositoryListener listener : listeners )
392 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
398 projectVersions = new ArrayList<>( projectVersions );
399 projectVersions.addAll( storageProjectVersions );
401 return projectVersions;
403 catch ( RepositoryStorageRuntimeException e )
405 throw new MetadataResolutionException( e.getMessage(), e );
410 public Collection<ArtifactMetadata> resolveArtifacts( RepositorySession session, String repoId, String namespace,
411 String projectId, String projectVersion )
412 throws MetadataResolutionException
416 MetadataRepository metadataRepository = session.getRepository();
417 Collection<ArtifactMetadata> artifacts =
418 metadataRepository.getArtifacts( repoId, namespace, projectId, projectVersion );
419 ExcludesFilter<String> filter = new ExcludesFilter<String>( createArtifactIdList( artifacts ) );
421 ReadMetadataRequest readMetadataRequest =
422 new ReadMetadataRequest().repositoryId( repoId ).namespace( namespace ).projectId(
423 projectId ).projectVersion( projectVersion ).filter( filter );
425 Collection<ArtifactMetadata> storageArtifacts =
426 repositoryStorage.readArtifactsMetadata( readMetadataRequest );
427 if ( storageArtifacts != null && !storageArtifacts.isEmpty() )
430 log.debug( "Resolved artifacts from storage: {}", storageArtifacts );
432 for ( ArtifactMetadata artifact : storageArtifacts )
436 metadataRepository.updateArtifact( repoId, namespace, projectId, projectVersion, artifact );
438 catch ( MetadataRepositoryException e )
440 log.warn( "Unable to persist resolved information: {}", e.getMessage(), e );
445 artifacts = new ArrayList<>( artifacts );
446 artifacts.addAll( storageArtifacts );
450 catch ( RepositoryStorageRuntimeException e )
452 for ( RepositoryListener listener : listeners )
454 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
456 throw new MetadataResolutionException( e.getMessage(), e );
460 private Collection<String> createArtifactIdList( Collection<ArtifactMetadata> artifacts )
462 Collection<String> artifactIds = new ArrayList<>();
463 for ( ArtifactMetadata artifact : artifacts )
465 artifactIds.add( artifact.getId() );