View Javadoc

1   package org.apache.continuum.purge.repository.scanner;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.commons.collections.CollectionUtils;
23  import org.apache.continuum.model.repository.LocalRepository;
24  import org.apache.continuum.purge.controller.PurgeController;
25  import org.apache.continuum.purge.executor.ContinuumPurgeExecutorException;
26  import org.apache.continuum.purge.repository.utils.FileTypes;
27  import org.codehaus.plexus.util.DirectoryWalker;
28  
29  import java.io.File;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * Codes were taken from Archiva and made some changes.
35   *
36   * @plexus.component role="org.apache.continuum.purge.repository.scanner.RepositoryScanner" role-hint="repository-scanner"
37   */
38  public class DefaultRepositoryScanner
39      implements RepositoryScanner
40  {
41      /**
42       * @plexus.requirement role-hint="file-types"
43       */
44      private FileTypes filetypes;
45  
46      public void scan( LocalRepository repository, PurgeController purgeController )
47          throws ContinuumPurgeExecutorException
48      {
49          List<String> ignoredPatterns = filetypes.getIgnoredFileTypePatterns();
50          scan( repository, purgeController, ignoredPatterns );
51      }
52  
53      public void scan( LocalRepository repository, PurgeController purgeController, List<String> ignoredContentPatterns )
54          throws ContinuumPurgeExecutorException
55      {
56          File repositoryBase = new File( repository.getLocation() );
57  
58          if ( !repositoryBase.exists() )
59          {
60              throw new UnsupportedOperationException(
61                  "Unable to scan a repository, directory " + repositoryBase.getAbsolutePath() + " does not exist." );
62          }
63  
64          if ( !repositoryBase.isDirectory() )
65          {
66              throw new UnsupportedOperationException(
67                  "Unable to scan a repository, path " + repositoryBase.getAbsolutePath() + " is not a directory." );
68          }
69  
70          // Setup Includes / Excludes.
71  
72          List<String> allExcludes = new ArrayList<String>();
73          List<String> allIncludes = new ArrayList<String>();
74  
75          if ( CollectionUtils.isNotEmpty( ignoredContentPatterns ) )
76          {
77              allExcludes.addAll( ignoredContentPatterns );
78          }
79  
80          // Scan All Content. (intentional)
81          allIncludes.add( "**/*" );
82  
83          // Setup Directory Walker
84          DirectoryWalker dirWalker = new DirectoryWalker();
85  
86          dirWalker.setBaseDir( repositoryBase );
87  
88          dirWalker.setIncludes( allIncludes );
89          dirWalker.setExcludes( allExcludes );
90  
91          RepositoryScannerInstance scannerInstance = new RepositoryScannerInstance( repository, purgeController );
92  
93          dirWalker.addDirectoryWalkListener( scannerInstance );
94  
95          // Execute scan.
96          dirWalker.scan();
97  
98      }
99  }