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.util.ArrayList;
24 import java.util.Date;
25 import java.util.List;
27 import org.apache.archiva.metadata.model.ArtifactMetadata;
28 import org.apache.archiva.metadata.model.ProjectBuildMetadata;
29 import org.apache.archiva.metadata.model.ProjectMetadata;
30 import org.apache.archiva.metadata.repository.MetadataRepository;
31 import org.apache.archiva.metadata.repository.file.FileMetadataRepository;
32 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
33 import org.apache.maven.archiva.configuration.ConfigurationNames;
34 import org.apache.maven.archiva.configuration.FileTypes;
35 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
36 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
37 import org.apache.maven.archiva.consumers.ConsumerException;
38 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
39 import org.apache.maven.archiva.model.ArtifactReference;
40 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
41 import org.apache.maven.archiva.repository.layout.LayoutException;
42 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
43 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
44 import org.codehaus.plexus.registry.Registry;
45 import org.codehaus.plexus.registry.RegistryListener;
48 * Take an artifact off of disk and put it into the metadata repository.
50 * @version $Id: ArtifactUpdateDatabaseConsumer.java 718864 2008-11-19 06:33:35Z brett $
51 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
52 * role-hint="create-archiva-metadata" instantiation-strategy="per-lookup"
54 public class ArchivaMetadataCreationConsumer
55 extends AbstractMonitoredConsumer
56 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
59 * @plexus.configuration default-value="create-archiva-metadata"
64 * @plexus.configuration default-value="Create basic metadata for Archiva to be able to reference the artifact"
66 private String description;
71 private ArchivaConfiguration configuration;
76 private FileTypes filetypes;
78 private Date whenGathered;
83 private ManagedRepositoryContent repository;
85 private List<String> includes = new ArrayList<String>();
87 private MetadataRepository metadataRepository;
94 public String getDescription()
96 return this.description;
99 public boolean isPermanent()
104 public List<String> getExcludes()
106 return getDefaultArtifactExclusions();
109 public List<String> getIncludes()
111 return this.includes;
114 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
115 throws ConsumerException
117 this.repository.setRepository( repo );
118 // FIXME: remove hardcoding
119 this.metadataRepository = new FileMetadataRepository( new File( repository.getRepoRoot(), ".metadata" ) );
120 this.whenGathered = whenGathered;
123 public void processFile( String path )
124 throws ConsumerException
126 // note that we do minimal processing including checksums and POM information for performance of
127 // the initial scan. Any request for this information will be intercepted and populated on-demand
128 // or picked up by subsequent scans
129 ArtifactReference artifact;
132 artifact = repository.toArtifactReference( path );
134 catch ( LayoutException e )
136 throw new ConsumerException( e.getMessage(), e );
139 File file = new File( repository.getRepoRoot(), path );
141 // TODO: needed in a more central place, but trying to isolate impact to start with
142 String metadataId = artifact.getGroupId() + "." + artifact.getArtifactId();
144 ProjectMetadata project = new ProjectMetadata();
145 project.setId( metadataId );
147 ProjectBuildMetadata build = new ProjectBuildMetadata();
148 build.setId( artifact.getVersion() );
150 ArtifactMetadata artifactMeta = new ArtifactMetadata();
151 artifactMeta.setId( file.getName() );
152 artifactMeta.setUpdated( file.lastModified() );
153 artifactMeta.setSize( file.length() );
155 build.addArtifact( artifactMeta );
156 project.addBuild( build );
158 // TODO: store "whenGathered"
160 // read the metadata and update it if it is newer or doesn't exist
161 metadataRepository.update( project );
164 public void completeScan()
169 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
171 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
177 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
182 private void initIncludes()
186 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
189 public void initialize()
190 throws InitializationException
192 configuration.addChangeListener( this );