1 30 31 32 package org.hsqldb; 33 34 import java.lang.reflect.Constructor ; 35 import java.lang.reflect.InvocationTargetException ; 36 import java.net.InetAddress ; 37 import java.net.ServerSocket ; 38 import java.net.Socket ; 39 40 48 public class HsqlSocketFactory { 49 50 private static HsqlSocketFactory plainImpl; 52 private static HsqlSocketFactory sslImpl; 53 54 56 60 protected HsqlSocketFactory() throws Exception {} 61 62 64 73 public static HsqlSocketFactory getInstance(boolean tls) 74 throws Exception { 75 return tls ? getSSLImpl() 76 : getPlainImpl(); 77 } 78 79 public void configureSocket(Socket socket) { 81 82 } 84 85 94 public ServerSocket createServerSocket(int port) throws Exception { 95 return new ServerSocket (port); 96 } 97 98 107 public ServerSocket createServerSocket(int port, 108 String address) throws Exception { 109 return new ServerSocket (port, 128, InetAddress.getByName(address)); 110 } 111 112 122 public Socket createSocket(String host, int port) throws Exception { 123 return new Socket (host, port); 124 } 125 126 131 public boolean isSecure() { 132 return false; 133 } 134 135 private static HsqlSocketFactory getPlainImpl() throws Exception { 137 138 synchronized (HsqlSocketFactory.class) { 139 if (plainImpl == null) { 140 plainImpl = new HsqlSocketFactory(); 141 } 142 } 143 144 return plainImpl; 145 } 146 147 private static HsqlSocketFactory getSSLImpl() throws Exception { 148 149 synchronized (HsqlSocketFactory.class) { 150 if (sslImpl == null) { 151 sslImpl = newFactory("org.hsqldb.HsqlSocketFactorySecure"); 152 } 153 } 154 155 return sslImpl; 156 } 157 158 173 private static HsqlSocketFactory newFactory(String implClass) 174 throws Exception { 175 176 Class clazz; 177 Constructor ctor; 178 Class [] ctorParm; 179 Object [] ctorArg; 180 Object factory; 181 182 clazz = Class.forName(implClass); 183 ctorParm = new Class [0]; 184 185 ctor = clazz.getDeclaredConstructor(ctorParm); 187 ctorArg = new Object [0]; 188 189 try { 190 factory = ctor.newInstance(ctorArg); 191 } catch (InvocationTargetException e) { 192 Throwable t = e.getTargetException(); 193 194 throw (t instanceof Exception ) ? ((Exception ) t) 195 : new RuntimeException ( 196 t.toString()); 197 } 198 199 return (HsqlSocketFactory) factory; 200 } 201 202 } 204 | Popular Tags |