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.types.Path;
023:
024: /**
025: * Abstract class to support vendor-specific hot deployment tools.
026: * This class will validate boilerplate attributes.
027: *
028: * Subclassing this class for a vendor specific tool involves the
029: * following.
030: * <ol><li>Implement the <code>isActionValid()<code> method to insure the
031: * action supplied as the "action" attribute of ServerDeploy is valid.
032: * <li>Implement the <code>validateAttributes()</code> method to insure
033: * all required attributes are supplied, and are in the correct format.
034: * <li>Add a <code>add<TOOL></code> method to the ServerDeploy
035: * class. This method will be called when Ant encounters a
036: * <code>add<TOOL></code> task nested in the
037: * <code>serverdeploy</code> task.
038: * <li>Define the <code>deploy</code> method. This method should perform
039: * whatever task it takes to hot-deploy the component. IE: spawn a JVM and
040: * run class, exec a native executable, run Java code...
041: *
042: * @see org.apache.tools.ant.taskdefs.optional.j2ee.HotDeploymentTool
043: * @see org.apache.tools.ant.taskdefs.optional.j2ee.ServerDeploy
044: */
045: public abstract class AbstractHotDeploymentTool implements
046: HotDeploymentTool {
047: /** The parent task **/
048: private ServerDeploy task;
049:
050: /** The classpath passed to the JVM on execution. **/
051: private Path classpath;
052:
053: /** The username for the deployment server. **/
054: private String userName;
055:
056: /** The password for the deployment server. **/
057: private String password;
058:
059: /** The address of the deployment server **/
060: private String server;
061:
062: /**
063: * Add a classpath as a nested element.
064: * @return A Path object representing the classpath to be used.
065: */
066: public Path createClasspath() {
067: if (classpath == null) {
068: classpath = new Path(task.getProject());
069: }
070: return classpath.createPath();
071: }
072:
073: /**
074: * Determines if the "action" attribute defines a valid action.
075: * <p>Subclasses should determine if the action passed in is
076: * supported by the vendor's deployment tool.
077: * <p>Actions may by "deploy", "delete", etc... It all depends
078: * on the tool.
079: * @return true if the "action" attribute is valid, false if not.
080: */
081: protected abstract boolean isActionValid();
082:
083: /**
084: * Validates the passed in attributes.
085: * Subclasses should chain to this super-method to insure
086: * validation of boilerplate attributes.
087: * <p>Only the "action" attribute is required in the
088: * base class. Subclasses should check attributes accordingly.
089: * @exception org.apache.tools.ant.BuildException if the attributes are invalid or incomplete.
090: */
091: public void validateAttributes() throws BuildException {
092: if (task.getAction() == null) {
093: throw new BuildException(
094: "The \"action\" attribute must be set");
095: }
096:
097: if (!isActionValid()) {
098: throw new BuildException("Invalid action \""
099: + task.getAction() + "\" passed");
100: }
101:
102: if (classpath == null) {
103: throw new BuildException(
104: "The classpath attribute must be set");
105: }
106: }
107:
108: /**
109: * Perform the actual deployment.
110: * It's up to the subclasses to implement the actual behavior.
111: * @exception org.apache.tools.ant.BuildException if the attributes are invalid or incomplete.
112: */
113: public abstract void deploy() throws BuildException;
114:
115: /**
116: * Sets the parent task.
117: * @param task a ServerDeploy object representing the parent task.
118: * @ant.attribute ignore="true"
119: */
120: public void setTask(ServerDeploy task) {
121: this .task = task;
122: }
123:
124: /**
125: * Returns the task field, a ServerDeploy object.
126: * @return An ServerDeploy representing the parent task.
127: */
128: protected ServerDeploy getTask() {
129: return task;
130: }
131:
132: /**
133: * gets the classpath field.
134: * @return A Path representing the "classpath" attribute.
135: */
136: public Path getClasspath() {
137: return classpath;
138: }
139:
140: /**
141: * The classpath to be passed to the JVM running the tool;
142: * optional depending upon the tool.
143: * The classpath may also be supplied as a nested element.
144: * @param classpath A Path object representing the "classpath" attribute.
145: */
146: public void setClasspath(Path classpath) {
147: this .classpath = classpath;
148: }
149:
150: /**
151: * Returns the userName field.
152: * @return A String representing the "userName" attribute.
153: */
154: public String getUserName() {
155: return userName;
156: }
157:
158: /**
159: * The user with privileges to deploy applications to the server; optional.
160: * @param userName A String representing the "userName" attribute.
161: */
162: public void setUserName(String userName) {
163: this .userName = userName;
164: }
165:
166: /**
167: * Returns the password field.
168: * @return A String representing the "password" attribute.
169: */
170: public String getPassword() {
171: return password;
172: }
173:
174: /**
175: * The password of the user; optional.
176: * @param password A String representing the "password" attribute.
177: */
178: public void setPassword(String password) {
179: this .password = password;
180: }
181:
182: /**
183: * Returns the server field.
184: * @return A String representing the "server" attribute.
185: */
186: public String getServer() {
187: return server;
188: }
189:
190: /**
191: * The address or URL for the server where the component will be deployed.
192: * @param server A String representing the "server" attribute.
193: */
194: public void setServer(String server) {
195: this.server = server;
196: }
197: }
|