KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > sun > appserv > 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 /*
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.Project;
81 import org.apache.tools.ant.BuildException;
82
83 import java.util.Map JavaDoc;
84 import java.util.HashMap JavaDoc;
85
86 /**
87  * This task controls Sun ONE Application Server 7 instances. It supports
88  * starting, stopping, restarting, creating, and removing application server
89  * instances.
90  *
91  * In addition to the server-based attributes, this task introduces one
92  * attribute:
93  * <ul>
94  * <li><i>action</i> -- The control command for the application server.
95  * Valid values are "start", "stop", "restart",
96  * "create" and "delete". A restart will first send
97  * the stop command and subsequently send the start
98  * command
99  * </ul>
100  * <p>
101  *
102  * @see AppServerAdmin
103  * @author Greg Nelson <a HREF="mailto:gn@sun.com">gn@sun.com</a>
104  */

105 public class InstanceTask extends AppServerAdmin {
106     private String JavaDoc action = null;
107
108     /*
109      * Constants for the valid actions. In addition, the action strings are
110      * mapped to their appropriate commands in the Sun ONE Application Server CLI
111      */

112     private static final String JavaDoc ACTION_START = "start";
113     private static final String JavaDoc ACTION_STOP = "stop";
114     private static final String JavaDoc ACTION_RESTART = "restart";
115     private static final String JavaDoc ACTION_CREATE = "create";
116     private static final String JavaDoc ACTION_DELETE = "delete";
117
118     private static final Map JavaDoc ACTION_MAP = new HashMap JavaDoc(5);
119     static {
120         ACTION_MAP.put(ACTION_START, "start-instance");
121         ACTION_MAP.put(ACTION_STOP, "stop-instance");
122         ACTION_MAP.put(ACTION_RESTART, "restart-instance");
123         ACTION_MAP.put(ACTION_CREATE, "create-instance");
124         ACTION_MAP.put(ACTION_DELETE, "delete-instance");
125     };
126
127     LocalStringsManager lsm = new LocalStringsManager();
128
129
130     /**
131      * Sets the action for the instance command.
132      *
133      * @param action The action for the instance command.
134      */

135     public void setAction(String JavaDoc action) {
136         this.action = action;
137     }
138
139     protected Server getNewServer() {
140         return new InstanceServer(server);
141     }
142
143         /**
144          * Sets the nodeagent to be used by a new application server instance.
145          *
146          * @param nodeagent The nodeagent of the new application server
147          * instance
148          */

149         public void setNodeagent(String JavaDoc nodeagent) {
150             ((InstanceServer)server).setNodeagent(nodeagent); // Delegates to server object
151
}
152
153         /**
154          * Sets the property to be used by a new application server instance.
155          *
156          * @param property The nodeagent of the new application server
157          * instance
158          */

159         public void setProperty(String JavaDoc property) {
160             ((InstanceServer)server).setProperty(property); // Delegates to server object
161
}
162
163         /**
164          * Sets the config to be used by a new application server instance.
165          *
166          * @param config The config of the new application server
167          * instance
168          */

169         public void setConfig(String JavaDoc config) {
170             ((InstanceServer)server).setConfig(config); // Delegates to server object
171
}
172
173         /**
174          * Sets the cluster to be used by a new application server instance.
175          *
176          * @param cluster The cluster of the new application server
177          * instance
178          */

179         public void setCluster(String JavaDoc cluster) {
180             ((InstanceServer)server).setCluster(cluster); // Delegates to server object
181
}
182
183     /**
184      * Set if the instance should be started in debug mode. Defaults to
185      * <code>false</code>.
186      *
187      * @param debug If set to <code>true</code>, the instance will be
188      * in debug mode.
189      */

190     public void setDebug(boolean debug) {
191             final String JavaDoc msg = lsm.getString("AttributeNotSupported",
192                                                 new Object JavaDoc[] {"debug"});
193             log(msg, Project.MSG_WARN);
194             //((InstanceServer)server).setDebug(debug); // Delegates to server object
195
}
196
197     /**
198      * Set if the instance should be started without an admin server
199      * running.
200      *
201      * @param local If set to <code>true</code>, the instance will be
202      * started without the need for an admin server.
203      */

204     public void setLocal(boolean local) {
205             final String JavaDoc msg = lsm.getString("AttributeNotSupported",
206                                                 new Object JavaDoc[] {"local"});
207             log(msg, Project.MSG_WARN);
208             //((InstanceServer)server).setLocal(local); // Delegates to server object
209
}
210
211     /**
212      * Sets the domain for the administrative command -- some commands allow the
213      * domain to be set for "local" commands instead of username, password, host
214      * and password.
215      *
216      * @param domain The domain name for the administrative command.
217      */

218     public void setDomain(String JavaDoc domain) {
219             final String JavaDoc msg = lsm.getString("AttributeNotSupported",
220                                                 new Object JavaDoc[] {"domain"});
221             log(msg, Project.MSG_WARN);
222             //((InstanceServer)server).setDomain(domain); // Delegates to server object
223
}
224
225     /**
226      * Sets the port number to be used by a new application server instance.
227      *
228      * @param instanceport The port number of the new application server instance
229      */

230     public void setInstanceport(int instanceport) {
231             final String JavaDoc msg = lsm.getString("AttributeNotSupported",
232                                                 new Object JavaDoc[] {"instanceport"});
233             log(msg, Project.MSG_WARN);
234             //((InstanceServer)server).setInstanceport(instanceport);
235
}
236
237     protected void checkConfiguration() throws BuildException {
238             if (action == null) {
239                 final String JavaDoc msg = lsm.getString("ActionCommandMustBeSpecified");
240                 throw new BuildException(msg, getLocation());
241             }
242
243             if (!ACTION_MAP.containsKey(action)) {
244                 final String JavaDoc msg = lsm.getString("InvalidActionCommand", new Object JavaDoc[] {action});
245                 throw new BuildException(msg, getLocation());
246             }
247             super.checkConfiguration();
248         }
249
250     protected void checkConfiguration(Server aServer) throws BuildException {
251             if (aServer.getInstance() == null) {
252                 final String JavaDoc msg = lsm.getString("InstanceAttributeRequired");
253                 throw new BuildException(msg, getLocation());
254             }
255             InstanceServer instanceSvr = (InstanceServer) aServer;
256 /*
257         if (instanceSvr.isLocal() || (instanceSvr.getDomain() != null)) {
258             if (instanceSvr.getHost() != null) {
259                 final String msg = lsm.getString("HostAttributeIgnored");
260                 log(msg, Project.MSG_WARN);
261             }
262             if (instanceSvr.getPort() != 0) {
263                 final String msg = lsm.getString("PortAttributeIgnored");
264                 log(msg, Project.MSG_WARN);
265             }
266             if ((instanceSvr.getUser() != null) && (!instanceSvr.getUser().equals("admin"))) {
267                 final String msg = lsm.getString("UserAttributeIgnored");
268                 log(msg, Project.MSG_WARN);
269             }
270             if (instanceSvr.hasPassword()) {
271                 final String msg = lsm.getString("PasswordAttributeIgnored");
272                 log(msg, Project.MSG_WARN);
273             }
274         } else {
275             if (!instanceSvr.hasPassword()) {
276                 final String msg = lsm.getString("PasswordAttributeNotSpecified");
277                 throw new BuildException(msg, getLocation());
278             }
279         }
280
281         if ((instanceSvr.getDomain() != null) && !instanceSvr.isLocal()) {
282             final String msg = lsm.getString("DomainAttributeIgnored");
283             log(msg, Project.MSG_WARN);
284         }
285 */

286             if (!instanceSvr.hasPassword()) {
287                 final String JavaDoc msg = lsm.getString("PasswordAttributeNotSpecified");
288                 throw new BuildException(msg, getLocation());
289             }
290             if (action.equals(ACTION_CREATE)) {
291                 if (instanceSvr.getNodeagent() == null) {
292                     final String JavaDoc msg = lsm.getString("AttributeMustBeSpecified", new Object JavaDoc[] {"nodeagent"});
293                     throw new BuildException(msg, getLocation());
294                 }
295             }
296     }
297
298     protected void execute(Server aServer) throws BuildException {
299         InstanceServer instanceSvr = (InstanceServer) aServer;
300         
301         if ( instanceSvr.getConfig() != null &&
302              instanceSvr.getCluster() != null ) {
303             final String JavaDoc msg = lsm.getString("MutuallyExclusivelyAttribute",
304                                              new Object JavaDoc[] {"config", "cluster"});
305             throw new BuildException(msg, getLocation());
306         }
307
308         StringBuffer JavaDoc cmd = new StringBuffer JavaDoc((String JavaDoc) ACTION_MAP.get(action));
309                 cmd.append(instanceSvr.getCommandParameters(false));
310         if (action.equals(ACTION_CREATE)) {
311             //cmd.append(" --instanceport " + instanceSvr.getInstanceport());
312
cmd.append(" --nodeagent " + instanceSvr.getNodeagent());
313                     if (instanceSvr.getConfig() != null)
314                         cmd.append(" --config " + instanceSvr.getConfig());
315                     if (instanceSvr.getCluster() != null)
316                         cmd.append(" --cluster " + instanceSvr.getCluster());
317                     if (instanceSvr.getProperty() != null)
318                         cmd.append(" --property " + instanceSvr.getProperty());
319         } else if (action.equals(ACTION_START)) {
320             //cmd.append(" --debug=" + instanceSvr.isDebug());
321
}
322 /*
323         if (instanceSvr.isLocal()) {
324             if (instanceSvr.getDomain() != null) {
325                 cmd.append(" --domain ").append(instanceSvr.getDomain());
326             }
327             cmd.append(" --local=true");
328         } else {
329             cmd.append(instanceSvr.getCommandParameters(false));
330         }
331 */

332         cmd.append(' ').append(instanceSvr.getInstance());
333
334         execAdminCommand(cmd.toString());
335
336 /* String cmdString = (String) ACTION_MAP.get(action);
337         cmdString += aServer.getCommandParameters(false);
338         if (action.equals(ACTION_CREATE)) {
339             cmdString += " --instanceport " + aServer.getInstanceport();
340         }
341         if (aServer.getInstance() != null) {
342             cmdString += " " + aServer.getInstance();
343         }
344         execAdminCommand(cmdString);
345  */

346     }
347
348     /**
349      * This inner class is used to represent administration instances of the
350      * Sun ONE Application Server for instance-based command. The admin
351      * instance may be sent commands which enable users to configure their
352      * application servers.
353      *
354      * @author Greg Nelson <a HREF="mailto:gn@sun.com">gn@sun.com</a>
355      */

356     public class InstanceServer extends Server {
357         private boolean debug; // Starts instance in debug mode if true
358
private boolean local; // Starts instance without admin instance if true
359
private String JavaDoc domain; // Domain where the admin command is executed
360
private int instanceport; // Port new server instance should use
361
private String JavaDoc nodeagent; // nodeagent when create instance command is executed
362
private String JavaDoc config; // instance configuration when create instance command is executed
363
private String JavaDoc cluster; // cluster name when create instance command is executed
364
private String JavaDoc property; // property when create instance command is executed
365

366         /*
367          * It's easy to determine if reference types have been explicitly set by
368          * the user, since they're null if unset. These variables track whether
369          * or not the boolean attribute have been explictly set by the user or
370          * not.
371          */

372         private boolean debugIsSet = false;
373         private boolean localIsSet = false;
374
375         private static final boolean DEFAULT_DEBUG = false;
376         private static final boolean DEFAULT_LOCAL = false;
377
378         /**
379          * Constructs a new InstanceServer object without specifying a parent
380          * server from which values are inherited.
381          */

382         public InstanceServer() {
383             this(null);
384         }
385
386         /**
387          * Constructs a new InstanceServer object and specifies the parent server
388          * from which attribute values are inherited
389          *
390          * @param server The parent server for this object.
391          */

392         public InstanceServer(Server theParent) {
393             super(theParent);
394         }
395
396         /**
397          * Set if the instance should be started in debug mode. Defaults to
398          * <code>false</code>.
399          *
400          * @param debug If set to <code>true</code>, the instance will be
401          * in debug mode.
402          */

403         public void setDebug(boolean debug) {
404                 this.debug = debug;
405             debugIsSet = true; // Indicates that "debug" has been explicitly set
406
}
407
408         /**
409          * Indicates if the instance should be started in debug mode.
410          *
411          * @return <code>true</code> if the instance should be started in debug
412          * mode.
413          */

414         protected boolean isDebug() {
415             InstanceServer theParent = (InstanceServer) getParent();
416             if (!debugIsSet) {
417                 return (theParent == null) ? DEFAULT_DEBUG : theParent.isDebug();
418             }
419             return debug;
420         }
421
422         /**
423          * Set if the instance should be started without an admin server
424          * running.
425          *
426          * @param local If set to <code>true</code>, the instance will be
427          * started without the need for an admin server.
428          */

429         public void setLocal(boolean local) {
430             this.local = local;
431             localIsSet = true; // Indicates that "local" has been explicitly set
432
}
433
434         /**
435          * Indicates if the instance should be started without an admin server
436          * running.
437          *
438          * @return <code>true</code> if the instance should be started in
439          * without an admin server.
440          */

441         protected boolean isLocal() {
442             InstanceServer theParent = (InstanceServer) getParent();
443             if (!localIsSet) {
444                 return (theParent == null) ? DEFAULT_LOCAL : theParent.isLocal();
445             }
446             return local;
447         }
448
449         /**
450          * Sets the domain for the administrative command -- some commands allow the
451          * domain to be set for "local" commands instead of username, password, host
452          * and password.
453          *
454          * @param domain The domain name for the administrative command.
455          */

456         public void setDomain(String JavaDoc domain) {
457             this.domain = domain;
458         }
459
460         /**
461          * Returns the domain for the administrative command.
462          *
463          * @return The domain name where the command is to be executed
464          */

465         protected String JavaDoc getDomain() {
466             InstanceServer theParent = (InstanceServer) getParent();
467             if (domain == null) {
468                 return (theParent == null) ? null : theParent.getDomain();
469             }
470             return domain;
471         }
472
473         /**
474          * Sets the port number to be used by a new application server instance.
475          *
476          * @param instanceport The port number of the new application server
477          * instance
478          */

479         public void setInstanceport(int instanceport) {
480             this.instanceport = instanceport;
481         }
482
483         /**
484          * Returns the port number to be used by a new application server
485          * instance.
486          *
487          * @return The port number of the new application server instance
488          */

489         protected int getInstanceport() {
490             InstanceServer theParent = (InstanceServer) getParent();
491             if ((instanceport == 0) && (theParent != null)) {
492                 return theParent.getInstanceport();
493             }
494             return instanceport;
495         }
496
497                 /**
498                  * Sets the nodeagent to be used by a new application server instance.
499                  *
500                  * @param nodeagent The nodeagent of the new application server
501                  * instance
502                  */

503                 public void setNodeagent(String JavaDoc nodeagent) {
504                     this.nodeagent = nodeagent;
505                 }
506
507                 /**
508                  * Returns the nodeagent to be used by a new application server
509                  * instance.
510                  *
511                  * @return The nodeagent of the new application server instance
512                  */

513                 protected String JavaDoc getNodeagent() {
514                     InstanceServer theParent = (InstanceServer) getParent();
515                     return nodeagent;
516                 }
517
518                 /**
519                  * Sets the cluster to be used by a new application server instance.
520                  *
521                  * @param cluster The cluster of the new application server
522                  * instance
523                  */

524                 public void setCluster(String JavaDoc cluster) {
525                     this.cluster = cluster;
526                 }
527
528                 /**
529                  * Returns the cluster to be used by a new application server
530                  * instance.
531                  *
532                  * @return The cluster of the new application server instance
533                  */

534                 protected String JavaDoc getCluster() {
535                     InstanceServer theParent = (InstanceServer) getParent();
536                     return cluster;
537                 }
538                 /**
539                  * Sets the config to be used by a new application server instance.
540                  *
541                  * @param config The config of the new application server
542                  * instance
543                  */

544                 public void setConfig(String JavaDoc config) {
545                     this.config = config;
546                 }
547
548                 /**
549                  * Returns the config to be used by a new application server
550                  * instance.
551                  *
552                  * @return The config of the new application server instance
553                  */

554                 protected String JavaDoc getConfig() {
555                     return config;
556                 }
557                 /**
558                  * Sets the property to be used by a new application server instance.
559                  *
560                  * @param property The nodeagent of the new application server
561                  * instance
562                  */

563                 public void setProperty(String JavaDoc property) {
564                     this.property = property;
565                 }
566
567                 /**
568                  * Returns the property to be used by a new application server
569                  * instance.
570                  *
571                  * @return The property of the new application server instance
572                  */

573                 protected String JavaDoc getProperty() {
574                     return property;
575                 }
576         }
577 }
578
Popular Tags