1 45 package org.openejb.client; 46 47 import java.io.IOException ; 48 import java.util.Properties ; 49 import java.net.URI ; 50 51 52 57 public class ConnectionManager { 58 59 private static ConnectionFactory factory; 60 private static Class defaultFactoryClass = SocketConnectionFactory.class; 61 private static String factoryName; 62 63 static { 64 try{ 65 installFactory( defaultFactoryClass.getName()); 66 } catch (Exception e) { 67 } 69 } 70 71 public static Connection getConnection(ServerMetaData server) throws IOException { 72 URI location = server.getLocation(); 73 if (location.getScheme().equals("http")){ 74 return new HttpConnectionFactory().getConnection(server); 75 } else { 76 return factory.getConnection(server); 77 } 78 } 79 80 public static void setFactory(String factoryName) throws IOException { 81 installFactory(factoryName); 82 } 83 84 public static ConnectionFactory getFactory() { 85 return factory; 86 } 87 88 public static String getFactoryName() { 89 return factoryName; 90 } 91 92 private static void installFactory(String factoryName) throws IOException { 93 94 Class factoryClass = null; 95 ConnectionFactory factory = null; 96 97 try { 98 ClassLoader cl = getContextClassLoader(); 99 factoryClass = Class.forName(factoryName, true, cl); 100 } catch ( Exception e ) { 101 throw new IOException ("No ConnectionFactory Can be installed. Unable to load the class "+factoryName); 102 } 103 104 try { 105 factory = (ConnectionFactory)factoryClass.newInstance(); 106 } catch ( Exception e ) { 107 throw new IOException ("No ConnectionFactory Can be installed. Unable to instantiate the class "+factoryName); 108 } 109 110 try { 111 factory.init( new Properties () ); 114 } catch ( Exception e ) { 115 throw new IOException ("No ConnectionFactory Can be installed. Unable to initialize the class "+factoryName); 116 } 117 118 ConnectionManager.factory = factory; 119 ConnectionManager.factoryName = factoryName; 120 } 121 122 public static ClassLoader getContextClassLoader() { 123 return (ClassLoader ) java.security.AccessController.doPrivileged( 124 new java.security.PrivilegedAction () { 125 public Object run() { 126 return Thread.currentThread().getContextClassLoader(); 127 } 128 } 129 ); 130 } 131 132 } 133 | Popular Tags |