KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > rmijdbc > RJJdbcServer


1
2 /**
3  * RmiJdbc client/server JDBC Driver
4  * (C) GIE Dyade (Groupe BULL / INRIA Research Center) 1997
5  *
6  * @version 1.0
7  * @author Pierre-Yves Gibello (pierreyves.gibello@experlog.com)
8  * Additional SSL support
9  * Douglas Hammond(djhammond@sympatico.ca)
10  */

11
12 package org.objectweb.rmijdbc;
13
14 import java.sql.DriverManager JavaDoc;
15 import java.sql.Driver JavaDoc;
16 import java.rmi.*;
17 import java.rmi.registry.*;
18 import java.rmi.server.*;
19 import java.net.InetAddress JavaDoc;
20 import java.util.Vector JavaDoc;
21 import java.util.Hashtable JavaDoc;
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 /**
31  * The main class for RMI/JDBC Server
32  */

33 public class RJJdbcServer
34 {
35 static
36   {
37 // load the Jdbc-Odbc bridge by default
38
// added by Mike Jennings in the summer of 1999
39
try {
40       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
41     } catch(ClassNotFoundException JavaDoc cnfe) {
42       System.out.println("WARNING: Could not load the JDBC/ODBC Bridge");
43     }
44 }
45
46
47   static Vector JavaDoc drivers_ = new Vector JavaDoc();
48   static int port_ = -1;
49   static int lport_ = 0;
50   static boolean startreg_ = true;
51   static String JavaDoc admpasswd_ = null;
52
53   public static boolean verboseMode = true;
54
55   static boolean installRMISecurityMgr = false;
56   private static final int rmiJdbcDefaultPort = 1099;
57
58   //Default value of the listener port for remote objects
59
public static int rmiJdbcListenerPort = 0;
60
61   //Set communication type defaul id normal sockets
62
public static RMIClientSocketFactory rmiClientSocketFactory = new RJClientSocketFactory();
63   public static RMIServerSocketFactory rmiServerSocketFactory = new RJServerSocketFactory();
64
65   // GC problem on Linux Red Hat 6.0 + JDK v1.1.7B (Blackdown)
66
// Thanks to Per Widerlund from Parallel Systems for the fix.
67
static DriverManager JavaDoc dummy_for_gc;
68
69   /**
70    * Process the command-line parameters
71    */

72 static void processArgs(String JavaDoc args[])
73   {
74
75     int nb = args.length;
76
77     Hashtable JavaDoc hash = new Hashtable JavaDoc();
78     hash.put("-noreg",new Integer JavaDoc(0));
79     hash.put("-port",new Integer JavaDoc(1));
80
81     hash.put("-sm",new Integer JavaDoc(2));
82     hash.put("-securitymanager",new Integer JavaDoc(2));
83
84     hash.put("-lp",new Integer JavaDoc(3));
85     hash.put("-listenerport",new Integer JavaDoc(3));
86
87     hash.put("-ssl",new Integer JavaDoc(4));
88
89     hash.put("-passwd",new Integer JavaDoc(5));
90     
91     hash.put("-?",new Integer JavaDoc(99));
92     hash.put("?",new Integer JavaDoc(99));
93     hash.put("-h",new Integer JavaDoc(99));
94     hash.put("-help",new Integer JavaDoc(99));
95     hash.put("help",new Integer JavaDoc(99));
96
97     
98     for (int argnum=0; argnum < nb; argnum++) {
99
100       Integer JavaDoc opt = (Integer JavaDoc)hash.get(args[argnum]);
101       if(opt == null) opt = new Integer JavaDoc(-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 JavaDoc 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           // Check for valid port number entry
125
lport_ = Integer.parseInt(args[++argnum]);
126         } catch(Exception JavaDoc 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             // Change this line if using another security provider
135
// You can also remove it if your security provider is statically
136
// registered in java.security
137
Security.addProvider((java.security.Provider JavaDoc)Class.forName(
138               "com.sun.net.ssl.internal.ssl.Provider").newInstance());
139           } catch(Exception JavaDoc 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 JavaDoc 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 JavaDoc name, int port, boolean startreg) throws Exception JavaDoc
189   {
190     String JavaDoc host;
191     try {
192       String JavaDoc hostprop="java.rmi.server.hostname";
193       host = System.getProperty(hostprop);
194       //System.out.println(hostprop+"="+host);
195

196     } catch (Exception JavaDoc 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     // assume localhost if no default rmi server
206
if (host == null) host = InetAddress.getLocalHost().getHostName();
207
208     String JavaDoc rmiRef = "//" + host + "/" + name;
209
210     if (port > 0) {
211       rmiRef = new String JavaDoc("//" + host + ":" + port + "/" + name);
212     }
213
214     // RWS REMOVED
215
// RJDriverServer theDriver = new RJDriverServer();
216
RJDriverServer theDriver = buildDriverServer(); // RWS ADDED
217

218     if (!startreg) { // External registry assumed
219
Naming.rebind(rmiRef, theDriver);
220       return;
221     }
222
223     // No external registry, start one
224
if (port <= 0) port = rmiJdbcDefaultPort;
225     Registry registry = LocateRegistry.createRegistry(port);
226     registry.rebind(name, theDriver);
227 // registry.rebind(rmiRef, theDriver);
228
}
229   
230   
231   /**
232    * Build the driver server object. This is a separate method
233    * so extensions can build different driver-server classes.
234    *
235    * @since 8-May-1999
236    */

237   RJDriverServer buildDriverServer() throws java.rmi.RemoteException JavaDoc {
238     return new RJDriverServer(admpasswd_);
239   }
240
241   public static void main(String JavaDoc[] args) {
242     try {
243       Class.forName("org.objectweb.rmijdbc.RJDriverServer_Stub");
244     } catch(ClassNotFoundException JavaDoc 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     // RWS ADDED -- broke up main into two routines
257
initServer(new RJJdbcServer()); // RWS ADDED
258
// RWS ADDED
259
}
260
261
262   static void initServer(RJJdbcServer theServer) { // RWS ADDED
263

264     try {
265
266       // Check for valid listener port
267

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       // Args on the command line are interpreted as jdbc Driver class names
276
// Try to register them in the jdbc DriverManager
277
// For example, to register the driver for the InstantDB database:
278
// java org.objectweb.rmijdbc.RJJdbcServer org.enhydra.instantdb.jdbc.idbDriver
279
// Of course, you can also use the jdbc.drivers System property
280

281       for(int i = 0; i < drivers_.size(); i++)
282       {
283         String JavaDoc drv = (String JavaDoc)drivers_.elementAt(i);
284         try {
285           // Class.forName(...) should be enough, without newInstance() !
286
// On some platforms, it is not...
287
Class.forName(drv).newInstance();
288           printMsg(drv + " registered in DriverManager");
289         } catch(Exception JavaDoc 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       // The following code is useful on some platforms where the ClassLoader
296
// has a strange behaviour (that causes "No suitable driver" exceptions
297
// concerning properly registered drivers !)
298
java.util.Enumeration JavaDoc ed = DriverManager.getDrivers();
299       while(ed.hasMoreElements()) { Driver d = (Driver)ed.nextElement(); }
300
301       // We do load a special RMISecurityManager to relax RMI Security Mgr.
302
// restrictions. This is because we need to remove some of the
303
// restrictions from the default RMISecurityManager, since the RMI
304
// threads will/might need to access database or local files and this
305
// is not allowed by default.
306
//
307
// IMPORTANT NOTE: the specialized RJRMISecurityManager is only
308
// required if your RMI threads might do I/O operations, otherwise
309
// you can use the default RMISecurityManager and uncomment the
310
// following line below to use it instead.
311
//
312
// FIXME: Make it optional (configurable).
313
//
314
// NOTE: If RmiJdbc is ran _embedded_ within an application, then
315
// there might be a possibility that the application has already
316
// installed a Security Manager; so in that case we will let that
317
// one run instead of the RMI Security Manager and we will print
318
// a warning.
319
//
320
// System.setSecurityManager(new RMISecurityManager());
321
//
322
if (System.getSecurityManager() == (SecurityManager JavaDoc) null) {
323         // check if we've asked not to install rmi security manager
324
if (installRMISecurityMgr)
325           System.setSecurityManager(
326            (SecurityManager JavaDoc)new RJRMISecurityManager());
327         else
328           printMsg("No installation of RMI Security Manager...");
329
330       } else {
331         // We print a warning message and decide to continue
332
// (ignore this at it is possible that the application
333
// embedding us had already installed set a security Mgr.
334

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       // RWS REMOVED
342
// RJJdbcServer theServer = new RJJdbcServer();
343

344       printMsg("Binding RmiJdbcServer...");
345       theServer.register("RmiJdbcServer", port_, startreg_);
346       printMsg("RmiJdbcServer bound in rmi registry");
347
348       // If the server has its own registry, make sure the process keeps running
349
if (startreg_) {
350         //System.out.println("server has its own registry");
351
// suspend the main thread to ensure that this process
352
// will continue to run
353
// Thread.currentThread().suspend();
354
Thread JavaDoc tt = new Thread JavaDoc();
355         tt.suspend();
356         }
357
358     }
359   catch(Exception JavaDoc e)
360     {
361       System.err.println("Got Exception: "+e.getMessage());
362       e.printStackTrace();
363       System.exit(1);
364     }
365   }
366
367  /**
368   * Convenient static method to dump timestamped passed-in
369   * message to the standard output.
370   *
371   * If RmiJdbc.verbose system property is set to false, then the messages
372   * are not written out to standard output at all.
373   */

374   public static void printMsg(String JavaDoc msg) {
375     if (verboseMode)
376       System.out.println(new java.util.Date JavaDoc().toString()
377        + ": [RmiJdbc] " + msg);
378   }
379
380 };
381
382
Popular Tags