1 23 24 77 78 package org.apache.tools.ant.taskdefs.optional.sun.appserv; 79 80 import org.apache.tools.ant.Task; 81 import org.apache.tools.ant.BuildException; 82 import org.apache.tools.ant.Project; 83 import org.apache.tools.ant.AntClassLoader; 84 import org.apache.tools.ant.types.Path; 85 86 import java.io.File ; 87 import java.util.List ; 88 import java.util.ArrayList ; 89 import java.util.Iterator ; 90 import java.lang.reflect.Method ; 91 import java.lang.reflect.InvocationTargetException ; 92 93 147 public abstract class AppServerAdmin extends Task { 148 static Method invokeCLI = null; 149 static Class adminMain = null; 150 static Class inputsAndOutputs = null; 151 static Class systemPropertyConstants = null; 152 153 154 158 protected Server server; 159 160 164 protected List servers = new ArrayList (); 165 166 167 private File asinstalldir; 168 169 174 private boolean executeCommand = true; 175 176 177 private static final String CLASS_INPUTS_AND_OUTPUTS = 178 "com.sun.enterprise.cli.framework.InputsAndOutputs"; 179 private static final String CLASS_ADMIN_MAIN = 180 "com.sun.enterprise.cli.framework.CLIMain"; 181 private static final String METHOD_INVOKE_CLI = "invokeCLI"; 182 183 LocalStringsManager lsm = new LocalStringsManager(); 184 185 private static final String CLASS_SYSTEM_PROPERTY_CONSTANTS = 186 "com.sun.enterprise.util.SystemPropertyConstants"; 187 188 191 public AppServerAdmin() { 192 server = getNewServer(); 193 } 194 195 201 public void setUser(String user) { 202 server.setUser(user); } 204 205 211 public void setPassword(String password) { 212 final String msg = lsm.getString("DeprecatedAttribute", new Object [] {"password", 213 "passwordfile"}); 214 log(msg, Project.MSG_WARN); 215 server.setPassword(password); } 217 218 224 public void setPasswordfile(String passwordfile) { 225 server.setPasswordfile(passwordfile); } 227 228 234 public void setHost(String host) { 235 server.setHost(host); } 237 238 244 public void setPort(int port) { 245 server.setPort(port); } 247 248 254 public void setSecure(String secure) { 255 server.setSecure(secure); } 257 258 259 265 public void setInstance(String instance) { 266 server.setInstance(instance); } 268 269 277 public void setExecuteCommand(boolean executeCommand) { 278 this.executeCommand = executeCommand; 279 } 280 281 289 public void setSunonehome(File sunonehome) { 290 final String msg = lsm.getString("DeprecatedAttribute", new Object [] {"sunonehome", 291 "asinstalldir"}); 292 log(msg, Project.MSG_WARN); 293 this.asinstalldir = sunonehome; 294 } 295 296 304 public void setAsinstalldir(File asinstalldir) { 305 this.asinstalldir = asinstalldir; 306 } 307 308 309 320 protected File getAsinstalldir() throws ClassNotFoundException { 321 if (asinstalldir == null) { 322 String home = getProject().getProperty("asinstall.dir"); 323 if (home != null) { 324 asinstalldir = new File (home); 325 } 326 else { 327 home = getProject().getProperty("sunone.home"); 328 if (home != null) 329 { 330 final String msg = lsm.getString("DeprecatedProperty", new Object [] {"sunone.home", "asinstall.dir"}); 331 log(msg, Project.MSG_WARN); 332 asinstalldir = new File (home); 333 } 334 335 } 336 } 337 if (asinstalldir!=null) verifyAsinstalldir(asinstalldir); 338 return asinstalldir; 339 } 340 341 342 349 private boolean verifyAsinstalldir(File home) throws ClassNotFoundException { 350 if (home!= null && home.isDirectory()) { 351 if ( new File (home, "config").isDirectory() ) { 352 return true; 353 } 354 } 355 throw new ClassNotFoundException ("ClassCouldNotBeFound"); 356 } 357 358 359 371 private String getInstallRoot(final String installRootConstant) throws ClassNotFoundException { 372 final File installDir = getAsinstalldir(); 373 String installRoot; 374 if (installDir == null) { 375 installRoot = System.getProperty(installRootConstant); 376 if (installRoot == null) 377 throw new ClassNotFoundException ("ClassCouldNotBeFound"); 378 } else { 379 installRoot = installDir.getPath(); 380 System.setProperty(installRootConstant, installRoot); 381 } 382 return installRoot; 383 } 384 385 386 391 public Server createServer() { 392 log("createServer", Project.MSG_DEBUG); 393 Server aNestedServer = getNewServer(); 394 servers.add(aNestedServer); 395 return aNestedServer; 396 } 397 398 404 protected Server getNewServer() { 405 return new Server(server); 406 } 407 408 415 public void execute() throws BuildException { 416 prepareToExecute(); 417 checkConfiguration(); 418 419 Iterator it = servers.iterator(); 420 while (it.hasNext()) { 421 Server aServer = (Server)it.next(); 422 execute(aServer); 423 } 424 } 425 426 430 protected void prepareToExecute() { 431 if (servers.size() == 0) { 432 servers.add(server); 433 } 434 } 435 436 442 protected void checkConfiguration() throws BuildException { 443 444 log(servers.size() + " servers were found.", Project.MSG_DEBUG); 445 446 if (servers.size() == 0) { 448 final String msg = lsm.getString("SpecifyOneServer"); 449 throw new BuildException(msg, getLocation()); 450 } 451 452 Iterator it = servers.iterator(); 454 while (it.hasNext()) { 455 Server aServer = (Server)it.next(); 456 checkConfiguration(aServer); 457 } 458 } 459 460 467 protected abstract void checkConfiguration(Server aServer) throws BuildException; 468 469 475 protected abstract void execute(Server server) throws BuildException; 476 477 484 protected void execAdminCommand(String command) throws BuildException { 485 log("Executing: " + command, Project.MSG_INFO); 486 487 try { 488 489 if (invokeCLI == null) { 490 java.lang.ClassLoader antClassLoader = new AntClassLoader( 491 AppservClassLoader.getClassLoader(), getProject(), null, false); 492 log("class = " + ((AntClassLoader)antClassLoader).getClasspath(), Project.MSG_DEBUG); 493 inputsAndOutputs = Class.forName(CLASS_INPUTS_AND_OUTPUTS, true, antClassLoader); 494 adminMain = Class.forName(CLASS_ADMIN_MAIN, true, antClassLoader); 495 systemPropertyConstants = Class.forName(CLASS_SYSTEM_PROPERTY_CONSTANTS, true, antClassLoader); 496 } 497 498 499 log("***** INSTALL_ROOT_PROPERTY = " + (String )systemPropertyConstants.getField("INSTALL_ROOT_PROPERTY").get(null), Project.MSG_DEBUG); 500 501 final String installRootConstant = (String )systemPropertyConstants.getField("INSTALL_ROOT_PROPERTY").get(null); 502 final String configRootConstant = (String )systemPropertyConstants.getField("CONFIG_ROOT_PROPERTY").get(null); 503 504 final String installRoot = getInstallRoot(installRootConstant); 505 506 log("installRoot: " + installRoot, Project.MSG_DEBUG); 507 System.setProperty("java.library.path", installRoot+"/lib"); 508 System.setProperty(installRootConstant, installRoot); 509 System.setProperty(configRootConstant, installRoot+"/config"); 510 System.setProperty("java.endorsed.dirs", installRoot+"/lib/endorsed"); 511 512 521 522 Class [] parameterClasses = {String .class, inputsAndOutputs}; 523 invokeCLI = adminMain.getDeclaredMethod(METHOD_INVOKE_CLI, parameterClasses); 524 525 Object [] parameters = {command, null}; 526 527 if (executeCommand) { 528 invokeCLI.invoke(adminMain, parameters); 529 } 530 } catch (ClassNotFoundException e) { 531 final String msg = lsm.getString("ClassCouldNotBeFound", new Object [] {e.getMessage()}); 532 throw new BuildException(msg, getLocation()); 533 } catch (NoSuchMethodException e) { 534 final String msg = lsm.getString("CouldNotFindInvokeCLI", new Object [] {e.getMessage()}); 535 throw new BuildException(msg, getLocation()); 536 } catch (InvocationTargetException e) { 537 final String msg = lsm.getString("ExceptionOccuredRunningTheCommand", new Object [] {e.getTargetException().getMessage()}); 538 throw new BuildException(msg, getLocation()); 539 } catch (IllegalAccessException e) { 540 final String msg = lsm.getString("ExceptionOccuredInvokeingCLI", new Object [] {e.getMessage()}); 541 throw new BuildException(msg, getLocation()); 542 } catch (Exception e) { 543 throw new BuildException(e.getMessage(), getLocation()); 544 } 545 546 547 } 548 549 556 public class Server { 557 private Server parent; private String user; private String password; private String passwordfile; private String host; private int port; private String secure; private String instance; 566 protected static final String DEFAULT_USER = "admin"; 567 protected static final String DEFAULT_HOST = "localhost"; 568 protected static final String DEFAULT_PORT = "4848"; 569 570 574 public Server() { 575 this(null); 576 } 577 578 584 public Server(Server parent) { 585 this.parent = parent; 586 } 587 588 595 public void setParent(Server parent) { 596 this.parent = parent; 597 } 598 599 606 public Server getParent() { 607 return parent; 608 } 609 610 616 public void setUser(String user) { 617 this.user = user; 618 } 619 620 626 protected String getUser() { 627 if (user == null) { 628 return (parent == null) ? DEFAULT_USER : parent.getUser(); 629 } 630 return user; 631 } 632 633 639 public void setPassword(String password) { 640 this.password = password; 641 } 642 643 650 protected String getPasswordCommand() { 651 652 655 if(!hasPassword()) 656 return null; 657 658 663 if(passwordfile != null) 665 return " --passwordfile " + passwordfile + " "; 666 667 if (password != null) 669 return " --password " + password + " "; 670 671 if( parent != null) 673 return parent.getPasswordCommand(); 674 675 return null; 677 } 678 686 private String getPassword() { 687 if (password == null) { 688 return (parent == null) ? null : parent.getPassword(); 689 } 690 return password; 691 } 692 695 protected boolean hasPassword() { 696 697 if(passwordfile != null || password != null) 698 return true; 699 700 if(parent != null) 701 return parent.hasPassword(); 702 703 return false; 705 } 706 707 713 public void setPasswordfile(String passwordfile) { 714 this.passwordfile = "\"" + new File (passwordfile).getPath() + "\""; 718 } 719 720 728 private String getPasswordfile() { 729 if (passwordfile == null) { 730 return (parent == null) ? null : parent.getPasswordfile(); 731 } 732 return passwordfile; 733 } 734 735 741 public void setHost(String host) { 742 this.host = host; 743 } 744 745 751 protected String getHost() { 752 if (host == null) { 753 return (parent == null) ? null : parent.getHost(); 754 } 755 return host; 756 } 757 758 764 public void setPort(int port) { 765 this.port = port; 766 } 767 768 774 protected int getPort() { 775 if (port == 0) { 776 return (parent == null) ? 0 : parent.getPort(); 777 } 778 return port; 779 } 780 781 787 public void setSecure(String secure) { 788 this.secure = secure; 789 } 790 791 796 protected String getSecure() { 797 if (secure == null) { 798 return (parent == null) ? null : parent.getSecure(); 799 } 800 return secure; 801 } 802 803 809 public void setInstance(String instance) { 810 this.instance = instance; 811 } 812 813 819 protected String getInstance() { 820 if (instance == null) { 821 return (parent == null) ? null : parent.getInstance(); 822 } 823 return instance; 824 } 825 826 837 protected String getCommandParameters(boolean includeInstance) { 838 StringBuffer cmdString = new StringBuffer (); 839 cmdString.append(" --user ").append(getUser()); 840 841 String pwc = getPasswordCommand(); 845 846 if(pwc != null){ 847 cmdString.append(pwc); 848 } 849 850 if (getHost() != null) { 851 cmdString.append(" --host ").append(getHost()); 852 } 853 if (getPort() != 0) { 854 cmdString.append(" --port ").append(getPort()); 855 } 856 if (includeInstance && (getInstance() != null)) { 857 cmdString.append(" --instance ").append(getInstance()); 858 } 859 if (getSecure() != null) { 860 cmdString.append(" --secure=").append(getSecure()); 861 } 862 863 864 return cmdString.toString(); 865 } 866 867 872 public String toString() { 873 StringBuffer sb; 874 sb = new StringBuffer ((getHost() == null) ? DEFAULT_HOST : getHost()); 875 sb.append(':'); 876 sb.append((getPort() == 0) ? DEFAULT_PORT : String.valueOf(getPort())); 877 return sb.toString(); 878 } 879 }; } 881 | Popular Tags |