1 package org.apache.archiva.consumers.metadata;
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
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.List;
28 import org.apache.archiva.checksum.ChecksumAlgorithm;
29 import org.apache.archiva.checksum.ChecksummedFile;
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.maven.archiva.common.utils.VersionUtil;
35 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
36 import org.apache.maven.archiva.configuration.ConfigurationNames;
37 import org.apache.maven.archiva.configuration.FileTypes;
38 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
39 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
40 import org.apache.maven.archiva.consumers.ConsumerException;
41 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
42 import org.apache.maven.archiva.model.ArtifactReference;
43 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
44 import org.apache.maven.archiva.repository.layout.LayoutException;
45 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
46 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
47 import org.codehaus.plexus.registry.Registry;
48 import org.codehaus.plexus.registry.RegistryListener;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * Take an artifact off of disk and put it into the metadata repository.
55 * @version $Id: ArtifactUpdateDatabaseConsumer.java 718864 2008-11-19 06:33:35Z brett $
56 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
57 * role-hint="create-archiva-metadata" instantiation-strategy="per-lookup"
59 public class ArchivaMetadataCreationConsumer
60 extends AbstractMonitoredConsumer
61 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
64 * @plexus.configuration default-value="create-archiva-metadata"
69 * @plexus.configuration default-value="Create basic metadata for Archiva to be able to reference the artifact"
71 private String description;
76 private ArchivaConfiguration configuration;
81 private FileTypes filetypes;
83 private Date whenGathered;
88 private ManagedRepositoryContent repository;
90 private List<String> includes = new ArrayList<String>();
95 private MetadataRepository metadataRepository;
97 private static final Logger log = LoggerFactory.getLogger( ArchivaMetadataCreationConsumer.class );
104 public String getDescription()
106 return this.description;
109 public boolean isPermanent()
114 public List<String> getExcludes()
116 return getDefaultArtifactExclusions();
119 public List<String> getIncludes()
121 return this.includes;
124 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
125 throws ConsumerException
127 this.repository.setRepository( repo );
128 this.whenGathered = whenGathered;
131 public void processFile( String path )
132 throws ConsumerException
134 // note that we do minimal processing including checksums and POM information for performance of
135 // the initial scan. Any request for this information will be intercepted and populated on-demand
136 // or picked up by subsequent scans
137 ArtifactReference artifact;
140 artifact = repository.toArtifactReference( path );
142 catch ( LayoutException e )
144 throw new ConsumerException( e.getMessage(), e );
147 File file = new File( repository.getRepoRoot(), path );
149 ProjectMetadata project = new ProjectMetadata();
150 project.setNamespace( artifact.getGroupId() );
151 project.setId( artifact.getArtifactId() );
153 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
154 versionMetadata.setId( VersionUtil.getBaseVersion( artifact.getVersion() ) );
156 ArtifactMetadata artifactMeta = new ArtifactMetadata();
157 artifactMeta.setId( file.getName() );
158 artifactMeta.setFileLastModified( file.lastModified() );
159 artifactMeta.setSize( file.length() );
160 artifactMeta.setVersion( artifact.getVersion() );
161 artifactMeta.setWhenGathered( whenGathered );
163 ChecksummedFile checksummedFile = new ChecksummedFile( file );
166 artifactMeta.setMd5( checksummedFile.calculateChecksum( ChecksumAlgorithm.MD5 ) );
168 catch ( IOException e )
170 log.error( "Error attempting to get MD5 checksum for " + file + ": " + e.getMessage() );
174 artifactMeta.setSha1( checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 ) );
176 catch ( IOException e )
178 log.error( "Error attempting to get SHA-1 checksum for " + file + ": " + e.getMessage() );
181 // TODO: read the POM and fill in the rest of the information
184 // read the metadata and update it if it is newer or doesn't exist
185 metadataRepository.updateArtifact( repository.getId(), project.getNamespace(), project.getId(),
186 versionMetadata.getId(), artifactMeta );
187 metadataRepository.updateProjectVersion( repository.getId(), project.getNamespace(), project.getId(),
189 metadataRepository.updateProject( repository.getId(), project );
192 public void completeScan()
197 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
199 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
205 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
210 private void initIncludes()
214 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
217 public void initialize()
218 throws InitializationException
220 configuration.addChangeListener( this );