bf33409a607a5b4cd41cb650edd87fdcbee3f0fa
[jabaws.git] / engine / compbio / engine / conf / PropertyHelperManager.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.conf;\r
20 \r
21 import java.io.File;\r
22 import java.io.IOException;\r
23 import java.net.URISyntaxException;\r
24 import java.net.URL;\r
25 \r
26 import org.apache.log4j.Logger;\r
27 \r
28 import compbio.util.PropertyHelper;\r
29 import compbio.util.Util;\r
30 \r
31 public final class PropertyHelperManager {\r
32 \r
33         private static Logger log = Logger.getLogger(PropertyHelperManager.class);\r
34         private static PropertyHelper ph = null;\r
35         public static final String confDir = "conf" + File.separator;\r
36 \r
37         /**\r
38          * Ways to fix path problem: \r
39          * 1) find a path to WEB-INF directory based on the path to a known class. \r
40          * Then prepend this absolute path to the rest of paths \r
41          * pros: no input from user \r
42          * cons: relocation of the source may cause problems \r
43          * \r
44          * 2) Require users to add configuration directories to the class\r
45          * path and then load entries from it. \r
46          * pros: \r
47          * cons: Many paths needs to be added. Put significant burden on the user. \r
48          * Hard to tell web appl server to add these entries to its class path. \r
49          * \r
50          * 3) Ask for project source directory explicitly in the configuration. \r
51          * pros:\r
52          * cons: similar to 1, but this initial configuration file must reside in \r
53          * well known location! Why ask users what can be found automatically?\r
54          * \r
55          * 4) Have everything in the location already in class path for tomcat. \r
56          * pros:\r
57          * cons: only classes and lib/*.jar are added, Eclipse will remove non \r
58          * classses from classes dir.\r
59          * \r
60          * Try 1 - succeed.\r
61          * \r
62          * @return an instance\r
63          */\r
64         public static PropertyHelper getPropertyHelper() {\r
65                 if (ph == null) {\r
66                         try {\r
67                                 File locEngineProp = getResourceFromClasspath(confDir + "Engine.local.properties");\r
68                                 File clustEngineProp = getResourceFromClasspath(confDir + "Engine.cluster.properties");\r
69                                 File execProp = getResourceFromClasspath(confDir + "Executable.properties");\r
70                                 File gaProp = getResourceFromClasspath(confDir + "GA.properties");\r
71                                 ph = new PropertyHelper(locEngineProp, clustEngineProp, execProp, gaProp);\r
72                         } catch (IOException e) {\r
73                                 log.warn("Cannot read property files! Reason: " + e.getLocalizedMessage(), e.getCause());\r
74                         }\r
75                 }\r
76                 return ph;\r
77         }\r
78 \r
79         static File getResourceFromClasspath(String resourceName) {\r
80                 assert !Util.isEmpty(resourceName);\r
81                 String locPath = getLocalPath();\r
82                 File prop = new File(locPath + resourceName);\r
83                 if (!prop.exists()) {\r
84                         log.warn("Could not find a resource " + resourceName + " in the classpath!");\r
85                 }\r
86                 return prop;\r
87         }\r
88 \r
89         /**\r
90          * Method return the absolute path to the project root directory. It assumes\r
91          * the following structure of the project:\r
92          * project-root: \r
93          * conf/settings\r
94          * binaries \r
95          * WEB-INF/classes/compbio/engine/conf/PropertyHelperManager.class\r
96          * If the structure changes it must be reflected in this method\r
97          * \r
98          * @return the local path\r
99          * @throws RuntimeException\r
100          *             if cannot determine the local path\r
101          */\r
102         public static String getLocalPath() {\r
103                 String clname = PropertyHelperManager.class.getSimpleName();\r
104                 URL url = PropertyHelperManager.class.getResource(clname + ".class");\r
105                 File f = null;\r
106                 try {\r
107                         f = new File(url.toURI());\r
108                         // Iterate up the hierarchy to find a root project directory\r
109                         for (int i = 0; i < 6; i++) {\r
110                                 f = f.getParentFile();\r
111                         }\r
112                 } catch (URISyntaxException e) {\r
113                         String message = "Could not find resources path! Problems locating PropertyHelperManager class! " + e.getLocalizedMessage();\r
114                         log.error(message, e.getCause());\r
115                         throw new RuntimeException(message, e.getCause());\r
116                 } catch (IllegalArgumentException e) {\r
117                         // Classes are in the jar file, using different method to determine\r
118                         // the path new File(INCORRECT URL) throws it\r
119                         log.debug(\r
120                                         "It looks like classes are in the jar file. "\r
121                                                         + "Attempting a different method to determinine the path to the resources "\r
122                                                         + e.getLocalizedMessage(), e.getCause());\r
123                         try {\r
124                                 f = new File(PropertyHelperManager.class.getProtectionDomain()\r
125                                                 .getCodeSource().getLocation().toURI().getPath());\r
126 \r
127                                 // Iterate up the hierarchy to find a root project directory\r
128                                 // This time there is not need to walk up all class packages\r
129                                 // WEB_APPL_NAME\WEB-INF\lib\JAR-FILE-NAME\r
130                                 // jws2-1.0\WEB-INF\lib\full-jws2-1.0.jar\r
131                                 for (int i = 0; i < 3; i++) {\r
132                                         f = f.getParentFile();\r
133                                 }\r
134                         } catch (URISyntaxException e1) {\r
135                                 log.error("Could not find resources path! " + e1.getLocalizedMessage(), e1.getCause());\r
136                                 throw new RuntimeException("Could not find resources path! ", e1.getCause());\r
137                         }\r
138                 }\r
139                 log.debug("Project directory is: " + f.getAbsolutePath());\r
140                 return f.getAbsolutePath() + File.separator;\r
141         }\r
142 \r
143         public static int getIntProperty(String propValue) {\r
144                 if (!Util.isEmpty(propValue)) {\r
145                         return Integer.parseInt(propValue.trim());\r
146                 }\r
147                 return -1;\r
148         }\r
149 \r
150         public static boolean getBooleanProperty(String propValue) {\r
151                 if (!Util.isEmpty(propValue)) {\r
152                         propValue = propValue.trim();\r
153                         return Boolean.parseBoolean(propValue);\r
154                 }\r
155                 return false;\r
156         }}\r