KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > sun > appserv > ComponentTask


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 /*
25  * The Apache Software License, Version 1.1
26  *
27  * Copyright (c) 1999 The Apache Software Foundation. All rights
28  * reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without
31  * modification, are permitted provided that the following conditions
32  * are met:
33  *
34  * 1. Redistributions of source code must retain the above copyright
35  * notice, this list of conditions and the following disclaimer.
36  *
37  * 2. Redistributions in binary form must reproduce the above copyright
38  * notice, this list of conditions and the following disclaimer in
39  * the documentation and/or other materials provided with the
40  * distribution.
41  *
42  * 3. The end-user documentation included with the redistribution, if
43  * any, must include the following acknowlegement:
44  * "This product includes software developed by the
45  * Apache Software Foundation (http://www.apache.org/)."
46  * Alternately, this acknowlegement may appear in the software itself,
47  * if and wherever such third-party acknowlegements normally appear.
48  *
49  * 4. The names "The Jakarta Project", "Ant", and "Apache Software
50  * Foundation" must not be used to endorse or promote products derived
51  * from this software without prior written permission. For written
52  * permission, please contact apache@apache.org.
53  *
54  * 5. Products derived from this software may not be called "Apache"
55  * nor may "Apache" appear in their names without prior written
56  * permission of the Apache Group.
57  *
58  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
59  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
61  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
62  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
63  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
64  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
65  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
66  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
67  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
68  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  * ====================================================================
71  *
72  * This software consists of voluntary contributions made by many
73  * individuals on behalf of the Apache Software Foundation. For more
74  * information on the Apache Software Foundation, please see
75  * <http://www.apache.org/>.
76  */

77
78 package org.apache.tools.ant.taskdefs.optional.sun.appserv;
79
80 import org.apache.tools.ant.BuildException;
81
82 import java.util.Map JavaDoc;
83 import java.util.HashMap JavaDoc;
84
85 /**
86  * This task enables or disables J2EE components which have been deployed to the
87  * Sun ONE Application Server 7. The following components may be enabled or
88  * disabled:
89  * <ul>
90  * <li>Enterprise application (EAR file)
91  * <li>Web application (WAR file)
92  * <li>Enterprise Java Bean (EJB-JAR file)
93  * <li>Enterprise connector (RAR file)
94  * <li>Application client
95  * </ul>
96  * <p>
97  * The archive is not required to enable or disable a component -- only the
98  * component name is required. The component archive may be used, however, as
99  * it implies the component name.
100  *
101  * In addition to the server-based and component-based attributes, this task
102  * introduces one attribute:
103  * <ul>
104  * <li><i>action</i> -- The command for the application server. Valid
105  * values are "enable" and "disable"
106  * </ul>
107  * <p>
108  *
109  * @see AppServerAdmin
110  * @see ComponentAdmin
111  * @author Greg Nelson <a HREF="mailto:gn@sun.com">gn@sun.com</a>
112  */

113 public class ComponentTask extends ComponentAdmin {
114     private String JavaDoc action; // action to take -- enable or disable
115

116     LocalStringsManager lsm = new LocalStringsManager();
117
118     /*
119      * Constants for both of the actions. In addition, both action strings are
120      * mapped to their appropriate commands in the Sun ONE Application Server CLI
121      */

122     private static final String JavaDoc ACTION_ENABLE = "enable";
123     private static final String JavaDoc ACTION_DISABLE = "disable";
124
125     private static final Map JavaDoc ACTION_MAP = new HashMap JavaDoc(2);
126     static {
127         ACTION_MAP.put(ACTION_ENABLE, "enable");
128         ACTION_MAP.put(ACTION_DISABLE, "disable");
129     };
130
131     /**
132      * Sets the action for the component command.
133      *
134      * @param action The action for the component command.
135      */

136     public void setAction(String JavaDoc action) {
137         this.action = action;
138     }
139
140     protected void checkComponentConfig(Server aServer, Component comp)
141             throws BuildException {
142         super.checkComponentConfig(aServer, comp);
143
144         if (action == null) {
145             final String JavaDoc msg = lsm.getString("ActionCommandMustBeSpecified");
146             throw new BuildException(msg, getLocation());
147         }
148
149         if (!ACTION_MAP.containsKey(action)) {
150             final String JavaDoc msg = lsm.getString("InvalidActionCommand", new Object JavaDoc[] {action});
151             throw new BuildException(msg, getLocation());
152         }
153
154         // name must be valid string
155
String JavaDoc theName = comp.getName();
156         if ((theName == null) || (theName.length() == 0)) {
157             final String JavaDoc msg = lsm.getString("InvalidComponentName", new Object JavaDoc[] {theName});
158             throw new BuildException(msg, getLocation());
159         }
160     }
161
162     protected String JavaDoc getCommandString(Server server, Component comp) {
163         StringBuffer JavaDoc cmdString = new StringBuffer JavaDoc();
164         cmdString.append(ACTION_MAP.get(action));
165         cmdString.append(server.getCommandParameters(true));
166         if (comp.getType() != null) {
167             cmdString.append(" --type ").append(comp.getType());
168         }
169
170         // check the value and append target
171
String JavaDoc lTarget = comp.getTarget();
172         if ((lTarget != null) && (lTarget.length() > 0)) {
173                 cmdString.append(" --target ").append(lTarget);
174         }
175
176         cmdString.append(" ").append(comp.getName());
177
178         return cmdString.toString();
179     }
180 }
181
Popular Tags