KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 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 import org.objectweb.rmijdbc.RJRMISecurityManager;
31
32 /**
33  * The main class for RMI/JDBC Server
34  */

35 public class RJJdbcServer
36 {
37 static
38   {
39 // load the Jdbc-Odbc bridge by default
40
// added by Mike Jennings in the summer of 1999
41
try {
42       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
43     } catch(ClassNotFoundException JavaDoc cnfe) {
44       System.out.println("WARNING: Could not load the JDBC/ODBC Bridge");
45     }
46 }
47
48
49   static Vector JavaDoc drivers_ = new Vector JavaDoc();
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   //Default value of the listener port for remote objects
60
public static int rmiJdbcListenerPort = 0;
61
62   //Set communication type defaul id normal sockets
63
public static RMIClientSocketFactory rmiClientSocketFactory = new RJClientSocketFactory();
64   public static RMIServerSocketFactory rmiServerSocketFactory = new RJServerSocketFactory();
65
66   // GC problem on Linux Red Hat 6.0 + JDK v1.1.7B (Blackdown)
67
// Thanks to Per Widerlund from Parallel Systems for the fix.
68
static DriverManager JavaDoc dummy_for_gc;
69
70   /**
71    * Process the command-line parameters
72    */

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

184     } catch (Exception JavaDoc 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     // assume localhost if no default rmi server
194
if (host == null) host = InetAddress.getLocalHost().getHostName();
195
196     String JavaDoc rmiRef = "//" + host + "/" + name;
197
198     if (port > 0) {
199       rmiRef = new String JavaDoc("//" + host + ":" + port + "/" + name);
200     }
201
202     // RWS REMOVED
203
// RJDriverServer theDriver = new RJDriverServer();
204
RJDriverServer theDriver = buildDriverServer(); // RWS ADDED
205

206     if (!startreg) { // External registry assumed
207
Naming.rebind(rmiRef, theDriver);
208       return;
209     }
210
211     // No external registry, start one
212
if (port <= 0) port = rmiJdbcDefaultPort;
213     Registry registry = LocateRegistry.createRegistry(port);
214     registry.rebind(name, theDriver);
215 // registry.rebind(rmiRef, theDriver);
216
}
217   
218   
219   /**
220    * Build the driver server object. This is a separate method
221    * so extensions can build different driver-server classes.
222    *
223    * @since 8-May-1999
224    */

225   RJDriverServer buildDriverServer() throws java.rmi.RemoteException JavaDoc {
226     return new RJDriverServer();
227   }
228
229   public static void main(String JavaDoc[] args) {
230     try {
231       Class.forName("org.objectweb.rmijdbc.RJDriverServer_Stub");
232     } catch(ClassNotFoundException JavaDoc 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     // RWS ADDED -- broke up main into two routines
252
initServer(new RJJdbcServer()); // RWS ADDED
253
// RWS ADDED
254
}
255
256
257   static void initServer(RJJdbcServer theServer) { // RWS ADDED
258

259     try {
260
261       // Check for valid listener port
262

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       // Args on the command line are interpreted as jdbc Driver class names
271
// Try to register them in the jdbc DriverManager
272
// For example, to register the driver for InstantDB free database:
273
// java org.objectweb.rmijdbc.RJJdbcServer jdbc.idbDriver
274
// Of course, you can also use the jdbc.drivers System property
275

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

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

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

369   public static void printMsg(String JavaDoc msg) {
370     if (verboseMode)
371       System.out.println(new java.util.Date JavaDoc().toString()
372        + ": [RmiJdbc] " + msg);
373   }
374
375 };
376
377
Popular Tags