KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 import java.io.File JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.io.PrintStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import com.sun.enterprise.util.i18n.StringManager;
32 import com.sun.enterprise.cli.framework.CommandException;
33
34 /**
35  * This class uses Java reflection to invoke Derby
36  * NetworkServerControl class.
37  * This class is used to start/stop/ping derby database.
38  * The reason for creating this class instead of directly
39  * invoking NetworkServerControl from the StartDatabaseCommand
40  * class is so that a separate JVM is launched when starting the
41  * database and the control is return to CLI.
42  * @author <a HREF="mailto:jane.young@sun.com">Jane Young</a>
43  * @version $Revision: 1.10.2.1 $
44  */

45 public final class DerbyControl
46 {
47     final public static String JavaDoc DB_LOG_FILENAME = "derby.log";
48     final private String JavaDoc derbyCommand;
49     final private String JavaDoc derbyHost;
50     final private String JavaDoc derbyPort;
51     final private String JavaDoc derbyHome;
52     final private boolean redirect;
53
54         //constructor
55
public DerbyControl(final String JavaDoc dc, final String JavaDoc dht, final String JavaDoc dp,
56                         final String JavaDoc redirect, final String JavaDoc dhe)
57     {
58         this.derbyCommand = dc;
59         this.derbyHost = dht;
60         this.derbyPort = dp;
61         this.derbyHome = dhe;
62         this.redirect = new Boolean JavaDoc(redirect).booleanValue();
63         if (this.redirect) {
64
65             try {
66                 String JavaDoc dbLog = "";
67                 if (this.derbyHome == null) {
68                     // if derbyHome is null then redirect the output to a temporary file
69
// which gets deleted after the jvm exists.
70
dbLog = createTempLogFile();
71                 }
72             else {
73                 dbLog = createDBLog(this.derbyHome);
74             }
75             
76                 //redirect stdout and stderr to a file
77
PrintStream JavaDoc printStream = new PrintStream JavaDoc(new FileOutputStream JavaDoc(dbLog, true), true);
78                 System.setOut(printStream);
79                 System.setErr(printStream);
80             }
81             catch (Throwable JavaDoc t) {
82                 t.printStackTrace();
83                 //exit with an error code of 2
84
System.exit(2);
85             }
86         }
87         //do not set derby.system.home if derbyHome is empty
88
if (this.derbyHome!=null && this.derbyHome.length()>0) {
89             System.setProperty("derby.system.home", this.derbyHome);
90         }
91         //set the property to not overwrite log file
92
System.setProperty("derby.infolog.append", "true");
93     }
94     
95         //constructor
96
public DerbyControl(final String JavaDoc dc, final String JavaDoc dht, final String JavaDoc dp)
97     {
98         this(dc,dht,dp,"true", null);
99     }
100     
101         //constructor
102
public DerbyControl(final String JavaDoc dc, final String JavaDoc dht, final String JavaDoc dp, final String JavaDoc redirect)
103     {
104         this(dc,dht,dp,redirect,null);
105     }
106     
107         /**
108          * This methos invokes the Derby's NetworkServerControl to start/stop/ping
109          * the database.
110          */

111     private void invokeNetworkServerControl()
112     {
113         try {
114             Class JavaDoc networkServer = Class.forName("org.apache.derby.drda.NetworkServerControl");
115             Method JavaDoc networkServerMethod = networkServer.getDeclaredMethod("main",
116                                                        new Class JavaDoc[]{String JavaDoc[].class});
117             Object JavaDoc [] paramObj = new Object JavaDoc[]{new String JavaDoc[]{derbyCommand, "-h", derbyHost, "-p", derbyPort}};
118            
119             networkServerMethod.invoke(networkServer, paramObj);
120         }
121         catch (Throwable JavaDoc t) {
122             t.printStackTrace();
123             System.exit(2);
124         }
125     }
126
127
128         /**
129          * create a db.log file that stdout/stderr will redirect to.
130          * dbhome is the derby.system.home directory where derb.log
131          * gets created. if user specified --dbhome options, derby.log
132          * will be created there.
133          **/

134     private String JavaDoc createDBLog(final String JavaDoc dbHome) throws Exception JavaDoc
135     {
136         //dbHome must exist and have write permission
137
final File JavaDoc fDBHome = new File JavaDoc(dbHome);
138         String JavaDoc dbLogFileName = "";
139
140         final StringManager lsm = StringManager.getManager(DerbyControl.class);
141         if (fDBHome.isDirectory() && fDBHome.canWrite()) {
142             final File JavaDoc fDBLog = new File JavaDoc(dbHome, DB_LOG_FILENAME);
143             dbLogFileName = fDBLog.toString();
144
145             //if the file exists, check if it is writeable
146
if (fDBLog.exists() && !fDBLog.canWrite()) {
147             System.out.println(lsm.getString("UnableToAccessDatabaseLog", dbLogFileName));
148             System.out.println(lsm.getString("ContinueStartingDatabase"));
149             //if exist but not able to write then create a temporary
150
//log file and persist on starting the database
151
dbLogFileName = createTempLogFile();
152             }
153             else if (!fDBLog.exists()) {
154                 //create log file
155
fDBLog.createNewFile();
156             }
157         }
158         else {
159             System.out.println(lsm.getString("InvalidDirectory", dbHome));
160             System.out.println(lsm.getString("ContinueStartingDatabase"));
161             //if directory does not exist then create a temporary log file
162
//and persist on starting the database
163
dbLogFileName = createTempLogFile();
164         }
165         return dbLogFileName;
166     }
167
168    /**
169       creates a temporary log file.
170    */

171     private String JavaDoc createTempLogFile() throws CommandException
172     {
173         String JavaDoc tempFileName = "";
174         try {
175             final File JavaDoc fTemp = File.createTempFile("foo", null);
176             fTemp.deleteOnExit();
177             tempFileName = fTemp.toString();
178         }
179         catch (IOException JavaDoc ioe) {
180             final StringManager lsm = StringManager.getManager(DerbyControl.class);
181             throw new CommandException(lsm.getString("UnableToAccessDatabaseLog", tempFileName));
182         }
183         return tempFileName;
184     }
185     
186     public static void main(String JavaDoc[] args) {
187         
188         if (args.length<3){
189             System.out.println("paramters not specified.");
190             System.out.println("DerbyControl <derby command> <derby host> <derby port> <derby home> <redirect output>");
191             System.exit(1);
192         }
193         
194         DerbyControl derbyControl = null;
195         if (args.length == 3)
196             derbyControl = new DerbyControl(args[0], args[1], args[2]);
197         else if (args.length == 4 )
198             derbyControl = new DerbyControl(args[0], args[1], args[2], args[3]);
199         else if (args.length > 4)
200             derbyControl = new DerbyControl(args[0], args[1], args[2], args[3], args[4]);
201         if (derbyControl != null)
202             derbyControl.invokeNetworkServerControl();
203     }
204 }
205
206
207     
208
Popular Tags