1 23 24 package com.sun.enterprise.cli.commands; 25 import java.sql.*; 26 import java.io.File ; 27 import com.sun.enterprise.util.io.FileUtils; 28 import com.sun.enterprise.util.i18n.StringManager; 29 import com.sun.enterprise.cli.framework.CLILogger; 30 31 37 public class DatabaseReporter { 38 private static final StringManager lsm = StringManager.getManager(DatabaseReporter.class); 39 private static final CLILogger logger = CLILogger.getInstance(); 40 private static final String DERBY_CLIENT_DRIVER_CLASS_NAME = "org.apache.derby.jdbc.ClientDriver"; 41 private static final String URL = "jdbc:derby://localhost:"; 42 private static final String DB_NAME = "/testconnectivity;create=true"; 43 private static final String USER = "APP"; 44 private static final String PASS = "APP"; 45 private static final String DB_PORT = "1527"; 46 private static final String DB_HOME = System.getProperty("user.dir"); 47 final private String dcn, url, user, password, dbhome, dbport; 48 final private Connection conn; 49 50 public DatabaseReporter( final String dbhome, final String dbport, final String dcn, 52 final String url, final String user, final String password) throws Exception { 53 this.dcn = dcn; 54 this.user = user; 55 this.password = password; 56 this.dbhome = dbhome; 57 this.dbport = dbport; 58 this.url = url+dbport+DB_NAME; 59 Class.forName(dcn); 60 conn = DriverManager.getConnection(this.url, this.user, this.password); 61 } 62 63 66 private void display() throws Exception 67 { 68 logger.printMessage(getDatabaseInfoMsg()); 69 logger.printMessage(getDatabaseDriverNameMsg()); 70 logger.printMessage(getDatabaseDriverVersionMsg()); 71 logger.printMessage(getJDBCSpecificationMsg()); 72 } 73 74 77 private String getDatabaseInfoMsg() 78 { 79 return (lsm.getString("database.info.msg")); 80 } 81 82 85 private String getDatabaseDriverNameMsg() throws SQLException 86 { 87 final String sDriverName = conn.getMetaData().getDriverName(); 88 return ( lsm.getString("database.driver.name.msg", sDriverName)); 89 } 90 91 94 private String getDatabaseDriverVersionMsg() throws SQLException 95 { 96 final String sDriverVersion = conn.getMetaData().getDriverVersion(); 97 return ( lsm.getString("database.driver.version.msg", sDriverVersion)); 98 } 99 100 103 private String getJDBCSpecificationMsg() throws SQLException 104 { 105 final int iJDBCMajorVersion = conn.getMetaData().getJDBCMajorVersion(); 106 final int iJDBCMinorVersion = conn.getMetaData().getJDBCMinorVersion(); 107 final String sJDBCSpec = iJDBCMajorVersion + "." + iJDBCMinorVersion; 108 return ( lsm.getString("jdbc.version.msg", sJDBCSpec)); 109 } 110 111 114 private void removeDatabaseFile() 115 { 116 File f = new File (dbhome, "testconnectivity"); 117 FileUtils.whack(f); 118 } 119 120 121 122 public static void main(final String [] args) throws Exception { 123 DatabaseReporter dmt = null; 124 if (args == null || (args.length < 6 && args.length>2) ) { 125 dmt = new DatabaseReporter(DB_HOME, DB_PORT, DERBY_CLIENT_DRIVER_CLASS_NAME, URL, USER, PASS); 126 } 127 else if (args.length == 2) { 128 dmt = new DatabaseReporter(args[0], args[1], DERBY_CLIENT_DRIVER_CLASS_NAME, URL, USER, PASS); 129 } 130 else { 131 dmt = new DatabaseReporter(args[0], args[1], args[2], args[3], args[4], args[5]); 132 } 133 try { 134 dmt.display(); 135 dmt.removeDatabaseFile(); 136 } 137 catch (Exception e) { 138 logger.printMessage(lsm.getString("UnableToConnectToDatabase")); 139 System.exit(1); 140 } 141 142 } 143 } 144 | Popular Tags |