1 16 17 package org.apache.axis.tools.ant.axis; 18 19 import org.apache.axis.AxisFault; 20 import org.apache.axis.client.AdminClient; 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.DirectoryScanner; 23 import org.apache.tools.ant.Project; 24 import org.apache.tools.ant.taskdefs.Java; 25 import org.apache.tools.ant.taskdefs.MatchingTask; 26 import org.apache.tools.ant.types.Path; 27 import org.apache.tools.ant.types.Reference; 28 29 import javax.xml.rpc.ServiceException ; 30 import java.io.File ; 31 import java.util.Iterator ; 32 import java.util.LinkedList ; 33 34 40 public class AdminClientTask extends MatchingTask { 41 42 45 private boolean failOnError = true; 46 private String hostname; 47 private int port = 0; 48 private String servletPath; 49 private File srcDir = null; 50 private File xmlFile; 51 private String transportChain; 52 private String username; 53 private String password; 54 private String fileProtocol; 55 private String action = ""; 56 private String url; 57 private boolean debug; 58 private String newPassword; 59 private LinkedList argslist; 60 private boolean fork = false; 61 private Path classpath = null; 62 63 68 public void setDebug(boolean debug) { 69 this.debug = debug; 70 } 71 72 77 public void setNewPassword(String newPassword) { 78 this.newPassword = newPassword; 79 } 80 81 86 public void setUrl(String url) { 87 this.url = url; 88 } 89 90 95 public void setFileProtocol(String fileProtocol) { 96 this.fileProtocol = fileProtocol; 97 } 98 99 104 public void setHostname(String hostname) { 105 this.hostname = hostname; 106 } 107 108 113 public void setPassword(String password) { 114 this.password = password; 115 } 116 117 122 public void setPort(int port) { 123 this.port = port; 124 } 125 126 131 public void setServletPath(String servletPath) { 132 this.servletPath = servletPath; 133 } 134 135 138 public void setSrcdir(File srcDir) { 139 this.srcDir = srcDir; 140 } 141 142 147 public void setXmlFile(File xmlFile) { 148 this.xmlFile = xmlFile; 149 } 150 151 156 public void setTransportChain(String transportChain) { 157 this.transportChain = transportChain; 158 } 159 160 165 public void setUsername(String username) { 166 this.username = username; 167 } 168 169 173 public void setFailOnError(boolean fail) { 174 failOnError = fail; 175 } 176 177 182 public void setFork(boolean f) { 183 fork = f; 184 } 185 186 192 protected void validate() 193 throws BuildException { 194 if (srcDir != null) { 195 if (!srcDir.exists()) { 196 throw new BuildException("srcdir does not exist!"); 197 } 198 if (!srcDir.isDirectory()) { 199 throw new BuildException("srcdir is not a directory!"); 200 } 201 } 202 } 203 204 210 public void traceParams(int logLevel) { 211 log("Running axis-admin with parameters:", logLevel); 212 log(" action:" + action, logLevel); 213 log(" url:" + url, logLevel); 214 log(" hostname:" + hostname, logLevel); 215 log(" port:" + port, logLevel); 216 log(" servletPath:" + servletPath, logLevel); 217 log(" fileProtocol:" + fileProtocol, logLevel); 218 log(" username:" + username, logLevel); 219 log(" password:" + password, logLevel); 220 log(" transportChain:" + transportChain, logLevel); 221 log(" debug:" + debug, logLevel); 222 } 223 224 255 public void execute() throws BuildException { 256 traceParams(Project.MSG_VERBOSE); 257 validate(); 258 argslist = new LinkedList (); 259 260 addArgs("-l", url, url != null); 262 addArgs("-h", hostname, hostname != null); 263 addArgs("-p", Integer.toString(port), port != 0); 264 addArgs("-s", servletPath, servletPath != null); 265 addArgs("-f", fileProtocol, fileProtocol != null); 266 addArgs("-u", username, username != null); 267 addArgs("-w", password, password != null); 268 addArgs("-t", transportChain, transportChain != null); 269 addArg("-d", debug); 270 addArg(action, action != null); 272 if ("passwd".equals(action)) { 274 if (newPassword == null) { 275 throw new BuildException("No newpassword set for passwd"); 276 } 277 addArg(newPassword); 278 } else { 279 if (newPassword != null) { 280 throw new BuildException( 281 "newpassword is only used when action=passwd"); 282 } 283 } 284 285 if (xmlFile != null) { 287 if (!xmlFile.exists()) { 288 throw new BuildException("File " + xmlFile + " no found"); 289 } 290 addArg(xmlFile.toString()); 291 } 292 if (srcDir != null) { 293 DirectoryScanner ds = super.getDirectoryScanner(srcDir); 294 String [] files = ds.getIncludedFiles(); 295 for (int i = 0; i < files.length; i++) { 296 File srcFile = new File (srcDir, files[i]); 297 if (!srcFile.exists()) { 298 throw new BuildException("File " + srcFile + " no found"); 299 } 300 addArg(srcFile.getAbsolutePath()); 301 } 302 } 303 304 int counter = 0; 306 String [] args = new String [argslist.size()]; 307 Iterator it = argslist.iterator(); 308 while (it.hasNext()) { 309 String arg = (String ) it.next(); 310 args[counter] = arg; 311 counter++; 312 } 313 if (fork) { 314 executeInForkedVM(args); 315 } else { 316 executeInCurrentVM(args); 317 } 318 } 319 320 private void executeInForkedVM(String [] args) { 321 try { 322 Java java = (Java) (getProject().createTask("java")); 325 getProject().log("using classpath: " + classpath, 326 Project.MSG_DEBUG); 327 java.setClasspath(classpath); 328 java.setClassname("org.apache.axis.client.AdminClient"); 329 for (int i = 0; i < args.length; i++) { 330 java.createArg().setValue(args[i]); 331 } 332 java.setFailonerror(failOnError); 333 java.setFork(true); 336 java.setTaskName("AdminClient"); 337 java.execute(); 338 } catch (BuildException e) { 339 throw e; 341 } catch (Exception e) { 342 throw new BuildException("Exception in " + getTaskName(), e); 343 } 344 } 345 346 private void executeInCurrentVM(String [] args) { 347 AdminClient admin = null; 349 try { 350 admin = new AdminClient(true); 351 } catch (ServiceException e) { 352 throw new BuildException("failed to start the axis engine", e); 353 } 354 String result = null; 355 try { 356 result = admin.process(args); 357 if (result != null) { 358 log(result); 359 } else { 360 logOrThrow(getTaskName() + " got a null response"); 361 } 362 } catch (AxisFault fault) { 363 log(fault.dumpToString(), Project.MSG_ERR); 364 traceParams(Project.MSG_ERR); 365 logOrThrow(getTaskName() 366 + " failed with " 367 + fault.getFaultCode().toString() 368 + " " + fault.getFaultString()); 369 } catch (BuildException e) { 370 throw e; 372 } catch (Exception e) { 373 throw new BuildException("Exception in " + getTaskName(), e); 374 } 375 } 376 377 private void logOrThrow(String text) throws BuildException { 378 if (failOnError) { 379 throw new BuildException(text); 380 } else { 381 log(text, Project.MSG_ERR); 382 } 383 } 384 385 390 protected void addArg(String argument) { 391 argslist.add(argument); 392 } 393 394 399 protected void addArg(String argument, boolean test) { 400 if (test) { 401 argslist.add(argument); 402 } 403 } 404 405 411 protected void addArgs(String argument, String param) { 412 addArg(argument); 413 addArg(param); 414 } 415 416 423 protected void addArgs(String argument, String param, boolean test) { 424 if (test) { 425 addArg(argument); 426 addArg(param); 427 } 428 } 429 430 435 public void setClasspath(Path classpath) { 436 createClasspath().append(classpath); 437 } 438 439 444 public Path createClasspath() { 445 if (classpath == null) { 446 classpath = new Path(getProject()); 447 } 448 return classpath.createPath(); 449 } 450 451 456 public void setClasspathRef(Reference r) { 457 createClasspath().setRefid(r); 458 } 459 } 460 | Popular Tags |