KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.tools.ant.Project;
82
83 import java.io.File JavaDoc;
84
85 /**
86  * This task enables arbitrary administrative commands and scripts to be
87  * executed on the Sun ONE Application Server 7. This is useful for cases where
88  * a specific Ant task hasn't been developed or a set of related commands has
89  * been composed in a single script.
90  *
91  * In addition to the server-based attributes, this task introduces three
92  * attribute:
93  * <ul>
94  * <li><i>command</i> -- The command to execute. If the "user", "password", "passwordfile",
95  * "host", "port" or "instance" attributes are also
96  * specified, they will automatically be inserted into
97  * the command before it is exectuted. If any of
98  * these options is specified in the command string,
99  * the corresponding attribute value will be ignored
100  * <li><i>commandfile</i> -- The command script to execute. If the "user",
101  * "password", "passwordfile", "host", "port" or "instance"
102  * attributes are also specified, they will
103  * automatically be inserted into the command
104  * environment before the script is exectuted
105  * <li><i>explicitcommand</i> -- The exact command to execute. No
106  * processing of the command is done, and all
107  * other attributes are ignored
108  * </ul>
109  * <p>
110  *
111  * @see AppServerAdmin
112  * @author Greg Nelson <a HREF="mailto:gn@sun.com">gn@sun.com</a>
113  */

