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