This project has retired. For details please refer to its Attic page.
ArchivaMetadataCreationConsumer xref
View Javadoc
1   package org.apache.archiva.consumers.metadata;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.archiva.admin.model.beans.ManagedRepository;
23  import org.apache.archiva.common.utils.VersionUtil;
24  import org.apache.archiva.configuration.ArchivaConfiguration;
25  import org.apache.archiva.configuration.ConfigurationNames;
26  import org.apache.archiva.configuration.FileTypes;
27  import org.apache.archiva.consumers.AbstractMonitoredConsumer;
28  import org.apache.archiva.consumers.ConsumerException;
29  import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
30  import org.apache.archiva.metadata.model.ArtifactMetadata;
31  import org.apache.archiva.metadata.model.ProjectMetadata;
32  import org.apache.archiva.metadata.model.ProjectVersionMetadata;
33  import org.apache.archiva.metadata.repository.MetadataRepository;
34  import org.apache.archiva.metadata.repository.MetadataRepositoryException;
35  import org.apache.archiva.metadata.repository.RepositorySession;
36  import org.apache.archiva.metadata.repository.RepositorySessionFactory;
37  import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest;
38  import org.apache.archiva.metadata.repository.storage.RepositoryStorage;
39  import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException;
40  import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException;
41  import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException;
42  import org.apache.archiva.redback.components.registry.Registry;
43  import org.apache.archiva.redback.components.registry.RegistryListener;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  import org.springframework.context.annotation.Scope;
47  import org.springframework.stereotype.Service;
48  
49  import javax.annotation.PostConstruct;
50  import javax.inject.Inject;
51  import javax.inject.Named;
52  import java.util.ArrayList;
53  import java.util.Date;
54  import java.util.List;
55  
56  /**
57   * Take an artifact off of disk and put it into the metadata repository.
58   */
59  @Service ("knownRepositoryContentConsumer#create-archiva-metadata")
60  @Scope ("prototype")
61  public class ArchivaMetadataCreationConsumer
62      extends AbstractMonitoredConsumer
63      implements KnownRepositoryContentConsumer, RegistryListener
64  {
65      private String id = "create-archiva-metadata";
66  
67      private String description = "Create basic metadata for Archiva to be able to reference the artifact";
68  
69      @Inject
70      private ArchivaConfiguration configuration;
71  
72      @Inject
73      private FileTypes filetypes;
74  
75      private Date whenGathered;
76  
77      private List<String> includes = new ArrayList<>( 0 );
78  
79      /**
80       * FIXME: this could be multiple implementations and needs to be configured.
81       */
82      @Inject
83      private RepositorySessionFactory repositorySessionFactory;
84  
85      /**
86       * FIXME: this needs to be configurable based on storage type - and could also be instantiated per repo. Change to a
87       * factory.
88       */
89      @Inject
90      @Named (value = "repositoryStorage#maven2")
91      private RepositoryStorage repositoryStorage;
92  
93      private static final Logger log = LoggerFactory.getLogger( ArchivaMetadataCreationConsumer.class );
94  
95      private String repoId;
96  
97      @Override
98      public String getId()
99      {
100         return this.id;
101     }
102 
103     @Override
104     public String getDescription()
105     {
106         return this.description;
107     }
108 
109     @Override
110     public List<String> getExcludes()
111     {
112         return getDefaultArtifactExclusions();
113     }
114 
115     @Override
116     public List<String> getIncludes()
117     {
118         return this.includes;
119     }
120 
121     @Override
122     public void beginScan( ManagedRepository repo, Date whenGathered )
123         throws ConsumerException
124     {
125         repoId = repo.getId();
126         this.whenGathered = whenGathered;
127     }
128 
129     @Override
130     public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
131         throws ConsumerException
132     {
133         beginScan( repository, whenGathered );
134     }
135 
136     @Override
137     public void processFile( String path )
138         throws ConsumerException
139     {
140 
141         RepositorySession repositorySession = repositorySessionFactory.createSession();
142         try
143         {
144             // note that we do minimal processing including checksums and POM information for performance of
145             // the initial scan. Any request for this information will be intercepted and populated on-demand
146             // or picked up by subsequent scans
147 
148             ArtifactMetadata artifact = repositoryStorage.readArtifactMetadataFromPath( repoId, path );
149 
150             ProjectMetadata project = new ProjectMetadata();
151             project.setNamespace( artifact.getNamespace() );
152             project.setId( artifact.getProject() );
153 
154             String projectVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
155 
156             MetadataRepository metadataRepository = repositorySession.getRepository();
157 
158             boolean createVersionMetadata = false;
159 
160             // FIXME: maybe not too efficient since it may have already been read and stored for this artifact
161             ProjectVersionMetadata versionMetadata = null;
162             try
163             {
164                 ReadMetadataRequest readMetadataRequest =
165                     new ReadMetadataRequest().repositoryId( repoId ).namespace( artifact.getNamespace() ).projectId(
166                         artifact.getProject() ).projectVersion( projectVersion );
167                 versionMetadata = repositoryStorage.readProjectVersionMetadata( readMetadataRequest );
168                 createVersionMetadata = true;
169             }
170             catch ( RepositoryStorageMetadataNotFoundException e )
171             {
172                 log.warn( "Missing or invalid POM for artifact:{} (repository:{}); creating empty metadata", path,
173                           repoId );
174 
175                 versionMetadata = new ProjectVersionMetadata();
176                 versionMetadata.setId( projectVersion );
177                 versionMetadata.setIncomplete( true );
178                 createVersionMetadata = true;
179             }
180             catch ( RepositoryStorageMetadataInvalidException e )
181             {
182                 log.warn( "Error occurred resolving POM for artifact:{} (repository:{}); message: {}",
183                           new Object[]{ path, repoId, e.getMessage() } );
184             }
185 
186             // read the metadata and update it if it is newer or doesn't exist
187             artifact.setWhenGathered( whenGathered );
188             metadataRepository.updateArtifact( repoId, project.getNamespace(), project.getId(), projectVersion,
189                                                artifact );
190             if ( createVersionMetadata )
191             {
192                 metadataRepository.updateProjectVersion( repoId, project.getNamespace(), project.getId(),
193                                                          versionMetadata );
194             }
195             metadataRepository.updateProject( repoId, project );
196             repositorySession.save();
197         }
198         catch ( MetadataRepositoryException e )
199         {
200             log.warn(
201                 "Error occurred persisting metadata for artifact:{} (repository:{}); message: {}" ,
202                 path, repoId, e.getMessage(), e );
203             repositorySession.revert();
204         }
205         catch ( RepositoryStorageRuntimeException e )
206         {
207             log.warn(
208                 "Error occurred persisting metadata for artifact:{} (repository:{}); message: {}",
209                 path, repoId, e.getMessage(), e );
210             repositorySession.revert();
211         }
212         finally
213         {
214             repositorySession.close();
215         }
216     }
217 
218     @Override
219     public void processFile( String path, boolean executeOnEntireRepo )
220         throws ConsumerException
221     {
222         processFile( path );
223     }
224 
225     @Override
226     public void completeScan()
227     {
228         /* do nothing */
229     }
230 
231     @Override
232     public void completeScan( boolean executeOnEntireRepo )
233     {
234         completeScan();
235     }
236 
237     @Override
238     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
239     {
240         if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
241         {
242             initIncludes();
243         }
244     }
245 
246     @Override
247     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
248     {
249         /* do nothing */
250     }
251 
252     private void initIncludes()
253     {
254         includes = new ArrayList<String>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
255     }
256 
257     @PostConstruct
258     public void initialize()
259     {
260         configuration.addChangeListener( this );
261 
262         initIncludes();
263     }
264 }