1 2 11 12 package org.objectweb.rmijdbc; 13 14 import java.sql.DriverManager ; 15 import java.sql.Driver ; 16 import java.rmi.*; 17 import java.rmi.registry.*; 18 import java.rmi.server.*; 19 import java.net.InetAddress ; 20 import java.util.Vector ; 21 import java.util.Hashtable ; 22 import java.security.*; 23 24 import org.objectweb.rmijdbc.RJDriverServer; 25 import org.objectweb.rmijdbc.RJSSLClientSocketFactory; 26 import org.objectweb.rmijdbc.RJSSLServerSocketFactory; 27 import org.objectweb.rmijdbc.RJClientSocketFactory; 28 import org.objectweb.rmijdbc.RJServerSocketFactory; 29 30 33 public class RJJdbcServer 34 { 35 static 36 { 37 try { 40 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 41 } catch(ClassNotFoundException cnfe) { 42 System.out.println("WARNING: Could not load the JDBC/ODBC Bridge"); 43 } 44 } 45 46 47 static Vector drivers_ = new Vector (); 48 static int port_ = -1; 49 static int lport_ = 0; 50 static boolean startreg_ = true; 51 static String admpasswd_ = null; 52 53 public static boolean verboseMode = true; 54 55 static boolean installRMISecurityMgr = false; 56 private static final int rmiJdbcDefaultPort = 1099; 57 58 public static int rmiJdbcListenerPort = 0; 60 61 public static RMIClientSocketFactory rmiClientSocketFactory = new RJClientSocketFactory(); 63 public static RMIServerSocketFactory rmiServerSocketFactory = new RJServerSocketFactory(); 64 65 static DriverManager dummy_for_gc; 68 69 72 static void processArgs(String args[]) 73 { 74 75 int nb = args.length; 76 77 Hashtable hash = new Hashtable (); 78 hash.put("-noreg",new Integer (0)); 79 hash.put("-port",new Integer (1)); 80 81 hash.put("-sm",new Integer (2)); 82 hash.put("-securitymanager",new Integer (2)); 83 84 hash.put("-lp",new Integer (3)); 85 hash.put("-listenerport",new Integer (3)); 86 87 hash.put("-ssl",new Integer (4)); 88 89 hash.put("-passwd",new Integer (5)); 90 91 hash.put("-?",new Integer (99)); 92 hash.put("?",new Integer (99)); 93 hash.put("-h",new Integer (99)); 94 hash.put("-help",new Integer (99)); 95 hash.put("help",new Integer (99)); 96 97 98 for (int argnum=0; argnum < nb; argnum++) { 99 100 Integer opt = (Integer )hash.get(args[argnum]); 101 if(opt == null) opt = new Integer (-1); 102 103 switch (opt.intValue()) { 104 105 case 0: 106 startreg_ = false; 107 break; 108 109 case 1: 110 try { 111 port_ = Integer.parseInt(args[++argnum]); 112 } catch(Exception e) { 113 System.err.println("Error: port must be a number"); 114 System.exit(1); 115 } 116 break; 117 118 case 2: 119 installRMISecurityMgr = true; 120 break; 121 122 case 3: 123 try { 124 lport_ = Integer.parseInt(args[++argnum]); 126 } catch(Exception e) { 127 System.err.println("Error: Listener port must be a number"); 128 System.exit(1); 129 } 130 break; 131 132 case 4: 133 try { 134 Security.addProvider((java.security.Provider )Class.forName( 138 "com.sun.net.ssl.internal.ssl.Provider").newInstance()); 139 } catch(Exception ee) { 140 ee.printStackTrace(); 141 System.exit(1); 142 } 143 rmiClientSocketFactory = new RJSSLClientSocketFactory(); 144 rmiServerSocketFactory = new RJSSLServerSocketFactory(); 145 break; 146 147 case 5: 148 try { 149 admpasswd_ = args[++argnum]; 150 } catch(Exception e) { 151 System.err.println("Error: no value specified for -passwd option"); 152 System.exit(1); 153 } 154 break; 155 156 case 99: 157 RJJdbcServer.printUsage(); 158 System.exit(1); 159 160 default: 161 drivers_.addElement(args[argnum]); 162 break; 163 } 164 } 165 } 166 167 static void printUsage() { 168 169 System.out.println( 170 "Usage:\tjava org.objectweb.rmijdbc.RJJdbcServer [-noreg] [-port port] [-lp port] [-sm] [-ssl] [driver]*"); 171 172 System.out.println( 173 "-noreg: No internal registry, requires rmiregistry to be started"); 174 System.out.println( 175 "-port: specify a TCP port number for the rmi registry (default: 1099)"); 176 System.out.println( 177 "-lp: specify a TCP port number for the remote objects to listen (default: anonymous)"); 178 System.out.println( 179 "-sm: Install RMI security manager (not installed by default)"); 180 System.out.println( 181 "-ssl: Run in SSL mode (both javax.net.ssl.trustStore and javax.net.ssl.trustStorePassword must be defined"); 182 System.out.println( 183 "-passwd: specify an administrative password"); 184 System.out.println("[driver]*: A list of JDBC driver classes"); 185 } 186 187 188 void register(String name, int port, boolean startreg) throws Exception 189 { 190 String host; 191 try { 192 String hostprop="java.rmi.server.hostname"; 193 host = System.getProperty(hostprop); 194 196 } catch (Exception e) { 197 198 host = null; 199 System.out.println("WARNING: java.rmi.server.hostname property" 200 + " can\'t be read (access denied)"); 201 System.out.println("If you use java.rmi.server.hostname, set the" 202 + " corresponding property to \"read\" in your java.policy file"); 203 } 204 205 if (host == null) host = InetAddress.getLocalHost().getHostName(); 207 208 String rmiRef = "//" + host + "/" + name; 209 210 if (port > 0) { 211 rmiRef = new String ("//" + host + ":" + port + "/" + name); 212 } 213 214 RJDriverServer theDriver = buildDriverServer(); 218 if (!startreg) { Naming.rebind(rmiRef, theDriver); 220 return; 221 } 222 223 if (port <= 0) port = rmiJdbcDefaultPort; 225 Registry registry = LocateRegistry.createRegistry(port); 226 registry.rebind(name, theDriver); 227 } 229 230 231 237 RJDriverServer buildDriverServer() throws java.rmi.RemoteException { 238 return new RJDriverServer(admpasswd_); 239 } 240 241 public static void main(String [] args) { 242 try { 243 Class.forName("org.objectweb.rmijdbc.RJDriverServer_Stub"); 244 } catch(ClassNotFoundException cnfe) { 245 System.out.println("Can't find stub!"); 246 System.exit(0); 247 } 248 249 verboseMode = Boolean.valueOf( 250 System.getProperty("RmiJdbc.verbose", "true")).booleanValue(); 251 252 processArgs(args); 253 254 printMsg("Starting RmiJdbc Server !"); 255 256 initServer(new RJJdbcServer()); } 260 261 262 static void initServer(RJJdbcServer theServer) { 264 try { 265 266 268 if(lport_<0) { 269 printMsg(" Invalid TCP port \" "+lport_+" \" as listener port for remote objects: Using an anonymous port"); 270 } else if(lport_>0) { 271 rmiJdbcListenerPort=lport_; 272 printMsg("Remote objects will be listening on port number: "+rmiJdbcListenerPort); 273 } 274 275 281 for(int i = 0; i < drivers_.size(); i++) 282 { 283 String drv = (String )drivers_.elementAt(i); 284 try { 285 Class.forName(drv).newInstance(); 288 printMsg(drv + " registered in DriverManager"); 289 } catch(Exception e) { 290 System.err.println("*** Can't register jdbc Driver for " + drv); 291 System.err.println("Error message is: " + e.getMessage()); 292 } 293 } 294 295 java.util.Enumeration ed = DriverManager.getDrivers(); 299 while(ed.hasMoreElements()) { Driver d = (Driver)ed.nextElement(); } 300 301 if (System.getSecurityManager() == (SecurityManager ) null) { 323 if (installRMISecurityMgr) 325 System.setSecurityManager( 326 (SecurityManager )new RJRMISecurityManager()); 327 else 328 printMsg("No installation of RMI Security Manager..."); 329 330 } else { 331 335 printMsg( 336 "** Warning: RMI Security Manager has NOT been installed as "); 337 printMsg( 338 "** a Security Manager was already installed by the application...\n"); 339 } 340 341 344 printMsg("Binding RmiJdbcServer..."); 345 theServer.register("RmiJdbcServer", port_, startreg_); 346 printMsg("RmiJdbcServer bound in rmi registry"); 347 348 if (startreg_) { 350 Thread tt = new Thread (); 355 tt.suspend(); 356 } 357 358 } 359 catch(Exception e) 360 { 361 System.err.println("Got Exception: "+e.getMessage()); 362 e.printStackTrace(); 363 System.exit(1); 364 } 365 } 366 367 374 public static void printMsg(String msg) { 375 if (verboseMode) 376 System.out.println(new java.util.Date ().toString() 377 + ": [RmiJdbc] " + msg); 378 } 379 380 }; 381 382 | Popular Tags |