View Javadoc
1   package org.apache.archiva.consumers.core;
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.configuration.ArchivaConfiguration;
24  import org.apache.archiva.configuration.ConfigurationNames;
25  import org.apache.archiva.configuration.FileTypes;
26  import org.apache.archiva.consumers.AbstractMonitoredConsumer;
27  import org.apache.archiva.consumers.ConsumerException;
28  import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
29  import org.apache.archiva.model.ArtifactReference;
30  import org.apache.archiva.model.ProjectReference;
31  import org.apache.archiva.model.VersionedReference;
32  import org.apache.archiva.repository.ContentNotFoundException;
33  import org.apache.archiva.repository.ManagedRepositoryContent;
34  import org.apache.archiva.repository.RepositoryContentFactory;
35  import org.apache.archiva.repository.RepositoryException;
36  import org.apache.archiva.repository.RepositoryNotFoundException;
37  import org.apache.archiva.repository.layout.LayoutException;
38  import org.apache.archiva.repository.metadata.MetadataTools;
39  import org.apache.archiva.repository.metadata.RepositoryMetadataException;
40  import org.apache.archiva.redback.components.registry.Registry;
41  import org.apache.archiva.redback.components.registry.RegistryListener;
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  import org.springframework.context.annotation.Scope;
45  import org.springframework.stereotype.Service;
46  
47  import javax.annotation.PostConstruct;
48  import javax.inject.Inject;
49  import java.io.File;
50  import java.io.IOException;
51  import java.util.ArrayList;
52  import java.util.Date;
53  import java.util.List;
54  
55  /**
56   * MetadataUpdaterConsumer will create and update the metadata present within the repository.
57   *
58   *
59   */
60  @Service( "knownRepositoryContentConsumer#metadata-updater" )
61  @Scope( "prototype" )
62  public class MetadataUpdaterConsumer
63      extends AbstractMonitoredConsumer
64      implements KnownRepositoryContentConsumer
65      // it's prototype bean so we assume configuration won't change during a run
66      //, RegistryListener
67  {
68      private Logger log = LoggerFactory.getLogger( MetadataUpdaterConsumer.class );
69  
70      /**
71       * default-value="metadata-updater"
72       */
73      private String id = "metadata-updater";
74  
75      private String description = "Update / Create maven-metadata.xml files";
76  
77      @Inject
78      private RepositoryContentFactory repositoryFactory;
79  
80      @Inject
81      private MetadataTools metadataTools;
82  
83      @Inject
84      private ArchivaConfiguration configuration;
85  
86      @Inject
87      private FileTypes filetypes;
88  
89      private static final String TYPE_METADATA_BAD_INTERNAL_REF = "metadata-bad-internal-ref";
90  
91      private static final String TYPE_METADATA_WRITE_FAILURE = "metadata-write-failure";
92  
93      private static final String TYPE_METADATA_IO = "metadata-io-warning";
94  
95      private ManagedRepositoryContent repository;
96  
97      private File repositoryDir;
98  
99      private List<String> includes = new ArrayList<>( 0 );
100 
101     private long scanStartTimestamp = 0;
102 
103     @Override
104     public String getDescription()
105     {
106         return description;
107     }
108 
109     @Override
110     public String getId()
111     {
112         return id;
113     }
114 
115     public void setIncludes( List<String> includes )
116     {
117         this.includes = includes;
118     }
119 
120     @Override
121     public void beginScan( ManagedRepository repoConfig, Date whenGathered )
122         throws ConsumerException
123     {
124         try
125         {
126             this.repository = repositoryFactory.getManagedRepositoryContent( repoConfig.getId() );
127             this.repositoryDir = new File( repository.getRepoRoot() );
128             this.scanStartTimestamp = System.currentTimeMillis();
129         }
130         catch ( RepositoryNotFoundException e )
131         {
132             throw new ConsumerException( e.getMessage(), e );
133         }
134         catch ( RepositoryException e )
135         {
136             throw new ConsumerException( e.getMessage(), e );
137         }
138     }
139 
140     @Override
141     public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
142         throws ConsumerException
143     {
144         beginScan( repository, whenGathered );
145     }
146 
147     @Override
148     public void completeScan()
149     {
150         /* do nothing here */
151     }
152 
153     @Override
154     public void completeScan( boolean executeOnEntireRepo )
155     {
156         completeScan();
157     }
158 
159     @Override
160     public List<String> getExcludes()
161     {
162         return getDefaultArtifactExclusions();
163     }
164 
165     @Override
166     public List<String> getIncludes()
167     {
168         return this.includes;
169     }
170 
171     @Override
172     public void processFile( String path )
173         throws ConsumerException
174     {
175         // Ignore paths like .index etc
176         if ( !path.startsWith( "." ) )
177         {
178             try
179             {
180                 ArtifactReference artifact = repository.toArtifactReference( path );
181                 updateVersionMetadata( artifact, path );
182                 updateProjectMetadata( artifact, path );
183             }
184             catch ( LayoutException e )
185             {
186                 log.info( "Not processing path that is not an artifact: {} ({})", path, e.getMessage() );
187             }
188         }
189     }
190 
191     @Override
192     public void processFile( String path, boolean executeOnEntireRepo )
193         throws Exception
194     {
195         processFile( path );
196     }
197 
198     private void updateProjectMetadata( ArtifactReference artifact, String path )
199     {
200         ProjectReference projectRef = new ProjectReference();
201         projectRef.setGroupId( artifact.getGroupId() );
202         projectRef.setArtifactId( artifact.getArtifactId() );
203 
204         try
205         {
206             String metadataPath = this.metadataTools.toPath( projectRef );
207 
208             File projectMetadata = new File( this.repositoryDir, metadataPath );
209 
210             if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= this.scanStartTimestamp ) )
211             {
212                 // This metadata is up to date. skip it.
213                 log.debug( "Skipping uptodate metadata: {}", this.metadataTools.toPath( projectRef ) );
214                 return;
215             }
216 
217             metadataTools.updateMetadata( this.repository, projectRef );
218             log.debug( "Updated metadata: {}", this.metadataTools.toPath( projectRef ) );
219         }
220         catch ( LayoutException e )
221         {
222             log.warn( "Unable to convert path [{}] to an internal project reference: ", path, e );
223             triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF,
224                                     "Unable to convert path [" + path + "] to an internal project reference: "
225                                         + e.getMessage() );
226         }
227         catch ( RepositoryMetadataException e )
228         {
229             log.error( "Unable to write project metadat for artifact [{}]:", path, e );
230             triggerConsumerError( TYPE_METADATA_WRITE_FAILURE,
231                                   "Unable to write project metadata for artifact [" + path + "]: " + e.getMessage() );
232         }
233         catch ( IOException e )
234         {
235             log.warn( "Project metadata not written due to IO warning: ", e );
236             triggerConsumerWarning( TYPE_METADATA_IO,
237                                     "Project metadata not written due to IO warning: " + e.getMessage() );
238         }
239         catch ( ContentNotFoundException e )
240         {
241             log.warn( "Project metadata not written because no versions were found to update: ", e );
242             triggerConsumerWarning( TYPE_METADATA_IO,
243                                     "Project metadata not written because no versions were found to update: "
244                                         + e.getMessage() );
245         }
246     }
247 
248     private void updateVersionMetadata( ArtifactReference artifact, String path )
249     {
250         VersionedReference versionRef = new VersionedReference();
251         versionRef.setGroupId( artifact.getGroupId() );
252         versionRef.setArtifactId( artifact.getArtifactId() );
253         versionRef.setVersion( artifact.getVersion() );
254 
255         try
256         {
257             String metadataPath = this.metadataTools.toPath( versionRef );
258 
259             File projectMetadata = new File( this.repositoryDir, metadataPath );
260 
261             if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= this.scanStartTimestamp ) )
262             {
263                 // This metadata is up to date. skip it.
264                 log.debug( "Skipping uptodate metadata: {}", this.metadataTools.toPath( versionRef ) );
265                 return;
266             }
267 
268             metadataTools.updateMetadata( this.repository, versionRef );
269             log.debug( "Updated metadata: {}", this.metadataTools.toPath( versionRef ) );
270         }
271         catch ( LayoutException e )
272         {
273             log.warn( "Unable to convert path [{}] to an internal version reference: ", path, e );
274             triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF,
275                                     "Unable to convert path [" + path + "] to an internal version reference: "
276                                         + e.getMessage() );
277         }
278         catch ( RepositoryMetadataException e )
279         {
280             log.error( "Unable to write version metadata for artifact [{}]: ", path, e ); 
281             triggerConsumerError( TYPE_METADATA_WRITE_FAILURE,
282                                   "Unable to write version metadata for artifact [" + path + "]: " + e.getMessage() );
283         }
284         catch ( IOException e )
285         {
286             log.warn( "Version metadata not written due to IO warning: ", e );
287             triggerConsumerWarning( TYPE_METADATA_IO,
288                                     "Version metadata not written due to IO warning: " + e.getMessage() );
289         }
290         catch ( ContentNotFoundException e )
291         {
292             log.warn( "Version metadata not written because no versions were found to update: ", e ); 
293             triggerConsumerWarning( TYPE_METADATA_IO,
294                                     "Version metadata not written because no versions were found to update: "
295                                         + e.getMessage() );
296         }
297     }
298 
299     /*
300     @Override
301     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
302     {
303         if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
304         {
305             initIncludes();
306         }
307     }
308 
309     @Override
310     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
311     {
312         // do nothing here
313     }
314     */
315 
316     private void initIncludes()
317     {
318         includes = new ArrayList<>( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
319     }
320 
321     @PostConstruct
322     public void initialize()
323     {
324         //configuration.addChangeListener( this );
325 
326         initIncludes();
327     }
328 }