1 package org.apache.maven.archiva.consumers.core;
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.RepositoryContentFactory;
42 import org.apache.maven.archiva.repository.RepositoryException;
43 import org.apache.maven.archiva.repository.layout.LayoutException;
44 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
45 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
46 import org.codehaus.plexus.registry.Registry;
47 import org.codehaus.plexus.registry.RegistryListener;
50 * ArtifactUpdateDatabaseConsumer - Take an artifact off of disk and put it into the repository.
52 * @version $Id: ArtifactUpdateDatabaseConsumer.java 718864 2008-11-19 06:33:35Z brett $
53 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
54 * role-hint="create-archiva-metadata" instantiation-strategy="per-lookup"
56 public class ArchivaMetadataCreationConsumer
57 extends AbstractMonitoredConsumer
58 implements KnownRepositoryContentConsumer, RegistryListener, Initializable
61 * @plexus.configuration default-value="create-archiva-metadata"
66 * @plexus.configuration default-value="Create basic metadata for Archiva to be able to reference the artifact"
68 private String description;
73 private ArchivaConfiguration configuration;
78 private FileTypes filetypes;
83 private RepositoryContentFactory repositoryFactory;
85 private Date whenGathered;
87 private ManagedRepositoryContent repository;
89 private List<String> includes = new ArrayList<String>();
91 private MetadataRepository metadataRepository;
98 public String getDescription()
100 return this.description;
103 public boolean isPermanent()
108 public List<String> getExcludes()
110 return getDefaultArtifactExclusions();
113 public List<String> getIncludes()
115 return this.includes;
118 public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered )
119 throws ConsumerException
123 this.repository = repositoryFactory.getManagedRepositoryContent( repo.getId() );
124 this.metadataRepository = new FileMetadataRepository( new File( repository.getRepoRoot(), ".metadata" ) );
125 this.whenGathered = whenGathered;
127 catch ( RepositoryException e )
129 throw new ConsumerException( "Unable to start ArtifactUpdateDatabaseConsumer: " + e.getMessage(), e );
133 public void processFile( String path )
134 throws ConsumerException
136 // note that we do minimal processing including checksums and POM information for performance of
137 // the initial scan. Any request for this information will be intercepted and populated on-demand
138 // or picked up by subsequent scans
139 ArtifactReference artifact;
142 artifact = repository.toArtifactReference( path );
144 catch ( LayoutException e )
146 throw new ConsumerException( e.getMessage(), e );
149 File file = new File( repository.getRepoRoot(), path );
151 // TODO: needed in a more central place, but trying to isolate impact to start with
152 String metadataId = artifact.getGroupId() + "." + artifact.getArtifactId();
154 ProjectMetadata project = new ProjectMetadata();
155 project.setId( metadataId );
157 ProjectBuildMetadata build = new ProjectBuildMetadata();
158 build.setId( artifact.getVersion() );
160 ArtifactMetadata artifactMeta = new ArtifactMetadata();
161 artifactMeta.setId( file.getName() );
162 artifactMeta.setUpdated( file.lastModified() );
163 artifactMeta.setSize( file.length() );
165 build.addArtifact( artifactMeta );
166 project.addBuild( build );
168 // TODO: store "whenGathered"
170 // read the metadata and update it if it is newer or doesn't exist
171 metadataRepository.update( project );
174 public void completeScan()
179 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
181 if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
187 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
192 private void initIncludes()
196 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
199 public void initialize()
200 throws InitializationException
202 configuration.addChangeListener( this );