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