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