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.Collections;
\r
22 import java.util.Map;
\r
24 import org.apache.log4j.Logger;
\r
26 import compbio.util.Util;
\r
28 public class EnvVariableProcessor {
\r
31 * Special variable keys Absolute path(s) will be merged with the content of
\r
32 * the system PATH variable
\r
34 public static final String PATH = "PATH";
\r
36 private static Logger log = Logger.getLogger(EnvVariableProcessor.class);
\r
37 private static final String PROP_NAME_VALUE_SEPARATOR = "#";
\r
38 private static final String NEXT_ENV_PROPERTY_DELIMITER = ";";
\r
40 static boolean containsMultipleVariables(String property) {
\r
41 String[] arr = property.split(NEXT_ENV_PROPERTY_DELIMITER);
\r
42 if (arr.length == 0) {
\r
48 static String[] getEnvVariableList(String property) {
\r
49 if (containsMultipleVariables(property)) {
\r
50 return property.split(NEXT_ENV_PROPERTY_DELIMITER);
\r
52 return new String[] { property };
\r
55 static String getEnvVariableName(String property) {
\r
56 return property.split(PROP_NAME_VALUE_SEPARATOR)[0];
\r
59 static String getEnvVariableValue(String property) {
\r
60 String[] vars = property.split(PROP_NAME_VALUE_SEPARATOR);
\r
61 if (vars.length > 2) {
\r
62 throw new IllegalArgumentException(
\r
63 "Must be only one value per property! Make sure the property does not contain '"
\r
64 + PROP_NAME_VALUE_SEPARATOR + "' symbol!");
\r
66 if (vars.length == 0) {
\r
72 static boolean isValidEnvVariableProperty(String property) {
\r
73 if (property == null) {
\r
76 return property.contains(PROP_NAME_VALUE_SEPARATOR);
\r
80 * This method change relative paths to absolute for two environmental
\r
81 * variables MAFFT-BINARIES and FASTA_4_MAFFT to avoid requesting every user
\r
82 * to specify these path for every installation
\r
84 void absolutiseMafftpaths() {
\r
89 * This is a horrible hack, but it only requires to simplify configuration
\r
90 * for users, could be removed at any time if proved to be a burden to
\r
93 private final static String mafft_binaries = "MAFFT_BINARIES";
\r
94 private final static String fasta4mafft = "FASTA_4_MAFFT";
\r
96 public static Map<String, String> getEnvVariables(String property,
\r
98 Map<String, String> vars = Util.getNewHashMap();
\r
99 if (!isValidEnvVariableProperty(property)) {
\r
100 return Collections.emptyMap();
\r
102 for (String evar : getEnvVariableList(property)) {
\r
103 if (!isValidEnvVariableProperty(evar)) {
\r
105 .error(clazz.getName()
\r
106 + " environment variable is specified by is NOT VALID! Skipping. "
\r
107 + "Valid format is propertyName"
\r
108 + PROP_NAME_VALUE_SEPARATOR + "propertyValue. "
\r
109 + "Given values is: " + evar);
\r
111 String varName = getEnvVariableName(evar);
\r
112 String varValue = getEnvVariableValue(evar);
\r
113 // absolutise local paths to mafft env variables
\r
114 if (!PathValidator.isAbsolutePath(varValue)) {
\r
115 varName = varName.trim();
\r
116 if (varName.equalsIgnoreCase(mafft_binaries)
\r
117 || varName.equalsIgnoreCase(fasta4mafft)) {
\r
118 varValue = compbio.engine.client.Util
\r
119 .convertToAbsolute(varValue);
\r
122 vars.put(varName, varValue);
\r