1 23 24 77 78 package org.apache.tools.ant.taskdefs.optional.sun.appserv; 79 80 import org.apache.tools.ant.BuildException; 81 import org.apache.tools.ant.Project; 82 83 import java.io.File ; 84 85 114 public class AdminTask extends AppServerAdmin { 115 private String command; 116 private String explicitCommand; 117 private File commandFile; 118 LocalStringsManager lsm = new LocalStringsManager(); 119 120 127 public void setCommand(String command) { 128 this.command = command.trim(); 129 } 130 131 138 public void setExplicitcommand(String explicitCommand) { 139 this.explicitCommand = explicitCommand; 140 } 141 142 150 public void setCommandfile(File commandFile) { 151 final String msg = lsm.getString("DeprecatedAttribute", 152 new Object [] {"commandfile", 153 "- 'multimode --file <commandfile>'"}); 154 log(msg, Project.MSG_WARN); 155 this.commandFile = commandFile; 156 } 157 158 public void execute() throws BuildException { 159 checkCommandCount(); 160 161 if ((command != null) && (servers.size() == 0) && (server == null)) { 162 explicitCommand = command; 164 command = null; 165 } 166 167 if (explicitCommand != null) { 168 execAdminCommand(explicitCommand); 170 } else { 171 super.execute(); 172 } 173 } 174 175 182 private void checkCommandCount() throws BuildException { 183 int commandCount = 0; 184 185 if (command != null) { commandCount++; } 186 if (explicitCommand != null) { commandCount++; } 187 if (commandFile != null) { commandCount++; } 188 189 if (commandCount != 1) { 190 final String msg = lsm.getString("ExactlyOneCommandAttribute"); 191 throw new BuildException(msg, getLocation()); 192 } 193 } 194 195 protected void checkConfiguration(Server aServer) throws BuildException { 196 } 198 199 protected void execute(Server aServer) throws BuildException { 200 204 205 final String userOption[] = {"--user ", "-u "}; 206 final String passwordOption[] = {"--password ", "-w ", "--passwordfile "}; 207 final String hostOption[] = {"--host ", "-H "}; 208 final String portOption[] = {"--port ", "-p "}; 209 final String instanceOption[] = {"--instance ", "-i "}; 210 final String secureOption[] = {"--secure ", "-s "}; 211 212 StringBuffer cmd; 213 if (command != null) { 214 cmd = new StringBuffer (command); 215 if (!commandIncludes(cmd, userOption)) { 216 insertCommandOption(cmd, " --user " + aServer.getUser()); 217 } 218 if ((aServer.hasPassword()) 219 && (!commandIncludes(cmd, passwordOption))) { 220 insertCommandOption(cmd, aServer.getPasswordCommand()); 221 } 222 if (!commandIncludes(cmd, hostOption)) { 223 String theHost = aServer.getHost(); 224 if (theHost == null) { 225 theHost = Server.DEFAULT_HOST; 226 } 227 insertCommandOption(cmd, " --host " + theHost); 228 } 229 if (!commandIncludes(cmd, portOption)) { 230 String thePort = (aServer.getPort() == 0) ? 231 Server.DEFAULT_PORT : 232 String.valueOf(aServer.getPort()); 233 insertCommandOption(cmd, " --port " + thePort); 234 } 235 if ((aServer.getInstance() != null) 236 && (!commandIncludes(cmd, instanceOption))) { 237 insertCommandOption(cmd, " --instance " + aServer.getInstance()); 238 } 239 if (aServer.getSecure() != null && 240 !commandIncludes(cmd, secureOption)) { 241 insertCommandOption(cmd, " --secure=" + aServer.getSecure()); 242 } 243 244 } else { 245 String filename; 246 try 247 { 248 filename = commandFile.getCanonicalPath(); 249 } 250 catch(Exception e) 251 { 252 filename = commandFile.getAbsolutePath(); 253 } 254 filename = filename.replace('\\', '/'); 256 257 cmd = new StringBuffer ("multimode --file " + filename + " "); 258 } 259 260 execAdminCommand(cmd.toString()); 261 } 262 263 272 private boolean commandIncludes(StringBuffer cmd, String options[]) { 273 for (int i = 0; i < options.length; i++) { 274 if (cmd.indexOf(options[i]) > 0) { 275 return true; 276 } 277 } 278 return false; 279 } 280 281 288 private void insertCommandOption(StringBuffer cmdLine, String commandOption) { 289 int index = cmdLine.indexOf(" "); 290 index = (index >= 0) ? index : cmdLine.length(); 291 cmdLine.insert(index, commandOption).append(' '); 292 } 293 } 294 | Popular Tags |