1 package org.apache.maven.archiva.consumers.database;
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.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;
45 import java.util.ArrayList;
46 import java.util.Date;
47 import java.util.List;
50 * ArtifactUpdateDatabaseConsumer - Take an artifact off of disk and put it into the repository.
53 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
54 * role-hint="update-db-artifact"
55 * instantiation-strategy="per-lookup"
57 public class ArtifactUpdateDatabaseConsumer
58 extends AbstractMonitoredConsumer
59 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
61 private static final String TYPE_NOT_ARTIFACT = "file-not-artifact";
63 private static final String DB_ERROR = "db-error";
65 private static final String CHECKSUM_CALCULATION = "checksum-calc";
68 * @plexus.configuration default-value="update-db-artifact"
73 * @plexus.configuration default-value="Update the Artifact in the Database"
75 private String description;
78 * @plexus.requirement role-hint="jdo"
80 private ArchivaDAO dao;
85 private ArchivaConfiguration configuration;
90 private FileTypes filetypes;
95 private RepositoryContentFactory repositoryFactory;
98 * @plexus.requirement role-hint="sha1"
100 private Digester digestSha1;
103 * @plexus.requirement role-hint="md5";
105 private Digester digestMd5;
107 private ManagedRepositoryContent repository;
109 private File repositoryDir;
111 private List<String> includes = new ArrayList<String>();
113 private Date whenGathered;
115 public String getId()
120 public String getDescription()
122 return this.description;
125 public boolean isPermanent()
130 public List<String> getExcludes()
132 return getDefaultArtifactExclusions();
135 public List<String> getIncludes()
137 return this.includes;
140 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
141 throws ConsumerException
145 this.repository = repositoryFactory.getManagedRepositoryContent( repo.getId() );
146 this.repositoryDir = new File( repository.getRepoRoot() );
147 this.whenGathered = whenGathered;
149 catch(RepositoryException e)
151 throw new ConsumerException( "Unable to start ArtifactUpdateDatabaseConsumer: " + e.getMessage(), e );
155 public void processFile( String path )
156 throws ConsumerException
158 ArchivaArtifact artifact = getLiveArtifact( path );
160 if ( artifact == null )
167 if( artifact.getModel().getRepositoryId() == null )
169 artifact.getModel().setRepositoryId( this.repository.getId() );
172 // Calculate the hashcodes.
173 File artifactFile = new File( this.repositoryDir, path );
176 artifact.getModel().setChecksumMD5( digestMd5.calc( artifactFile ) );
178 catch ( DigesterException e )
180 triggerConsumerWarning( CHECKSUM_CALCULATION,
181 "Unable to calculate the MD5 checksum: " + e.getMessage() );
186 artifact.getModel().setChecksumSHA1( digestSha1.calc( artifactFile ) );
188 catch ( DigesterException e )
190 triggerConsumerWarning( CHECKSUM_CALCULATION,
191 "Unable to calculate the SHA1 checksum: " + e.getMessage() );
194 artifact.getModel().setLastModified( new Date( artifactFile.lastModified() ) );
195 artifact.getModel().setSize( artifactFile.length() );
196 artifact.getModel().setOrigin( "FileSystem" );
197 artifact.getModel().setWhenProcessed( null );
199 // set this to when the artifact was first discovered in the repo
200 if ( artifact.getModel().getWhenGathered() == null )
202 artifact.getModel().setWhenGathered( whenGathered );
205 dao.getArtifactDAO().saveArtifact( artifact );
207 catch ( ArchivaDatabaseException e )
209 triggerConsumerError( DB_ERROR, "Unable to save artifact to database: " + e.getMessage() );
214 * Get a Live Artifact from a Path.
216 * Will resolve the artifact details from the path, and then return a database live version
217 * of that artifact. Suitable for modification and saving (without the need to check for
218 * existance in database prior to save.)
220 * @param path the path to work from.
221 * @return the artifact that is suitable for database saving.
223 public ArchivaArtifact getLiveArtifact( String path )
227 ArtifactReference artifact = repository.toArtifactReference( path );
229 ArchivaArtifact liveArtifact = dao.getArtifactDAO().createArtifact( artifact.getGroupId(),
230 artifact.getArtifactId(),
231 artifact.getVersion(),
232 artifact.getClassifier(),
238 catch ( LayoutException e )
240 triggerConsumerError( TYPE_NOT_ARTIFACT,
241 "Path " + path + " cannot be converted to artifact: " + e.getMessage() );
246 public void completeScan()
251 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
253 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
259 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
264 private void initIncludes()
268 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
271 public void initialize()
272 throws InitializationException
274 configuration.addChangeListener( this );