KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > commands > DatabaseReporter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.cli.commands;
25 import java.sql.*;
26 import java.io.File JavaDoc;
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 /**
32  * This class will created a dummy database called testconnectivity.
33  * and handshake with the database to get the Client Driver info.
34  * @author <a HREF="mailto:jane.young@sun.com">Jane Young</a>
35  * @version $Revision: 1.1 $
36  */

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 JavaDoc DERBY_CLIENT_DRIVER_CLASS_NAME = "org.apache.derby.jdbc.ClientDriver";
41     private static final String JavaDoc URL = "jdbc:derby://localhost:";
42     private static final String JavaDoc DB_NAME = "/testconnectivity;create=true";
43     private static final String JavaDoc USER = "APP";
44     private static final String JavaDoc PASS = "APP";
45     private static final String JavaDoc DB_PORT = "1527";
46     private static final String JavaDoc DB_HOME = System.getProperty("user.dir");
47     final private String JavaDoc dcn, url, user, password, dbhome, dbport;
48     final private Connection conn;
49
50         //constructor
51
public DatabaseReporter( final String JavaDoc dbhome, final String JavaDoc dbport, final String JavaDoc dcn,
52                              final String JavaDoc url, final String JavaDoc user, final String JavaDoc password) throws Exception JavaDoc {
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         /**
64          * display the database info.
65          */

66     private void display() throws Exception JavaDoc
67     {
68         logger.printMessage(getDatabaseInfoMsg());
69         logger.printMessage(getDatabaseDriverNameMsg());
70         logger.printMessage(getDatabaseDriverVersionMsg());
71         logger.printMessage(getJDBCSpecificationMsg());
72     }
73
74         /**
75          * get database info msg.
76          */

77     private String JavaDoc getDatabaseInfoMsg()
78     {
79         return (lsm.getString("database.info.msg"));
80     }
81     
82         /**
83          * get database driver name.
84          */

85     private String JavaDoc getDatabaseDriverNameMsg() throws SQLException
86     {
87         final String JavaDoc sDriverName = conn.getMetaData().getDriverName();
88         return ( lsm.getString("database.driver.name.msg", sDriverName));
89     }
90
91         /**
92          * get database driver version.
93          */

94     private String JavaDoc getDatabaseDriverVersionMsg() throws SQLException
95     {
96         final String JavaDoc sDriverVersion = conn.getMetaData().getDriverVersion();
97         return ( lsm.getString("database.driver.version.msg", sDriverVersion));
98     }
99
100         /**
101          * get jdbc major and minor version.
102          */

103     private String JavaDoc getJDBCSpecificationMsg() throws SQLException
104     {
105         final int iJDBCMajorVersion = conn.getMetaData().getJDBCMajorVersion();
106         final int iJDBCMinorVersion = conn.getMetaData().getJDBCMinorVersion();
107         final String JavaDoc sJDBCSpec = iJDBCMajorVersion + "." + iJDBCMinorVersion;
108         return ( lsm.getString("jdbc.version.msg", sJDBCSpec));
109     }
110
111         /**
112          * remove database file
113          */

114     private void removeDatabaseFile()
115     {
116         File JavaDoc f = new File JavaDoc(dbhome, "testconnectivity");
117         FileUtils.whack(f);
118     }
119     
120     
121
122     public static void main(final String JavaDoc[] args) throws Exception JavaDoc {
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 JavaDoc e) {
138             logger.printMessage(lsm.getString("UnableToConnectToDatabase"));
139             System.exit(1);
140         }
141         
142     }
143 }
144
Popular Tags