Source code: com/clra/util/DBConfiguration.java
1 /*
2 * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3 * Distributed under the GPL license. See doc/COPYING.
4 * $RCSfile: DBConfiguration.java,v $
5 * $Date: 2003/02/26 03:38:45 $
6 * $Revision: 1.5 $
7 */
8
9 package com.clra.util;
10
11 import java.io.InputStream;
12 import java.net.URL;
13 import java.sql.Connection;
14 import java.sql.Driver;
15 import java.sql.DriverManager;
16 import java.sql.SQLException;
17 import java.sql.Statement;
18 import java.sql.ResultSet;
19 import java.util.Properties;
20 import org.apache.log4j.Category;
21 import org.apache.log4j.helpers.Loader;
22
23 /**
24 * A collection of configurable properties used by this package.
25 * @version $Id: DBConfiguration.java,v 1.5 2003/02/26 03:38:45 rphall Exp $
26 * @author <a href="mailto:rphall@pluto.njcc.com">Rick Hall</a>
27 */
28 public class DBConfiguration {
29
30 /** All methods are static */
31 private DBConfiguration() {}
32
33 private final static String base = DBConfiguration.class.getName();
34
35 private final static Category theLog = Category.getInstance( base );
36
37 public final static String PN_DBTYPE = "clra.dbtype";
38
39 public final static String PN_PREFIX_DBDRIVER = "clra.dbdriver.";
40
41 public final static String PN_DBURL = "clra.dburl";
42
43 public final static String PN_DBUSER = "clra.dbuser";
44
45 public final static String PN_DBPASSWORD = "clra.dbpassword";
46
47 private final static String DEFAULTS_FILE =
48 "com/clra/util/util.properties";
49
50 private final static Properties defaultProperties = new Properties();
51 static {
52 InputStream is = null;
53 try {
54 URL url = Loader.getResource( DEFAULTS_FILE, DBConfiguration.class );
55 is = url.openStream();
56 defaultProperties.load( is );
57 if ( theLog.isDebugEnabled() ) {
58 theLog.debug( "loaded properties from '" + DEFAULTS_FILE + "'" );
59 java.util.Enumeration _e = defaultProperties.propertyNames();
60 while ( _e.hasMoreElements() ) {
61 theLog.debug( "property: " + _e.nextElement() );
62 }
63 }
64 }
65 catch( Exception x ){
66 String msg = "unable to load default properties from '"
67 + DEFAULTS_FILE + "'";
68 theLog.fatal(msg,x);
69 throw new IllegalStateException( msg );
70 }
71 finally {
72 if ( is != null ) {
73 try { is.close(); } catch( Exception x ) {}
74 is = null;
75 }
76 } // finally
77 } // static
78
79 private static String DEFAULT_DBTYPE = null;
80 static {
81 String tmp = defaultProperties.getProperty( PN_DBTYPE );
82 if ( tmp == null || tmp.trim().length() == 0 ) {
83 String msg = "invalid or missing value for '" + PN_DBTYPE + "'";
84 theLog.error(msg);
85 }
86 else {
87 DEFAULT_DBTYPE = tmp.trim();
88 }
89 theLog.info( "DEFAULT_DBTYPE == '" + DEFAULT_DBTYPE + "'" );
90 } // static
91
92 public static String DBTYPE = null;
93 static {
94 DBTYPE = System.getProperty( PN_DBTYPE );
95 if ( DBTYPE == null || DBTYPE.trim().length() == 0 ) {
96 DBTYPE = DEFAULT_DBTYPE;
97 }
98 if ( DBTYPE == null || DBTYPE.trim().length() == 0 ) {
99 String msg = "no property for '" + PN_DBTYPE + "'";
100 theLog.fatal( msg );
101 throw new IllegalStateException( msg );
102 }
103 DBTYPE = DBTYPE.trim();
104 theLog.info( "DBTYPE == '" + DBTYPE + "'" );
105 } // static
106
107 public static boolean isOracle() {
108 return DBConfiguration.DBTYPE.equalsIgnoreCase("oracle");
109 }
110
111 private static String DEFAULT_DBDRIVER = null;
112 static {
113 if ( DEFAULT_DBTYPE != null ) {
114 final String PN_DBDRIVER = PN_PREFIX_DBDRIVER + DEFAULT_DBTYPE;
115 String tmp = defaultProperties.getProperty( PN_DBDRIVER );
116 if ( tmp == null || tmp.trim().length() == 0 ) {
117 String msg = "invalid or missing value for '" + PN_DBDRIVER + "'";
118 theLog.error(msg);
119 }
120 else {
121 DEFAULT_DBDRIVER = tmp.trim();
122 }
123 }
124 else {
125 String msg = "no default DBDRIVER for DBConfiguration query";
126 theLog.error(msg);
127 }
128 theLog.info( "DEFAULT_DBDRIVER == '" + DEFAULT_DBDRIVER + "'" );
129 } // static
130
131 public static String DBDRIVER = null;
132 static {
133 final String PN_DBDRIVER = PN_PREFIX_DBDRIVER + DBTYPE;
134 DBDRIVER = System.getProperty( PN_DBDRIVER );
135 if ( DBDRIVER == null || DBDRIVER.trim().length() == 0 ) {
136 DBDRIVER = DEFAULT_DBDRIVER;
137 }
138 if ( DBDRIVER == null || DBDRIVER.trim().length() == 0 ) {
139 String msg = "no property for '" + PN_DBDRIVER + "'";
140 theLog.fatal( msg );
141 throw new IllegalStateException( msg );
142 }
143 theLog.info( "DBDRIVER == '" + DBDRIVER + "'" );
144 } // static
145
146 private static String DEFAULT_DBURL = null;
147 static {
148 if ( DEFAULT_DBTYPE != null ) {
149 String tmp = defaultProperties.getProperty( PN_DBURL );
150 if ( tmp == null || tmp.trim().length() == 0 ) {
151 String msg = "invalid or missing value for '" + PN_DBURL + "'";
152 theLog.error(msg);
153 }
154 else {
155 DEFAULT_DBURL = tmp.trim();
156 }
157 }
158 else {
159 String msg = "no default DBURL for DBConfiguration query";
160 theLog.error(msg);
161 }
162 theLog.info( "DEFAULT_DBURL == '" + DEFAULT_DBURL + "'" );
163 } // static
164
165 public static String DBURL = null;
166 static {
167 DBURL = System.getProperty( PN_DBURL );
168 if ( DBURL == null || DBURL.trim().length() == 0 ) {
169 DBURL = DEFAULT_DBURL;
170 }
171 if ( DBURL == null || DBURL.trim().length() == 0 ) {
172 String msg = "no property for '" + PN_DBURL + "'";
173 theLog.fatal( msg );
174 throw new IllegalStateException( msg );
175 }
176 theLog.info( "DB URL == '" + DBURL + "'" );
177 } // static
178
179 private static String DEFAULT_DBUSER = null;
180 static {
181 if ( DEFAULT_DBTYPE != null ) {
182 String tmp = defaultProperties.getProperty( PN_DBUSER );
183 if ( tmp == null || tmp.trim().length() == 0 ) {
184 String msg = "invalid or missing value for '" + PN_DBUSER + "'";
185 theLog.error(msg);
186 }
187 else {
188 DEFAULT_DBUSER = tmp.trim();
189 }
190 }
191 else {
192 String msg = "no default DBUSER for DBConfiguration query";
193 theLog.error(msg);
194 }
195 theLog.info( "DEFAULT_DBUSER == '" + DEFAULT_DBUSER + "'" );
196 } // static
197
198 public static String DBUSER = null;
199 static {
200 DBUSER = System.getProperty( PN_DBUSER );
201 if ( DBUSER == null || DBUSER.trim().length() == 0 ) {
202 DBUSER = DEFAULT_DBUSER;
203 }
204 if ( DBUSER == null || DBUSER.trim().length() == 0 ) {
205 String msg = "no property for '" + PN_DBUSER + "'";
206 theLog.fatal( msg );
207 throw new IllegalStateException( msg );
208 }
209 theLog.info( "DBUSER == '" + DBUSER + "'" );
210 } // static
211
212 private static String DEFAULT_DBPASSWORD = null;
213 static {
214 if ( DEFAULT_DBTYPE != null ) {
215 String tmp = defaultProperties.getProperty( PN_DBPASSWORD );
216 if ( tmp == null || tmp.trim().length() == 0 ) {
217 String msg = "invalid or missing value for '" + PN_DBPASSWORD + "'";
218 theLog.error(msg);
219 }
220 else {
221 DEFAULT_DBPASSWORD = tmp.trim();
222 }
223 }
224 else {
225 String msg = "no default DBPASSWORD for DBConfiguration query";
226 theLog.error(msg);
227 }
228 theLog.info( "DEFAULT_DBPASSWORD == '" + DEFAULT_DBPASSWORD + "'" );
229 } // static
230
231 public static String DBPASSWORD = null;
232 static {
233 DBPASSWORD = System.getProperty( PN_DBPASSWORD );
234 if ( DBPASSWORD == null || DBPASSWORD.trim().length() == 0 ) {
235 DBPASSWORD = DEFAULT_DBPASSWORD;
236 }
237 if ( DBPASSWORD == null || DBPASSWORD.trim().length() == 0 ) {
238 String msg = "no property for '" + PN_DBPASSWORD + "'";
239 theLog.fatal( msg );
240 throw new IllegalStateException( msg );
241 }
242 theLog.info( "DBPASSWORD == '" + DBPASSWORD + "'" );
243 } // static
244
245 // Register the DB driver used by this class
246 static {
247 try {
248 Class driverClass = Class.forName( DBConfiguration.DBDRIVER );
249 Driver driver = (Driver) driverClass.newInstance();
250 DriverManager.registerDriver( driver );
251 }
252 catch(ClassNotFoundException e) {
253 String msg = "unable to find '" + DBConfiguration.DBDRIVER + "'";
254 theLog.fatal( msg );
255 throw new IllegalStateException(msg);
256 }
257 catch(IllegalAccessException e) {
258 String msg = "unable to instantiate '" + DBConfiguration.DBDRIVER + "'";
259 theLog.fatal( msg );
260 throw new IllegalStateException(msg);
261 }
262 catch(InstantiationException e) {
263 String msg = "unable to instantiate '" + DBConfiguration.DBDRIVER + "'";
264 theLog.fatal( msg );
265 throw new IllegalStateException(msg);
266 }
267 catch(SQLException e) {
268 String msg = "unable to register '" + DBConfiguration.DBDRIVER + "'";
269 theLog.fatal( msg );
270 throw new IllegalStateException(msg);
271 }
272 } // static
273
274 /** Gets a SQL connection */
275 public static Connection getConnection() throws SQLException {
276
277 final String url = DBConfiguration.DBURL;
278 final String usr = DBConfiguration.DBUSER;
279 final String pwd = DBConfiguration.DBPASSWORD;
280 Connection conn = null;
281 try {
282 conn = DriverManager.getConnection( url, usr, pwd );
283 }
284 catch( SQLException x ) {
285 String msg = "unable to get SQL connection for '" + url
286 + "', " + usr + "'," + pwd + "'";
287 theLog.debug( msg );
288 throw x;
289 }
290
291 return conn;
292 } // getConnection()
293
294 /** Closes SQL result set */
295 public static void closeSQLResultSet( ResultSet rs ) {
296 if (rs != null) {
297 try {
298 rs.close();
299 rs = null;
300 }
301 catch(Exception x) {
302 theLog.error( x.getMessage(), x );
303 }
304 }
305 } // closeSQLStatement(Statement)
306
307 /** Closes SQL statement */
308 public static void closeSQLStatement( Statement stmt ) {
309 if (stmt != null) {
310 try {
311 stmt.close();
312 stmt = null;
313 }
314 catch(Exception x) {
315 theLog.error( x.getMessage(), x );
316 }
317 }
318 } // closeSQLStatement(Statement)
319
320 /** Closes SQL connection */
321 public static void closeSQLConnection( Connection conn ) {
322 if (conn != null) {
323 try {
324 conn.close();
325 conn = null;
326 }
327 catch(Exception x) {
328 theLog.error( x.getMessage(), x );
329 }
330 }
331
332 return;
333 } // closeSQLConnection(Connection)
334
335 /**
336 * Converts an int value to an Integer object. Per JDBC, a
337 * zero-valued int is converted to null.
338 */
339 public static Integer convertIntToIntegerOrNull( int i ) {
340 Integer retVal = null;
341 if ( i != 0 ) {
342 retVal = new Integer( i );
343 }
344 return retVal;
345 }
346
347 } // DBConfiguration
348
349 /*
350 * $Log: DBConfiguration.java,v $
351 * Revision 1.5 2003/02/26 03:38:45 rphall
352 * Added copyright and GPL license
353 *
354 * Revision 1.4 2003/02/21 05:03:39 rphall
355 * Parameterized deployment-specific properties
356 *
357 * Revision 1.3 2003/02/19 22:09:16 rphall
358 * Removed gratuitous use of CLRA acronym
359 *
360 * Revision 1.2 2002/01/30 15:13:40 rphall
361 * Changed 'memberset' prefix to 'clra'
362 * Changed default DB to mySQL
363 */
364