Delete old job directory automatically
[jabaws.git] / engine / compbio / engine / Cleaner.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.engine;\r
19 \r
20 import java.io.File;\r
21 import java.util.List;\r
22 \r
23 import org.apache.log4j.Logger;\r
24 \r
25 import compbio.engine.conf.PropertyHelperManager;\r
26 import compbio.engine.client.ConfiguredExecutable;\r
27 import compbio.engine.client.PathValidator;\r
28 import compbio.engine.local.ExecutableWrapper;\r
29 import compbio.util.PropertyHelper;\r
30 \r
31 //@Deprecated\r
32 \r
33 // TODO\r
34 // understand what this class does and why it was set as deprecated ...\r
35 // how to check timestamps of files before deleting\r
36 public class Cleaner {\r
37 \r
38         private static final Logger log = Logger.getLogger(Cleaner.class);\r
39 \r
40         /**\r
41          * This method returns true if all files specified by List files were\r
42          * successfully removed or there was no files to remove (files list was\r
43          * empty)\r
44          * \r
45          * @param workDirectory\r
46          * @param files\r
47          * @return This method returns true if all files specified by List files\r
48          *         were successfully removed, false otherwise\r
49          */\r
50         public static boolean deleteFiles(ConfiguredExecutable<?> exec) {\r
51 \r
52                 if (exec == null) {\r
53                         throw new IllegalArgumentException("Executable must be provided!");\r
54                 }\r
55 \r
56                 List<String> files = exec.getCreatedFiles();\r
57                 for (String fname : files) {\r
58                         assert PathValidator.isAbsolutePath(fname) : " Absolute path must be provided but got: "\r
59                                         + fname;\r
60                         removeFile(fname);\r
61                 }\r
62                 // Remove process std output and error capture files, do not care\r
63                 // whether succeed or not\r
64                 // as these are only created for local processes, so may not exist\r
65                 removeFile(exec.getWorkDirectory() + File.separator + ExecutableWrapper.PROC_OUT_FILE);\r
66                 removeFile(exec.getWorkDirectory() + File.separator + ExecutableWrapper.PROC_ERR_FILE);\r
67                 // Remove the task directory if all files were successfully removed\r
68                 return removeFile(exec.getWorkDirectory());\r
69         }\r
70 \r
71         static boolean removeFile(String filename) {\r
72                 File outfile = new File(filename);\r
73                 boolean success = false;\r
74                 if (outfile.exists()) {\r
75                         String type = outfile.isDirectory() ? "Directory " : "File ";\r
76                         success = outfile.delete();\r
77                         if (success) {\r
78                                 log.trace(type + filename + " was successfully removed");\r
79                         } else {\r
80                                 log.debug("Could not remove "\r
81                                                 + type\r
82                                                 + ": "\r
83                                                 + filename\r
84                                                 + " reportedly created by executable. Insufficient access right?");\r
85                         }\r
86                 } else {\r
87                         log.debug("File: " + filename\r
88                                         + " does not appear ro exist. Could have been removed?");\r
89                 }\r
90                 return success;\r
91         }\r
92 \r
93         public static boolean deleteAllFiles(String directory) {\r
94                 if (compbio.util.Util.isEmpty(directory)) {\r
95                         throw new NullPointerException("Directory must be provided! ");\r
96                 }\r
97                 File rootdir = new File(directory);\r
98                 if (!rootdir.exists()) {\r
99                         log.error("Directory " + directory + " does not exist. Have been deleted already?");\r
100                         return false;\r
101                 }\r
102                 if (!rootdir.isDirectory()) {\r
103                         log.error("Directory is expected by file given! Skipping... ");\r
104                         return false;\r
105                 }\r
106 \r
107                 File[] files = rootdir.listFiles();\r
108                 int deletedCount = 0;\r
109                 for (File f : files) {\r
110                         if (f.isDirectory()) {\r
111                                 log.error("Cannot delete subdirectories! Skipping...");\r
112                         } else {\r
113                                 if (f.delete()) {\r
114                                         deletedCount++;\r
115                                 }\r
116                         }\r
117                 }\r
118                 rootdir.delete();\r
119                 return deletedCount == files.length;\r
120         }\r
121 \r
122         public static boolean deleteDirectory(String directory) {\r
123                 if (deleteAllFiles (directory)) {\r
124                         File rootdir = new File(directory);\r
125                         return rootdir.delete();\r
126                 }\r
127                 return false;\r
128         }\r
129 \r
130         \r
131 }\r