92c59996e0b753778f220c41bf87a3edc1918f41
[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                 return exec.canExecute();\r
37         }\r
38 \r
39         public static void validateExecutable(String command)\r
40                         throws IllegalArgumentException {\r
41                 if (!PathValidator.isValidExecutable(command)) {\r
42                         throw new IllegalArgumentException(\r
43                                         "External executable: "\r
44                                                         + command\r
45                                                         + " could not be found or executed. Under *nix systems check that the executable flag is set!");\r
46                 }\r
47         }\r
48 \r
49         /*\r
50          * TODO move to Utils\r
51          */\r
52         public static boolean isValidDirectory(String directory) {\r
53                 if (Util.isEmpty(directory)) {\r
54                         return false;\r
55                 }\r
56                 File exec = new File(directory);\r
57                 return exec.exists() && exec.isDirectory() && exec.canRead();\r
58         }\r
59 \r
60         /**\r
61          * \r
62          * @param filenames\r
63          * @param type\r
64          *            - merely a string to be added to error message to explain what\r
65          *            type of files are lacking\r
66          */\r
67         public static void validatePathNames(List<String> filenames, String type)\r
68                         throws IllegalArgumentException {\r
69                 for (String filename : filenames) {\r
70                         if (isAbsolutePath(filename)) {\r
71                                 throw new IllegalArgumentException(\r
72                                                 "Working directory cannot be set as absolute paths are "\r
73                                                                 + "defined for at least some of the "\r
74                                                                 + type\r
75                                                                 + " files! This is not advisable. Violating path is : "\r
76                                                                 + filename);\r
77                         }\r
78                 }\r
79         }\r
80 \r
81         /**\r
82          * Whether a certain path is absolute or not is operation system dependent!\r
83          * \r
84          * @param path\r
85          * @return\r
86          */\r
87         public static boolean isAbsolutePath(String path) {\r
88                 assert path != null : "Path is NULL! Not NULL path expected";\r
89                 return new File(path).isAbsolute();\r
90         }\r
91 \r
92         public static void validateDirectory(String workDirectory)\r
93                         throws IllegalArgumentException {\r
94                 if (!PathValidator.isValidDirectory(workDirectory)) {\r
95                         throw new IllegalArgumentException("Working directory: "\r
96                                         + workDirectory + " is inaccessible or does not exist!");\r
97                 }\r
98         }\r
99 \r
100 }\r