001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: JonasHotDeploymentTool.java 4695 2004-05-05 14:19:30Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.ant;
026:
027: import org.apache.tools.ant.BuildException;
028: import org.apache.tools.ant.Project;
029: import org.apache.tools.ant.taskdefs.Java;
030:
031: /**
032: * Class used to manage deployment on JOnAS (with ant and ServerDeploy task)
033: * @author Florent Benoit
034: */
035: public class JonasHotDeploymentTool extends BootstrapTask implements
036: HotDeploymentTool {
037:
038: /**
039: * Admin class (JonasAdmin class)
040: */
041: private static final String ADMIN_CLASS = "org.objectweb.jonas.adm.JonasAdmin";
042:
043: /**
044: * The parent task
045: */
046: private ServerDeploy task;
047:
048: /**
049: * All actions supported by JOnAS
050: */
051: private static final String[] VALID_ACTIONS = { ACTION_DELETE,
052: ACTION_DEPLOY, ACTION_LIST, ACTION_UNDEPLOY, ACTION_UPDATE };
053:
054: /**
055: * Validates the passed in attributes. Subclasses should chain to this
056: * super-method to insure validation of boilerplate attributes. <p>Only the
057: * "action" attribute is required in the base class. Subclasses should check
058: * attributes accordingly.
059: * @exception org.apache.tools.ant.BuildException if the attributes are
060: * invalid or incomplete.
061: */
062: public void validateAttributes() throws BuildException {
063: if (task.getAction() == null) {
064: throw new BuildException(
065: "The \"action\" attribute must be set");
066: }
067:
068: if (!isActionValid()) {
069: throw new BuildException("Invalid action \""
070: + task.getAction() + "\" passed");
071: }
072:
073: }
074:
075: /**
076: * Sets the parent task.
077: * @param task a ServerDeploy object representing the parent task.
078: * @ant.attribute ignore="true"
079: */
080: public void setTask(ServerDeploy task) {
081: this .task = task;
082: }
083:
084: /**
085: * Returns the task field, a ServerDeploy object.
086: * @return An ServerDeploy representing the parent task.
087: */
088: protected ServerDeploy getTask() {
089: return task;
090: }
091:
092: /**
093: * Determines if the "action" attribute defines a valid action. <p>
094: * Subclasses should determine if the action passed in is supported by the
095: * vendor's deployment tool. <p>Actions may by "deploy", "delete", etc...
096: * It all depends on the tool.
097: * @return true if the "action" attribute is valid, false if not.
098: */
099: protected boolean isActionValid() {
100:
101: String action = getTask().getAction();
102:
103: for (int i = 0; i < VALID_ACTIONS.length; i++) {
104: if (action.equals(VALID_ACTIONS[i])) {
105: return true;
106: }
107: }
108:
109: return false;
110: }
111:
112: /**
113: * Perform the actual deployment. It's up to the subclasses to implement the
114: * actual behavior.
115: * @exception org.apache.tools.ant.BuildException if the attributes are
116: * invalid or incomplete.
117: */
118: public void deploy() throws BuildException {
119:
120: String action = getTask().getAction();
121: Java bootstrapTask = getBootstraptask(ADMIN_CLASS);
122: String fileName = getTask().getSource().getPath();
123:
124: if (action.equals(ACTION_DEPLOY)
125: || action.equals(ACTION_UPDATE)) {
126: bootstrapTask.setTaskName("JOnAS/Deploy");
127: bootstrapTask.createArg().setValue("-a");
128: bootstrapTask.createArg().setValue(fileName);
129: bootstrapTask.log("Deploying '" + fileName + "'...",
130: Project.MSG_INFO);
131: } else if (action.equals(ACTION_DELETE)
132: || action.equals(ACTION_UNDEPLOY)) {
133: bootstrapTask.setTaskName("JOnAS/Undeploy");
134: bootstrapTask.createArg().setValue("-r");
135: bootstrapTask.createArg().setValue(fileName);
136: bootstrapTask.log("Undeploying '" + fileName + "'...",
137: Project.MSG_INFO);
138: } else if (action.equals(ACTION_LIST)) {
139: bootstrapTask.setTaskName("JOnAS/List");
140: bootstrapTask.createArg().setValue("-l");
141: getTask().log("Listing beans ...", Project.MSG_INFO);
142: } else {
143: throw new BuildException("Invalid action \"" + action
144: + "\" passed");
145: }
146:
147: bootstrapTask.createArg().setValue("-n");
148: bootstrapTask.createArg().setValue(getServerName());
149: bootstrapTask.executeJava();
150: }
151:
152: }
|