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