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