KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.enterprise.cli.framework.CommandException;
27 import com.sun.enterprise.cli.framework.CommandValidationException;
28 import com.sun.enterprise.cli.framework.CLILogger;
29 import java.lang.IllegalThreadStateException JavaDoc;
30 import com.sun.enterprise.util.SystemPropertyConstants;
31 import com.sun.enterprise.util.OS;
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34
35
36 /**
37  * start-database command
38  * This command class will invoke DerbyControl to first ping
39  * if the database is running. If not then it will start
40  * the database. If the database is already started, then
41  * a message will be displayed to the user.
42  * @author <a HREF="mailto:jane.young@sun.com">Jane Young</a>
43  * @version $Revision: 1.11.2.1 $
44  */

45 public final class StartDatabaseCommand extends DatabaseCommand
46 {
47     private final static String JavaDoc DB_HOME = "dbhome";
48     private String JavaDoc dbHome;
49
50     /**
51      * An method that validates the options
52      * on the specification in the xml properties file
53      * This method verifies for the correctness of number of
54      * operands and if all the required options are supplied by the client.
55      * @return boolean returns true if success else returns false
56      */

57     public boolean validateOptions() throws CommandValidationException
58     {
59         return super.validateOptions();
60     }
61
62
63     /**
64      * defines the command to start the derby database
65      * Note that when using Darwin (Mac), the property,
66      * "-Dderby.storage.fileSyncTransactionLog=True" is defined.
67      */

68     public String JavaDoc[] startDatabaseCmd() throws Exception JavaDoc
69     {
70         if (OS.isDarwin()) {
71             return new String JavaDoc [] {
72                 sJavaHome+File.separator+"bin"+File.separator+"java",
73                 "-Djava.library.path="+sInstallRoot+File.separator+"lib",
74                 "-Dderby.storage.fileSyncTransactionLog=True",
75                 "-cp",
76                 sClasspath + File.pathSeparator + sDatabaseClasspath,
77                 "com.sun.enterprise.cli.commands.DerbyControl",
78                 "start",
79                 dbHost, dbPort, "true", dbHome
80             };
81         }
82         else {
83             return new String JavaDoc [] {
84                 sJavaHome+File.separator+"bin"+File.separator+"java",
85                 "-Djava.library.path="+sInstallRoot+File.separator+"lib",
86                 "-cp",
87                 sClasspath + File.pathSeparator + sDatabaseClasspath,
88                 "com.sun.enterprise.cli.commands.DerbyControl",
89                 "start",
90                 dbHost, dbPort, "true", dbHome
91             };
92         }
93     }
94
95
96     /**
97      * defines the command to print out the database sysinfo
98      * Note that when using Darwin (Mac), the property,
99      * "-Dderby.storage.fileSyncTransactionLog=True" is defined.
100      */

101     public String JavaDoc[] sysinfoCmd() throws Exception JavaDoc
102     {
103         if (OS.isDarwin()) {
104             return new String JavaDoc [] {
105                 sJavaHome+File.separator+"bin"+File.separator+"java",
106                 "-Djava.library.path="+sInstallRoot+File.separator+"lib",
107                 "-Dderby.storage.fileSyncTransactionLog=True",
108                 "-cp",
109                 sClasspath + File.pathSeparator + sDatabaseClasspath,
110                 "com.sun.enterprise.cli.commands.DerbyControl",
111                 "sysinfo",
112                 dbHost, dbPort, "false"
113             };
114         }
115         else {
116             return new String JavaDoc [] {
117                 sJavaHome+File.separator+"bin"+File.separator+"java",
118                 "-Djava.library.path="+sInstallRoot+File.separator+"lib",
119                 "-cp",
120                 sClasspath + File.pathSeparator + sDatabaseClasspath,
121                 "com.sun.enterprise.cli.commands.DerbyControl",
122                 "sysinfo",
123                 dbHost, dbPort, "false"
124             };
125         }
126     }
127
128
129     /**
130      * method that execute the command
131      * @throws CommandException
132     */

133     public void runCommand() throws CommandException, CommandValidationException
134     {
135         validateOptions();
136         final CLIProcessExecutor cpe = new CLIProcessExecutor();
137         String JavaDoc dbLog = "";
138         try {
139             prepareProcessExecutor();
140
141             //if dbhome is not specified, then it's the current directory.
142
dbHome = getOption(DB_HOME)==null?System.getProperty("user.dir"):
143                                       getOption(DB_HOME);
144             dbLog = dbHome + File.separator + DerbyControl.DB_LOG_FILENAME;
145
146             CLILogger.getInstance().printDebugMessage("Ping Database");
147             cpe.execute(pingDatabaseCmd(true), true);
148             //if ping is unsuccesfull then database is not up and running
149
if (cpe.exitValue() > 0) {
150                 CLILogger.getInstance().printDebugMessage("Start Database");
151                 cpe.execute(startDatabaseCmd(), false);
152                 if (cpe.exitValue() != 0) {
153                     throw new CommandException(getLocalizedString("UnableToStartDatabase",
154                                                      new Object JavaDoc[]{dbLog}));
155                 }
156             }
157             else if (cpe.exitValue() < 0) {
158                     // Something terribly wrong!
159
throw new CommandException(getLocalizedString("CommandUnSuccessful",
160                                                               new Object JavaDoc[] {name} ));
161             }
162             else {
163                     //database already started
164
CLILogger.getInstance().printMessage(getLocalizedString(
165                                                      "StartDatabaseStatus",
166                                                      new Object JavaDoc[]{dbHost, dbPort}));
167                 CLILogger.getInstance().printDetailMessage(getLocalizedString(
168                                                            "CommandSuccessful",
169                                                            new Object JavaDoc[] {name}));
170             }
171         }
172         catch (IllegalThreadStateException JavaDoc ite) {
173             // IllegalThreadStateException is thrown if the
174
// process has not yet teminated and is still running.
175
// see http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Process.html#exitValue()
176
// This is good since that means the database is up and running.
177

178             if (!getBooleanOption(TERSE)) {
179                 try {
180                     CLILogger.getInstance().printDetailMessage(
181                                             getLocalizedString("database.info.msg",
182                                             new Object JavaDoc[]{dbHost, dbPort}));
183                     //try getting sysinfo
184
CLILogger.getInstance().printDebugMessage("Database SysInfo");
185                     new CLIProcessExecutor().execute(sysinfoCmd(), true);
186                 }
187                 catch (Exception JavaDoc e) {
188                     throw new CommandException(getLocalizedString("CommandUnSuccessful",
189                                                            new Object JavaDoc[] {name}), e);
190                 }
191             }
192             CLILogger.getInstance().printMessage(getLocalizedString("DatabaseStartMsg"));
193             if ((new File JavaDoc(dbLog)).canWrite()) {
194                 CLILogger.getInstance().printMessage(getLocalizedString("LogRedirectedTo",
195                                                      new Object JavaDoc[]{dbLog}));
196             }
197
198             CLILogger.getInstance().printDetailMessage(getLocalizedString(
199                                                        "CommandSuccessful",
200                                                        new Object JavaDoc[] {name}));
201         }
202         catch (Exception JavaDoc e) {
203             displayExceptionMessage(e);
204         }
205     }
206
207 }
208
Popular Tags