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 org.apache.commons.io.filefilter.DirectoryFileFilter;
23 import org.apache.continuum.purge.ContinuumPurgeConstants;
24 import org.apache.maven.archiva.consumers.core.repository.ArtifactFilenameFilter;
25 import org.codehaus.plexus.util.FileUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import java.io.File;
30 import java.io.FileFilter;
31 import java.io.FilenameFilter;
32 import java.io.IOException;
33
34
35
36
37 public class CleanAllPurgeExecutor
38 extends AbstractContinuumPurgeExecutor
39 {
40 private Logger log = LoggerFactory.getLogger( CleanAllPurgeExecutor.class );
41
42 private final String purgeType;
43
44 public CleanAllPurgeExecutor( String purgeType )
45 {
46 this.purgeType = purgeType;
47 }
48
49 public void purge( String path )
50 throws ContinuumPurgeExecutorException
51 {
52 if ( purgeType.equals( ContinuumPurgeConstants.PURGE_REPOSITORY ) )
53 {
54 purgeRepository( path );
55 }
56 else if ( purgeType.equals( ContinuumPurgeConstants.PURGE_DIRECTORY_RELEASES ) )
57 {
58 purgeReleases( path );
59 }
60 else if ( purgeType.equals( ContinuumPurgeConstants.PURGE_DIRECTORY_BUILDOUTPUT ) )
61 {
62 purgeBuildOutput( path );
63 }
64 }
65
66 private void purgeRepository( String path )
67 throws ContinuumPurgeExecutorException
68 {
69 try
70 {
71 FileUtils.cleanDirectory( path );
72 }
73 catch ( IOException e )
74 {
75 throw new ContinuumPurgeExecutorException( "Error while purging all artifacts or directories in " + path,
76 e );
77 }
78 log.info( ContinuumPurgeConstants.PURGE_REPO_CONTENTS + " - " + path );
79 }
80
81 private void purgeReleases( String path )
82 throws ContinuumPurgeExecutorException
83 {
84 File workingDir = new File( path );
85
86 FilenameFilter filter = new ArtifactFilenameFilter( "releases-" );
87
88 File[] releasesDir = workingDir.listFiles( filter );
89
90 try
91 {
92 for ( File releaseDir : releasesDir )
93 {
94 FileUtils.deleteDirectory( releaseDir );
95 log.info( ContinuumPurgeConstants.PURGE_DIR_CONTENTS + " - " + releaseDir.getName() );
96 }
97 }
98 catch ( IOException e )
99 {
100 throw new ContinuumPurgeExecutorException( "Error while purging all releases directories", e );
101 }
102 }
103
104 private void purgeBuildOutput( String path )
105 throws ContinuumPurgeExecutorException
106 {
107 File buildOutputDir = new File( path );
108
109 FileFilter filter = DirectoryFileFilter.DIRECTORY;
110
111 File[] projectsDir = buildOutputDir.listFiles( filter );
112
113 try
114 {
115 for ( File projectDir : projectsDir )
116 {
117 FileUtils.cleanDirectory( projectDir );
118 log.info( ContinuumPurgeConstants.PURGE_DIR_CONTENTS + " - " + projectDir.getName() );
119 }
120 }
121 catch ( IOException e )
122 {
123 throw new ContinuumPurgeExecutorException( "Error while purging all buildOutput directories", e );
124 }
125 }
126 }