Next version of JABA
[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         public static boolean isValidDirectory(String directory) {\r
50                 if (Util.isEmpty(directory)) {\r
51                         return false;\r
52                 }\r
53                 File exec = new File(directory);\r
54                 return exec.exists() && exec.isDirectory();\r
55         }\r
56 \r
57         /**\r
58          * \r
59          * @param filenames\r
60          * @param type\r
61          *            - merely a string to be added to error message to explain what\r
62          *            type of files are lacking\r
63          */\r
64         public static void validatePathNames(List<String> filenames, String type)\r
65                         throws IllegalArgumentException {\r
66                 for (String filename : filenames) {\r
67                         if (isAbsolutePath(filename)) {\r
68                                 throw new IllegalArgumentException(\r
69                                                 "Working directory cannot be set as absolute paths are "\r
70                                                                 + "defined for at least some of the "\r
71                                                                 + type\r
72                                                                 + " files! This is not advisable. Violating path is : "\r
73                                                                 + filename);\r
74                         }\r
75                 }\r
76         }\r
77 \r
78         /**\r
79          * Whether a certain path is absolute or not is operation system dependent!\r
80          * \r
81          * @param path\r
82          * @return\r
83          */\r
84         public static boolean isAbsolutePath(String path) {\r
85                 assert path != null : "Path is NULL! Not NULL path expected";\r
86                 return new File(path).isAbsolute();\r
87         }\r
88 \r
89         public static void validateDirectory(String workDirectory)\r
90                         throws IllegalArgumentException {\r
91                 if (!PathValidator.isValidDirectory(workDirectory)) {\r
92                         throw new IllegalArgumentException("Working directory: "\r
93                                         + workDirectory + " is inaccessible or does not exist!");\r
94                 }\r
95         }\r
96 \r
97 }\r