report both operating and non-operating services
[jabaws.git] / engine / compbio / engine / client / PathValidator.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 \r
19 package compbio.engine.client;\r
20 \r
21 import java.io.File;\r
22 import java.util.List;\r
23 \r
24 import compbio.util.Util;\r
25 \r
26 public final class PathValidator {\r
27 \r
28         public static boolean isValidExecutable(String command) {\r
29                 if (Util.isEmpty(command)) {\r
30                         return false;\r
31                 }\r
32                 File exec = new File(command);\r
33                 if (!exec.exists()) {\r
34                         return false;\r
35                 }\r
36                 if (!exec.canExecute()) {\r
37                         try {\r
38                                 return exec.setExecutable(true);\r
39                         } catch (Exception x)\r
40                         {\r
41                                 \r
42                         }catch (Error e)\r
43                         {\r
44                         }\r
45                         return false;\r
46                 }\r
47                 return true;\r
48         }\r
49 \r
50         public static void validateExecutable(String command)\r
51                         throws IllegalArgumentException {\r
52                 if (!PathValidator.isValidExecutable(command)) {\r
53                         throw new IllegalArgumentException(\r
54                                         "External executable: "\r
55                                                         + command\r
56                                                         + " could not be found or executed. Under *nix systems check that the executable flag is set!");\r
57                 }\r
58         }\r
59 \r
60         /*\r
61          * TODO move to Utils\r
62          */\r
63         public static boolean isValidDirectory(String directory) {\r
64                 if (Util.isEmpty(directory)) {\r
65                         return false;\r
66                 }\r
67                 File exec = new File(directory);\r
68                 return exec.exists() && exec.isDirectory() && exec.canRead();\r
69         }\r
70 \r
71         /**\r
72          * \r
73          * @param filenames\r
74          * @param type\r
75          *            - merely a string to be added to error message to explain what\r
76          *            type of files are lacking\r
77          */\r
78         public static void validatePathNames(List<String> filenames, String type)\r
79                         throws IllegalArgumentException {\r
80                 for (String filename : filenames) {\r
81                         if (isAbsolutePath(filename)) {\r
82                                 throw new IllegalArgumentException(\r
83                                                 "Working directory cannot be set as absolute paths are "\r
84                                                                 + "defined for at least some of the "\r
85                                                                 + type\r
86                                                                 + " files! This is not advisable. Violating path is : "\r
87                                                                 + filename);\r
88                         }\r
89                 }\r
90         }\r
91 \r
92         /**\r
93          * Whether a certain path is absolute or not is operation system dependent!\r
94          * \r
95          * @param path\r
96          * @return true is the path is absolute, false otherwise\r
97          */\r
98         public static boolean isAbsolutePath(String path) {\r
99                 assert path != null : "Path is NULL! Not NULL path expected";\r
100                 return new File(path).isAbsolute();\r
101         }\r
102 \r
103         public static void validateDirectory(String workDirectory)\r
104                         throws IllegalArgumentException {\r
105                 if (!PathValidator.isValidDirectory(workDirectory)) {\r
106                         throw new IllegalArgumentException("Working directory: "\r
107                                         + workDirectory + " is inaccessible or does not exist!");\r
108                 }\r
109         }\r
110 \r
111 }\r