1 package org.apache.maven.archiva.repository.scanner;
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.List;
26 import org.apache.archiva.rss.processor.RssFeedProcessor;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.maven.archiva.configuration.FileTypes;
29 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
31 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
32 import org.apache.maven.archiva.consumers.RepositoryContentConsumer;
33 import org.apache.maven.archiva.model.ArchivaArtifact;
34 import org.apache.maven.archiva.model.ArtifactReference;
35 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
36 import org.apache.maven.archiva.repository.RepositoryContentFactory;
37 import org.apache.maven.archiva.repository.RepositoryException;
38 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
39 import org.apache.maven.archiva.repository.layout.LayoutException;
40 import org.codehaus.plexus.util.DirectoryWalker;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * DefaultRepositoryScanner
47 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
49 * @plexus.component role="org.apache.maven.archiva.repository.scanner.RepositoryScanner"
51 public class DefaultRepositoryScanner
52 implements RepositoryScanner
54 private Logger log = LoggerFactory.getLogger( DefaultRepositoryScanner.class );
59 private FileTypes filetypes;
64 private RepositoryContentConsumers consumerUtil;
69 private RepositoryContentFactory repositoryFactory;
72 * @plexus.requirement role-hint="new-artifacts"
74 private RssFeedProcessor rssFeedProcessor;
76 public RepositoryScanStatistics scan( ManagedRepositoryConfiguration repository, long changesSince )
77 throws RepositoryException
79 List<KnownRepositoryContentConsumer> knownContentConsumers = consumerUtil.getSelectedKnownConsumers();
80 List<InvalidRepositoryContentConsumer> invalidContentConsumers = consumerUtil.getSelectedInvalidConsumers();
81 List<String> ignoredPatterns = filetypes.getFileTypePatterns( FileTypes.IGNORED );
83 return scan( repository, knownContentConsumers, invalidContentConsumers, ignoredPatterns, changesSince );
86 public RepositoryScanStatistics scan( ManagedRepositoryConfiguration repository,
87 List<KnownRepositoryContentConsumer> knownContentConsumers,
88 List<InvalidRepositoryContentConsumer> invalidContentConsumers,
89 List<String> ignoredContentPatterns, long changesSince )
90 throws RepositoryException
92 if ( repository == null )
94 throw new IllegalArgumentException( "Unable to operate on a null repository." );
97 File repositoryBase = new File( repository.getLocation() );
99 if ( !repositoryBase.exists() )
101 throw new UnsupportedOperationException( "Unable to scan a repository, directory "
102 + repositoryBase.getAbsolutePath() + " does not exist." );
105 if ( !repositoryBase.isDirectory() )
107 throw new UnsupportedOperationException( "Unable to scan a repository, path "
108 + repositoryBase.getAbsolutePath() + " is not a directory." );
111 // Setup Includes / Excludes.
113 List<String> allExcludes = new ArrayList<String>();
114 List<String> allIncludes = new ArrayList<String>();
116 if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
118 allExcludes.addAll( ignoredContentPatterns );
121 // Scan All Content. (intentional)
122 allIncludes.add( "**/*" );
124 // Setup Directory Walker
125 DirectoryWalker dirWalker = new DirectoryWalker();
127 dirWalker.setBaseDir( repositoryBase );
129 dirWalker.setIncludes( allIncludes );
130 dirWalker.setExcludes( allExcludes );
132 // Setup the Scan Instance
133 RepositoryScannerInstance scannerInstance = new RepositoryScannerInstance( repository, knownContentConsumers,
134 invalidContentConsumers, changesSince );
136 dirWalker.addDirectoryWalkListener( scannerInstance );
141 RepositoryScanStatistics stats = scannerInstance.getStatistics();
143 stats.setKnownConsumers( gatherIds( knownContentConsumers ) );
144 stats.setInvalidConsumers( gatherIds( invalidContentConsumers ) );
146 // generate RSS feeds
147 List<ArchivaArtifact> newArtifacts = getNewArtifacts( scannerInstance.getNewFiles(), repository.getId() );
148 rssFeedProcessor.process( newArtifacts );
153 private List<String> gatherIds( List<? extends RepositoryContentConsumer> consumers )
155 List<String> ids = new ArrayList<String>();
156 for ( RepositoryContentConsumer consumer : consumers )
158 ids.add( consumer.getId() );
163 private List<ArchivaArtifact> getNewArtifacts( List<File> files, String repoId )
165 List<ArchivaArtifact> newArtifacts = new ArrayList<ArchivaArtifact>();
167 // TODO: filter the file types of artifacts that will be included in the rss feeds
170 ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repoId );
171 for( File file : files )
175 ArtifactReference ref = repository.toArtifactReference( file.getAbsolutePath() );
176 ArchivaArtifact artifact = new ArchivaArtifact( ref.getGroupId(),ref.getArtifactId(), ref.getVersion(),
177 ref.getClassifier(), ref.getType() );
178 artifact.getModel().setRepositoryId( repoId );
179 newArtifacts.add( artifact );
181 catch ( LayoutException le )
187 catch ( RepositoryNotFoundException re )
189 log.error( re.getMessage() );
191 catch ( RepositoryException e )
193 log.error( e.getMessage() );