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.archiva.configuration.ArchivaConfiguration;
23 import org.apache.archiva.configuration.FileTypes;
24 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
25 import org.apache.archiva.consumers.ConsumerException;
26 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
27 import org.apache.archiva.metadata.repository.MetadataResolutionException;
28 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
29 import org.apache.archiva.metadata.repository.RepositorySession;
30 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
31 import org.apache.archiva.model.ArtifactReference;
32 import org.apache.archiva.components.registry.Registry;
33 import org.apache.archiva.components.registry.RegistryListener;
34 import org.apache.archiva.repository.ManagedRepositoryContent;
35 import org.apache.archiva.repository.LayoutException;
36 import org.apache.archiva.repository.ManagedRepository;
37 import org.apache.archiva.repository.BaseRepositoryContentLayout;
38 import org.apache.archiva.repository.RepositoryContentFactory;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.context.annotation.Scope;
42 import org.springframework.stereotype.Service;
44 import javax.annotation.PostConstruct;
45 import javax.inject.Inject;
46 import javax.inject.Named;
47 import java.util.ArrayList;
48 import java.util.Date;
49 import java.util.List;
52 * <code>SimpleArtifactConsumer</code>
55 @Service("knownRepositoryContentConsumer#simple")
57 public class SimpleArtifactConsumer
58 extends AbstractMonitoredConsumer
59 implements KnownRepositoryContentConsumer, RegistryListener
62 private Logger log = LoggerFactory.getLogger( SimpleArtifactConsumer.class );
65 * default-value="simple-artifact-consumer"
67 private String id = "simple-artifact-consumer";
69 private String description = "Simple consumer to illustrate how to consume the contents of a repository.";
72 private FileTypes filetypes;
75 private ArchivaConfiguration configuration;
77 private List<String> propertyNameTriggers = new ArrayList<>();
79 private List<String> includes = new ArrayList<>();
81 /** current repository being scanned */
82 private ManagedRepository repository;
85 @Named( value = "repositoryContentFactory#default" )
86 private RepositoryContentFactory repositoryContentFactory;
89 private RepositorySessionFactory repositorySessionFactory;
91 private RepositorySession repositorySession;
93 public void beginScan( ManagedRepository repository, Date whenGathered )
94 throws ConsumerException
96 beginScan( repository, whenGathered, true );
99 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
100 throws ConsumerException
102 this.repository = repository;
103 log.info( "Beginning scan of repository [{}]", this.repository.getId() );
107 repositorySession = repositorySessionFactory.createSession( );
108 } catch (MetadataRepositoryException e) {
109 log.error("Could not create repository session {}", e.getMessage());
110 throw new ConsumerException( "Could not create repository session: " + e.getMessage( ), e );
114 public void processFile( String path )
115 throws ConsumerException
117 processFile( path, true );
120 public void processFile( String path, boolean executeOnEntireRepo )
121 throws ConsumerException
123 log.info( "Processing entry [{}] from repository [{}]", path, this.repository.getId() );
127 ManagedRepositoryContent repositoryContent = repository.getContent();
128 BaseRepositoryContentLayout layout = repositoryContent.getLayout( BaseRepositoryContentLayout.class );
129 ArtifactReference artifact = layout.toArtifactReference( path );
131 repositorySession.getRepository().getArtifacts( repositorySession, repository.getId(), artifact.getGroupId(),
132 artifact.getArtifactId(), artifact.getVersion() );
134 catch ( LayoutException | MetadataResolutionException e )
136 throw new ConsumerException( e.getLocalizedMessage(), e );
140 public void completeScan()
142 completeScan( true );
145 public void completeScan( boolean executeOnEntireRepo )
147 log.info( "Finished scan of repository [" + this.repository.getId() + "]" );
149 repositorySession.close();
154 * Used by archiva to determine if the consumer wishes to process all of a repository's entries or just those that
155 * have been modified since the last scan.
157 * @return boolean true if the consumer wishes to process all entries on each scan, false for only those modified
158 * since the last scan
160 public boolean isProcessUnmodified()
162 return super.isProcessUnmodified();
165 public void afterConfigurationChange( org.apache.archiva.components.registry.Registry registry, String propertyName, Object propertyValue )
167 if ( propertyNameTriggers.contains( propertyName ) )
173 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
178 private void initIncludes()
181 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
185 public void initialize()
187 propertyNameTriggers = new ArrayList<>();
188 propertyNameTriggers.add( "repositoryScanning" );
189 propertyNameTriggers.add( "fileTypes" );
190 propertyNameTriggers.add( "fileType" );
191 propertyNameTriggers.add( "patterns" );
192 propertyNameTriggers.add( "pattern" );
194 configuration.addChangeListener( this );
199 public String getId()
204 public String getDescription()
206 return this.description;
209 public List<String> getExcludes()
214 public List<String> getIncludes()
216 return this.includes;
219 public boolean isPermanent()