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.repository.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";
70 private String description = "Simple consumer to illustrate how to consume the contents of a repository.";
73 private FileTypes filetypes;
76 private ArchivaConfiguration configuration;
78 private List<String> propertyNameTriggers = new ArrayList<>();
80 private List<String> includes = new ArrayList<>();
82 /** current repository being scanned */
83 private ManagedRepository repository;
86 @Named( value = "repositoryContentFactory#default" )
87 private RepositoryContentFactory repositoryContentFactory;
90 private RepositorySessionFactory repositorySessionFactory;
92 private RepositorySession repositorySession;
94 public void beginScan( ManagedRepository repository, Date whenGathered )
95 throws ConsumerException
97 beginScan( repository, whenGathered, true );
100 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
101 throws ConsumerException
103 this.repository = repository;
104 log.info( "Beginning scan of repository [{}]", this.repository.getId() );
106 repositorySession = repositorySessionFactory.createSession();
109 public void processFile( String path )
110 throws ConsumerException
112 processFile( path, true );
115 public void processFile( String path, boolean executeOnEntireRepo )
116 throws ConsumerException
118 log.info( "Processing entry [{}] from repository [{}]", path, this.repository.getId() );
122 ManagedRepositoryContent repositoryContent = repository.getContent();
123 ArtifactReference artifact = repositoryContent.toArtifactReference( path );
125 repositorySession.getRepository().getArtifacts( repository.getId(), artifact.getGroupId(),
126 artifact.getArtifactId(), artifact.getVersion() );
128 catch ( LayoutException | MetadataResolutionException e )
130 throw new ConsumerException( e.getLocalizedMessage(), e );
134 public void completeScan()
136 completeScan( true );
139 public void completeScan( boolean executeOnEntireRepo )
141 log.info( "Finished scan of repository [" + this.repository.getId() + "]" );
143 repositorySession.close();
148 * Used by archiva to determine if the consumer wishes to process all of a repository's entries or just those that
149 * have been modified since the last scan.
151 * @return boolean true if the consumer wishes to process all entries on each scan, false for only those modified
152 * since the last scan
154 public boolean isProcessUnmodified()
156 return super.isProcessUnmodified();
159 public void afterConfigurationChange( org.apache.archiva.redback.components.registry.Registry registry, String propertyName, Object propertyValue )
161 if ( propertyNameTriggers.contains( propertyName ) )
167 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
172 private void initIncludes()
175 includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
179 public void initialize()
181 propertyNameTriggers = new ArrayList<>();
182 propertyNameTriggers.add( "repositoryScanning" );
183 propertyNameTriggers.add( "fileTypes" );
184 propertyNameTriggers.add( "fileType" );
185 propertyNameTriggers.add( "patterns" );
186 propertyNameTriggers.add( "pattern" );
188 configuration.addChangeListener( this );
193 public String getId()
198 public String getDescription()
200 return this.description;
203 public List<String> getExcludes()
208 public List<String> getIncludes()
210 return this.includes;
213 public boolean isPermanent()