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;
\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 org.apache.log4j.Logger;
\r
28 import compbio.util.PropertyHelper;
\r
29 import compbio.util.Util;
\r
31 public final class ProteoCachePropertyHelperManager {
\r
33 private static Logger log = Logger.getLogger(ProteoCachePropertyHelperManager.class);
\r
34 private static PropertyHelper ph = null;
\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
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
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
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
57 * @return an instance
\r
59 public static PropertyHelper getPropertyHelper() {
\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
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
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
87 * @return the local path
\r
88 * @throws RuntimeException
\r
89 * if cannot determine the local path
\r
91 public static String getLocalPath() {
\r
92 String clname = ProteoCachePropertyHelperManager.class.getSimpleName();
\r
93 URL url = ProteoCachePropertyHelperManager.class.getResource(clname + ".class");
\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
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
112 f = new File(ProteoCachePropertyHelperManager.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
\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
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
126 log.debug("Project directory is: " + f.getAbsolutePath());
\r
127 return f.getAbsolutePath() + File.separator;
\r
130 public static int getIntProperty(String propValue) {
\r
131 if (!Util.isEmpty(propValue)) {
\r
132 return Integer.parseInt(propValue.trim());
\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