1 2 11 12 package 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 import org.objectweb.rmijdbc.RJRMISecurityManager; 31 32 35 public class RJJdbcServer 36 { 37 static 38 { 39 try { 42 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 43 } catch(ClassNotFoundException cnfe) { 44 System.out.println("WARNING: Could not load the JDBC/ODBC Bridge"); 45 } 46 } 47 48 49 static Vector drivers_ = new Vector (); 50 static int port_ = -1; 51 static int lport_ = 0; 52 static boolean startreg_ = true; 53 54 public static boolean verboseMode = true; 55 56 static boolean installRMISecurityMgr = false; 57 private static final int rmiJdbcDefaultPort = 1099; 58 59 public static int rmiJdbcListenerPort = 0; 61 62 public static RMIClientSocketFactory rmiClientSocketFactory = new RJClientSocketFactory(); 64 public static RMIServerSocketFactory rmiServerSocketFactory = new RJServerSocketFactory(); 65 66 static DriverManager dummy_for_gc; 69 70 73 static void processArgs(String args[]) 74 { 75 76 int nb = args.length; 77 78 Hashtable hash = new Hashtable (); 79 hash.put("-noreg",new Integer (0)); 80 hash.put("-port",new Integer (1)); 81 82 hash.put("-sm",new Integer (2)); 83 hash.put("-securitymanager",new Integer (2)); 84 85 hash.put("-lp",new Integer (3)); 86 hash.put("-listenerport",new Integer (3)); 87 88 hash.put("-ssl",new Integer (4)); 89 90 hash.put("-?",new Integer (5)); 91 hash.put("?",new Integer (5)); 92 hash.put("-h",new Integer (5)); 93 hash.put("-help",new Integer (5)); 94 hash.put("help",new Integer (5)); 95 96 97 for (int argnum=0; argnum < nb; argnum++) { 98 99 Integer opt = (Integer )hash.get(args[argnum]); 100 if(opt == null) opt = new Integer (-1); 101 102 switch (opt.intValue()) { 103 104 case 0: 105 startreg_ = false; 106 break; 107 108 case 1: 109 try { 110 port_ = Integer.parseInt(args[++argnum]); 111 } catch(Exception e) { 112 System.err.println("Error: port must be a number"); 113 System.exit(1); 114 } 115 break; 116 117 case 2: 118 installRMISecurityMgr = true; 119 break; 120 121 case 3: 122 try { 123 lport_ = Integer.parseInt(args[++argnum]); 125 } catch(Exception e) { 126 System.err.println("Error: Listener port must be a number"); 127 System.exit(1); 128 } 129 break; 130 131 case 4: 132 try { 133 Security.addProvider((java.security.Provider )Class.forName( 137 "com.sun.net.ssl.internal.ssl.Provider").newInstance()); 138 } catch(Exception ee) { 139 ee.printStackTrace(); 140 System.exit(1); 141 } 142 rmiClientSocketFactory = new RJSSLClientSocketFactory(); 143 rmiServerSocketFactory = new RJSSLServerSocketFactory(); 144 break; 145 146 case 5: 147 RJJdbcServer.printUsage(); 148 System.exit(1); 149 150 default: 151 drivers_.addElement(args[argnum]); 152 break; 153 } 154 } 155 } 156 157 static void printUsage() { 158 159 System.out.println( 160 "Usage:\tjava org.objectweb.rmijdbc.RJJdbcServer [-noreg] [-port port] [-lp port] [-sm] [-ssl] [driver]*"); 161 162 System.out.println( 163 "-noreg: No internal registry, requires rmiregistry to be started"); 164 System.out.println( 165 "-port: specify a TCP port number for the rmi registry (default: 1099)"); 166 System.out.println( 167 "-lp: specify a TCP port number for the remote objects to listen (default: anonymous)"); 168 System.out.println( 169 "-sm: Install RMI security manager (not installed by default)"); 170 System.out.println( 171 "-ssl: Run in SSL mode (both javax.net.ssl.trustStore and javax.net.ssl.trustStorePassword must be defined"); 172 System.out.println("[driver]*: A list of JDBC driver classes"); 173 } 174 175 176 void register(String name, int port, boolean startreg) throws Exception 177 { 178 String host; 179 try { 180 String hostprop="java.rmi.server.hostname"; 181 host = System.getProperty(hostprop); 182 184 } catch (Exception e) { 185 186 host = null; 187 System.out.println("WARNING: java.rmi.server.hostname property" 188 + " can\'t be read (access denied)"); 189 System.out.println("If you use java.rmi.server.hostname, set the" 190 + " corresponding property to \"read\" in your java.policy file"); 191 } 192 193 if (host == null) host = InetAddress.getLocalHost().getHostName(); 195 196 String rmiRef = "//" + host + "/" + name; 197 198 if (port > 0) { 199 rmiRef = new String ("//" + host + ":" + port + "/" + name); 200 } 201 202 RJDriverServer theDriver = buildDriverServer(); 206 if (!startreg) { Naming.rebind(rmiRef, theDriver); 208 return; 209 } 210 211 if (port <= 0) port = rmiJdbcDefaultPort; 213 Registry registry = LocateRegistry.createRegistry(port); 214 registry.rebind(name, theDriver); 215 } 217 218 219 225 RJDriverServer buildDriverServer() throws java.rmi.RemoteException { 226 return new RJDriverServer(); 227 } 228 229 public static void main(String [] args) { 230 try { 231 Class.forName("org.objectweb.rmijdbc.RJDriverServer_Stub"); 232 } catch(ClassNotFoundException cnfe) { 233 System.out.println("Can't find stub!"); 234 System.exit(0); 235 } 236 237 verboseMode = Boolean.valueOf( 238 System.getProperty("RmiJdbc.verbose", "true")).booleanValue(); 239 240 processArgs(args); 241 242 System.out.println("===================================================="); 243 System.out.println("WARNING: " 244 + "This class is deprecated, and provided for backward compatibility"); 245 System.out.println("The right way to start the RmiJdbc server is now:"); 246 System.out.println(" java org.objectweb.rmijdbc.RJJdbcServer"); 247 System.out.println("===================================================="); 248 249 printMsg("Starting RmiJdbc Server !"); 250 251 initServer(new RJJdbcServer()); } 255 256 257 static void initServer(RJJdbcServer theServer) { 259 try { 260 261 263 if(lport_<0) { 264 printMsg(" Invalid TCP port \" "+lport_+" \" as listener port for remote objects: Using an anonymous port"); 265 } else if(lport_>0) { 266 rmiJdbcListenerPort=lport_; 267 printMsg("Remote objects will be listening on port number: "+rmiJdbcListenerPort); 268 } 269 270 276 for(int i = 0; i < drivers_.size(); i++) 277 { 278 String drv = (String )drivers_.elementAt(i); 279 try { 280 Class.forName(drv).newInstance(); 283 printMsg(drv + " registered in DriverManager"); 284 } catch(Exception e) { 285 System.err.println("*** Can't register jdbc Driver for " + drv); 286 System.err.println("Error message is: " + e.getMessage()); 287 } 288 } 289 290 java.util.Enumeration ed = DriverManager.getDrivers(); 294 while(ed.hasMoreElements()) { Driver d = (Driver )ed.nextElement(); } 295 296 if (System.getSecurityManager() == (SecurityManager ) null) { 318 if (installRMISecurityMgr) 320 System.setSecurityManager( 321 (SecurityManager )new RJRMISecurityManager()); 322 else 323 printMsg("No installation of RMI Security Manager..."); 324 325 } else { 326 330 printMsg( 331 "** Warning: RMI Security Manager has NOT been installed as "); 332 printMsg( 333 "** a Security Manager was already installed by the application...\n"); 334 } 335 336 339 printMsg("Binding RmiJdbcServer..."); 340 theServer.register("RmiJdbcServer", port_, startreg_); 341 printMsg("RmiJdbcServer bound in rmi registry"); 342 343 if (startreg_) { 345 Thread tt = new Thread (); 350 tt.suspend(); 351 } 352 353 } 354 catch(Exception e) 355 { 356 System.err.println("Got Exception: "+e.getMessage()); 357 e.printStackTrace(); 358 System.exit(1); 359 } 360 } 361 362 369 public static void printMsg(String msg) { 370 if (verboseMode) 371 System.out.println(new java.util.Date ().toString() 372 + ": [RmiJdbc] " + msg); 373 } 374 375 }; 376 377 | Popular Tags |