1. /*
  2. * Copyright 2002,2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.j2ee;
  18. import org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.taskdefs.Java;
  20. import org.apache.tools.ant.types.Commandline;
  21. /**
  22. * A generic tool for J2EE server hot deployment.
  23. * <p>The simple implementation spawns a JVM with the supplied
  24. * class name, jvm args, and arguments.
  25. *
  26. *
  27. * @see org.apache.tools.ant.taskdefs.optional.j2ee.HotDeploymentTool
  28. * @see org.apache.tools.ant.taskdefs.optional.j2ee.AbstractHotDeploymentTool
  29. * @see org.apache.tools.ant.taskdefs.optional.j2ee.ServerDeploy
  30. */
  31. public class GenericHotDeploymentTool extends AbstractHotDeploymentTool {
  32. /** A Java task used to run the deployment tool **/
  33. private Java java;
  34. /** The fully qualified class name of the deployment tool **/
  35. private String className;
  36. /** List of valid actions **/
  37. private static final String[] VALID_ACTIONS = {ACTION_DEPLOY};
  38. /**
  39. * Add a nested argument element to hand to the deployment tool; optional.
  40. * @return A Commandline.Argument object representing the
  41. * command line argument being passed when the deployment
  42. * tool is run. IE: "-user=mark", "-password=venture"...
  43. */
  44. public Commandline.Argument createArg() {
  45. return java.createArg();
  46. }
  47. /**
  48. * Add a nested argment element to hand to the JVM running the
  49. * deployment tool.
  50. * Creates a nested arg element.
  51. * @return A Commandline.Argument object representing the
  52. * JVM command line argument being passed when the deployment
  53. * tool is run. IE: "-ms64m", "-mx128m"...
  54. */
  55. public Commandline.Argument createJvmarg() {
  56. return java.createJvmarg();
  57. }
  58. /**
  59. * Determines if the "action" attribute defines a valid action.
  60. * <p>Subclasses should determine if the action passed in is
  61. * supported by the vendor's deployment tool.
  62. * For this generic implementation, the only valid action is "deploy"
  63. * @return true if the "action" attribute is valid, false if not.
  64. */
  65. protected boolean isActionValid() {
  66. return (getTask().getAction().equals(VALID_ACTIONS[0]));
  67. }
  68. /**
  69. * Sets the parent task.
  70. * @param task An ServerDeploy object representing the parent task.
  71. * @ant.attribute ignored="true"
  72. */
  73. public void setTask(ServerDeploy task) {
  74. super.setTask(task);
  75. java = (Java) task.getProject().createTask("java");
  76. }
  77. /**
  78. * Perform the actual deployment.
  79. * For this generic implementation, a JVM is spawned using the
  80. * supplied classpath, classname, JVM args, and command line arguments.
  81. * @exception org.apache.tools.ant.BuildException if the attributes are invalid or incomplete.
  82. */
  83. public void deploy() throws BuildException {
  84. java.setClassname(className);
  85. java.setClasspath(getClasspath());
  86. java.setFork(true);
  87. java.setFailonerror(true);
  88. java.execute();
  89. }
  90. /**
  91. * Validates the passed in attributes.
  92. * Ensures the className and arguments attribute have been set.
  93. * @exception org.apache.tools.ant.BuildException if the attributes are invalid or incomplete.
  94. */
  95. public void validateAttributes() throws BuildException {
  96. super.validateAttributes();
  97. if (className == null) {
  98. throw new BuildException("The classname attribute must be set");
  99. }
  100. }
  101. /**
  102. * The name of the class to execute to perfom
  103. * deployment; required.
  104. * Example: "com.foobar.tools.deploy.DeployTool"
  105. * @param className The fully qualified class name of the class
  106. * to perform deployment.
  107. */
  108. public void setClassName(String className) {
  109. this.className = className;
  110. }
  111. /**
  112. *
  113. */
  114. public Java getJava() {
  115. return java;
  116. }
  117. public String getClassName() {
  118. return className;
  119. }
  120. }