KickJava   Java API By Example, From Geeks To Geeks.

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


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.rmi.*;
15 import java.sql.*;
16 import java.rmi.server.Unreferenced JavaDoc;
17
18 /**
19  * <P>The Java SQL framework allows for multiple database drivers.
20  *
21  * <P>Each driver should supply a class that implements
22  * the Driver interface.
23  *
24  * <P>The DriverManager will try to load as many drivers as it can
25  * find and then for any given connection request, it will ask each
26  * driver in turn to try to connect to the target URL.
27  *
28  * <P>It is strongly recommended that each Driver class should be
29  * small and standalone so that the Driver class can be loaded and
30  * queried without bringing in vast quantities of supporting code.
31  *
32  * <P>When a Driver class is loaded, it should create an instance of
33  * itself and register it with the DriverManager. This means that a
34  * user can load and register a driver by doing
35  * Class.forName("foo.bah.Driver").
36  *
37  * @see DriverManager
38  * @see Connection
39  */

40 public class RJDriverServer
41 extends java.rmi.server.UnicastRemoteObject JavaDoc
42 implements RJDriverInterface, Unreferenced JavaDoc
43 {
44
45   private String JavaDoc admpasswd_ = null;
46
47   public RJDriverServer() throws RemoteException {
48     this(null);
49   }
50
51   public RJDriverServer(String JavaDoc admpasswd) throws RemoteException {
52     super(RJJdbcServer.rmiJdbcListenerPort,
53      RJJdbcServer.rmiClientSocketFactory, RJJdbcServer.rmiServerSocketFactory);
54     //System.out.println("constructing a RJDriverServer");
55
admpasswd_ = admpasswd;
56   }
57
58   public void unreferenced() {
59     //System.out.println("RJDriverServer.unreferenced() -> garbage collecting");
60
Runtime.getRuntime().gc();
61   }
62
63   /**
64    * Administrative methods
65    */

66
67   public void shutdown(String JavaDoc pwd) throws RemoteException {
68     if(admpasswd_ == null)
69             throw new RemoteException(
70        "No administrative password defined for this server: shutdown is not allowed");
71
72     if(admpasswd_.equals(pwd)) {
73       System.out.println("Shutting down RmiJdbc server, good bye!");
74       System.exit(0);
75     } else {
76       throw new RemoteException("Wrong password: shutdown is not allowed");
77     }
78   }
79
80   /**
81    * Try to make a database connection to the given URL.
82    * The driver should return "null" if it realizes it is the wrong kind
83    * of driver to connect to the given URL. This will be common, as when
84    * the JDBC driver manager is asked to connect to a given URL it passes
85    * the URL to each loaded driver in turn.
86    *
87    * <P>The driver should raise a java.rmi.RemoteException if it is the right
88    * driver to connect to the given URL, but has trouble connecting to
89    * the database.
90    *
91    * <P>The java.util.Properties argument can be used to passed arbitrary
92    * string tag/value pairs as connection arguments.
93    * Normally at least "user" and "password" properties should be
94    * included in the Properties.
95    *
96    * @param url The URL of the database to connect to
97    *
98    * @param info a list of arbitrary string tag/value pairs as
99    * connection arguments; normally at least a "user" and
100    * "password" property should be included
101    *
102    * @return a Connection to the URL
103    */

104   public RJConnectionInterface connect(String JavaDoc url, java.util.Properties JavaDoc info)
105   throws RemoteException, SQLException {
106     java.sql.Driver JavaDoc jdbcDriver;
107     if ((jdbcDriver = DriverManager.getDriver(url)) == null) {
108       throw new java.rmi.RemoteException JavaDoc(
109        "RJDriverServer::connect: No suitable Driver");
110     }
111     //System.out.println("RJDriverServer.connect("+url+")");
112
Connection c = jdbcDriver.connect(url, info);
113     if(c == null) {
114       throw new java.rmi.RemoteException JavaDoc(
115         "RJDriverServer::connect: Underlying driver couldn\'t establish the connection: connect() returned null, check the configuration");
116     }
117     return new RJConnectionServer(c);
118   }
119
120   /**
121    * Returns true if the driver thinks that it can open a connection
122    * to the given URL. Typically drivers will return true if they
123    * understand the subprotocol specified in the URL and false if
124    * they don't.
125    *
126    * @param url The URL of the database.
127    * @return True if this driver can connect to the given URL.
128    */

129   public boolean acceptsURL(String JavaDoc url) throws RemoteException, SQLException {
130     return(DriverManager.getDriver(url) != null);
131   }
132
133     /**
134      * <p>The getPropertyInfo method is intended to allow a generic GUI tool to
135      * discover what properties it should prompt a human for in order to get
136      * enough information to connect to a database. Note that depending on
137      * the values the human has supplied so far, additional values may become
138      * necessary, so it may be necessary to iterate though several calls
139      * to getPropertyInfo.
140      *
141      * @param url The URL of the database to connect to.
142      * @param info A proposed list of tag/value pairs that will be sent on
143      * connect open.
144      * @return An array of DriverPropertyInfo objects describing possible
145      * properties. This array may be an empty array if no properties
146      * are required.
147      */

148     public RJDriverPropertyInfo[] getPropertyInfo(String JavaDoc url,
149     java.util.Properties JavaDoc info) throws RemoteException, SQLException {
150       java.sql.Driver JavaDoc jdbcDriver;
151       if((jdbcDriver = DriverManager.getDriver(url)) == null) {
152         throw new java.rmi.RemoteException JavaDoc(
153           "RJDriverServer::getPropertyInfo: No suitable Driver");
154       }
155
156       // DriverPropertyInfo is not serializable:
157
// the reason for RJDriverPropertyInfo !
158
DriverPropertyInfo infos[] = jdbcDriver.getPropertyInfo(url, info);
159       if(infos == null) return null;
160       RJDriverPropertyInfo dpis[] = new RJDriverPropertyInfo[infos.length];
161       for(int i=0; i<infos.length; i++) {
162         if(infos[i] == null) dpis[i] = null;
163         dpis[i] = new RJDriverPropertyInfo(infos[i]);
164       }
165       return dpis;
166     }
167
168
169     /**
170      * Get the driver's major version number. Initially this should be 1.
171      */

172     public int getMajorVersion() throws RemoteException, SQLException {
173       return 0;
174     }
175
176     /**
177      * Get the driver's minor version number. Initially this should be 0.
178      */

179     public int getMinorVersion() throws RemoteException, SQLException {
180       return 1;
181     }
182
183
184     /**
185      * Report whether the Driver is a genuine JDBC COMPLIANT (tm) driver.
186      * A driver may only report "true" here if it passes the JDBC compliance
187      * tests, otherwise it is required to return false.
188      *
189      * JDBC compliance requires full support for the JDBC API and full support
190      * for SQL 92 Entry Level. It is expected that JDBC compliant drivers will
191      * be available for all the major commercial databases.
192      *
193      * This method is not intended to encourage the development of non-JDBC
194      * compliant drivers, but is a recognition of the fact that some vendors
195      * are interested in using the JDBC API and framework for lightweight
196      * databases that do not support full database functionality, or for
197      * special databases such as document information retrieval where a SQL
198      * implementation may not be feasible.
199      */

200     public boolean jdbcCompliant() {
201       return true;
202     }
203
204 };
205
206
Popular Tags