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. /**
  21. * An Ant wrapper task for the weblogic.deploy tool. This is used to
  22. * hot-deploy J2EE applications to a running WebLogic server.
  23. * This is <b>not</b> the same as creating the application archive.
  24. * This task assumes the archive (EAR, JAR, or WAR) file has been
  25. * assembled and is supplied as the "source" attribute.
  26. * <p>In the end, this task assembles the commadline parameters
  27. * and runs the weblogic.deploy tool in a seperate JVM.
  28. *
  29. *
  30. * @see org.apache.tools.ant.taskdefs.optional.j2ee.HotDeploymentTool
  31. * @see org.apache.tools.ant.taskdefs.optional.j2ee.AbstractHotDeploymentTool
  32. * @see org.apache.tools.ant.taskdefs.optional.j2ee.ServerDeploy
  33. */
  34. public class WebLogicHotDeploymentTool extends AbstractHotDeploymentTool
  35. implements HotDeploymentTool {
  36. /** The classname of the tool to run **/
  37. private static final String WEBLOGIC_DEPLOY_CLASS_NAME = "weblogic.deploy";
  38. /** All the valid actions that weblogic.deploy permits **/
  39. private static final String[] VALID_ACTIONS
  40. = {ACTION_DELETE, ACTION_DEPLOY, ACTION_LIST, ACTION_UNDEPLOY, ACTION_UPDATE};
  41. /** Represents the "-debug" flag from weblogic.deploy **/
  42. private boolean debug;
  43. /** The application name that is being deployed **/
  44. private String application;
  45. /** The component name:target(s) for the "-component" argument of weblogic.deploy **/
  46. private String component;
  47. /**
  48. * Perform the actual deployment.
  49. * For this implementation, a JVM is spawned and the weblogic.deploy
  50. * tools is executed.
  51. * @exception org.apache.tools.ant.BuildException if the attributes are invalid or incomplete.
  52. */
  53. public void deploy() {
  54. Java java = (Java) getTask().getProject().createTask("java");
  55. java.setFork(true);
  56. java.setFailonerror(true);
  57. java.setClasspath(getClasspath());
  58. java.setClassname(WEBLOGIC_DEPLOY_CLASS_NAME);
  59. java.createArg().setLine(getArguments());
  60. java.execute();
  61. }
  62. /**
  63. * Validates the passed in attributes.
  64. * <p>The rules are:
  65. * <ol><li>If action is "deploy" or "update" the "application" and "source"
  66. * attributes must be supplied.
  67. * <li>If action is "delete" or "undeploy" the "application" attribute must
  68. * be supplied.
  69. * @exception org.apache.tools.ant.BuildException if the attributes are invalid or incomplete
  70. */
  71. public void validateAttributes() throws BuildException {
  72. super.validateAttributes();
  73. String action = getTask().getAction();
  74. // check that the password has been set
  75. if ((getPassword() == null))
  76. throw new BuildException("The password attribute must be set.");
  77. // check for missing application on deploy & update
  78. if ((action.equals(ACTION_DEPLOY) || action.equals(ACTION_UPDATE))
  79. && application == null)
  80. throw new BuildException("The application attribute must be set "
  81. + "if action = " + action);
  82. // check for missing source on deploy & update
  83. if ((action.equals(ACTION_DEPLOY) || action.equals(ACTION_UPDATE))
  84. && getTask().getSource() == null)
  85. throw new BuildException("The source attribute must be set if "
  86. + "action = " + action);
  87. // check for missing application on delete & undeploy
  88. if ((action.equals(ACTION_DELETE) || action.equals(ACTION_UNDEPLOY))
  89. && application == null)
  90. throw new BuildException("The application attribute must be set if "
  91. + "action = " + action);
  92. }
  93. /**
  94. * Builds the arguments to pass to weblogic.deploy according to the
  95. * supplied action.
  96. * @return A String containing the arguments for the weblogic.deploy tool.
  97. */
  98. public String getArguments() throws BuildException {
  99. String action = getTask().getAction();
  100. String args = null;
  101. if (action.equals(ACTION_DEPLOY) || action.equals(ACTION_UPDATE))
  102. args = buildDeployArgs();
  103. else if (action.equals(ACTION_DELETE) || action.equals(ACTION_UNDEPLOY))
  104. args = buildUndeployArgs();
  105. else if (action.equals(ACTION_LIST))
  106. args = buildListArgs();
  107. return args;
  108. }
  109. /**
  110. * Determines if the action supplied is valid.
  111. * <p>Valid actions are contained in the static array VALID_ACTIONS
  112. * @return true if the action attribute is valid, false if not.
  113. */
  114. protected boolean isActionValid() {
  115. boolean valid = false;
  116. String action = getTask().getAction();
  117. for (int i = 0; i < VALID_ACTIONS.length; i++) {
  118. if (action.equals(VALID_ACTIONS[i])) {
  119. valid = true;
  120. break;
  121. }
  122. }
  123. return valid;
  124. }
  125. /**
  126. * Builds the prefix arguments to pass to weblogic.deploy.
  127. * These arguments are generic across all actions.
  128. * @return A StringBuffer containing the prefix arguments.
  129. * The action-specific build methods will append to this StringBuffer.
  130. */
  131. protected StringBuffer buildArgsPrefix() {
  132. ServerDeploy task = getTask();
  133. // constructs the "-url <url> -debug <action> <password>" portion
  134. // of the commmand line
  135. return new StringBuffer(1024)
  136. .append((getServer() != null)
  137. ? "-url " + getServer()
  138. : "")
  139. .append(" ")
  140. .append(debug ? "-debug " : "")
  141. .append((getUserName() != null)
  142. ? "-username " + getUserName()
  143. : "")
  144. .append(" ")
  145. .append(task.getAction()).append(" ")
  146. .append(getPassword()).append(" ");
  147. }
  148. /**
  149. * Builds the arguments to pass to weblogic.deploy for deployment actions
  150. * ("deploy" and "update").
  151. * @return A String containing the full argument string for weblogic.deploy.
  152. */
  153. protected String buildDeployArgs() {
  154. String args = buildArgsPrefix()
  155. .append(application).append(" ")
  156. .append(getTask().getSource())
  157. .toString();
  158. if (component != null) {
  159. args = "-component " + component + " " + args;
  160. }
  161. return args;
  162. }
  163. /**
  164. * Builds the arguments to pass to weblogic.deploy for undeployment actions
  165. * ("undeploy" and "delete").
  166. * @return A String containing the full argument string for weblogic.deploy.
  167. */
  168. protected String buildUndeployArgs() {
  169. return buildArgsPrefix()
  170. .append(application).append(" ")
  171. .toString();
  172. }
  173. /**
  174. * Builds the arguments to pass to weblogic.deploy for the list action
  175. * @return A String containing the full argument string for weblogic.deploy.
  176. */
  177. protected String buildListArgs() {
  178. return buildArgsPrefix()
  179. .toString();
  180. }
  181. /**
  182. * If set to true, additional information will be
  183. * printed during the deployment process; optional.
  184. * @param debug A boolean representing weblogic.deploy "-debug" flag.
  185. */
  186. public void setDebug(boolean debug) {
  187. this.debug = debug;
  188. }
  189. /**
  190. * The name of the application being deployed; required.
  191. * @param application A String representing the application portion of the
  192. * weblogic.deploy command line.
  193. */
  194. public void setApplication(String application) {
  195. this.application = application;
  196. }
  197. /**
  198. * the component string for the deployment targets; optional.
  199. * It is in the form <code><component>:<target1>,<target2>...</code>
  200. * Where component is the archive name (minus the .jar, .ear, .war
  201. * extension). Targets are the servers where the components will be deployed
  202. * @param component A String representing the value of the "-component"
  203. * argument of the weblogic.deploy command line argument.
  204. */
  205. public void setComponent(String component) {
  206. this.component = component;
  207. }
  208. }