--- /dev/null
+/* Copyright (c) 2009 Peter Troshin\r
+ * \r
+ * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0 \r
+ * \r
+ * This library is free software; you can redistribute it and/or modify it under the terms of the\r
+ * Apache License version 2 as published by the Apache Software Foundation\r
+ * \r
+ * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
+ * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
+ * License for more details.\r
+ * \r
+ * A copy of the license is in apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
+ * \r
+ * Any republication or derived work distributed in source code form\r
+ * must include this copyright and license notice.\r
+ */\r
+\r
+package compbio.engine;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.net.URISyntaxException;\r
+import java.net.URL;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import compbio.util.PropertyHelper;\r
+import compbio.util.Util;\r
+\r
+public final class ProteoCachePropertyHelperManager {\r
+\r
+ private static Logger log = Logger.getLogger(ProteoCachePropertyHelperManager.class);\r
+ private static PropertyHelper ph = null;\r
+\r
+ /**\r
+ * Ways to fix path problem: \r
+ * 1) find a path to WEB-INF directory based on the path to a known class. \r
+ * Then prepend this absolute path to the rest of paths \r
+ * pros: no input from user \r
+ * cons: relocation of the source may cause problems \r
+ * \r
+ * 2) Require users to add configuration directories to the class\r
+ * path and then load entries from it. \r
+ * pros: \r
+ * cons: Many paths needs to be added. Put significant burden on the user. \r
+ * Hard to tell web appl server to add these entries to its class path. \r
+ * \r
+ * 3) Ask for project source directory explicitly in the configuration. \r
+ * pros:\r
+ * cons: similar to 1, but this initial configuration file must reside in \r
+ * well known location! Why ask users what can be found automatically?\r
+ * \r
+ * 4) Have everything in the location already in class path for tomcat. \r
+ * pros:\r
+ * cons: only classes and lib/*.jar are added, Eclipse will remove non \r
+ * classses from classes dir.\r
+ * \r
+ * Try 1 - succeed.\r
+ * \r
+ * @return an instance\r
+ */\r
+ public static PropertyHelper getPropertyHelper() {\r
+ if (ph == null) {\r
+ try {\r
+ File MainPropertyFile = getResourceFromClasspath("conf/Proteocache.properties");\r
+ ph = new PropertyHelper(MainPropertyFile);\r
+ } catch (IOException e) {\r
+ log.warn("Cannot read property files! Reason: " + e.getLocalizedMessage(), e.getCause());\r
+ }\r
+ }\r
+ return ph;\r
+ }\r
+\r
+ static File getResourceFromClasspath(String resourceName) {\r
+ assert !Util.isEmpty(resourceName);\r
+ String locPath = getLocalPath();\r
+ File prop = new File(locPath + resourceName);\r
+ if (!prop.exists()) {\r
+ log.warn("Could not find a resource " + resourceName + " in the classpath!");\r
+ }\r
+ return prop;\r
+ }\r
+\r
+ /**\r
+ * Method return the absolute path to the project root directory. It assumes\r
+ * the following structure of the project:\r
+ * project-root: \r
+ * conf/settings\r
+ * binaries \r
+ * WEB-INF/classes/compbio/engine/conf/PropertyHelperManager.class\r
+ * If the structure changes it must be reflected in this method\r
+ * \r
+ * @return the local path\r
+ * @throws RuntimeException\r
+ * if cannot determine the local path\r
+ */\r
+ public static String getLocalPath() {\r
+ String clname = ProteoCachePropertyHelperManager.class.getSimpleName();\r
+ URL url = ProteoCachePropertyHelperManager.class.getResource(clname + ".class");\r
+ File f = null;\r
+ try {\r
+ f = new File(url.toURI());\r
+ // Iterate up the hierarchy to find a root project directory\r
+ for (int i = 0; i < 5; i++) {\r
+ f = f.getParentFile();\r
+ }\r
+ } catch (URISyntaxException e) {\r
+ String mes = "Could not find resources path! Problems locating PropertyHelperManager class! ";\r
+ log.error(mes + e.getLocalizedMessage(), e.getCause());\r
+ throw new RuntimeException(mes + e.getLocalizedMessage(), e.getCause());\r
+ } catch (IllegalArgumentException e) {\r
+ // Classes are in the jar file, using different method to determine\r
+ // the path new File(INCORRECT URL) throws it\r
+ String mes = "It looks like classes are in the jar file. " \r
+ + "Attempting a different method to determinine the path to the resources";\r
+ log.debug(mes + e.getLocalizedMessage(), e.getCause());\r
+ try {\r
+ f = new File(ProteoCachePropertyHelperManager.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());\r
+\r
+ // Iterate up the hierarchy to find a root project directory\r
+ // This time there is not need to walk up all class packages\r
+ // WEB_APPL_NAME\WEB-INF\lib\JAR-FILE-NAME\r
+ // jws2-1.0\WEB-INF\lib\full-jws2-1.0.jar\r
+ for (int i = 0; i < 3; i++) {\r
+ f = f.getParentFile();\r
+ }\r
+ } catch (URISyntaxException e1) {\r
+ log.error("Could not find resources path! " + e1.getLocalizedMessage(), e1.getCause());\r
+ throw new RuntimeException("Could not find resources path! ", e1.getCause());\r
+ }\r
+ }\r
+ log.debug("Project directory is: " + f.getAbsolutePath());\r
+ return f.getAbsolutePath() + File.separator;\r
+ }\r
+\r
+ public static int getIntProperty(String propValue) {\r
+ if (!Util.isEmpty(propValue)) {\r
+ return Integer.parseInt(propValue.trim());\r
+ }\r
+ return -1;\r
+ }\r
+\r
+ public static boolean getBooleanProperty(String propValue) {\r
+ if (!Util.isEmpty(propValue)) {\r
+ propValue = propValue.trim();\r
+ return Boolean.parseBoolean(propValue);\r
+ }\r
+ return false;\r
+ }}\r