001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.client;
017:
018: import java.io.IOException;
019: import java.util.Properties;
020: import java.net.URI;
021:
022: public class ConnectionManager {
023:
024: private static ConnectionFactory factory;
025:
026: private static Class defaultFactoryClass = SocketConnectionFactory.class;
027:
028: private static String factoryName;
029:
030: static {
031: try {
032: installFactory(defaultFactoryClass.getName());
033: } catch (Exception e) {
034:
035: }
036: }
037:
038: public static Connection getConnection(URI uri) throws IOException {
039: if (uri.getScheme().equals("http")
040: || uri.getScheme().equals("https")) {
041: return new HttpConnectionFactory().getConnection(uri);
042: } else {
043: return factory.getConnection(uri);
044: }
045: }
046:
047: public static void setFactory(String factoryName)
048: throws IOException {
049: installFactory(factoryName);
050: }
051:
052: public static void setFactory(ConnectionFactory factory)
053: throws IOException {
054: if (null == factory) {
055: throw new IllegalArgumentException("factory is required");
056: }
057: ConnectionManager.factory = factory;
058: factoryName = factory.getClass().getName();
059: }
060:
061: public static ConnectionFactory getFactory() {
062: return factory;
063: }
064:
065: public static String getFactoryName() {
066: return factoryName;
067: }
068:
069: private static void installFactory(String factoryName)
070: throws IOException {
071:
072: Class factoryClass = null;
073: ConnectionFactory factory = null;
074:
075: try {
076: ClassLoader cl = getContextClassLoader();
077: factoryClass = Class.forName(factoryName, true, cl);
078: } catch (Exception e) {
079: throw new IOException(
080: "No ConnectionFactory Can be installed. Unable to load the class "
081: + factoryName);
082: }
083:
084: try {
085: factory = (ConnectionFactory) factoryClass.newInstance();
086: } catch (Exception e) {
087: throw new IOException(
088: "No ConnectionFactory Can be installed. Unable to instantiate the class "
089: + factoryName);
090: }
091:
092: try {
093:
094: factory.init(new Properties());
095: } catch (Exception e) {
096: throw new IOException(
097: "No ConnectionFactory Can be installed. Unable to initialize the class "
098: + factoryName);
099: }
100:
101: ConnectionManager.factory = factory;
102: ConnectionManager.factoryName = factoryName;
103: }
104:
105: public static ClassLoader getContextClassLoader() {
106: return (ClassLoader) java.security.AccessController
107: .doPrivileged(new java.security.PrivilegedAction() {
108: public Object run() {
109: return Thread.currentThread()
110: .getContextClassLoader();
111: }
112: });
113: }
114:
115: }
|