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.
59 * plexus.component role="org.apache.archiva.metadata.repository.MetadataResolver"
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
76 * plexus.requirement role-hint="maven2"
79 @Named( value = "repositoryStorage#maven2" )
80 private RepositoryStorage repositoryStorage;
83 * plexus.requirement role="org.apache.archiva.repository.events.RepositoryListener"
86 private List<RepositoryListener> listeners;
90 //private ApplicationContext applicationContext;
93 private void initialize()
96 // new ArrayList<RepositoryListener>( applicationContext.getBeansOfType( RepositoryListener.class ).values() );
99 public ProjectVersionMetadata resolveProjectVersion( RepositorySession session, String repoId, String namespace,
100 String projectId, String projectVersion )
101 throws MetadataResolutionException
103 MetadataRepository metadataRepository = session.getRepository();
105 ProjectVersionMetadata metadata =
106 metadataRepository.getProjectVersion( repoId, namespace, projectId, projectVersion );
107 // TODO: do we want to detect changes as well by comparing timestamps? isProjectVersionNewerThan(updated)
108 // in such cases we might also remove/update stale metadata, including adjusting plugin-based facets
109 // This would also be better than checking for completeness - we can then refresh only when fixed (though
110 // sometimes this has an additional dependency - such as a parent - requesting the user to force an update
111 // may then work here and be more efficient than always trying again)
112 if ( metadata == null || metadata.isIncomplete() )
116 metadata = repositoryStorage.readProjectVersionMetadata( repoId, namespace, projectId, projectVersion );
118 if ( log.isDebugEnabled() )
120 log.debug( "Resolved project version metadata from storage: " + metadata );
122 // FIXME: make this a more generic post-processing that plugins can take advantage of
123 // eg. maven projects should be able to process parent here
124 if ( !metadata.getDependencies().isEmpty() )
126 ProjectVersionReference ref = new ProjectVersionReference();
127 ref.setNamespace( namespace );
128 ref.setProjectId( projectId );
129 ref.setProjectVersion( projectVersion );
130 ref.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
134 for ( RepositoryListener listener : listeners )
136 listener.addArtifact( session, repoId, namespace, projectId, metadata );
138 metadataRepository.updateProjectVersion( repoId, namespace, projectId, metadata );
140 catch ( MetadataRepositoryException e )
142 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
147 catch ( RepositoryStorageMetadataInvalidException e )
149 for ( RepositoryListener listener : listeners )
151 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
153 throw new MetadataResolutionException( e.getMessage(), e );
155 catch ( RepositoryStorageMetadataNotFoundException e )
157 for ( RepositoryListener listener : listeners )
159 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
161 // no need to rethrow - return null
167 public Collection<ProjectVersionReference> resolveProjectReferences( RepositorySession session, String repoId,
168 String namespace, String projectId,
169 String projectVersion )
170 throws MetadataResolutionException
172 // TODO: is this assumption correct? could a storage mech. actually know all references in a non-Maven scenario?
173 // not passed to the storage mechanism as resolving references would require iterating all artifacts
174 MetadataRepository metadataRepository = session.getRepository();
175 return metadataRepository.getProjectReferences( repoId, namespace, projectId, projectVersion );
178 public Collection<String> resolveRootNamespaces( RepositorySession session, String repoId )
179 throws MetadataResolutionException
181 MetadataRepository metadataRepository = session.getRepository();
182 Collection<String> namespaces = metadataRepository.getRootNamespaces( repoId );
183 Collection<String> storageNamespaces =
184 repositoryStorage.listRootNamespaces( repoId, new ExcludesFilter<String>( namespaces ) );
185 if ( storageNamespaces != null && !storageNamespaces.isEmpty() )
187 if ( log.isDebugEnabled() )
189 log.debug( "Resolved root namespaces from storage: " + storageNamespaces );
191 for ( String n : storageNamespaces )
195 metadataRepository.updateNamespace( repoId, n );
197 catch ( MetadataRepositoryException e )
199 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
204 namespaces = new ArrayList<String>( namespaces );
205 namespaces.addAll( storageNamespaces );
210 public Collection<String> resolveNamespaces( RepositorySession session, String repoId, String namespace )
211 throws MetadataResolutionException
213 MetadataRepository metadataRepository = session.getRepository();
214 Collection<String> namespaces = metadataRepository.getNamespaces( repoId, namespace );
215 Collection<String> exclusions = new ArrayList<String>( namespaces );
216 exclusions.addAll( metadataRepository.getProjects( repoId, namespace ) );
217 Collection<String> storageNamespaces =
218 repositoryStorage.listNamespaces( repoId, namespace, new ExcludesFilter<String>( exclusions ) );
219 if ( storageNamespaces != null && !storageNamespaces.isEmpty() )
221 if ( log.isDebugEnabled() )
223 log.debug( "Resolved namespaces from storage: " + storageNamespaces );
225 for ( String n : storageNamespaces )
229 metadataRepository.updateNamespace( repoId, namespace + "." + n );
231 catch ( MetadataRepositoryException e )
233 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
238 namespaces = new ArrayList<String>( namespaces );
239 namespaces.addAll( storageNamespaces );
244 public Collection<String> resolveProjects( RepositorySession session, String repoId, String namespace )
245 throws MetadataResolutionException
247 MetadataRepository metadataRepository = session.getRepository();
248 Collection<String> projects = metadataRepository.getProjects( repoId, namespace );
249 Collection<String> exclusions = new ArrayList<String>( projects );
250 exclusions.addAll( metadataRepository.getNamespaces( repoId, namespace ) );
251 Collection<String> storageProjects =
252 repositoryStorage.listProjects( repoId, namespace, new ExcludesFilter<String>( exclusions ) );
253 if ( storageProjects != null && !storageProjects.isEmpty() )
255 if ( log.isDebugEnabled() )
257 log.debug( "Resolved projects from storage: " + storageProjects );
259 for ( String projectId : storageProjects )
261 ProjectMetadata projectMetadata = repositoryStorage.readProjectMetadata( repoId, namespace, projectId );
262 if ( projectMetadata != null )
266 metadataRepository.updateProject( repoId, projectMetadata );
268 catch ( MetadataRepositoryException e )
270 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
276 projects = new ArrayList<String>( projects );
277 projects.addAll( storageProjects );
282 public Collection<String> resolveProjectVersions( RepositorySession session, String repoId, String namespace,
284 throws MetadataResolutionException
286 MetadataRepository metadataRepository = session.getRepository();
287 Collection<String> projectVersions = metadataRepository.getProjectVersions( repoId, namespace, projectId );
288 Collection<String> storageProjectVersions = repositoryStorage.listProjectVersions( repoId, namespace, projectId,
289 new ExcludesFilter<String>(
291 if ( storageProjectVersions != null && !storageProjectVersions.isEmpty() )
293 if ( log.isDebugEnabled() )
295 log.debug( "Resolved project versions from storage: " + storageProjectVersions );
297 for ( String projectVersion : storageProjectVersions )
301 ProjectVersionMetadata versionMetadata =
302 repositoryStorage.readProjectVersionMetadata( repoId, namespace, projectId, projectVersion );
303 for ( RepositoryListener listener : listeners )
305 listener.addArtifact( session, repoId, namespace, projectId, versionMetadata );
308 metadataRepository.updateProjectVersion( repoId, namespace, projectId, versionMetadata );
310 catch ( MetadataRepositoryException e )
312 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
314 catch ( RepositoryStorageMetadataInvalidException e )
316 log.warn( "Not update project in metadata repository due to an error resolving it from storage: "
319 for ( RepositoryListener listener : listeners )
321 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
324 catch ( RepositoryStorageMetadataNotFoundException e )
326 for ( RepositoryListener listener : listeners )
328 listener.addArtifactProblem( session, repoId, namespace, projectId, projectVersion, e );
334 projectVersions = new ArrayList<String>( projectVersions );
335 projectVersions.addAll( storageProjectVersions );
337 return projectVersions;
340 public Collection<ArtifactMetadata> resolveArtifacts( RepositorySession session, String repoId, String namespace,
341 String projectId, String projectVersion )
342 throws MetadataResolutionException
344 MetadataRepository metadataRepository = session.getRepository();
345 Collection<ArtifactMetadata> artifacts =
346 metadataRepository.getArtifacts( repoId, namespace, projectId, projectVersion );
347 ExcludesFilter<String> filter = new ExcludesFilter<String>( createArtifactIdList( artifacts ) );
348 Collection<ArtifactMetadata> storageArtifacts =
349 repositoryStorage.readArtifactsMetadata( repoId, namespace, projectId, projectVersion, filter );
350 if ( storageArtifacts != null && !storageArtifacts.isEmpty() )
352 if ( log.isDebugEnabled() )
354 log.debug( "Resolved artifacts from storage: " + storageArtifacts );
356 for ( ArtifactMetadata artifact : storageArtifacts )
360 metadataRepository.updateArtifact( repoId, namespace, projectId, projectVersion, artifact );
362 catch ( MetadataRepositoryException e )
364 log.warn( "Unable to persist resolved information: " + e.getMessage(), e );
369 artifacts = new ArrayList<ArtifactMetadata>( artifacts );
370 artifacts.addAll( storageArtifacts );
375 private Collection<String> createArtifactIdList( Collection<ArtifactMetadata> artifacts )
377 Collection<String> artifactIds = new ArrayList<String>();
378 for ( ArtifactMetadata artifact : artifacts )
380 artifactIds.add( artifact.getId() );