View Javadoc

1   package org.apache.maven.archiva.consumers.database;
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.maven.archiva.configuration.ArchivaConfiguration;
23  import org.apache.maven.archiva.configuration.ConfigurationNames;
24  import org.apache.maven.archiva.configuration.FileTypes;
25  import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26  import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
27  import org.apache.maven.archiva.consumers.ConsumerException;
28  import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
29  import org.apache.maven.archiva.database.ArchivaDAO;
30  import org.apache.maven.archiva.database.ArchivaDatabaseException;
31  import org.apache.maven.archiva.model.ArchivaArtifact;
32  import org.apache.maven.archiva.model.ArtifactReference;
33  import org.apache.maven.archiva.repository.ManagedRepositoryContent;
34  import org.apache.maven.archiva.repository.RepositoryContentFactory;
35  import org.apache.maven.archiva.repository.RepositoryException;
36  import org.apache.maven.archiva.repository.layout.LayoutException;
37  import org.codehaus.plexus.digest.Digester;
38  import org.codehaus.plexus.digest.DigesterException;
39  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
40  import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
41  import org.codehaus.plexus.registry.Registry;
42  import org.codehaus.plexus.registry.RegistryListener;
43  
44  import java.io.File;
45  import java.util.ArrayList;
46  import java.util.Date;
47  import java.util.List;
48  
49  /**
50   * ArtifactUpdateDatabaseConsumer - Take an artifact off of disk and put it into the repository.
51   *
52   * @version $Id: ArtifactUpdateDatabaseConsumer.java 1041824 2010-12-03 14:11:05Z brett $
53   * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
54   *                   role-hint="update-db-artifact"
55   *                   instantiation-strategy="per-lookup"
56   */
57  public class ArtifactUpdateDatabaseConsumer
58      extends AbstractMonitoredConsumer
59      implements KnownRepositoryContentConsumer, RegistryListener, Initializable
60  {                                                                  
61      private static final String TYPE_NOT_ARTIFACT = "file-not-artifact";
62  
63      private static final String DB_ERROR = "db-error";
64  
65      private static final String CHECKSUM_CALCULATION = "checksum-calc";
66  
67      /**
68       * @plexus.configuration default-value="update-db-artifact"
69       */
70      private String id;
71  
72      /**
73       * @plexus.configuration default-value="Update the Artifact in the Database"
74       */
75      private String description;
76  
77      /**
78       * @plexus.requirement role-hint="jdo"
79       */
80      private ArchivaDAO dao;
81  
82      /**
83       * @plexus.requirement
84       */
85      private ArchivaConfiguration configuration;
86  
87      /**
88       * @plexus.requirement
89       */
90      private FileTypes filetypes;
91  
92      /**
93       * @plexus.requirement
94       */
95      private RepositoryContentFactory repositoryFactory;
96  
97      /**
98       * @plexus.requirement role-hint="sha1"
99       */
100     private Digester digestSha1;
101 
102     /**
103      * @plexus.requirement role-hint="md5";
104      */
105     private Digester digestMd5;
106 
107     private ManagedRepositoryContent repository;
108 
109     private File repositoryDir;
110 
111     private List<String> includes = new ArrayList<String>();
112     
113     private Date whenGathered;
114 
115     public String getId()
116     {
117         return this.id;
118     }
119 
120     public String getDescription()
121     {
122         return this.description;
123     }
124 
125     public boolean isPermanent()
126     {
127         return true;
128     }
129 
130     public List<String> getExcludes()
131     {
132         return getDefaultArtifactExclusions();
133     }
134 
135     public List<String> getIncludes()
136     {
137         return this.includes;
138     }
139 
140     public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
141         throws ConsumerException
142     {
143         try
144         {
145             this.repository = repositoryFactory.getManagedRepositoryContent( repo.getId() );
146             this.repositoryDir = new File( repository.getRepoRoot() );
147             this.whenGathered = whenGathered;
148         }
149         catch(RepositoryException e)
150         {
151             throw new ConsumerException( "Unable to start ArtifactUpdateDatabaseConsumer: " + e.getMessage(), e );
152         }
153     }
154 
155     public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
156         throws ConsumerException
157     {
158         beginScan( repository, whenGathered );
159     }
160 
161     public void processFile( String path )
162         throws ConsumerException
163     {
164         ArchivaArtifact artifact = getLiveArtifact( path );
165 
166         if ( artifact == null )
167         {
168             return;
169         }
170         
171         try
172         {
173             if( artifact.getModel().getRepositoryId() == null )
174             {
175                 artifact.getModel().setRepositoryId( this.repository.getId() );
176             }
177 
178             // Calculate the hashcodes.
179             File artifactFile = new File( this.repositoryDir, path );
180             try
181             {
182                 artifact.getModel().setChecksumMD5( digestMd5.calc( artifactFile ) );
183             }
184             catch ( DigesterException e )
185             {
186                 triggerConsumerWarning( CHECKSUM_CALCULATION,
187                                         "Unable to calculate the MD5 checksum: " + e.getMessage() );
188             }
189 
190             try
191             {
192                 artifact.getModel().setChecksumSHA1( digestSha1.calc( artifactFile ) );
193             }
194             catch ( DigesterException e )
195             {
196                 triggerConsumerWarning( CHECKSUM_CALCULATION,
197                                         "Unable to calculate the SHA1 checksum: " + e.getMessage() );
198             }
199 
200             artifact.getModel().setLastModified( new Date( artifactFile.lastModified() ) );
201             artifact.getModel().setSize( artifactFile.length() );
202             artifact.getModel().setOrigin( "FileSystem" );
203             artifact.getModel().setWhenProcessed( null );
204             
205             // set this to when the artifact was first discovered in the repo
206             if ( artifact.getModel().getWhenGathered() == null )
207             {
208                 artifact.getModel().setWhenGathered( whenGathered );
209             }
210 
211             dao.getArtifactDAO().saveArtifact( artifact );
212         }
213         catch ( ArchivaDatabaseException e )
214         {
215             triggerConsumerError( DB_ERROR, "Unable to save artifact to database: " + e.getMessage() );
216         }
217     }
218 
219     public void processFile( String path, boolean executeOnEntireRepo )
220         throws Exception
221     {
222         processFile( path );
223     }
224 
225     /**
226      * Get a Live Artifact from a Path.
227      * <p/>
228      * Will resolve the artifact details from the path, and then return a database live version
229      * of that artifact.  Suitable for modification and saving (without the need to check for
230      * existance in database prior to save.)
231      *
232      * @param path the path to work from.
233      * @return the artifact that is suitable for database saving.
234      */
235     public ArchivaArtifact getLiveArtifact( String path )
236     {
237         try
238         {
239             ArtifactReference artifact = repository.toArtifactReference( path );
240 
241             ArchivaArtifact liveArtifact = dao.getArtifactDAO().createArtifact( artifact.getGroupId(),
242                                                                                 artifact.getArtifactId(),
243                                                                                 artifact.getVersion(),
244                                                                                 artifact.getClassifier(),
245                                                                                 artifact.getType(),
246                                                                                 repository.getId());
247 
248             return liveArtifact;
249         }
250         catch ( LayoutException e )
251         {
252             triggerConsumerError( TYPE_NOT_ARTIFACT,
253                                   "Path " + path + " cannot be converted to artifact: " + e.getMessage() );
254             return null;
255         }
256     }
257 
258     public void completeScan()
259     {
260         /* do nothing */
261     }
262 
263     public void completeScan( boolean executeOnEntireRepo )
264     {
265         completeScan();
266     }
267 
268     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
269     {
270         if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
271         {
272             initIncludes();
273         }
274     }
275 
276     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
277     {
278         /* do nothing */
279     }
280 
281     private void initIncludes()
282     {
283         includes.clear();
284 
285         includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
286     }
287 
288     public void initialize()
289         throws InitializationException
290     {
291         configuration.addChangeListener( this );
292 
293         initIncludes();
294     }
295 }