KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > iplanet > InstanceTask


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package org.apache.tools.ant.taskdefs.optional.iplanet;
25
26 import org.apache.tools.ant.BuildException;
27
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 public class InstanceTask extends IasAdmin {
32     private String JavaDoc action;
33
34     private static final String JavaDoc ACTION_START = "start";
35     private static final String JavaDoc ACTION_STOP = "stop";
36     private static final String JavaDoc ACTION_RESTART = "restart";
37     private static final String JavaDoc ACTION_CREATE = "create";
38     private static final String JavaDoc ACTION_DESTROY = "destroy";
39
40     private static final Map JavaDoc ACTION_MAP = new HashMap JavaDoc(5);
41     static {
42         ACTION_MAP.put(ACTION_START, "start-instance");
43         ACTION_MAP.put(ACTION_STOP, "stop-instance");
44         ACTION_MAP.put(ACTION_RESTART, null);
45         ACTION_MAP.put(ACTION_CREATE, "create-instance");
46         ACTION_MAP.put(ACTION_DESTROY, "delete-instance");
47     };
48
49     public void setAction(String JavaDoc action) {
50         this.action = action;
51     }
52
53     protected void checkConfiguration() throws BuildException {
54         super.checkConfiguration();
55     
56         if (action == null) {
57             String JavaDoc msg = "The action command must be specified.";
58             throw new BuildException(msg, getLocation());
59         }
60
61         if (!ACTION_MAP.containsKey(action)) {
62
63             String JavaDoc msg = "The action command (\"" + action + "\") is invalid.";
64             throw new BuildException(msg, getLocation());
65         }
66     }
67
68     protected void checkConfiguration(Server aServer) throws BuildException {
69         super.checkConfiguration(aServer);
70         if ((action.equals(ACTION_CREATE) || action.equals(ACTION_DESTROY)) &&
71                 (aServer.getInstance() == null)) {
72             String JavaDoc msg = "When creating or destroying an application server "
73                             + "instance, the \"instance\" attribute is required.";
74             throw new BuildException(msg, getLocation());
75         }
76     }
77
78     protected void execute(Server aServer) throws BuildException {
79         if (action.equals(ACTION_RESTART)) {
80             execute(ACTION_STOP, aServer);
81             execute(ACTION_START, aServer);
82         } else {
83             execute(action, aServer);
84         }
85     }
86
87     private void execute(String JavaDoc anAction, Server aServer) throws BuildException {
88         String JavaDoc cmdString = (String JavaDoc) ACTION_MAP.get(anAction);
89         cmdString +=aServer.getCommandParameters(false);
90         if (anAction.equals(ACTION_CREATE)) {
91             cmdString += " --instanceport " + aServer.getInstanceport();
92         }
93         if (aServer.getInstance() != null) {
94             cmdString += " " + aServer.getInstance();
95         }
96         execIasCommand(cmdString);
97     }
98 }
Popular Tags