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