1 23 24 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 42 public class Commands { 43 private static StringManager stringManager = StringManager.getManager("com.sun.enterprise.tools.upgrade.common"); 44 45 46 public Commands() { 47 } 48 49 public static boolean deploy(String modulePath, CommonInfoModel commonInfo) { 50 String currentDomain = commonInfo.getCurrentDomain(); 51 String adminPort = DomainsProcessor.getTargetDomainPort(currentDomain, commonInfo); 52 String adminSecurity = DomainsProcessor.getTargetDomainSecurity(currentDomain, commonInfo); 53 String edition = commonInfo.getTargetEdition(); 54 String [] deploy = { 55 "deploy", "--user", commonInfo.getAdminUserName(), 56 "--passwordfile ", "\"" + commonInfo.getPasswordFile()+ "\"", 58 "--port",adminPort, 59 "--secure=" + adminSecurity, 60 "\"" + modulePath + "\"" 61 }; 62 if(!commonInfo.getTargetEdition().equals(UpgradeConstants.EDITION_PE)){ 63 String targetName = commonInfo.getCurrentCluster(); 64 if(targetName == null){ 65 targetName = commonInfo.getCurrentSourceInstance(); 66 } 67 if((targetName != null) && (!("".equals(targetName)))){ 68 deploy = new String []{ 69 "deploy", "--user", commonInfo.getAdminUserName(), 70 "--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 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 domainName, CommonInfoModel commonInfo) { 90 String command[] = { 91 "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 t = ce.getCause(); 100 CommonInfoModel.getDefaultLogger().severe(stringManager.getString("commands.generalExceptionMsg") + ce.getMessage()); 101 if (t != null) { 102 String 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 domainName, CommonInfoModel commonInfo) { 117 String 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 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 commandStrings[]) throws CommandException { 134 try { 135 StringBuffer commandOneString = new StringBuffer (); 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 )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 e) { 154 Throwable 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 { 162 PipedInputStream pis = new PipedInputStream(); 163 public CommandOutputReader(PipedOutputStream pout) throws IOException { 164 pis.connect(pout); 165 } 166 167 public void run() { 168 BufferedReader buffReader = new BufferedReader(new InputStreamReader(pis)); 170 try { 173 String s = null; 175 while((s=buffReader.readLine()) != null) { 176 CommonInfoModel.getDefaultLogger().info(s); 178 } 179 pis.close(); 181 } catch (IOException ioe) { 182 try { 183 buffReader.close(); 185 pis.close(); 188 } catch (Exception e) { 189 CommonInfoModel.getDefaultLogger().info(e.getMessage()); 190 } 191 } 192 } 193 } 194 } 195 | Popular Tags |