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