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 java.util.ArrayList;
23 import java.util.Date;
24 import java.util.List;
26 import org.apache.archiva.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.FileTypes;
28 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
29 import org.apache.archiva.consumers.ConsumerException;
30 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
31 import org.apache.archiva.metadata.repository.MetadataResolutionException;
32 import org.apache.archiva.metadata.repository.RepositorySession;
33 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
34 import org.apache.archiva.model.ArtifactReference;
35 import org.apache.archiva.redback.components.registry.Registry;
36 import org.apache.archiva.redback.components.registry.RegistryListener;
38 import org.apache.archiva.repository.ManagedRepositoryContent;
39 import org.apache.archiva.repository.RepositoryContentFactory;
40 import org.apache.archiva.repository.RepositoryException;
41 import org.apache.archiva.repository.layout.LayoutException;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import javax.annotation.PostConstruct;
45 import javax.inject.Inject;
46 import javax.inject.Named;
48 import org.springframework.context.annotation.Scope;
49 import org.springframework.stereotype.Service;
50 import org.apache.archiva.admin.model.beans.ManagedRepository;
53 * <code>SimpleArtifactConsumer</code>
56 @Service("knownRepositoryContentConsumer#simple")
58 public class SimpleArtifactConsumer
59 extends AbstractMonitoredConsumer
60 implements KnownRepositoryContentConsumer, RegistryListener
63 private Logger log = LoggerFactory.getLogger( SimpleArtifactConsumer.class );
66 * default-value="simple-artifact-consumer"
68 private String id = "simple-artifact-consumer";
73 private String description = "Simple consumer to illustrate how to consume the contents of a repository.";
79 private FileTypes filetypes;
85 private ArchivaConfiguration configuration;
87 private List<String> propertyNameTriggers = new ArrayList<>();
89 private List<String> includes = new ArrayList<>();
91 /** current repository being scanned */
92 private ManagedRepository repository;
95 @Named( value = "repositoryContentFactory#default" )
96 private RepositoryContentFactory repositoryContentFactory;
99 private RepositorySessionFactory repositorySessionFactory;
101 private RepositorySession repositorySession;
103 public void beginScan( ManagedRepository repository, Date whenGathered )
104 throws ConsumerException
106 beginScan( repository, whenGathered, true );
109 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
110 throws ConsumerException
112 this.repository = repository;
113 log.info( "Beginning scan of repository [" + this.repository.getId() + "]" );
115 repositorySession = repositorySessionFactory.createSession();
118 public void processFile( String path )
119 throws ConsumerException
121 processFile( path, true );
124 public void processFile( String path, boolean executeOnEntireRepo )
125 throws ConsumerException
127 log.info( "Processing entry [" + path + "] from repository [" + this.repository.getId() + "]" );
131 ManagedRepositoryContent repositoryContent = repositoryContentFactory.getManagedRepositoryContent( repository.getId() );
132 ArtifactReference artifact = repositoryContent.toArtifactReference( path );
134 repositorySession.getRepository().getArtifacts( repository.getId(), artifact.getGroupId(),
135 artifact.getArtifactId(), artifact.getVersion() );
137 catch ( RepositoryException | LayoutException | MetadataResolutionException e )
139 throw new ConsumerException( e.getLocalizedMessage(), e );
143 public void completeScan()
145 completeScan( true );
148 public void completeScan( boolean executeOnEntireRepo )
150 log.info( "Finished scan of repository [" + this.repository.getId() + "]" );
152 repositorySession.close();
157 * Used by archiva to determine if the consumer wishes to process all of a repository's entries or just those that
158 * have been modified since the last scan.
160 * @return boolean true if the consumer wishes to process all entries on each scan, false for only those modified
161 * since the last scan
163 public boolean isProcessUnmodified()
165 return super.isProcessUnmodified();
168 public void afterConfigurationChange( org.apache.archiva.redback.components.registry.Registry registry, String propertyName, Object propertyValue )
170 if ( propertyNameTriggers.contains( propertyName ) )
176 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
181 private void initIncludes()
184 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
188 public void initialize()
190 propertyNameTriggers = new ArrayList<>();
191 propertyNameTriggers.add( "repositoryScanning" );
192 propertyNameTriggers.add( "fileTypes" );
193 propertyNameTriggers.add( "fileType" );
194 propertyNameTriggers.add( "patterns" );
195 propertyNameTriggers.add( "pattern" );
197 configuration.addChangeListener( this );
202 public String getId()
207 public String getDescription()
209 return this.description;
212 public List<String> getExcludes()
217 public List<String> getIncludes()
219 return this.includes;
222 public boolean isPermanent()