KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > dsi > DatabaseSettings


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.dsi;
20
21 import java.io.*;
22 import java.util.*;
23 import java.util.logging.*;
24
25 import org.openharmonise.rm.config.*;
26
27
28 /**
29  * Wraps the database connection properties.
30  *
31  * @author Matt Treanor
32  * @author jejking
33  * @version $Revision: 1.3 $
34  *
35  */

36 public class DatabaseSettings {
37     
38     /**
39      * Constant holding the name of the properties file - harmonise.properties
40      */

41     public static final String JavaDoc PROPERTIES_FILENAME = "/harmonise.properties";
42     /**
43      * The properties file.
44      */

45     private File properties_file;
46     
47     /**
48      * Logger for this class
49      */

50     private static Logger logger = Logger.getLogger(DatabaseSettings.class.getName());
51
52     
53     private String JavaDoc dsiClass = null;
54     private String JavaDoc dbUrl = null;
55     private String JavaDoc dbPwd = null;
56     private String JavaDoc dbDriver = null;
57     private String JavaDoc dbUser = null;
58     
59     private static DatabaseSettings instance;
60     
61     /**
62      * Constructs and configures instance using the params given using Contructor injection pattern.
63      *
64      * @param dbUser
65      * @param dbPassword
66      * @param dbUrl
67      * @param dbDriver
68      * @param dsiClass
69      * @throws ConfigException
70      */

71     public static void createDatabaseSettings(String JavaDoc dbUser, String JavaDoc dbPwd, String JavaDoc dbUrl, String JavaDoc dbDriver, String JavaDoc dsiClass) throws ConfigException {
72         // check none are null or empty strings as an initial sanity check on input
73
instance = new DatabaseSettings();
74         instance.setDbUser(dbUser);
75         instance.setDbPwd(dbPwd);
76         instance.setDbUrl(dbUrl);
77         instance.setDbDriver(dbDriver);
78         instance.setDsiClass(dsiClass);
79     }
80
81     
82     /**
83      * Constructs an instance of <code>DatabaseSettings</code>
84      *
85      * @throws ConfigException if an error occurs creating the
86      * <code>PropertyResourceBundle</code>
87      */

88     private DatabaseSettings() throws ConfigException {
89     }
90
91     public static void createDatabaseSettingsFromFile() throws ConfigException {
92         Properties props = new Properties();
93
94         String JavaDoc classpath = System.getProperty("java.class.path");
95         StringTokenizer paths = new StringTokenizer(classpath, File.pathSeparator);
96         File propertiesFile = null;
97         boolean found = false;
98         while (!found && paths.hasMoreTokens()) {
99             String JavaDoc path = paths.nextToken();
100             propertiesFile = new File(path, "harmonise.properties");
101             found = propertiesFile.exists();
102         }
103         if (found) {
104             
105                 try {
106                     props.load(new FileInputStream(propertiesFile));
107                 } catch (FileNotFoundException e) {
108                     throw new ConfigException(e);
109                 } catch (IOException e) {
110                     throw new ConfigException(e);
111                 }
112                 instance = new DatabaseSettings();
113                 instance.setDbUser(props.getProperty(DataStoreInterfaceFactory.DBUSR_PNAME));
114                 instance.setDbPwd(props.getProperty(DataStoreInterfaceFactory.DBPWD_PNAME));
115                 instance.setDbUrl(props.getProperty(DataStoreInterfaceFactory.DBURL_PNAME));
116                 instance.setDbDriver(props.getProperty(DataStoreInterfaceFactory.DBDRV_PNAME));
117                 instance.setDsiClass(props.getProperty(DataStoreInterfaceFactory.DSI_PNAME));
118
119             
120         }
121     }
122
123     /**
124      * Returns the singleton instance of this class
125      *
126      * @return the singleton instance of this class
127      * @throws ConfigException if an error occurs creating the singleton instance
128      */

129      public static DatabaseSettings getInstance() throws ConfigException {
130         /*
131          * Logic here is simple. If, earlier in the object lifecycle, something
132          * has called createDatabaseSettings and injected configuration params, then
133          * the object created will be returned. This option can be taken by, for example,
134          * ServletContextListener on context startup.
135          *
136          *
137          * Else, we will attempt to read the configuration file and set our properties from that,
138          * construct a new instance and return that. This option can be taken when testing,
139          * for example.
140          *
141          */

142          if (instance == null) {
143             createDatabaseSettingsFromFile();
144         }
145         return instance;
146     }
147
148     /**
149      * @return Returns the dbDriver.
150      */

151     public String JavaDoc getDbDriver() {
152         return dbDriver;
153     }
154     /**
155      * @param dbDriver The dbDriver to set.
156      */

157     private void setDbDriver(String JavaDoc dbDriver) {
158         if (dbDriver == null || dbDriver.length() == 0) {
159             throw new IllegalArgumentException JavaDoc("dbDriver may not be null or zero length");
160         }
161         this.dbDriver = dbDriver;
162     }
163     /**
164      * @return Returns the dbPwd.
165      */

166     public String JavaDoc getDbPwd() {
167         return dbPwd;
168     }
169     /**
170      * @param dbPwd The dbPwd to set.
171      */

172     private void setDbPwd(String JavaDoc dbPwd) {
173         if (dbPwd == null || dbPwd.length() == 0) {
174             throw new IllegalArgumentException JavaDoc("dbPwd may not be null or zero length");
175         }
176         this.dbPwd = dbPwd;
177     }
178     
179     /**
180      * @return Returns the dbUrl.
181      */

182     public String JavaDoc getDbUrl() {
183         return dbUrl;
184     }
185     
186     /**
187      * @param dbUrl The dbUrl to set.
188      */

189     private void setDbUrl(String JavaDoc dbUrl) {
190         if (dbUrl == null || dbUrl.length() == 0) {
191             throw new IllegalArgumentException JavaDoc("dbUrl may not be null or zero length");
192         }
193         this.dbUrl = dbUrl;
194     }
195     
196     /**
197      * @return Returns the dbUser.
198      */

199     public String JavaDoc getDbUser() {
200         return dbUser;
201     }
202     
203     /**
204      * @param dbUser The dbUser to set.
205      */

206     private void setDbUser(String JavaDoc dbUser) {
207         if (dbUser == null || dbUser.length() == 0) {
208             throw new IllegalArgumentException JavaDoc("dbUser may not be null or zero length");
209         }
210         this.dbUser = dbUser;
211     }
212     
213     /**
214      * @return Returns the dsiClass.
215      */

216     public String JavaDoc getDsiClass() {
217         return dsiClass;
218     }
219     
220     /**
221      * @param dsiClass The dsiClass to set.
222      */

223     private void setDsiClass(String JavaDoc dsiClass) {
224         if (dsiClass == null || dsiClass.length() == 0) {
225             throw new IllegalArgumentException JavaDoc("dsiClass may not be null or zero length");
226         }
227         this.dsiClass = dsiClass;
228     }
229     
230
231 }
232
Popular Tags