001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.j2ee;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.taskdefs.Java;
023:
024: /**
025: * An Ant wrapper task for the weblogic.deploy tool. This is used to
026: * hot-deploy J2EE applications to a running WebLogic server.
027: * This is <b>not</b> the same as creating the application archive.
028: * This task assumes the archive (EAR, JAR, or WAR) file has been
029: * assembled and is supplied as the "source" attribute.
030: * <p>In the end, this task assembles the commadline parameters
031: * and runs the weblogic.deploy tool in a seperate JVM.
032: *
033: * @see org.apache.tools.ant.taskdefs.optional.j2ee.HotDeploymentTool
034: * @see org.apache.tools.ant.taskdefs.optional.j2ee.AbstractHotDeploymentTool
035: * @see org.apache.tools.ant.taskdefs.optional.j2ee.ServerDeploy
036: */
037: public class WebLogicHotDeploymentTool extends
038: AbstractHotDeploymentTool implements HotDeploymentTool {
039: /** The classname of the tool to run **/
040: private static final String WEBLOGIC_DEPLOY_CLASS_NAME = "weblogic.deploy";
041:
042: /** All the valid actions that weblogic.deploy permits **/
043: private static final String[] VALID_ACTIONS = { ACTION_DELETE,
044: ACTION_DEPLOY, ACTION_LIST, ACTION_UNDEPLOY, ACTION_UPDATE };
045:
046: /** Represents the "-debug" flag from weblogic.deploy **/
047: private boolean debug;
048:
049: /** The application name that is being deployed **/
050: private String application;
051:
052: /** The component name:target(s) for the "-component" argument of weblogic.deploy **/
053: private String component;
054:
055: /**
056: * Perform the actual deployment.
057: * For this implementation, a JVM is spawned and the weblogic.deploy
058: * tools is executed.
059: * @exception org.apache.tools.ant.BuildException if the attributes are invalid or incomplete.
060: */
061: public void deploy() {
062: Java java = new Java(getTask());
063: java.setFork(true);
064: java.setFailonerror(true);
065: java.setClasspath(getClasspath());
066:
067: java.setClassname(WEBLOGIC_DEPLOY_CLASS_NAME);
068: java.createArg().setLine(getArguments());
069: java.execute();
070: }
071:
072: /**
073: * Validates the passed in attributes.
074: * <p>The rules are:
075: * <ol><li>If action is "deploy" or "update" the "application" and "source"
076: * attributes must be supplied.
077: * <li>If action is "delete" or "undeploy" the "application" attribute must
078: * be supplied.
079: * @exception org.apache.tools.ant.BuildException if the attributes are invalid or incomplete
080: */
081: public void validateAttributes() throws BuildException {
082: super .validateAttributes();
083:
084: String action = getTask().getAction();
085:
086: // check that the password has been set
087: if ((getPassword() == null)) {
088: throw new BuildException(
089: "The password attribute must be set.");
090: }
091:
092: // check for missing application on deploy & update
093: if ((action.equals(ACTION_DEPLOY) || action
094: .equals(ACTION_UPDATE))
095: && application == null) {
096: throw new BuildException(
097: "The application attribute must be set "
098: + "if action = " + action);
099: }
100:
101: // check for missing source on deploy & update
102: if ((action.equals(ACTION_DEPLOY) || action
103: .equals(ACTION_UPDATE))
104: && getTask().getSource() == null) {
105: throw new BuildException(
106: "The source attribute must be set if "
107: + "action = " + action);
108: }
109:
110: // check for missing application on delete & undeploy
111: if ((action.equals(ACTION_DELETE) || action
112: .equals(ACTION_UNDEPLOY))
113: && application == null) {
114: throw new BuildException(
115: "The application attribute must be set if "
116: + "action = " + action);
117: }
118: }
119:
120: /**
121: * Builds the arguments to pass to weblogic.deploy according to the
122: * supplied action.
123: * @return A String containing the arguments for the weblogic.deploy tool.
124: * @throws BuildException if there is an error.
125: */
126: public String getArguments() throws BuildException {
127: String action = getTask().getAction();
128: String args = null;
129:
130: if (action.equals(ACTION_DEPLOY)
131: || action.equals(ACTION_UPDATE)) {
132: args = buildDeployArgs();
133: } else if (action.equals(ACTION_DELETE)
134: || action.equals(ACTION_UNDEPLOY)) {
135: args = buildUndeployArgs();
136: } else if (action.equals(ACTION_LIST)) {
137: args = buildListArgs();
138: }
139:
140: return args;
141: }
142:
143: /**
144: * Determines if the action supplied is valid.
145: * <p>Valid actions are contained in the static array VALID_ACTIONS
146: * @return true if the action attribute is valid, false if not.
147: */
148: protected boolean isActionValid() {
149: boolean valid = false;
150:
151: String action = getTask().getAction();
152:
153: for (int i = 0; i < VALID_ACTIONS.length; i++) {
154: if (action.equals(VALID_ACTIONS[i])) {
155: valid = true;
156: break;
157: }
158: }
159:
160: return valid;
161: }
162:
163: /**
164: * Builds the prefix arguments to pass to weblogic.deploy.
165: * These arguments are generic across all actions.
166: * @return A StringBuffer containing the prefix arguments.
167: * The action-specific build methods will append to this StringBuffer.
168: */
169: protected StringBuffer buildArgsPrefix() {
170: ServerDeploy task = getTask();
171: // constructs the "-url <url> -debug <action> <password>" portion
172: // of the commmand line
173: return new StringBuffer(1024).append(
174: (getServer() != null) ? "-url " + getServer() : "")
175: .append(" ").append(debug ? "-debug " : "").append(
176: (getUserName() != null) ? "-username "
177: + getUserName() : "").append(" ")
178: .append(task.getAction()).append(" ").append(
179: getPassword()).append(" ");
180: }
181:
182: /**
183: * Builds the arguments to pass to weblogic.deploy for deployment actions
184: * ("deploy" and "update").
185: * @return A String containing the full argument string for weblogic.deploy.
186: */
187: protected String buildDeployArgs() {
188: String args = buildArgsPrefix().append(application).append(" ")
189: .append(getTask().getSource()).toString();
190:
191: if (component != null) {
192: args = "-component " + component + " " + args;
193: }
194:
195: return args;
196: }
197:
198: /**
199: * Builds the arguments to pass to weblogic.deploy for undeployment actions
200: * ("undeploy" and "delete").
201: * @return A String containing the full argument string for weblogic.deploy.
202: */
203: protected String buildUndeployArgs() {
204: return buildArgsPrefix().append(application).append(" ")
205: .toString();
206: }
207:
208: /**
209: * Builds the arguments to pass to weblogic.deploy for the list action
210: * @return A String containing the full argument string for weblogic.deploy.
211: */
212: protected String buildListArgs() {
213: return buildArgsPrefix().toString();
214: }
215:
216: /**
217: * If set to true, additional information will be
218: * printed during the deployment process; optional.
219: * @param debug A boolean representing weblogic.deploy "-debug" flag.
220: */
221: public void setDebug(boolean debug) {
222: this .debug = debug;
223: }
224:
225: /**
226: * The name of the application being deployed; required.
227: * @param application A String representing the application portion of the
228: * weblogic.deploy command line.
229: */
230: public void setApplication(String application) {
231: this .application = application;
232: }
233:
234: /**
235: * the component string for the deployment targets; optional.
236: * It is in the form <code><component>:<target1>,<target2>...</code>
237: * Where component is the archive name (minus the .jar, .ear, .war
238: * extension). Targets are the servers where the components will be deployed
239:
240: * @param component A String representing the value of the "-component"
241: * argument of the weblogic.deploy command line argument.
242: */
243: public void setComponent(String component) {
244: this.component = component;
245: }
246: }
|