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.commons.io.FileUtils;
23  import org.apache.commons.io.comparator.LastModifiedFileComparator;
24  import org.apache.commons.io.filefilter.DirectoryFileFilter;
25  import org.apache.continuum.purge.ContinuumPurgeConstants;
26  import org.apache.maven.archiva.consumers.core.repository.ArtifactFilenameFilter;
27  
28  import java.io.File;
29  import java.io.FileFilter;
30  import java.io.FilenameFilter;
31  import java.io.IOException;
32  import java.util.Arrays;
33  
34  /**
35   * @author Maria Catherine Tan
36   */
37  public class RetentionCountDirectoryPurgeExecutor
38      extends AbstractContinuumPurgeExecutor
39      implements ContinuumPurgeExecutor
40  {
41      private final int retentionCount;
42  
43      private final String directoryType;
44  
45      public RetentionCountDirectoryPurgeExecutor( int retentionCount, String directoryType )
46      {
47          this.retentionCount = retentionCount;
48  
49          this.directoryType = directoryType;
50      }
51  
52      public void purge( String path )
53          throws ContinuumPurgeExecutorException
54      {
55          if ( directoryType.equals( ContinuumPurgeConstants.PURGE_DIRECTORY_RELEASES ) )
56          {
57              purgeReleaseDirectory( path );
58          }
59          else if ( directoryType.equals( ContinuumPurgeConstants.PURGE_DIRECTORY_BUILDOUTPUT ) )
60          {
61              purgeBuildOutputDirectory( path );
62          }
63      }
64  
65      private void purgeReleaseDirectory( String path )
66      {
67          File releaseDir = new File( path );
68  
69          FilenameFilter filter = new ArtifactFilenameFilter( "releases-" );
70  
71          File[] files = releaseDir.listFiles( filter );
72  
73          if ( retentionCount > files.length )
74          {
75              return;
76          }
77  
78          Arrays.sort( files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR );
79  
80          int countToPurge = files.length - retentionCount;
81  
82          for ( File file : files )
83          {
84              if ( countToPurge <= 0 )
85              {
86                  break;
87              }
88  
89              try
90              {
91                  FileUtils.deleteDirectory( file );
92                  countToPurge--;
93              }
94              catch ( IOException e )
95              {
96                  //throw new ContinuumPurgeExecutorException( "Error while purging release directories", e );
97              }
98          }
99      }
100 
101     private void purgeBuildOutputDirectory( String path )
102     {
103         File buildOutputDir = new File( path );
104 
105         FileFilter filter = DirectoryFileFilter.DIRECTORY;
106 
107         File[] projectsDir = buildOutputDir.listFiles( filter );
108 
109         for ( File projectDir : projectsDir )
110         {
111             File[] buildsDir = projectDir.listFiles( filter );
112 
113             if ( retentionCount > buildsDir.length )
114             {
115                 continue;
116             }
117 
118             Arrays.sort( buildsDir, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR );
119 
120             int countToPurge = buildsDir.length - retentionCount;
121 
122             for ( File buildDir : buildsDir )
123             {
124                 if ( countToPurge-- <= 0 )
125                 {
126                     break;
127                 }
128 
129                 try
130                 {
131                     FileUtils.deleteDirectory( buildDir );
132                     File logFile = new File( buildDir.getAbsoluteFile() + ".log.txt" );
133 
134                     if ( logFile.exists() )
135                     {
136                         logFile.delete();
137                     }
138                 }
139                 catch ( IOException e )
140                 {
141                     //swallow?
142                 }
143             }
144         }
145     }
146 }