001: /*
002: * Copyright (c) Mateusz Prokopowicz. All Rights Reserved.
003: */
004:
005: package com.technoetic.xplanner;
006:
007: import java.io.IOException;
008: import java.io.InputStream;
009: import java.util.Iterator;
010: import java.util.Properties;
011:
012: import org.apache.log4j.Logger;
013:
014: public class XPlannerProperties {
015: private static final Logger LOG = Logger
016: .getLogger(XPlannerProperties.class);
017:
018: public static final String SUPPORT_PRODUCTION_EMAIL_KEY = "support.production.email";
019: public static final String SUPPORT_ISSUE_URL_KEY = "support.issue.url";
020: public static final String ERROR_FILING_INFO_KEY = "error.filingInfo";
021: public static final String XPLANNER_VERSION_KEY = "xplanner.version";
022: public static final String XPLANNER_BUILD_REVISION_KEY = "xplanner.build.revision";
023: public static final String XPLANNER_BUILD_DATE_KEY = "xplanner.build.date";
024: public static final String XPLANNER_BUILD_PACKAGE_KEY = "xplanner.build.package";
025:
026: public static final String CONNECTION_URL_KEY = "hibernate.connection.url";
027: public static final String DRIVER_CLASS_KEY = "hibernate.connection.driver_class";
028: public static final String CONNECTION_USERNAME_KEY = "hibernate.connection.username";
029: public static final String CONNECTION_PASSWORD_KEY = "hibernate.connection.password";
030: public static final String PATCH_DATABASE_TYPE_KEY = "xplanner.migration.databasetype";
031: public static final String PATCH_PATH_KEY = "xplanner.migration.patchpath";
032: public static final String EMAIL_FROM = "xplanner.mail.from";
033: public static final String APPLICATION_URL_KEY = "xplanner.application.url";
034: public static final String WIKI_URL_KEY = "twiki.scheme.wiki";
035: public static final String SEND_NOTIFICATION_KEY = "xplanner.project.send.notification";
036:
037: private static final class PropertyInitializer {
038: public static Properties properties = loadProperties();
039: private static final String FILENAME = "xplanner.properties";
040: private static final String OVERRIDES_KEY = "xplanner.overrides";
041: private static final String OVERRIDES_DEFAULT = "xplanner-custom.properties";
042:
043: private static Properties loadProperties() {
044: Properties properties = new Properties();
045: try {
046: InputStream in = XPlannerProperties.class
047: .getClassLoader().getResourceAsStream(FILENAME);
048: if (in != null) {
049: properties.load(in);
050: setCustomPropertyOverrides(properties);
051: } else {
052: LOG.error("Can't load xplanner.properties!!!");
053: }
054: } catch (IOException ex) {
055: LOG.error("error during xplanner property loading");
056: }
057: return properties;
058: }
059:
060: /**
061: * This brute-force approach is being used instead of normal java.util.Property defaults
062: * because Hibernate copies the properties (without the defaults).
063: */
064: private static void setCustomPropertyOverrides(
065: Properties properties) {
066: Properties customProperties = new Properties();
067: try {
068: String customPropertyFileName = System.getProperty(
069: OVERRIDES_KEY, OVERRIDES_DEFAULT);
070: InputStream in = XPlannerProperties.class
071: .getClassLoader().getResourceAsStream(
072: customPropertyFileName);
073: if (in != null) {
074: customProperties.load(in);
075: for (Iterator iterator = customProperties.keySet()
076: .iterator(); iterator.hasNext();) {
077: String key = (String) iterator.next();
078: properties.put(key, customProperties.get(key));
079: }
080: }
081: } catch (IOException ex) {
082: LOG.error("error during custom property loading");
083: }
084: }
085: }
086:
087: public XPlannerProperties() {
088: }
089:
090: public Properties get() {
091: return PropertyInitializer.properties;
092: }
093:
094: //DEBT(Spring) Remove this method once XPlannerProperties is loaded from spring
095: public static Properties getProperties() {
096: return PropertyInitializer.properties;
097: }
098:
099: public String getProperty(String key) {
100: return PropertyInitializer.properties.getProperty(key);
101: }
102:
103: public String getProperty(String key, String defaultValue) {
104: String val = getProperty(key);
105: if (val != null)
106: return val;
107: return defaultValue;
108: }
109:
110: public boolean getProperty(String key, boolean defaultValue) {
111: String val = getProperty(key);
112: if (val != null)
113: return Boolean.valueOf(val).booleanValue();
114: return defaultValue;
115: }
116:
117: public Iterator getPropertyNames() {
118: return PropertyInitializer.properties.keySet().iterator();
119: }
120:
121: public void setProperty(String key, String value) {
122: PropertyInitializer.properties.setProperty(key, value);
123: }
124:
125: public void removeProperty(String key) {
126: PropertyInitializer.properties.remove(key);
127: }
128:
129: public static void main(String[] args) {
130: XPlannerProperties properties = new XPlannerProperties();
131: LOG.info(properties
132: .getProperty("hibernate.connection.driver_class"));
133: }
134: }
|