KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jdbc > JDBCAdapterFactory


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

17 package org.apache.servicemix.jdbc;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21
22 import org.apache.activeio.util.FactoryFinder;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.servicemix.jdbc.adapter.DefaultJDBCAdapter;
26
27 public class JDBCAdapterFactory {
28
29     private static final Log log = LogFactory.getLog(JDBCAdapterFactory.class);
30     private static FactoryFinder factoryFinder = new FactoryFinder("META-INF/services/org/apache/servicemix/jdbc/");
31
32     public static JDBCAdapter getAdapter(Connection JavaDoc connection) {
33         JDBCAdapter adapter = null;
34         try {
35
36             // Make the filename file system safe.
37
String JavaDoc driverName = connection.getMetaData().getDriverName();
38             driverName = driverName.replaceAll("[^a-zA-Z0-9\\-]", "_").toLowerCase();
39
40             try {
41                 adapter = (JDBCAdapter) factoryFinder.newInstance(driverName);
42                 log.info("Database driver recognized: [" + driverName + "]");
43             } catch (Throwable JavaDoc e) {
44                 log.warn("Database driver NOT recognized: [" + driverName
45                         + "]. Will use default JDBC implementation.");
46             }
47
48         } catch (SQLException JavaDoc e) {
49             log.warn("JDBC error occurred while trying to detect database type. Will use default JDBC implementation: "
50                             + e.getMessage());
51             log("Failure details: ", e);
52         }
53
54         // Use the default JDBC adapter if the
55
// Database type is not recognized.
56
if (adapter == null) {
57             adapter = new DefaultJDBCAdapter();
58         }
59         
60         return adapter;
61     }
62     
63     public static void log(String JavaDoc msg, SQLException JavaDoc e) {
64         if (log.isDebugEnabled()) {
65             if (log.isDebugEnabled()) {
66                 String JavaDoc s = msg + e.getMessage();
67                 while (e.getNextException() != null) {
68                     e = e.getNextException();
69                     s += ", due to: " + e.getMessage();
70                 }
71                 log.debug(s, e);
72             }
73         }
74     }
75 }
76
Popular Tags