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.types.Path;
  20. /**
  21. * Abstract class to support vendor-specific hot deployment tools.
  22. * This class will validate boilerplate attributes.
  23. *
  24. * Subclassing this class for a vendor specific tool involves the
  25. * following.
  26. * <ol><li>Implement the <code>isActionValid()<code> method to insure the
  27. * action supplied as the "action" attribute of ServerDeploy is valid.
  28. * <li>Implement the <code>validateAttributes()</code> method to insure
  29. * all required attributes are supplied, and are in the correct format.
  30. * <li>Add a <code>add<TOOL></code> method to the ServerDeploy
  31. * class. This method will be called when Ant encounters a
  32. * <code>add<TOOL></code> task nested in the
  33. * <code>serverdeploy</code> task.
  34. * <li>Define the <code>deploy</code> method. This method should perform
  35. * whatever task it takes to hot-deploy the component. IE: spawn a JVM and
  36. * run class, exec a native executable, run Java code...
  37. *
  38. *
  39. * @see org.apache.tools.ant.taskdefs.optional.j2ee.HotDeploymentTool
  40. * @see org.apache.tools.ant.taskdefs.optional.j2ee.ServerDeploy
  41. */
  42. public abstract class AbstractHotDeploymentTool implements HotDeploymentTool {
  43. /** The parent task **/
  44. private ServerDeploy task;
  45. /** The classpath passed to the JVM on execution. **/
  46. private Path classpath;
  47. /** The username for the deployment server. **/
  48. private String userName;
  49. /** The password for the deployment server. **/
  50. private String password;
  51. /** The address of the deployment server **/
  52. private String server;
  53. /**
  54. * Add a classpath as a nested element.
  55. * @return A Path object representing the classpath to be used.
  56. */
  57. public Path createClasspath() {
  58. if (classpath == null) {
  59. classpath = new Path(task.getProject());
  60. }
  61. return classpath.createPath();
  62. }
  63. /**
  64. * Determines if the "action" attribute defines a valid action.
  65. * <p>Subclasses should determine if the action passed in is
  66. * supported by the vendor's deployment tool.
  67. * <p>Actions may by "deploy", "delete", etc... It all depends
  68. * on the tool.
  69. * @return true if the "action" attribute is valid, false if not.
  70. */
  71. protected abstract boolean isActionValid();
  72. /**
  73. * Validates the passed in attributes.
  74. * Subclasses should chain to this super-method to insure
  75. * validation of boilerplate attributes.
  76. * <p>Only the "action" attribute is required in the
  77. * base class. Subclasses should check attributes accordingly.
  78. * @exception org.apache.tools.ant.BuildException if the attributes are invalid or incomplete.
  79. */
  80. public void validateAttributes() throws BuildException {
  81. if (task.getAction() == null) {
  82. throw new BuildException("The \"action\" attribute must be set");
  83. }
  84. if (!isActionValid()) {
  85. throw new BuildException("Invalid action \"" + task.getAction() + "\" passed");
  86. }
  87. if (classpath == null) {
  88. throw new BuildException("The classpath attribute must be set");
  89. }
  90. }
  91. /**
  92. * Perform the actual deployment.
  93. * It's up to the subclasses to implement the actual behavior.
  94. * @exception org.apache.tools.ant.BuildException if the attributes are invalid or incomplete.
  95. */
  96. public abstract void deploy() throws BuildException;
  97. /**
  98. * Sets the parent task.
  99. * @param task a ServerDeploy object representing the parent task.
  100. * @ant.attribute ignore="true"
  101. */
  102. public void setTask(ServerDeploy task) {
  103. this.task = task;
  104. }
  105. /**
  106. * Returns the task field, a ServerDeploy object.
  107. * @return An ServerDeploy representing the parent task.
  108. */
  109. protected ServerDeploy getTask() {
  110. return task;
  111. }
  112. /**
  113. * gets the classpath field.
  114. * @return A Path representing the "classpath" attribute.
  115. */
  116. public Path getClasspath() {
  117. return classpath;
  118. }
  119. /**
  120. * The classpath to be passed to the JVM running the tool;
  121. * optional depending upon the tool.
  122. * The classpath may also be supplied as a nested element.
  123. * @param classpath A Path object representing the "classpath" attribute.
  124. */
  125. public void setClasspath(Path classpath) {
  126. this.classpath = classpath;
  127. }
  128. /**
  129. * Returns the userName field.
  130. * @return A String representing the "userName" attribute.
  131. */
  132. public String getUserName() {
  133. return userName;
  134. }
  135. /**
  136. * The user with privileges to deploy applications to the server; optional.
  137. * @param userName A String representing the "userName" attribute.
  138. */
  139. public void setUserName(String userName) {
  140. this.userName = userName;
  141. }
  142. /**
  143. * Returns the password field.
  144. * @return A String representing the "password" attribute.
  145. */
  146. public String getPassword() {
  147. return password;
  148. }
  149. /**
  150. * The password of the user; optional.
  151. * @param password A String representing the "password" attribute.
  152. */
  153. public void setPassword(String password) {
  154. this.password = password;
  155. }
  156. /**
  157. * Returns the server field.
  158. * @return A String representing the "server" attribute.
  159. */
  160. public String getServer() {
  161. return server;
  162. }
  163. /**
  164. * The address or URL for the server where the component will be deployed.
  165. * @param server A String representing the "server" attribute.
  166. */
  167. public void setServer(String server) {
  168. this.server = server;
  169. }
  170. }