KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > common > Commands


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 /*
25  * Commands.java
26  *
27  * Created on June 7, 2004, 2:41 PM
28  */

29
30 package com.sun.enterprise.tools.upgrade.common;
31
32 import java.io.*;
33 import com.sun.enterprise.cli.framework.CLIMain;
34 import com.sun.enterprise.cli.framework.InputsAndOutputs;
35 import com.sun.enterprise.cli.framework.CommandException;
36 import com.sun.enterprise.util.i18n.StringManager;
37
38 /**
39  *
40  * @author hans
41  */

42 public class Commands {
43     private static StringManager stringManager = StringManager.getManager("com.sun.enterprise.tools.upgrade.common");
44     
45     /** Creates a new instance of Commands */
46     public Commands() {
47     }
48     
49     public static boolean deploy(String JavaDoc modulePath, CommonInfoModel commonInfo) {
50         String JavaDoc currentDomain = commonInfo.getCurrentDomain();
51         String JavaDoc adminPort = DomainsProcessor.getTargetDomainPort(currentDomain, commonInfo);
52         String JavaDoc adminSecurity = DomainsProcessor.getTargetDomainSecurity(currentDomain, commonInfo);
53         String JavaDoc edition = commonInfo.getTargetEdition();
54         String JavaDoc [] deploy = {
55                 "deploy", "--user", commonInfo.getAdminUserName(),
56                 //"--password", commonInfo.getAdminPassword(),
57
"--passwordfile ", "\"" + commonInfo.getPasswordFile()+ "\"",
58                 "--port",adminPort,
59                 "--secure=" + adminSecurity,
60                 "\"" + modulePath + "\""
61             };
62         if(!commonInfo.getTargetEdition().equals(UpgradeConstants.EDITION_PE)){
63             String JavaDoc targetName = commonInfo.getCurrentCluster();
64             if(targetName == null){
65                 targetName = commonInfo.getCurrentSourceInstance();
66             }
67             if((targetName != null) && (!("".equals(targetName)))){
68                 deploy = new String JavaDoc[]{
69                     "deploy", "--user", commonInfo.getAdminUserName(),
70                     //"--password", commonInfo.getAdminPassword(),
71
"--passwordfile ", "\"" + commonInfo.getPasswordFile()+ "\"",
72                     "--port",adminPort,
73                     "--secure=" + adminSecurity,
74                     "--target", targetName,
75                     "\"" + modulePath + "\""
76                 };
77             }
78         }
79         try {
80             return executeCommand(deploy);
81         } catch (CommandException ce) {
82             Throwable JavaDoc t = ce.getCause();
83             CommonInfoModel.getDefaultLogger().warning(stringManager.getString("commands.generalExceptionMsg") + " " + (t==null?ce.getMessage():t.getMessage()));
84         }
85         CommonInfoModel.getDefaultLogger().warning(stringManager.getString("commands.errorDeployingMsg") + modulePath);
86         return false;
87     }
88     
89     public static boolean startDomain(String JavaDoc domainName, CommonInfoModel commonInfo) {
90         String JavaDoc command[] = {
91             //"start-domain", "--user", commonInfo.getAdminUserName(), "--password", commonInfo.getAdminPassword(), "--passwordfile ", "\"" + commonInfo.getPasswordFile() +"\"", domainName
92
"start-domain", "--domaindir", "\"" + commonInfo.getTargetDomainRoot() +"\"", "--user", commonInfo.getAdminUserName(), "--passwordfile ", "\"" + commonInfo.getPasswordFile() +"\"", domainName
93         };
94         
95         try {
96             boolean b = executeCommand(command);
97             return b;
98         } catch (CommandException ce) {
99             Throwable JavaDoc t = ce.getCause();
100             CommonInfoModel.getDefaultLogger().severe(stringManager.getString("commands.generalExceptionMsg") + ce.getMessage());
101             if (t != null) {
102                 String JavaDoc message = t.getMessage();
103                 if(message != null){
104                     CommonInfoModel.getDefaultLogger().severe(stringManager.getString("commands.generalExceptionMsg") + message);
105                     if ( message.indexOf(stringManager.getString("commands.DomainRunningFragment")) != -1 ||
106                         (stringManager.getString("commands.DomainRunningFragment").equalsIgnoreCase("No local string defined") &&
107                         message.indexOf("running") != -1 )) {
108                         CommonInfoModel.getDefaultLogger().severe(stringManager.getString("commands.DomainRunningMsg", domainName));
109                     }
110                 }
111             }
112         }
113         return false;
114     }
115     
116     public static boolean stopDomain(String JavaDoc domainName, CommonInfoModel commonInfo) {
117         String JavaDoc command[] = {
118             "stop-domain", "--domaindir", "\"" + commonInfo.getTargetDomainRoot() +"\"", domainName
119         };
120         try {
121             boolean b = executeCommand(command);
122             return b;
123         } catch (CommandException ce) {
124             Throwable JavaDoc t = ce.getCause();
125             if (t != null && t.getMessage().indexOf("is not running") != -1) {
126                 return true;
127             }
128             CommonInfoModel.getDefaultLogger().warning(stringManager.getString("commands.generalExceptionMsg") + ce.getMessage());
129         }
130         return false;
131     }
132     
133     public static boolean executeCommand(String JavaDoc commandStrings[]) throws CommandException {
134         try {
135             StringBuffer JavaDoc commandOneString = new StringBuffer JavaDoc();
136             for(int i = 0; i < commandStrings.length; i++) {
137                 commandOneString.append(commandStrings[i]).append(" ");
138             }
139             InputsAndOutputs io = InputsAndOutputs.getInstance();
140             PipedOutputStream pos = new PipedOutputStream();
141             io.setErrorOutput(pos);
142             io.setUserOutput(pos);
143             CommandOutputReader cor = new CommandOutputReader(pos);
144             ((Thread JavaDoc)cor).start();
145             CommonInfoModel.getDefaultLogger().info(stringManager.getString("commands.executingCommandMsg") + commandOneString);
146             CLIMain.invokeCLI(commandOneString.toString(), io);
147             pos.flush();
148             return true;
149         }
150         catch(CommandException ce) {
151             throw ce;
152         }
153         catch(Exception JavaDoc e) {
154             Throwable JavaDoc t = e.getCause();
155             CommonInfoModel.getDefaultLogger().warning(stringManager.getString("commands.generalExceptionMsg") + (t==null?e.getMessage():t.getMessage()));
156         }
157         return false;
158     }
159     
160     
161     static class CommandOutputReader extends Thread JavaDoc {
162         PipedInputStream pis = new PipedInputStream();
163         public CommandOutputReader(PipedOutputStream pout) throws IOException {
164             pis.connect(pout);
165         }
166         
167         public void run() {
168             //Start CR 6376140
169
BufferedReader buffReader = new BufferedReader(new InputStreamReader(pis));
170             //DataInputStream dis = new DataInputStream(pis);
171
//end CR 6376140
172
try {
173                 //Start CR 6376140
174
String JavaDoc s = null;
175                 while((s=buffReader.readLine()) != null) {
176                 //while ( dis.available() > -1 ) {
177
CommonInfoModel.getDefaultLogger().info(s);
178                 }
179                 //end CR 6376140
180
pis.close();
181             } catch (IOException ioe) {
182                 try {
183                     //start CR 6376140
184
buffReader.close();
185                     //dis.close();
186
//end CR 6376140
187
pis.close();
188                 } catch (Exception JavaDoc e) {
189                     CommonInfoModel.getDefaultLogger().info(e.getMessage());
190                 }
191             }
192         }
193     }
194 }
195
Popular Tags