View Javadoc

1   package org.apache.continuum.purge.executor;
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.continuum.purge.ContinuumPurgeConstants;
23  import org.apache.continuum.purge.repository.content.RepositoryManagedContent;
24  import org.apache.maven.archiva.consumers.core.repository.ArtifactFilenameFilter;
25  import org.apache.maven.archiva.model.ArtifactReference;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import java.io.File;
30  import java.io.FilenameFilter;
31  import java.util.Set;
32  
33  /**
34   * Some codes were taken from Archiva.
35   *
36   * @author Maria Catherine Tan
37   */
38  public abstract class AbstractContinuumPurgeExecutor
39      implements ContinuumPurgeExecutor
40  {
41      private static final Logger log = LoggerFactory.getLogger( AbstractContinuumPurgeExecutor.class );
42  
43      public void purge( Set<ArtifactReference> references, RepositoryManagedContent repository )
44      {
45          if ( references != null && !references.isEmpty() )
46          {
47              for ( ArtifactReference reference : references )
48              {
49                  File artifactFile = repository.toFile( reference );
50                  artifactFile.delete();
51                  log.info( ContinuumPurgeConstants.PURGE_ARTIFACT + " - " + artifactFile.getName() );
52                  purgeSupportFiles( artifactFile, artifactFile.getName() );
53                  // purge maven metadata
54                  purgeSupportFiles( artifactFile.getParentFile(), "maven-metadata" );
55              }
56          }
57      }
58  
59      /**
60       * <p>
61       * This find support files for the artifactFile and deletes them.
62       * </p>
63       * <p>
64       * Support Files are things like ".sha1", ".md5", ".asc", etc.
65       * </p>
66       *
67       * @param artifactFile the file to base off of.
68       */
69      protected void purgeSupportFiles( File artifactFile, String filename )
70      {
71          File parentDir = artifactFile.getParentFile();
72  
73          if ( !parentDir.exists() )
74          {
75              return;
76          }
77  
78          FilenameFilter filter = new ArtifactFilenameFilter( filename );
79  
80          File[] files = parentDir.listFiles( filter );
81  
82          for ( File file : files )
83          {
84              if ( file.exists() && file.isFile() )
85              {
86                  file.delete();
87                  log.info( ContinuumPurgeConstants.PURGE_FILE + " - " + file.getName() );
88              }
89          }
90      }
91  }