KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jdbclogger > JdbcLoggerDriver


1 package net.sourceforge.jdbclogger;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import net.sourceforge.jdbclogger.core.AbstractWrapperDriver;
20 import net.sourceforge.jdbclogger.core.ConnectionWrapper;
21 import net.sourceforge.jdbclogger.core.config.Environment;
22 import net.sourceforge.jdbclogger.core.config.JdbcLoggerConstants;
23 import net.sourceforge.jdbclogger.core.util.ConfigHelper;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.Driver JavaDoc;
32 import java.sql.DriverManager JavaDoc;
33 import java.sql.SQLException JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.Properties JavaDoc;
36
37
38 /**
39  * @author Catalin Kormos (latest modification by $Author: sorin7486 $)
40  * @version $Revision: 92 $ $Date: 2007-07-09 15:20:51 +0300 (Mon, 09 Jul 2007) $
41  */

42 public class JdbcLoggerDriver extends AbstractWrapperDriver {
43
44     private static Log log = LogFactory.getLog(JdbcLoggerDriver.class);
45
46     static
47     {
48         // getting the name of the driver requested by the user (in the jdbclogger.properties file )
49
String JavaDoc userDriverClassName = Environment.getProperties().getProperty(JdbcLoggerConstants.USER_DRIVER_PROPERTY_NAME);
50
51         // in case no driver name is found an error is reported
52
if (userDriverClassName == null){
53             log.fatal("Missing jdbclogger.properties in classpath with content:\n" +
54                     JdbcLoggerConstants.USER_DRIVER_PROPERTY_NAME+"=<your driver class name>");
55         }
56         else {
57             try {
58                 //look in the classpath for a custom driver that supports the requested driver
59
Driver userDriver = findRegisteredUserDriver(userDriverClassName);
60
61                 if (userDriver == null) {
62                     //there is no registered user driver, try registering it now
63
try {
64                         Class.forName(userDriverClassName);
65                         //try again, getting the instance from the driver manager
66
userDriver = findRegisteredUserDriver(userDriverClassName);
67                     }
68                     catch (ClassNotFoundException JavaDoc exc) {
69                         //ignore, we failed to initialize the user driver
70
}
71                 }
72
73                 if (userDriver != null) {
74                     AbstractWrapperDriver wrapperDriver = getWrapperDriver(userDriverClassName);
75                     wrapperDriver.setDriver(userDriver);
76
77                     DriverManager.deregisterDriver(userDriver);
78                     DriverManager.registerDriver(wrapperDriver);
79                 }
80                 else {
81                     log.fatal("Could not find user driver '"+userDriverClassName+"' in the DriverManager.");
82                 }
83             }
84             catch(Exception JavaDoc exc) {
85                 log.error("Exception while registering driver.", exc);
86             }
87         }
88     }
89
90     /**
91      *
92      */

93     public JdbcLoggerDriver() {
94         super();
95     }
96
97     /**
98      * @param userDriverClassName
99      * @return
100      */

101     public static Driver findRegisteredUserDriver(String JavaDoc userDriverClassName) {
102         Enumeration JavaDoc en = DriverManager.getDrivers();
103         while (en.hasMoreElements()){
104             Driver aDriver = (Driver) en.nextElement();
105             if (aDriver.getClass().getName().equals(userDriverClassName)){
106                 return aDriver;
107             }
108         }
109
110         return null;
111     }
112
113     /**
114      * @param userDriverClassName
115      * @return
116      */

117     public static AbstractWrapperDriver getWrapperDriver(String JavaDoc userDriverClassName){
118         Enumeration JavaDoc driverList = ConfigHelper.getResources("META-INF/" + JdbcLoggerConstants.WRAPPER_DRIVER_CONFIG_FILE_NAME);
119         while (driverList.hasMoreElements()) {
120             Properties JavaDoc driverProperties = new Properties JavaDoc();
121             URL JavaDoc resUrl = (URL JavaDoc) driverList.nextElement();
122             InputStream JavaDoc res = null;
123             try {
124                 res = resUrl.openStream();
125                 driverProperties.load(res);
126             }
127             catch (IOException JavaDoc e) {
128                 log.error("Error reading "+JdbcLoggerConstants.WRAPPER_DRIVER_CONFIG_FILE_NAME);
129             }
130             finally {
131                 if (res != null)
132                     try {
133                         res.close();
134                     }
135                     catch (IOException JavaDoc e) {
136                         log.error("Closing input stream", e);
137                     }
138             }
139
140             if (userDriverClassName.equals(driverProperties.getProperty(JdbcLoggerConstants.TARGET_DRIVER_PROPERTY_NAME))){
141                 String JavaDoc wrapperDriver = driverProperties.getProperty(JdbcLoggerConstants.WRAPPER_DRIVER_PROPERTY_NAME);
142                 try {
143                     return (AbstractWrapperDriver) Class.forName(wrapperDriver).newInstance();
144                 }
145                 catch (Exception JavaDoc exc) {
146                     log.error("Unable to instantiate wrapper class: " + exc);
147                 }
148             }
149         }
150         return new JdbcLoggerDriver();
151     }
152
153     public Connection JavaDoc connect(String JavaDoc url, Properties JavaDoc properties) throws SQLException JavaDoc {
154         if (getDriver() == null)
155             return null;
156
157         return new ConnectionWrapper(getDriver().connect(url, properties), formatters);
158     }
159 }
160
Popular Tags