001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.dsi;
020:
021: import java.io.*;
022: import java.util.*;
023: import java.util.logging.*;
024:
025: import org.openharmonise.rm.config.*;
026:
027: /**
028: * Wraps the database connection properties.
029: *
030: * @author Matt Treanor
031: * @author jejking
032: * @version $Revision: 1.3 $
033: *
034: */
035: public class DatabaseSettings {
036:
037: /**
038: * Constant holding the name of the properties file - harmonise.properties
039: */
040: public static final String PROPERTIES_FILENAME = "/harmonise.properties";
041: /**
042: * The properties file.
043: */
044: private File properties_file;
045:
046: /**
047: * Logger for this class
048: */
049: private static Logger logger = Logger
050: .getLogger(DatabaseSettings.class.getName());
051:
052: private String dsiClass = null;
053: private String dbUrl = null;
054: private String dbPwd = null;
055: private String dbDriver = null;
056: private String dbUser = null;
057:
058: private static DatabaseSettings instance;
059:
060: /**
061: * Constructs and configures instance using the params given using Contructor injection pattern.
062: *
063: * @param dbUser
064: * @param dbPassword
065: * @param dbUrl
066: * @param dbDriver
067: * @param dsiClass
068: * @throws ConfigException
069: */
070: public static void createDatabaseSettings(String dbUser,
071: String dbPwd, String dbUrl, String dbDriver, String dsiClass)
072: throws ConfigException {
073: // check none are null or empty strings as an initial sanity check on input
074: instance = new DatabaseSettings();
075: instance.setDbUser(dbUser);
076: instance.setDbPwd(dbPwd);
077: instance.setDbUrl(dbUrl);
078: instance.setDbDriver(dbDriver);
079: instance.setDsiClass(dsiClass);
080: }
081:
082: /**
083: * Constructs an instance of <code>DatabaseSettings</code>
084: *
085: * @throws ConfigException if an error occurs creating the
086: * <code>PropertyResourceBundle</code>
087: */
088: private DatabaseSettings() throws ConfigException {
089: }
090:
091: public static void createDatabaseSettingsFromFile()
092: throws ConfigException {
093: Properties props = new Properties();
094:
095: String classpath = System.getProperty("java.class.path");
096: StringTokenizer paths = new StringTokenizer(classpath,
097: File.pathSeparator);
098: File propertiesFile = null;
099: boolean found = false;
100: while (!found && paths.hasMoreTokens()) {
101: String path = paths.nextToken();
102: propertiesFile = new File(path, "harmonise.properties");
103: found = propertiesFile.exists();
104: }
105: if (found) {
106:
107: try {
108: props.load(new FileInputStream(propertiesFile));
109: } catch (FileNotFoundException e) {
110: throw new ConfigException(e);
111: } catch (IOException e) {
112: throw new ConfigException(e);
113: }
114: instance = new DatabaseSettings();
115: instance
116: .setDbUser(props
117: .getProperty(DataStoreInterfaceFactory.DBUSR_PNAME));
118: instance
119: .setDbPwd(props
120: .getProperty(DataStoreInterfaceFactory.DBPWD_PNAME));
121: instance
122: .setDbUrl(props
123: .getProperty(DataStoreInterfaceFactory.DBURL_PNAME));
124: instance
125: .setDbDriver(props
126: .getProperty(DataStoreInterfaceFactory.DBDRV_PNAME));
127: instance.setDsiClass(props
128: .getProperty(DataStoreInterfaceFactory.DSI_PNAME));
129:
130: }
131: }
132:
133: /**
134: * Returns the singleton instance of this class
135: *
136: * @return the singleton instance of this class
137: * @throws ConfigException if an error occurs creating the singleton instance
138: */
139: public static DatabaseSettings getInstance() throws ConfigException {
140: /*
141: * Logic here is simple. If, earlier in the object lifecycle, something
142: * has called createDatabaseSettings and injected configuration params, then
143: * the object created will be returned. This option can be taken by, for example,
144: * ServletContextListener on context startup.
145: *
146: *
147: * Else, we will attempt to read the configuration file and set our properties from that,
148: * construct a new instance and return that. This option can be taken when testing,
149: * for example.
150: *
151: */
152: if (instance == null) {
153: createDatabaseSettingsFromFile();
154: }
155: return instance;
156: }
157:
158: /**
159: * @return Returns the dbDriver.
160: */
161: public String getDbDriver() {
162: return dbDriver;
163: }
164:
165: /**
166: * @param dbDriver The dbDriver to set.
167: */
168: private void setDbDriver(String dbDriver) {
169: if (dbDriver == null || dbDriver.length() == 0) {
170: throw new IllegalArgumentException(
171: "dbDriver may not be null or zero length");
172: }
173: this .dbDriver = dbDriver;
174: }
175:
176: /**
177: * @return Returns the dbPwd.
178: */
179: public String getDbPwd() {
180: return dbPwd;
181: }
182:
183: /**
184: * @param dbPwd The dbPwd to set.
185: */
186: private void setDbPwd(String dbPwd) {
187: if (dbPwd == null || dbPwd.length() == 0) {
188: throw new IllegalArgumentException(
189: "dbPwd may not be null or zero length");
190: }
191: this .dbPwd = dbPwd;
192: }
193:
194: /**
195: * @return Returns the dbUrl.
196: */
197: public String getDbUrl() {
198: return dbUrl;
199: }
200:
201: /**
202: * @param dbUrl The dbUrl to set.
203: */
204: private void setDbUrl(String dbUrl) {
205: if (dbUrl == null || dbUrl.length() == 0) {
206: throw new IllegalArgumentException(
207: "dbUrl may not be null or zero length");
208: }
209: this .dbUrl = dbUrl;
210: }
211:
212: /**
213: * @return Returns the dbUser.
214: */
215: public String getDbUser() {
216: return dbUser;
217: }
218:
219: /**
220: * @param dbUser The dbUser to set.
221: */
222: private void setDbUser(String dbUser) {
223: if (dbUser == null || dbUser.length() == 0) {
224: throw new IllegalArgumentException(
225: "dbUser may not be null or zero length");
226: }
227: this .dbUser = dbUser;
228: }
229:
230: /**
231: * @return Returns the dsiClass.
232: */
233: public String getDsiClass() {
234: return dsiClass;
235: }
236:
237: /**
238: * @param dsiClass The dsiClass to set.
239: */
240: private void setDsiClass(String dsiClass) {
241: if (dsiClass == null || dsiClass.length() == 0) {
242: throw new IllegalArgumentException(
243: "dsiClass may not be null or zero length");
244: }
245: this.dsiClass = dsiClass;
246: }
247:
248: }
|