KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > j2ee > WebLogicHotDeploymentTool


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional.j2ee;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.taskdefs.Java;
23
24 /**
25  * An Ant wrapper task for the weblogic.deploy tool. This is used to
26  * hot-deploy J2EE applications to a running WebLogic server.
27  * This is <b>not</b> the same as creating the application archive.
28  * This task assumes the archive (EAR, JAR, or WAR) file has been
29  * assembled and is supplied as the "source" attribute.
30  * <p>In the end, this task assembles the commadline parameters
31  * and runs the weblogic.deploy tool in a seperate JVM.
32  *
33  * @see org.apache.tools.ant.taskdefs.optional.j2ee.HotDeploymentTool
34  * @see org.apache.tools.ant.taskdefs.optional.j2ee.AbstractHotDeploymentTool
35  * @see org.apache.tools.ant.taskdefs.optional.j2ee.ServerDeploy
36  */

37 public class WebLogicHotDeploymentTool extends AbstractHotDeploymentTool
38                                        implements HotDeploymentTool {
39     /** The classname of the tool to run **/
40     private static final String JavaDoc WEBLOGIC_DEPLOY_CLASS_NAME = "weblogic.deploy";
41
42     /** All the valid actions that weblogic.deploy permits **/
43     private static final String JavaDoc[] VALID_ACTIONS
44         = {ACTION_DELETE, ACTION_DEPLOY, ACTION_LIST, ACTION_UNDEPLOY, ACTION_UPDATE};
45
46     /** Represents the "-debug" flag from weblogic.deploy **/
47     private boolean debug;
48
49     /** The application name that is being deployed **/
50     private String JavaDoc application;
51
52     /** The component name:target(s) for the "-component" argument of weblogic.deploy **/
53     private String JavaDoc component;
54
55     /**
56      * Perform the actual deployment.
57      * For this implementation, a JVM is spawned and the weblogic.deploy
58      * tools is executed.
59      * @exception org.apache.tools.ant.BuildException if the attributes are invalid or incomplete.
60      */

61     public void deploy() {
62         Java java = new Java(getTask());
63         java.setFork(true);
64         java.setFailonerror(true);
65         java.setClasspath(getClasspath());
66
67         java.setClassname(WEBLOGIC_DEPLOY_CLASS_NAME);
68         java.createArg().setLine(getArguments());
69         java.execute();
70     }
71
72     /**
73      * Validates the passed in attributes.
74      * <p>The rules are:
75      * <ol><li>If action is "deploy" or "update" the "application" and "source"
76      * attributes must be supplied.
77      * <li>If action is "delete" or "undeploy" the "application" attribute must
78      * be supplied.
79      * @exception org.apache.tools.ant.BuildException if the attributes are invalid or incomplete
80      */

81     public void validateAttributes() throws BuildException {
82         super.validateAttributes();
83
84         String JavaDoc action = getTask().getAction();
85
86         // check that the password has been set
87
if ((getPassword() == null)) {
88             throw new BuildException("The password attribute must be set.");
89         }
90
91         // check for missing application on deploy & update
92
if ((action.equals(ACTION_DEPLOY) || action.equals(ACTION_UPDATE))
93             && application == null) {
94             throw new BuildException("The application attribute must be set "
95                 + "if action = " + action);
96         }
97
98         // check for missing source on deploy & update
99
if ((action.equals(ACTION_DEPLOY) || action.equals(ACTION_UPDATE))
100             && getTask().getSource() == null) {
101             throw new BuildException("The source attribute must be set if "
102                 + "action = " + action);
103         }
104
105         // check for missing application on delete & undeploy
106
if ((action.equals(ACTION_DELETE) || action.equals(ACTION_UNDEPLOY))
107             && application == null) {
108             throw new BuildException("The application attribute must be set if "
109                 + "action = " + action);
110         }
111     }
112
113     /**
114      * Builds the arguments to pass to weblogic.deploy according to the
115      * supplied action.
116      * @return A String containing the arguments for the weblogic.deploy tool.
117      * @throws BuildException if there is an error.
118      */

119     public String JavaDoc getArguments() throws BuildException {
120         String JavaDoc action = getTask().getAction();
121         String JavaDoc args = null;
122
123         if (action.equals(ACTION_DEPLOY) || action.equals(ACTION_UPDATE)) {
124             args = buildDeployArgs();
125         } else if (action.equals(ACTION_DELETE) || action.equals(ACTION_UNDEPLOY)) {
126             args = buildUndeployArgs();
127         } else if (action.equals(ACTION_LIST)) {
128             args = buildListArgs();
129         }
130
131         return args;
132     }
133
134     /**
135      * Determines if the action supplied is valid.
136      * <p>Valid actions are contained in the static array VALID_ACTIONS
137      * @return true if the action attribute is valid, false if not.
138      */

139     protected boolean isActionValid() {
140         boolean valid = false;
141
142         String JavaDoc action = getTask().getAction();
143
144         for (int i = 0; i < VALID_ACTIONS.length; i++) {
145             if (action.equals(VALID_ACTIONS[i])) {
146                 valid = true;
147                 break;
148             }
149         }
150
151         return valid;
152     }
153
154     /**
155      * Builds the prefix arguments to pass to weblogic.deploy.
156      * These arguments are generic across all actions.
157      * @return A StringBuffer containing the prefix arguments.
158      * The action-specific build methods will append to this StringBuffer.
159      */

160     protected StringBuffer JavaDoc buildArgsPrefix() {
161         ServerDeploy task = getTask();
162         // constructs the "-url <url> -debug <action> <password>" portion
163
// of the commmand line
164
return new StringBuffer JavaDoc(1024)
165                 .append((getServer() != null)
166                     ? "-url " + getServer()
167                     : "")
168                 .append(" ")
169                 .append(debug ? "-debug " : "")
170                 .append((getUserName() != null)
171                     ? "-username " + getUserName()
172                     : "")
173                 .append(" ")
174                 .append(task.getAction()).append(" ")
175                 .append(getPassword()).append(" ");
176     }
177
178     /**
179      * Builds the arguments to pass to weblogic.deploy for deployment actions
180      * ("deploy" and "update").
181      * @return A String containing the full argument string for weblogic.deploy.
182      */

183     protected String JavaDoc buildDeployArgs() {
184         String JavaDoc args = buildArgsPrefix()
185                 .append(application).append(" ")
186                 .append(getTask().getSource())
187                 .toString();
188
189         if (component != null) {
190             args = "-component " + component + " " + args;
191         }
192
193         return args;
194     }
195
196     /**
197      * Builds the arguments to pass to weblogic.deploy for undeployment actions
198      * ("undeploy" and "delete").
199      * @return A String containing the full argument string for weblogic.deploy.
200      */

201     protected String JavaDoc buildUndeployArgs() {
202         return buildArgsPrefix()
203                 .append(application).append(" ")
204                 .toString();
205     }
206
207     /**
208      * Builds the arguments to pass to weblogic.deploy for the list action
209      * @return A String containing the full argument string for weblogic.deploy.
210      */

211     protected String JavaDoc buildListArgs() {
212         return buildArgsPrefix()
213                 .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 JavaDoc application) {
231         this.application = application;
232     }
233
234     /**
235      * the component string for the deployment targets; optional.
236      * It is in the form <code>&lt;component&gt;:&lt;target1&gt;,&lt;target2&gt;...</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 JavaDoc component) {
244         this.component = component;
245     }
246 }
247
Popular Tags