3423c8f0405d0718d5c4f77353141f2839942e81
[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.Collections;\r
22 import java.util.Map;\r
23 \r
24 import org.apache.log4j.Logger;\r
25 \r
26 import compbio.util.Util;\r
27 \r
28 public class EnvVariableProcessor {\r
29 \r
30         /**\r
31          * Special variable keys Absolute path(s) will be merged with the content of\r
32          * the system PATH variable\r
33          */\r
34         public static final String PATH = "PATH";\r
35 \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
39 \r
40         static boolean containsMultipleVariables(String property) {\r
41                 String[] arr = property.split(NEXT_ENV_PROPERTY_DELIMITER);\r
42                 if (arr.length == 0) {\r
43                         return false;\r
44                 }\r
45                 return true;\r
46         }\r
47 \r
48         static String[] getEnvVariableList(String property) {\r
49                 if (containsMultipleVariables(property)) {\r
50                         return property.split(NEXT_ENV_PROPERTY_DELIMITER);\r
51                 }\r
52                 return new String[]{property};\r
53         }\r
54 \r
55         static String getEnvVariableName(String property) {\r
56                 return property.split(PROP_NAME_VALUE_SEPARATOR)[0];\r
57         }\r
58 \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
65                 }\r
66                 if (vars.length == 0) {\r
67                         return "";\r
68                 }\r
69                 return vars[1];\r
70         }\r
71 \r
72         static boolean isValidEnvVariableProperty(String property) {\r
73                 if (property == null) {\r
74                         return false;\r
75                 }\r
76                 return property.contains(PROP_NAME_VALUE_SEPARATOR);\r
77         }\r
78 \r
79         /**\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
83          */\r
84         void absolutiseMafftpaths() {\r
85 \r
86         }\r
87 \r
88         /*\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
91          * maintanance.\r
92          */\r
93         private final static String mafft_binaries = "MAFFT_BINARIES";\r
94         private final static String fasta4mafft = "FASTA_4_MAFFT";\r
95         private final static String iupred_path = "IUPred_PATH";\r
96 \r
97         public static Map<String, String> getEnvVariables(String property,\r
98                         Class<?> clazz) {\r
99                 Map<String, String> vars = Util.getNewHashMap();\r
100                 if (!isValidEnvVariableProperty(property)) {\r
101                         return Collections.emptyMap();\r
102                 }\r
103                 for (String evar : getEnvVariableList(property)) {\r
104                         if (!isValidEnvVariableProperty(evar)) {\r
105                                 log.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
110                         }\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                                                 || varName.equalsIgnoreCase(iupred_path)) {\r
119                                         varValue = compbio.engine.client.Util\r
120                                                         .convertToAbsolute(varValue);\r
121                                 }\r
122                         }\r
123                         vars.put(varName, varValue);\r
124                 }\r
125 \r
126                 return vars;\r
127         }\r
128 }\r