KickJava   Java API By Example, From Geeks To Geeks.

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


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  */

9
10 package org.objectweb.rmijdbc;
11
12 import java.sql.*;
13
14 /**
15  * This is a sample program for RmiJdbc client/server jdbc Driver
16  * RmiJdbc relies on Java RMI for jdbc objects distribution
17  */

18 public class RJAdmin {
19
20   public static void main(String JavaDoc args[]) {
21
22     if(args.length <= 0) {
23       printUsage();
24       return;
25     }
26     String JavaDoc op = args[0];
27
28     String JavaDoc url = "";
29     if(args.length > 1) {
30       url = args[1];
31     }
32
33     String JavaDoc user = null;
34     if(args.length > 2) {
35       user = args[2];
36     }
37
38     String JavaDoc passwd = "";
39     if(args.length > 3) {
40       passwd = args[3];
41     }
42
43     try {
44
45       RJAdmin adm = new RJAdmin();
46
47       // Register RmiJdbc Driver in jdbc DriverManager
48
// The call to newInstance() is necessary on some platforms
49
// (with some java VM implementations)
50
Class.forName("org.objectweb.rmijdbc.Driver").newInstance();
51
52       //System.out.println(url);
53

54       if(op.toUpperCase().equals("SHUTDOWN")) {
55         adm.shutdown(url, user); // the 2nd arg ("user") is the admin password !
56

57       } else if(op.toUpperCase().equals("PING")) {
58         boolean cnx=false, info=false;
59         try {
60           DriverPropertyInfo[] drv_inf = adm.pingDriverInfo(url);
61           info = true;
62           cnx = adm.pingDriverConnect(url, user, passwd);
63         } catch(Exception JavaDoc e) {
64         }
65
66         if(cnx) {
67           System.out.println("[" + url + "] server is alive.");
68         } else {
69           System.out.println("[" + url +
70            "] server is responding, but database connection failed for [" +
71            user + "," + passwd + "]");
72         }
73  
74       } else if(op.toUpperCase().equals("INFO")) {
75
76         System.out.println("Asking for driver info, URL=" + url);
77         DriverPropertyInfo[] drv_inf = adm.pingDriverInfo(url);
78
79         System.out.println("\nJDBC driver info - RmiJdbc server is alive.\n");
80
81         if (drv_inf.length == 0) {
82           System.out.println("No JDBC driver info available for this driver");
83
84         } else {
85
86           for (int i = 0;i<drv_inf.length;i++) {
87             System.out.println("----------------------------------------");
88             System.out.println("DriverPropertyInfo[" + i + "].name = "
89                  + drv_inf[i].name );
90             System.out.println("DriverPropertyInfo[" + i + "].value = "
91                  + drv_inf[i].value );
92             System.out.println("DriverPropertyInfo[" + i + "].description = "
93                  + drv_inf[i].description );
94             System.out.println("DriverPropertyInfo[" + i + "].required = "
95                  + drv_inf[i].required );
96
97             System.out.print("DriverPropertyInfo[" + i + "].choices[] = [");
98             for (int j=0; j < drv_inf[i].choices.length; j++) {
99               if (j > 0)
100                 System.out.print(",");
101               System.out.print(drv_inf[i].choices[j]);
102             }
103             System.out.println("]");
104             System.out.println("----------------------------------------");
105           }
106         }
107
108         System.out.println("Trying database connection, URL=" + url);
109         boolean ret = adm.pingDriverConnect(url, user, passwd);
110         if(ret == true) System.out.println("Connection OK");
111
112       }
113
114     } catch(Exception JavaDoc e) {
115       e.printStackTrace();
116     }
117   }
118
119   static void printUsage() {
120     System.out.println("Usage: RJAdmin op url [user] [passwd]");
121     System.out.println("op is one of PING or INFO");
122     System.out.println("example: RJAdmin PING jdbc:rmi:jdbc:idb=sample.prp");
123   }
124
125   public DriverPropertyInfo[] pingDriverInfo(String JavaDoc driverURL)
126   throws Exception JavaDoc {
127
128     java.sql.Driver JavaDoc drv = DriverManager.getDriver(driverURL);
129     java.util.Properties JavaDoc p = new java.util.Properties JavaDoc();
130     return drv.getPropertyInfo(driverURL, p);
131   }
132
133   public boolean pingDriverConnect(String JavaDoc driverURL, String JavaDoc usr, String JavaDoc passwd)
134   throws Exception JavaDoc {
135     Connection c;
136     if(usr != null) c = DriverManager.getConnection(driverURL, usr, passwd);
137     else c = DriverManager.getConnection(driverURL);
138     c.close();
139     return true;
140   }
141
142   public void shutdown(String JavaDoc url, String JavaDoc passwd)
143   throws Exception JavaDoc {
144     org.objectweb.rmijdbc.Driver drv
145      = (org.objectweb.rmijdbc.Driver)DriverManager.getDriver(url);
146     drv.shutdown(url, passwd);
147   }
148
149 };
150
151
Popular Tags