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 |
|
---|
18 | package org.apache.tools.ant.taskdefs.optional.j2ee;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.taskdefs.Java;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * An Ant wrapper task for the weblogic.deploy tool. This is used to
|
---|
25 | * hot-deploy J2EE applications to a running WebLogic server.
|
---|
26 | * This is <b>not</b> the same as creating the application archive.
|
---|
27 | * This task assumes the archive (EAR, JAR, or WAR) file has been
|
---|
28 | * assembled and is supplied as the "source" attribute.
|
---|
29 | * <p>In the end, this task assembles the commadline parameters
|
---|
30 | * and runs the weblogic.deploy tool in a seperate JVM.
|
---|
31 | *
|
---|
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 WEBLOGIC_DEPLOY_CLASS_NAME = "weblogic.deploy";
|
---|
41 |
|
---|
42 | /** All the valid actions that weblogic.deploy permits **/
|
---|
43 | private static final String[] 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 application;
|
---|
51 |
|
---|
52 | /** The component name:target(s) for the "-component" argument of weblogic.deploy **/
|
---|
53 | private String 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 = (Java) getTask().getProject().createTask("java");
|
---|
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 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 | // check for missing application on deploy & update
|
---|
91 | if ((action.equals(ACTION_DEPLOY) || action.equals(ACTION_UPDATE))
|
---|
92 | && application == null)
|
---|
93 | throw new BuildException("The application attribute must be set "
|
---|
94 | + "if action = " + action);
|
---|
95 |
|
---|
96 | // check for missing source on deploy & update
|
---|
97 | if ((action.equals(ACTION_DEPLOY) || action.equals(ACTION_UPDATE))
|
---|
98 | && getTask().getSource() == null)
|
---|
99 | throw new BuildException("The source attribute must be set if "
|
---|
100 | + "action = " + action);
|
---|
101 |
|
---|
102 | // check for missing application on delete & undeploy
|
---|
103 | if ((action.equals(ACTION_DELETE) || action.equals(ACTION_UNDEPLOY))
|
---|
104 | && application == null)
|
---|
105 | throw new BuildException("The application attribute must be set if "
|
---|
106 | + "action = " + action);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Builds the arguments to pass to weblogic.deploy according to the
|
---|
111 | * supplied action.
|
---|
112 | * @return A String containing the arguments for the weblogic.deploy tool.
|
---|
113 | */
|
---|
114 | public String getArguments() throws BuildException {
|
---|
115 | String action = getTask().getAction();
|
---|
116 | String args = null;
|
---|
117 |
|
---|
118 | if (action.equals(ACTION_DEPLOY) || action.equals(ACTION_UPDATE))
|
---|
119 | args = buildDeployArgs();
|
---|
120 | else if (action.equals(ACTION_DELETE) || action.equals(ACTION_UNDEPLOY))
|
---|
121 | args = buildUndeployArgs();
|
---|
122 | else if (action.equals(ACTION_LIST))
|
---|
123 | args = buildListArgs();
|
---|
124 |
|
---|
125 | return args;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Determines if the action supplied is valid.
|
---|
130 | * <p>Valid actions are contained in the static array VALID_ACTIONS
|
---|
131 | * @return true if the action attribute is valid, false if not.
|
---|
132 | */
|
---|
133 | protected boolean isActionValid() {
|
---|
134 | boolean valid = false;
|
---|
135 |
|
---|
136 | String action = getTask().getAction();
|
---|
137 |
|
---|
138 | for (int i = 0; i < VALID_ACTIONS.length; i++) {
|
---|
139 | if (action.equals(VALID_ACTIONS[i])) {
|
---|
140 | valid = true;
|
---|
141 | break;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | return valid;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Builds the prefix arguments to pass to weblogic.deploy.
|
---|
150 | * These arguments are generic across all actions.
|
---|
151 | * @return A StringBuffer containing the prefix arguments.
|
---|
152 | * The action-specific build methods will append to this StringBuffer.
|
---|
153 | */
|
---|
154 | protected StringBuffer buildArgsPrefix() {
|
---|
155 | ServerDeploy task = getTask();
|
---|
156 | // constructs the "-url <url> -debug <action> <password>" portion
|
---|
157 | // of the commmand line
|
---|
158 | return new StringBuffer(1024)
|
---|
159 | .append((getServer() != null)
|
---|
160 | ? "-url " + getServer()
|
---|
161 | : "")
|
---|
162 | .append(" ")
|
---|
163 | .append(debug ? "-debug " : "")
|
---|
164 | .append((getUserName() != null)
|
---|
165 | ? "-username " + getUserName()
|
---|
166 | : "")
|
---|
167 | .append(" ")
|
---|
168 | .append(task.getAction()).append(" ")
|
---|
169 | .append(getPassword()).append(" ");
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Builds the arguments to pass to weblogic.deploy for deployment actions
|
---|
174 | * ("deploy" and "update").
|
---|
175 | * @return A String containing the full argument string for weblogic.deploy.
|
---|
176 | */
|
---|
177 | protected String buildDeployArgs() {
|
---|
178 | String args = buildArgsPrefix()
|
---|
179 | .append(application).append(" ")
|
---|
180 | .append(getTask().getSource())
|
---|
181 | .toString();
|
---|
182 |
|
---|
183 | if (component != null) {
|
---|
184 | args = "-component " + component + " " + args;
|
---|
185 | }
|
---|
186 |
|
---|
187 | return args;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Builds the arguments to pass to weblogic.deploy for undeployment actions
|
---|
192 | * ("undeploy" and "delete").
|
---|
193 | * @return A String containing the full argument string for weblogic.deploy.
|
---|
194 | */
|
---|
195 | protected String buildUndeployArgs() {
|
---|
196 | return buildArgsPrefix()
|
---|
197 | .append(application).append(" ")
|
---|
198 | .toString();
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Builds the arguments to pass to weblogic.deploy for the list action
|
---|
203 | * @return A String containing the full argument string for weblogic.deploy.
|
---|
204 | */
|
---|
205 | protected String buildListArgs() {
|
---|
206 | return buildArgsPrefix()
|
---|
207 | .toString();
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * If set to true, additional information will be
|
---|
212 | * printed during the deployment process; optional.
|
---|
213 | * @param debug A boolean representing weblogic.deploy "-debug" flag.
|
---|
214 | */
|
---|
215 | public void setDebug(boolean debug) {
|
---|
216 | this.debug = debug;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * The name of the application being deployed; required.
|
---|
221 | * @param application A String representing the application portion of the
|
---|
222 | * weblogic.deploy command line.
|
---|
223 | */
|
---|
224 | public void setApplication(String application) {
|
---|
225 | this.application = application;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * the component string for the deployment targets; optional.
|
---|
230 | * It is in the form <code><component>:<target1>,<target2>...</code>
|
---|
231 | * Where component is the archive name (minus the .jar, .ear, .war
|
---|
232 | * extension). Targets are the servers where the components will be deployed
|
---|
233 |
|
---|
234 | * @param component A String representing the value of the "-component"
|
---|
235 | * argument of the weblogic.deploy command line argument.
|
---|
236 | */
|
---|
237 | public void setComponent(String component) {
|
---|
238 | this.component = component;
|
---|
239 | }
|
---|
240 | }
|
---|