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.
52 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
54 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
55 * role-hint="update-db-artifact"
56 * instantiation-strategy="per-lookup"
58 public class ArtifactUpdateDatabaseConsumer
59 extends AbstractMonitoredConsumer
60 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
62 private static final String TYPE_NOT_ARTIFACT = "file-not-artifact";
64 private static final String DB_ERROR = "db-error";
66 private static final String CHECKSUM_CALCULATION = "checksum-calc";
69 * @plexus.configuration default-value="update-db-artifact"
74 * @plexus.configuration default-value="Update the Artifact in the Database"
76 private String description;
79 * @plexus.requirement role-hint="jdo"
81 private ArchivaDAO dao;
86 private ArchivaConfiguration configuration;
91 private FileTypes filetypes;
96 private RepositoryContentFactory repositoryFactory;
99 * @plexus.requirement role-hint="sha1"
101 private Digester digestSha1;
104 * @plexus.requirement role-hint="md5";
106 private Digester digestMd5;
108 private ManagedRepositoryContent repository;
110 private File repositoryDir;
112 private List<String> includes = new ArrayList<String>();
114 public String getId()
119 public String getDescription()
121 return this.description;
124 public boolean isPermanent()
129 public List<String> getExcludes()
134 public List<String> getIncludes()
136 return this.includes;
139 public void beginScan( ManagedRepositoryConfiguration repo )
140 throws ConsumerException
144 this.repository = repositoryFactory.getManagedRepositoryContent( repo.getId() );
145 this.repositoryDir = new File( repository.getRepoRoot() );
147 catch(RepositoryException e)
149 throw new ConsumerException( "Unable to start ArtifactUpdateDatabaseConsumer: " + e.getMessage(), e );
153 public void processFile( String path )
154 throws ConsumerException
156 ArchivaArtifact artifact = getLiveArtifact( path );
158 if ( artifact == null )
165 artifact.getModel().setRepositoryId( this.repository.getId() );
167 // Calculate the hashcodes.
168 File artifactFile = new File( this.repositoryDir, path );
171 artifact.getModel().setChecksumMD5( digestMd5.calc( artifactFile ) );
173 catch ( DigesterException e )
175 triggerConsumerWarning( CHECKSUM_CALCULATION,
176 "Unable to calculate the MD5 checksum: " + e.getMessage() );
181 artifact.getModel().setChecksumSHA1( digestSha1.calc( artifactFile ) );
183 catch ( DigesterException e )
185 triggerConsumerWarning( CHECKSUM_CALCULATION,
186 "Unable to calculate the SHA1 checksum: " + e.getMessage() );
189 artifact.getModel().setLastModified( new Date( artifactFile.lastModified() ) );
190 artifact.getModel().setSize( artifactFile.length() );
191 artifact.getModel().setOrigin( "FileSystem" );
193 dao.getArtifactDAO().saveArtifact( artifact );
195 catch ( ArchivaDatabaseException e )
197 triggerConsumerError( DB_ERROR, "Unable to save artifact to database: " + e.getMessage() );
202 * Get a Live Artifact from a Path.
204 * Will resolve the artifact details from the path, and then return a database live version
205 * of that artifact. Suitable for modification and saving (without the need to check for
206 * existance in database prior to save.)
208 * @param path the path to work from.
209 * @return the artifact that is suitable for database saving.
211 public ArchivaArtifact getLiveArtifact( String path )
215 ArtifactReference artifact = repository.toArtifactReference( path );
217 ArchivaArtifact liveArtifact = dao.getArtifactDAO().createArtifact( artifact.getGroupId(),
218 artifact.getArtifactId(),
219 artifact.getVersion(),
220 artifact.getClassifier(),
221 artifact.getType() );
225 catch ( LayoutException e )
227 triggerConsumerError( TYPE_NOT_ARTIFACT,
228 "Path " + path + " cannot be converted to artifact: " + e.getMessage() );
233 public void completeScan()
238 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
240 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
246 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
251 private void initIncludes()
255 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
258 public void initialize()
259 throws InitializationException
261 configuration.addChangeListener( this );