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.util.Arrays;
\r
22 import java.util.Collections;
\r
23 import java.util.Map;
\r
25 import org.apache.log4j.Logger;
\r
27 import compbio.util.Util;
\r
29 public class EnvVariableProcessor {
\r
32 * Special variable keys Absolute path(s) will be merged with the content of
\r
33 * the system PATH variable
\r
35 public static final String PATH = "PATH";
\r
37 private static Logger log = Logger.getLogger(EnvVariableProcessor.class);
\r
38 private static final String PROP_NAME_VALUE_SEPARATOR = "#";
\r
39 private static final String NEXT_ENV_PROPERTY_DELIMITER = ";";
\r
41 static boolean containsMultipleVariables(String property) {
\r
42 String[] arr = property.split(NEXT_ENV_PROPERTY_DELIMITER);
\r
43 if (arr.length == 0) {
\r
49 static String[] getEnvVariableList(String property) {
\r
50 property = property.trim();
\r
51 if (containsMultipleVariables(property)) {
\r
52 return property.split(NEXT_ENV_PROPERTY_DELIMITER);
\r
54 return new String[]{property};
\r
57 static String getEnvVariableName(String property) {
\r
58 return property.split(PROP_NAME_VALUE_SEPARATOR)[0];
\r
61 static String getEnvVariableValue(String property) {
\r
62 String[] vars = property.split(PROP_NAME_VALUE_SEPARATOR);
\r
63 if (vars.length > 2) {
\r
64 throw new IllegalArgumentException(
\r
65 "Must be only one value per property! Make sure the property does not contain '"
\r
66 + PROP_NAME_VALUE_SEPARATOR + "' symbol!");
\r
68 if (vars.length == 0 || vars.length==1) {
\r
69 log.warn("Environmental variable '"+property+"' does not have a value!");
\r
75 static boolean isValidEnvVariableProperty(String property) {
\r
76 if (property == null) {
\r
79 return property.contains(PROP_NAME_VALUE_SEPARATOR);
\r
85 * This is a horrible hack, but it only requires to simplify configuration
\r
86 * for users, could be removed at any time if proved to be a burden to
\r
89 private final static String mafft_binaries = "MAFFT_BINARIES";
\r
90 private final static String fasta4mafft = "FASTA_4_MAFFT";
\r
91 private final static String iupred_path = "IUPred_PATH";
\r
93 public static Map<String, String> getEnvVariables(String property,
\r
95 Map<String, String> vars = Util.getNewHashMap();
\r
96 if (!isValidEnvVariableProperty(property)) {
\r
97 return Collections.emptyMap();
\r
99 for (String evar : getEnvVariableList(property)) {
\r
100 if (!isValidEnvVariableProperty(evar)) {
\r
101 log.error(clazz.getName()
\r
102 + " environment variable is specified by is NOT VALID! Skipping. "
\r
103 + "Valid format is propertyName"
\r
104 + PROP_NAME_VALUE_SEPARATOR + "propertyValue. "
\r
105 + "Given values is: " + evar);
\r
108 String varName = getEnvVariableName(evar);
\r
109 String varValue = getEnvVariableValue(evar);
\r
110 // absolutise local paths to mafft env variables
\r
111 if (!PathValidator.isAbsolutePath(varValue)) {
\r
112 varName = varName.trim();
\r
113 if (varName.equalsIgnoreCase(mafft_binaries)
\r
114 || varName.equalsIgnoreCase(fasta4mafft)
\r
115 || varName.equalsIgnoreCase(iupred_path)) {
\r
116 varValue = compbio.engine.client.Util
\r
117 .convertToAbsolute(varValue);
\r
120 vars.put(varName, varValue);
\r