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