1 2 9 10 package org.objectweb.rmijdbc; 11 12 import java.sql.*; 13 14 18 public class RJAdmin { 19 20 public static void main(String args[]) { 21 22 if(args.length <= 0) { 23 printUsage(); 24 return; 25 } 26 String op = args[0]; 27 28 String url = ""; 29 if(args.length > 1) { 30 url = args[1]; 31 } 32 33 String user = null; 34 if(args.length > 2) { 35 user = args[2]; 36 } 37 38 String passwd = ""; 39 if(args.length > 3) { 40 passwd = args[3]; 41 } 42 43 try { 44 45 RJAdmin adm = new RJAdmin(); 46 47 Class.forName("org.objectweb.rmijdbc.Driver").newInstance(); 51 52 54 if(op.toUpperCase().equals("SHUTDOWN")) { 55 adm.shutdown(url, user); 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 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 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 driverURL) 126 throws Exception { 127 128 java.sql.Driver drv = DriverManager.getDriver(driverURL); 129 java.util.Properties p = new java.util.Properties (); 130 return drv.getPropertyInfo(driverURL, p); 131 } 132 133 public boolean pingDriverConnect(String driverURL, String usr, String passwd) 134 throws Exception { 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 url, String passwd) 143 throws Exception { 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 |