1 package org.apache.continuum.buildagent.manager;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.io.FileUtils;
23 import org.apache.commons.io.comparator.LastModifiedFileComparator;
24 import org.apache.commons.io.filefilter.AgeFileFilter;
25 import org.apache.commons.io.filefilter.AndFileFilter;
26 import org.apache.commons.io.filefilter.DirectoryFileFilter;
27 import org.apache.commons.io.filefilter.IOFileFilter;
28 import org.apache.commons.io.filefilter.NotFileFilter;
29 import org.apache.commons.io.filefilter.WildcardFileFilter;
30 import org.apache.continuum.buildagent.configuration.BuildAgentConfigurationService;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import java.io.File;
35 import java.io.FileFilter;
36 import java.io.IOException;
37 import java.util.Arrays;
38
39
40
41
42 public class DefaultBuildAgentPurgeManager
43 implements BuildAgentPurgeManager
44 {
45 private static final Logger logger = LoggerFactory.getLogger( DefaultBuildAgentPurgeManager.class );
46
47
48
49
50 private BuildAgentConfigurationService buildAgentConfigurationService;
51
52 public void executeDirectoryPurge( String directoryType, int daysOlder, int retentionCount, boolean deleteAll )
53 throws Exception
54 {
55 StringBuilder log = new StringBuilder().append(
56 "Executing directory purge with the following settings[directoryType=" ).
57 append( directoryType ).append( ",daysOlder=" ).
58 append( daysOlder ).append( ", retentionCount=" ).
59 append( retentionCount ).append( ", deleteAll=" ).
60 append( deleteAll ).append( "]" );
61 logger.info( log.toString() );
62
63 File directory = null;
64
65 if ( "working".equals( directoryType ) || "releases".equals( directoryType ) )
66 {
67 directory = buildAgentConfigurationService.getWorkingDirectory();
68 }
69 else
70 {
71 logger.warn( "Cannot execute purge: DirectoryType: " + directoryType + " is not valid." );
72 return;
73 }
74 if ( deleteAll )
75 {
76 purgeAll( directory, directoryType );
77 }
78 else
79 {
80 purgeFiles( directory, directoryType, daysOlder, retentionCount );
81 }
82
83 logger.info( "Directory purge execution done" );
84 }
85
86 private void purgeAll( File directory, String directoryType )
87 throws Exception
88 {
89 AndFileFilter filter = new AndFileFilter();
90 filter.addFileFilter( DirectoryFileFilter.DIRECTORY );
91 filter.addFileFilter( createFileFilterForDirectoryType( directoryType ) );
92
93 File[] files = directory.listFiles( (FileFilter) filter );
94 if ( files == null )
95 {
96 return;
97 }
98 for ( File file : files )
99 {
100 try
101 {
102 FileUtils.deleteDirectory( file );
103 }
104 catch ( IOException e )
105 {
106 logger.warn( "Unable to purge " + directoryType + " directory: " + file.getName() );
107 }
108 }
109 }
110
111 private void purgeFiles( File directory, String directoryType, int daysOlder, int retentionCount )
112 {
113 AndFileFilter filter = new AndFileFilter();
114 filter.addFileFilter( DirectoryFileFilter.DIRECTORY );
115 filter.addFileFilter( createFileFilterForDirectoryType( directoryType ) );
116
117 File[] files = directory.listFiles( (FileFilter) filter );
118
119 if ( files == null )
120 {
121 return;
122 }
123
124
125 int countToPurge = files.length - retentionCount;
126
127 if ( daysOlder > 0 )
128 {
129 long cutoff = System.currentTimeMillis() - ( 24 * 60 * 26 * 1000 * daysOlder );
130 filter.addFileFilter( new AgeFileFilter( cutoff ) );
131 }
132
133 files = directory.listFiles( (FileFilter) filter );
134
135 if ( files == null )
136 {
137 return;
138 }
139
140 Arrays.sort( files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR );
141
142 for ( File file : files )
143 {
144 if ( countToPurge - 1 < 0 )
145 {
146 break;
147 }
148 try
149 {
150 FileUtils.deleteDirectory( file );
151 countToPurge--;
152 }
153 catch ( IOException e )
154 {
155 logger.warn( "Unable to purge " + directoryType + " directory: " + file.getName() );
156 }
157 }
158
159 }
160
161 private IOFileFilter createFileFilterForDirectoryType( String directoryType )
162 {
163 WildcardFileFilter releasesFilter = new WildcardFileFilter( "releases-*" );
164
165 if ( "working".equals( directoryType ) )
166 {
167 return new NotFileFilter( releasesFilter );
168 }
169 else if ( "releases".equals( directoryType ) )
170 {
171 return releasesFilter;
172 }
173 else
174 {
175 return null;
176 }
177 }
178
179 public void setBuildAgentConfigurationService( BuildAgentConfigurationService buildAgentConfigurationService )
180 {
181 this.buildAgentConfigurationService = buildAgentConfigurationService;
182 }
183 }