Fix for testcases
[jabaws.git] / engine / compbio / engine / client / EnvVariableProcessor.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.util.Arrays;\r
22 import java.util.Collections;\r
23 import java.util.Map;\r
24 \r
25 import org.apache.log4j.Logger;\r
26 \r
27 import compbio.util.Util;\r
28 \r
29 public class EnvVariableProcessor {\r
30 \r
31         /**\r
32          * Special variable keys Absolute path(s) will be merged with the content of\r
33          * the system PATH variable\r
34          */\r
35         public static final String PATH = "PATH";\r
36 \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
40 \r
41         static boolean containsMultipleVariables(String property) {\r
42                 String[] arr = property.split(NEXT_ENV_PROPERTY_DELIMITER);\r
43                 if (arr.length == 0) {\r
44                         return false;\r
45                 }\r
46                 return true;\r
47         }\r
48 \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
53                 }\r
54                 return new String[]{property};\r
55         }\r
56 \r
57         static String getEnvVariableName(String property) {\r
58                 return property.split(PROP_NAME_VALUE_SEPARATOR)[0];\r
59         }\r
60 \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
67                 }\r
68                 if (vars.length == 0 || vars.length==1) {\r
69                         log.warn("Environmental variable '"+property+"' does not have a value!");\r
70                         return "";\r
71                 }\r
72                 return vars[1];\r
73         }\r
74 \r
75         static boolean isValidEnvVariableProperty(String property) {\r
76                 if (property == null) {\r
77                         return false;\r
78                 }\r
79                 return property.contains(PROP_NAME_VALUE_SEPARATOR);\r
80         }\r
81 \r
82 \r
83 \r
84         /*\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
87          * maintanance.\r
88          */\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
92 \r
93         public static Map<String, String> getEnvVariables(String property,\r
94                         Class<?> clazz) {\r
95                 Map<String, String> vars = Util.getNewHashMap();\r
96                 if (!isValidEnvVariableProperty(property)) {\r
97                         return Collections.emptyMap();\r
98                 }\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
106                         }\r
107                         evar=evar.trim(); \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
118                                 }\r
119                         }\r
120                         vars.put(varName, varValue);\r
121                 }\r
122 \r
123                 return vars;\r
124         }\r
125 }\r