1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\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
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
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
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
19 package compbio.engine.client;
\r
21 import java.io.File;
\r
22 import java.util.List;
\r
24 import compbio.util.Util;
\r
26 public final class PathValidator {
\r
28 public static boolean isValidExecutable(String command) {
\r
29 if (Util.isEmpty(command)) {
\r
32 File exec = new File(command);
\r
33 if (!exec.exists()) {
\r
36 if (!exec.canExecute()) {
\r
38 return exec.setExecutable(true);
\r
39 } catch (Exception x)
\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
56 + " could not be found or executed. Under *nix systems check that the executable flag is set!");
\r
61 * TODO move to Utils
\r
63 public static boolean isValidDirectory(String directory) {
\r
64 if (Util.isEmpty(directory)) {
\r
67 File exec = new File(directory);
\r
68 return exec.exists() && exec.isDirectory() && exec.canRead();
\r
75 * - merely a string to be added to error message to explain what
\r
76 * type of files are lacking
\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
86 + " files! This is not advisable. Violating path is : "
\r
93 * Whether a certain path is absolute or not is operation system dependent!
\r
96 * @return true is the path is absolute, false otherwise
\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
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