1 2 9 10 package org.objectweb.rmijdbc; 11 12 import java.sql.*; 13 import java.rmi.RemoteException ; 14 import java.rmi.registry.*; 15 import java.rmi.NotBoundException ; 16 import java.net.InetAddress ; 17 import java.util.*; 18 19 41 public class Driver implements java.sql.Driver , java.io.Serializable { 42 43 static { 44 try { 45 DriverManager.registerDriver(new org.objectweb.rmijdbc.Driver()); 47 } catch (Exception e) { 48 e.printStackTrace(); 49 } 50 } 51 52 public Driver() throws Exception {} 53 54 private static int RMI_REGISTRY = 0; 55 private static int RMI_ADDRESS = 1; 56 private static int JDBC_URL = 2; 57 58 private String [] splitURL(String url) { 59 60 65 66 String serverHostName, jdbcUrl; 67 if(url.substring(9,11).equals("//")) { 68 try { 69 serverHostName = url.substring(9, url.indexOf("/",11)); 70 } catch(Exception e) { serverHostName = url.substring(9); } 71 try { 72 jdbcUrl = url.substring(url.indexOf("/",11)+1); 73 } catch(Exception e) { jdbcUrl = null; } 74 } else { 75 try { 76 serverHostName = "//" + InetAddress.getLocalHost().getHostName(); 77 } catch(Exception e) { 78 System.err.println( 79 "WARNING: can\'t retrieve local host name, //localhost will be used !" 80 + " Exception follows:"); 81 e.printStackTrace(); 82 serverHostName = "//localhost"; 83 } 84 85 jdbcUrl = url.substring(9); 86 87 if(! jdbcUrl.toLowerCase().startsWith("jdbc")) { 90 System.err.println( 91 "WARNING: Wierd RMI server URL: localhost will be used."); 92 jdbcUrl = url.substring(url.indexOf("/", 9) + 1); 93 } 94 } 95 String rmiAddr = serverHostName + "/RmiJdbcServer"; 96 97 String split[] = new String [3]; 98 split[RMI_REGISTRY] = serverHostName.substring(2); 99 split[RMI_ADDRESS] = rmiAddr; 100 split[JDBC_URL] = jdbcUrl; 101 return split; 102 } 103 104 private RJDriverInterface lookupDriver(String url) throws Exception { 105 String split[] = splitURL(url); 106 107 StringTokenizer st = new StringTokenizer(split[RMI_REGISTRY], ":"); 109 if(!st.hasMoreTokens()) { 110 throw new SQLException("No RMI server host specified in JDBC URL"); 111 } 112 String host = st.nextToken(); 113 int port = 1099; 114 if(st.hasMoreTokens()) { 115 try { 116 port = Integer.parseInt(st.nextToken()); 117 } catch(NumberFormatException e) { 118 port = 1099; 119 } 120 } 121 122 Registry registry = LocateRegistry.getRegistry(host, port); 123 124 RJDriverInterface theDriver; 129 try { 130 theDriver = (RJDriverInterface)registry.lookup(split[RMI_ADDRESS]); 131 } catch(NotBoundException e) { 132 String obj; 133 int pos = split[RMI_ADDRESS].lastIndexOf("/"); 134 if(pos < 0) obj = split[RMI_ADDRESS]; 135 else obj = split[RMI_ADDRESS].substring(pos+1); 136 theDriver = (RJDriverInterface)registry.lookup(obj); 137 } 138 139 return theDriver; 140 } 141 142 143 146 public void shutdown(String url, String pwd) throws RemoteException { 147 148 try { 149 150 RJDriverInterface drv = lookupDriver(url); 151 if(drv != null) { 152 try { 153 drv.shutdown(pwd); 154 } catch(java.rmi.UnmarshalException e) { 155 } 157 } 158 159 } catch(RemoteException e) { 160 throw e; 161 } catch(Exception e) { 162 throw new RemoteException (e.getMessage()); 163 } 164 } 165 166 190 public java.sql.Connection connect(String url,Properties info) 191 throws SQLException { 192 193 if (!acceptsURL(url)) 194 return null; 196 String split[] = splitURL(url); 197 try { 198 return new RJConnection(lookupDriver(url), split[JDBC_URL], info); 200 201 } catch(SQLException e) { 202 e.printStackTrace(); 203 throw e; 204 } catch(Exception e) { 205 e.printStackTrace(); 206 throw new SQLException(e.getMessage()); 207 } 208 } 209 210 219 public boolean acceptsURL(String url) throws SQLException { 220 return url.startsWith("jdbc:rmi:"); 221 } 222 223 238 public java.sql.DriverPropertyInfo [] getPropertyInfo(String url, 239 java.util.Properties info) throws SQLException { 240 try { 241 242 RJDriverInterface drv = lookupDriver(url); 243 String split[] = splitURL(url); 244 245 RJDriverPropertyInfo infos[] = drv.getPropertyInfo(split[JDBC_URL], info); 248 if(infos == null) return null; 249 DriverPropertyInfo dpis[] = new DriverPropertyInfo[infos.length]; 250 for(int i=0; i<infos.length; i++) { 251 if(infos[i] == null) dpis[i] = null; 252 dpis[i] = infos[i].getPropertyInfo(); 253 } 254 return dpis; 255 256 } catch(RemoteException e) { 257 throw new SQLException("[RemoteException] " + e.getMessage()); 258 } catch(java.rmi.NotBoundException e) { 259 throw new SQLException("[java.rmi.NotBoundException] " + e.getMessage()); 260 } catch(Exception e) { 261 throw new SQLException(e.getMessage()); 262 } 263 } 264 265 266 269 public int getMajorVersion() { 270 return 1; 271 } 272 273 276 public int getMinorVersion() { 277 return 0; 278 } 279 280 281 297 public boolean jdbcCompliant() { 298 return true; 299 } 300 301 }; 302 303 | Popular Tags |