114 public class AdminTask extends AppServerAdmin {
115     private String JavaDoc command;
116     private String JavaDoc explicitCommand;
117     private File JavaDoc commandFile;
118     LocalStringsManager lsm = new LocalStringsManager();
119
120     /**
121      * Sets the command to be executed. The task will automatically add user,
122      * password or passwordfile, host, port, and instance parameters if they're not specified in
123      * the command.
124      *
125      * @param command The command to be executed
126      */

127     public void setCommand(String JavaDoc command) {
128         this.command = command.trim();
129     }
130
131     /**
132      * Sets the command to be executed. The task will execute the command as
133      * specified and will NOT automatically add user, password or passwordfile, host, port and
134      * instance parameters
135      *
136      * @param command The command to be executed
137      */

138     public void setExplicitcommand(String JavaDoc explicitCommand) {
139         this.explicitCommand = explicitCommand;
140     }
141
142     /**
143      * Sets the command file which contains zero or more administrative commands
144      * which will be read and executed. The user, password or passwordfile, host, port, and
145      * instance parameters will automatically be added to the environment when
146      * the command is executed
147      *
148      * @param commandFile The command file to be executed
149      */

150     public void setCommandfile(File JavaDoc commandFile) {
151             final String JavaDoc msg = lsm.getString("DeprecatedAttribute",
152                                                 new Object JavaDoc[] {"commandfile",
153                                                               "- 'multimode --file <commandfile>'"});
154             log(msg, Project.MSG_WARN);
155             this.commandFile = commandFile;
156     }
157
158     public void execute() throws BuildException {
159         checkCommandCount();
160
161         if ((command != null) && (servers.size() == 0) && (server == null)) {
162             // No other attributes or server elements -- treat as explicitCommand // FIXME -- this will no longer ever happen
163
explicitCommand = command;
164             command = null;
165         }
166
167         if (explicitCommand != null) {
168             // Normal execution process is skipped and command is run immediately
169
execAdminCommand(explicitCommand);
170         } else {
171             super.execute();
172         }
173     }
174
175     /**
176      * Verifies that one and only one of the command attributes (command,
177      * commandfile, and explicitcommand) has been set.
178      *
179      * @throws BuildException If none of the command attributes has been set or
180      * multiple command attributes has been set
181      */

182     private void checkCommandCount() throws BuildException {
183         int commandCount = 0;
184         
185         if (command != null) { commandCount++; }
186         if (explicitCommand != null) { commandCount++; }
187         if (commandFile != null) { commandCount++; }
188
189         if (commandCount != 1) {
190             final String JavaDoc msg = lsm.getString("ExactlyOneCommandAttribute");
191             throw new BuildException(msg, getLocation());
192         }
193     }
194
195     protected void checkConfiguration(Server aServer) throws BuildException {
196         // No error checking is done for this task
197
}
198
199     protected void execute(Server aServer) throws BuildException {
200         /*
201          * "command" and "commandfile" attributes are processed in this method.
202          * See the execute() method for "explicitcommand" processing.
203          */

204
205         final String JavaDoc userOption[] = {"--user ", "-u "};
206         final String JavaDoc passwordOption[] = {"--password ", "-w ", "--passwordfile "};
207         final String JavaDoc hostOption[] = {"--host ", "-H "};
208         final String JavaDoc portOption[] = {"--port ", "-p "};
209         final String JavaDoc instanceOption[] = {"--instance ", "-i "};
210         final String JavaDoc secureOption[] = {"--secure ", "-s "};
211
212         StringBuffer JavaDoc cmd;
213         if (command != null) {
214             cmd = new StringBuffer JavaDoc(command);
215             if (!commandIncludes(cmd, userOption)) {
216                 insertCommandOption(cmd, " --user " + aServer.getUser());
217             }
218             if ((aServer.hasPassword())
219                                 && (!commandIncludes(cmd, passwordOption))) {
220                 insertCommandOption(cmd, aServer.getPasswordCommand());
221             }
222             if (!commandIncludes(cmd, hostOption)) {
223                 String JavaDoc theHost = aServer.getHost();
224                 if (theHost == null) {
225                     theHost = Server.DEFAULT_HOST;
226                 }
227                 insertCommandOption(cmd, " --host " + theHost);
228             }
229             if (!commandIncludes(cmd, portOption)) {
230                 String JavaDoc thePort = (aServer.getPort() == 0) ?
231                                     Server.DEFAULT_PORT :
232                                     String.valueOf(aServer.getPort());
233                 insertCommandOption(cmd, " --port " + thePort);
234             }
235             if ((aServer.getInstance() != null)
236                                 && (!commandIncludes(cmd, instanceOption))) {
237                 insertCommandOption(cmd, " --instance " + aServer.getInstance());
238             }
239             if (aServer.getSecure() != null &&
240                 !commandIncludes(cmd, secureOption)) {
241                 insertCommandOption(cmd, " --secure=" + aServer.getSecure());
242             }
243
244         } else {
245             String JavaDoc filename;
246             try
247             {
248                 filename = commandFile.getCanonicalPath();
249             }
250             catch(Exception JavaDoc e)
251             {
252                 filename = commandFile.getAbsolutePath();
253             }
254             // passing in '\\' for Windows would make CLI choke!
255
filename = filename.replace('\\', '/');
256
257             cmd = new StringBuffer JavaDoc("multimode --file " + filename + " ");
258         }
259
260         execAdminCommand(cmd.toString());
261     }
262
263     /**
264      * Utility method that determines if command options have already been
265      * specified in the command line
266      *
267      * @param cmd The command line to search.
268      * @param options The options to search for.
269      * @return <code>true</code> if one of the options is found in the command
270      * line
271      */

272     private boolean commandIncludes(StringBuffer JavaDoc cmd, String JavaDoc options[]) {
273         for (int i = 0; i < options.length; i++) {
274             if (cmd.indexOf(options[i]) > 0) {
275                 return true;
276             }
277         }
278         return false;
279     }
280
281     /**
282      * Utility method that inserts a string into the current command line. The
283      * string is inserted after the command name.
284      *
285      * @param cmdLine The command line.
286      * @param commandOption The string to insert into the command line.
287      */

288     private void insertCommandOption(StringBuffer JavaDoc cmdLine, String JavaDoc commandOption) {
289         int index = cmdLine.indexOf(" ");
290         index = (index >= 0) ? index : cmdLine.length();
291         cmdLine.insert(index, commandOption).append(' ');
292     }
293 }
294
Popular Tags