Code for cleaning up old job directories
[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.client.ConfiguredExecutable;\r
26 import compbio.engine.client.PathValidator;\r
27 import compbio.engine.local.ExecutableWrapper;\r
28 \r
29 //@Deprecated\r
30 \r
31 // TODO\r
32 // understand what this class does and why it was set as deprecated ...\r
33 // how to check timestamps of files before deleting\r
34 public class Cleaner {\r
35 \r
36         private static final Logger log = Logger.getLogger(Cleaner.class);\r
37 \r
38         /**\r
39          * This method returns true if all files specified by List files were\r
40          * successfully removed or there was no files to remove (files list was\r
41          * empty)\r
42          * \r
43          * @param workDirectory\r
44          * @param files\r
45          * @return This method returns true if all files specified by List files\r
46          *         were successfully removed, false otherwise\r
47          */\r
48         public static boolean deleteFiles(ConfiguredExecutable<?> exec) {\r
49 \r
50                 if (exec == null) {\r
51                         throw new IllegalArgumentException("Executable must be provided!");\r
52                 }\r
53 \r
54                 List<String> files = exec.getCreatedFiles();\r
55                 for (String fname : files) {\r
56                         assert PathValidator.isAbsolutePath(fname) : " Absolute path must be provided but got: "\r
57                                         + fname;\r
58                         removeFile(fname);\r
59                 }\r
60                 // Remove process std output and error capture files, do not care\r
61                 // whether succeed or not\r
62                 // as these are only created for local processes, so may not exist\r
63                 removeFile(exec.getWorkDirectory() + File.separator + ExecutableWrapper.PROC_OUT_FILE);\r
64                 removeFile(exec.getWorkDirectory() + File.separator + ExecutableWrapper.PROC_ERR_FILE);\r
65                 // Remove the task directory if all files were successfully removed\r
66                 return removeFile(exec.getWorkDirectory());\r
67         }\r
68 \r
69         static boolean removeFile(String filename) {\r
70                 File outfile = new File(filename);\r
71                 boolean success = false;\r
72                 if (outfile.exists()) {\r
73                         String type = outfile.isDirectory() ? "Directory " : "File ";\r
74                         success = outfile.delete();\r
75                         if (success) {\r
76                                 log.trace(type + filename + " was successfully removed");\r
77                         } else {\r
78                                 log.debug("Could not remove "\r
79                                                 + type\r
80                                                 + ": "\r
81                                                 + filename\r
82                                                 + " reportedly created by executable. Insufficient access right?");\r
83                         }\r
84                 } else {\r
85                         log.debug("File: " + filename\r
86                                         + " does not appear ro exist. Could have been removed?");\r
87                 }\r
88                 return success;\r
89         }\r
90 \r
91         public static boolean deleteAllFiles(String directory) {\r
92                 if (compbio.util.Util.isEmpty(directory)) {\r
93                         throw new NullPointerException("Directory must be provided! ");\r
94                 }\r
95                 File rootdir = new File(directory);\r
96                 if (!rootdir.exists()) {\r
97                         log.error("Directory " + directory + " does not exist. Have been deleted already?");\r
98                         return false;\r
99                 }\r
100                 if (!rootdir.isDirectory()) {\r
101                         log.error("Directory is expected by file given! Skipping... ");\r
102                         return false;\r
103                 }\r
104 \r
105                 File[] files = rootdir.listFiles();\r
106                 int deletedCount = 0;\r
107                 for (File f : files) {\r
108                         if (f.isDirectory()) {\r
109                                 log.error("Cannot delete subdirectories! Skipping...");\r
110                         } else {\r
111                                 if (f.delete()) {\r
112                                         deletedCount++;\r
113                                         log.debug("file " + f.getName() + " removed");\r
114                                 } else {\r
115                                         log.debug("file " + f.getName() + " is not removed");\r
116                                 }\r
117                         }\r
118                         \r
119                 }\r
120                 rootdir.delete();\r
121                 return deletedCount == files.length;\r
122         }\r
123 \r
124         public static boolean deleteDirectory(String directory) {\r
125                 if (deleteAllFiles (directory)) {\r
126                         File rootdir = new File(directory);\r
127                         return rootdir.delete();\r
128                 }\r
129                 return false;\r
130         }\r
131 \r
132         \r
133 }\r