01: /**
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
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: */package org.apache.openejb.resource.jdbc;
18:
19: import org.apache.xbean.finder.ResourceFinder;
20:
21: import java.sql.SQLException;
22: import java.util.Map;
23: import java.io.IOException;
24:
25: public final class BasicDataSourceUtil {
26: private BasicDataSourceUtil() {
27: }
28:
29: public static DataSourcePlugin getDataSourcePlugin(String jdbcUrl)
30: throws SQLException {
31: // determine the vendor based on the jdbcUrl stirng "jdbc:${Vendor}:properties"
32: String vendor = getJdbcName(jdbcUrl);
33:
34: // no vendor so no plugin
35: if (vendor == null)
36: return null;
37:
38: // find the plugin class
39: String pluginClassName = null;
40: try {
41: ResourceFinder finder = new ResourceFinder("META-INF");
42: Map<String, String> plugins = finder
43: .mapAvailableStrings(DataSourcePlugin.class
44: .getName());
45: pluginClassName = plugins.get(vendor);
46: } catch (IOException ignored) {
47: // couldn't determine the plugins, which isn't fatal
48: }
49:
50: // no plugin found
51: if (pluginClassName == null || pluginClassName.length() <= 0) {
52: return null;
53: }
54:
55: // create the plugin
56: try {
57: Class pluginClass = Class.forName(pluginClassName);
58: DataSourcePlugin plugin = (DataSourcePlugin) pluginClass
59: .newInstance();
60: return plugin;
61: } catch (ClassNotFoundException e) {
62: throw new SQLException(
63: "Unable to load data source helper class '"
64: + pluginClassName + "' for database '"
65: + vendor + "'");
66: } catch (Exception e) {
67: throw (SQLException) new SQLException(
68: "Unable to create data source helper class '"
69: + pluginClassName + "' for database '"
70: + vendor + "'").initCause(e);
71: }
72: }
73:
74: public static String getJdbcName(String jdbcUrl) {
75: // nothing gets you nothing
76: if (jdbcUrl == null)
77: return null;
78:
79: // strip off "jdbc:"
80: if (!jdbcUrl.startsWith("jdbc:")) {
81: return null;
82: }
83: jdbcUrl = jdbcUrl.substring("jdbc:".length());
84:
85: // return text up to first ":" if present
86: int index = jdbcUrl.indexOf(':');
87:
88: // It is ok to have no trailing ':'. This may be a url like jdbc:specialDB.
89: if (index >= 0) {
90: jdbcUrl = jdbcUrl.substring(0, index);
91: }
92:
93: return jdbcUrl;
94: }
95: }
|