1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\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
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
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
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
19 package compbio.engine.conf;
\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
26 import javax.naming.ConfigurationException;
\r
28 import org.apache.log4j.Logger;
\r
30 import compbio.util.PropertyHelper;
\r
31 import compbio.util.Util;
\r
33 public final class PropertyHelperManager {
\r
35 private static Logger log = Logger.getLogger(PropertyHelperManager.class);
\r
36 private static PropertyHelper ph = null;
\r
37 public static final String confDir = "conf" + File.separator;
\r
40 * Ways to fix path problem: 1) find a path to WEB-INF directory based on
\r
41 * the path to a known class. Then prepend this absolute path to the rest of
\r
42 * paths pros: no input from user cons: relocation of the source may cause
\r
43 * problems 2) Require users to add configuration directories to the class
\r
44 * path and then load entries from it. pros: cons: Many paths needs to be
\r
45 * added. Put significant burden on the user. Hard to tell web appl server
\r
46 * to add these entries to its class path. 3) Ask for project source
\r
47 * directory explicitly in the configuration. pros cons: similar to 1, but
\r
48 * this initial configuration file must reside in well known location! Why
\r
49 * ask users what can be found automatically? 4) Have everything in the
\r
50 * location already in class path for tomcat. cons: only classes and
\r
51 * lib/*.jar are added, eclipse will remove non classses from classes dir.
\r
57 public static PropertyHelper getPropertyHelper() {
\r
60 File locEngineProp = getResourceFromClasspath(confDir
\r
61 + "Engine.local.properties");
\r
62 File clustEngineProp = getResourceFromClasspath(confDir
\r
63 + "Engine.cluster.properties");
\r
64 File execProp = getResourceFromClasspath(confDir
\r
65 + "Executable.properties");
\r
66 ph = new PropertyHelper(locEngineProp, clustEngineProp,
\r
68 } catch (IOException e) {
\r
69 log.warn("Cannot read property files! Reason: "
\r
70 + e.getLocalizedMessage(), e.getCause());
\r
76 static File getResourceFromClasspath(String resourceName) {
\r
77 assert !Util.isEmpty(resourceName);
\r
78 String locPath = getLocalPath();
\r
79 File prop = new File(locPath + resourceName);
\r
80 if (!prop.exists()) {
\r
81 log.warn("Could not find a resource " + resourceName
\r
82 + " in the classpath!");
\r
88 * Method return the absolute path to the project root directory. It assumes
\r
89 * the following structure of the project project root conf settings
\r
90 * binaries WEB-INF classes compbio engine conf If the structure changes it
\r
91 * must be reflected in this method
\r
94 * @throws ConfigurationException
\r
96 public static String getLocalPath() {
\r
97 String clname = PropertyHelperManager.class.getSimpleName();
\r
98 URL url = PropertyHelperManager.class.getResource(clname + ".class");
\r
101 f = new File(url.toURI());
\r
102 // Iterate up the hierarchy to find a root project directory
\r
103 for (int i = 0; i < 6; i++) {
\r
104 f = f.getParentFile();
\r
106 } catch (URISyntaxException e) {
\r
107 String message = "Could not find resources path! Problems locating PropertyHelperManager class! "
\r
108 + e.getLocalizedMessage();
\r
109 log.error(message, e.getCause());
\r
110 throw new RuntimeException(message, e.getCause());
\r
111 } catch (IllegalArgumentException e) {
\r
112 // Classes are in the jar file, using different method to determine
\r
113 // the path new File(INCORRECT URL) throws it
\r
116 "It looks like classes are in the jar file. "
\r
117 + "Attempting a different method to determinine the path to the resources "
\r
118 + e.getLocalizedMessage(), e.getCause());
\r
120 f = new File(PropertyHelperManager.class.getProtectionDomain()
\r
121 .getCodeSource().getLocation().toURI().getPath());
\r
123 // Iterate up the hierarchy to find a root project directory
\r
124 // This time there is not need to walk up all class packages
\r
125 // WEB_APPL_NAME\WEB-INF\lib\JAR-FILE-NAME
\r
126 // jws2-1.0\WEB-INF\lib\full-jws2-1.0.jar
\r
127 for (int i = 0; i < 3; i++) {
\r
128 f = f.getParentFile();
\r
130 } catch (URISyntaxException e1) {
\r
131 log.error("Could not find resources path! "
\r
132 + e1.getLocalizedMessage(), e1.getCause());
\r
133 throw new RuntimeException("Could not find resources path! ",
\r
137 log.debug("Project directory is: " + f.getAbsolutePath());
\r
138 return f.getAbsolutePath() + File.separator;
\r