KickJava   Java API By Example, From Geeks To Geeks.

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


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.Task;
81 import org.apache.tools.ant.BuildException;
82 import org.apache.tools.ant.Project;
83 import org.apache.tools.ant.AntClassLoader;
84 import org.apache.tools.ant.types.Path;
85
86 import java.io.File JavaDoc;
87 import java.util.List JavaDoc;
88 import java.util.ArrayList JavaDoc;
89 import java.util.Iterator JavaDoc;
90 import java.lang.reflect.Method JavaDoc;
91 import java.lang.reflect.InvocationTargetException JavaDoc;
92
93 /**
94  * Abstract class which includes base functionality for Tasks targeting the
95  * Sun ONE Application Server 8. This includes support for the following
96  * attributes:
97  *
98  * <ul>
99  * <li><i>user</i> -- The username used when logging into the application
100  * server administration instance. This attribute is
101  * inherited by nested &lt;server&gt; elements. Defaults to
102  * "admin"
103  * <li><i>password</i> -- The password used when logging into the application
104  * server administration instance. This attribute
105  * is inherited by nested &lt;server&gt; elements
106  * <li><i>host</i> -- Target server for the command(s). When deploying to a
107  * remote server, the fully qualified hostname should be
108  * used. This attribute is inherited by nested &lt;server&gt;
109  * elements. Defaults to "localhost"
110  * <li><i>port</i> -- Admin port on the target server. This attribute is
111  * inherited by nested &lt;server&gt; elements. Defaults to
112  * "4848"
113  * <li><i>secure</i> -- This attribute uses SSL/TLS to communicate with the
114  * domain application server.
115  * <li><i>instance</i> -- Target application server instance for the command.
116  * This attribute is inherited by nested &lt;server%gt
117  * elements. If not specified, the default instance
118  * name will be used
119  * <li><i>sunonehome</i> -- This attribute has been deprecated, use asinstall.dir
120  * instead.
121  * The installation directory for the local Sun ONE
122  * Application Server 8 installation -- this is
123  * used to find the correct administrative classes.
124  * If not specified, the task will check to see if
125  * the "sunone.home" parameter has been set.
126  * Otherwise, the Sun ONE Application Server 8
127  * admin classes must be on the system classpath
128  * <li><i>asinstalldir</i> -- The installation directory for the local Sun ONE
129  * Application Server 8 installation -- this is
130  * used to find the correct administrative classes.
131  * If not specified, the task will check to see if
132  * the "asinstall.dir" parameter has been set.
133  * Otherwise, the Sun ONE Application Server 8
134  * admin classes must be on the system classpath
135  * </ul>
136  * <p>
137  * In addition, this abstract class provides support for nested &lt;server&gt;
138  * elements. These nested elements enable commands to be executed against
139  * multiple Sun ONE Application Servers in a single Ant command. Attributes may
140  * be specified in the "root" command element and those values will be used
141  * ("inherited") by all of the nested &lt;server&gt; components. If an attribute is
142  * specified in both elements, the value specified in the &lt;server&gt; element will
143  * be used.
144  *
145  * @author Greg Nelson <a HREF="mailto:gn@sun.com">gn@sun.com</a>
146  */

147 public abstract class AppServerAdmin extends Task {
148     static Method JavaDoc invokeCLI = null;
149     static Class JavaDoc adminMain = null;
150     static Class JavaDoc inputsAndOutputs = null;
151     static Class JavaDoc systemPropertyConstants = null;
152     
153     
154     /**
155      * This Server instance will store user settings from the "root" command
156      * element.
157      */

158     protected Server server;
159
160     /**
161      * The list of servers will store user settings from nested &lt;server&gt;
162      * elements.
163      */

164     protected List JavaDoc servers = new ArrayList JavaDoc();
165
166     /** Sun ONE Application Server 8 installation directory */
167     private File JavaDoc asinstalldir;
168
169     /**
170      * This attribute is used only for debugging and is not included in the
171      * task documentation. When set to "true" the task behaves normally. When
172      * set to "false", each admin command is logged but not actually executed.
173      */

174     private boolean executeCommand = true;
175
176     /** Constants used to invoke the admin utilies using Java reflection */
177     private static final String JavaDoc CLASS_INPUTS_AND_OUTPUTS =
178                             "com.sun.enterprise.cli.framework.InputsAndOutputs";
179     private static final String JavaDoc CLASS_ADMIN_MAIN =
180                             "com.sun.enterprise.cli.framework.CLIMain";
181     private static final String JavaDoc METHOD_INVOKE_CLI = "invokeCLI";
182
183     LocalStringsManager lsm = new LocalStringsManager();
184
185     private static final String JavaDoc CLASS_SYSTEM_PROPERTY_CONSTANTS =
186                             "com.sun.enterprise.util.SystemPropertyConstants";
187
188     /**
189      * Creates a new instance with the infrastructure for running admin commands.
190      */

191     public AppServerAdmin() {
192         server = getNewServer();
193     }
194
195     /**
196      * Sets the username used when logging into the application server
197      * administration instance.
198      *
199      * @param user The username used to logon
200      */

201     public void setUser(String JavaDoc user) {
202         server.setUser(user); // Delegates to server object
203
}
204
205     /**
206      * Sets the password used when logging into the application server
207      * administration instance.
208      *
209      * @param password The password used to logon
210      */

211     public void setPassword(String JavaDoc password) {
212        final String JavaDoc msg = lsm.getString("DeprecatedAttribute", new Object JavaDoc[] {"password",
213          "passwordfile"});
214         log(msg, Project.MSG_WARN);
215         server.setPassword(password); // Delegates to server object
216
}
217
218     /**
219      * Sets the passwordfile used when logging into the application server
220      * administration instance.
221      *
222      * @param password The password used to logon
223      */

224     public void setPasswordfile(String JavaDoc passwordfile) {
225         server.setPasswordfile(passwordfile); // Delegates to server object
226
}
227
228     /**
229      * Sets the hostname where the application server administration instance
230      * is running.
231      *
232      * @param host The host name where the administration instance is running
233      */

234     public void setHost(String JavaDoc host) {
235         server.setHost(host); // Delegates to server object
236
}
237
238     /**
239      * Sets the port where the application server administration instance is
240      * running.
241      *
242      * @param port The port number of the administration instance
243      */

244     public void setPort(int port) {
245         server.setPort(port); // Delegates to server object
246
}
247
248     /**
249      * Sets secure option
250      *
251      * @param secure. The secure if true, uses SSL/TLS to communicate
252      * with the domain application server.
253      */

254     public void setSecure(String JavaDoc secure) {
255         server.setSecure(secure); // Delegates to server object
256
}
257
258
259     /**
260      * Sets the application server instance name. Typically, this is the
261      * "target" for administrative commands.
262      *
263      * @param instance The name of the application server instance
264      */

265     public void setInstance(String JavaDoc instance) {
266         server.setInstance(instance); // Delegates to server object
267
}
268
269     /**
270      * This attribute is used only for debugging and is not included in the
271      * task documentation. When set to "true" the task behaves normally. When
272      * set to "false", each admin command is logged but not actually executed.
273      *
274      * @param executeCommand Whether or not to execute the admin commands
275      * generated by this task
276      */

277     public void setExecuteCommand(boolean executeCommand) {
278         this.executeCommand = executeCommand;
279     }
280
281     /**
282      * Specifies the installation directory for the Sun ONE Application Server
283      * 8. This may be used if the application server is installed on the
284      * local machine.
285      *
286      * @param sunonehome The home directory for the user's app server
287      * installation.
288      */

289     public void setSunonehome(File JavaDoc sunonehome) {
290         final String JavaDoc msg = lsm.getString("DeprecatedAttribute", new Object JavaDoc[] {"sunonehome",
291          "asinstalldir"});
292         log(msg, Project.MSG_WARN);
293         this.asinstalldir = sunonehome;
294     }
295     
296     /**
297      * Specifies the installation directory for the Sun ONE Application Server
298      * 8. This may be used if the application server is installed on the
299      * local machine.
300      *
301      * @param asinstalldir The home directory for the user's app server
302      * installation.
303      */

304     public void setAsinstalldir(File JavaDoc asinstalldir) {
305         this.asinstalldir = asinstalldir;
306     }
307
308     
309     /**
310      * Returns the asinstalldir attribute specify by in the build script.
311      * If asinstalldir hasn't been explicitly set (using
312      * the <code>setAsinstalldir</code> method), the value stored in the <code>
313      * sunone.home</code> property will be returned.
314      *
315      * @return File representing the app server installation directory. Returns
316      * <code>null</code> if the installation directory hasn't been
317      * explictly set and the <code>sunone.home</code> property isn't set.
318      * @throws ClassNotFoundException if asinstalldir is an invalid directory
319      */

320     protected File JavaDoc getAsinstalldir() throws ClassNotFoundException JavaDoc {
321         if (asinstalldir == null) {
322             String JavaDoc home = getProject().getProperty("asinstall.dir");
323             if (home != null) {
324                 asinstalldir = new File JavaDoc(home);
325             }
326             else {
327                 home = getProject().getProperty("sunone.home");
328                 if (home != null)
329                 {
330                     final String JavaDoc msg = lsm.getString("DeprecatedProperty", new Object JavaDoc[] {"sunone.home", "asinstall.dir"});
331                     log(msg, Project.MSG_WARN);
332                     asinstalldir = new File JavaDoc(home);
333                 }
334                 
335             }
336         }
337         if (asinstalldir!=null) verifyAsinstalldir(asinstalldir);
338         return asinstalldir;
339     }
340
341
342     /**
343      * verify if asinsatlldir attribute is valid.
344      * asinstalldir must be a valid directory and must contain the config directory.
345      *
346      * @return true if asinstalldir is valid
347      * @throws ClassNotFoundException if asinstalldir is an invalid directory
348      */

349     private boolean verifyAsinstalldir(File JavaDoc home) throws ClassNotFoundException JavaDoc{
350         if (home!= null && home.isDirectory()) {
351             if ( new File JavaDoc(home, "config").isDirectory() ) {
352                 return true;
353             }
354         }
355         throw new ClassNotFoundException JavaDoc("ClassCouldNotBeFound");
356     }
357     
358
359     /**
360      * returns the appserver server installation directory
361      * first it will get asinstalldir from attribute of ant task.
362      * if user did not specify asinstalldir, then the retrieve the install directory
363      * from java system property using installrootConstant.
364      * if all else fails, an ClassNotFoundException is thrown and the appropraite
365      * msg will be displayed to bhe user.
366      * @param installRootconstant - the install root constant to retrieve from
367      * java system property.
368      * @return install directory
369      * @throws ClassNotFoundException if install directory can not be determined.
370      */

371     private String JavaDoc getInstallRoot(final String JavaDoc installRootConstant) throws ClassNotFoundException JavaDoc{
372         final File JavaDoc installDir = getAsinstalldir();
373         String JavaDoc installRoot;
374         if (installDir == null) {
375             installRoot = System.getProperty(installRootConstant);
376             if (installRoot == null)
377                 throw new ClassNotFoundException JavaDoc("ClassCouldNotBeFound");
378         } else {
379             installRoot = installDir.getPath();
380             System.setProperty(installRootConstant, installRoot);
381         }
382         return installRoot;
383     }
384     
385
386     /**
387      * Creates a nested <code>server</code> element.
388      *
389      * @return Server which has been added
390      */

391     public Server createServer() {
392         log("createServer", Project.MSG_DEBUG);
393         Server aNestedServer = getNewServer();
394         servers.add(aNestedServer);
395         return aNestedServer;
396     }
397
398     /**
399      * Builds a new server instance. This method is intended to be overridden
400      * by subclasses which implement their own subclass of Server.
401      *
402      * @return new Server instance.
403      */

404     protected Server getNewServer() {
405         return new Server(server);
406     }
407
408     /**
409      * Does the work.
410      *
411      * @throws BuildException If the user selections are invalid or an exception
412      * occurs while executing the app server admin
413      * commands.
414      */

415     public void execute() throws BuildException {
416         prepareToExecute();
417         checkConfiguration();
418
419         Iterator JavaDoc it = servers.iterator();
420         while (it.hasNext()) {
421             Server aServer = (Server)it.next();
422             execute(aServer);
423         }
424     }
425
426     /**
427      * Does any clean-up work required before the user configuration is checked
428      * and the command(s) executed.
429      */

430     protected void prepareToExecute() {
431         if (servers.size() == 0) {
432             servers.add(server);
433         }
434     }
435
436     /**
437      * Verifies that the options and parameters selected by the user are valid
438      * and consistent.
439      *
440      * @throws BuildException If the user selections are invalid.
441      */

442     protected void checkConfiguration() throws BuildException {
443
444         log(servers.size() + " servers were found.", Project.MSG_DEBUG);
445
446         // At least one target server must be specified
447
if (servers.size() == 0) {
448             final String JavaDoc msg = lsm.getString("SpecifyOneServer");
449             throw new BuildException(msg, getLocation());
450         }
451
452         // Check the configuration of each target server specified
453
Iterator JavaDoc it = servers.iterator();
454         while (it.hasNext()) {
455             Server aServer = (Server)it.next();
456             checkConfiguration(aServer);
457         }
458     }
459
460     /**
461      * Verifies that the options and parameters selected by the user are valid
462      * and consistent for a given server.
463      *
464      * @param aServer The server whose configuration is to be validated.
465      * @throws BuildException If the user selections are invalid.
466      */

467     protected abstract void checkConfiguration(Server aServer) throws BuildException;
468
469     /**
470      * Builds and executes the admin command on the server specified.
471      *
472      * @param aServer The target server for the administrative command.
473      * @throws BuildException If any error occurs while executing the command.
474      */

475     protected abstract void execute(Server server) throws BuildException;
476
477     /**
478      * Executes a command-string using the Sun ONE Application Server
479      * administrative CLI infrastructure.
480      *
481      * @param command The administrative command String to be executed.
482      * @throws BuildException If any error occurs while executing the command.
483      */

484     protected void execAdminCommand(String JavaDoc command) throws BuildException {
485         log("Executing: " + command, Project.MSG_INFO);
486
487         try {
488             
489             if (invokeCLI == null) {
490                 java.lang.ClassLoader JavaDoc antClassLoader = new AntClassLoader(
491                     AppservClassLoader.getClassLoader(), getProject(), null, false);
492                 log("class = " + ((AntClassLoader)antClassLoader).getClasspath(), Project.MSG_DEBUG);
493                 inputsAndOutputs = Class.forName(CLASS_INPUTS_AND_OUTPUTS, true, antClassLoader);
494                 adminMain = Class.forName(CLASS_ADMIN_MAIN, true, antClassLoader);
495                 systemPropertyConstants = Class.forName(CLASS_SYSTEM_PROPERTY_CONSTANTS, true, antClassLoader);
496             }
497
498             
499             log("***** INSTALL_ROOT_PROPERTY = " + (String JavaDoc)systemPropertyConstants.getField("INSTALL_ROOT_PROPERTY").get(null), Project.MSG_DEBUG);
500
501             final String JavaDoc installRootConstant = (String JavaDoc)systemPropertyConstants.getField("INSTALL_ROOT_PROPERTY").get(null);
502             final String JavaDoc configRootConstant = (String JavaDoc)systemPropertyConstants.getField("CONFIG_ROOT_PROPERTY").get(null);
503
504             final String JavaDoc installRoot = getInstallRoot(installRootConstant);
505             
506             log("installRoot: " + installRoot, Project.MSG_DEBUG);
507             System.setProperty("java.library.path", installRoot+"/lib");
508             System.setProperty(installRootConstant, installRoot);
509             System.setProperty(configRootConstant, installRoot+"/config");
510             System.setProperty("java.endorsed.dirs", installRoot+"/lib/endorsed");
511
512             //debug display all system properties
513
/*
514             for (java.util.Enumeration en = System.getProperties().propertyNames(); en.hasMoreElements() ;)
515             {
516                 String name = (String) en.nextElement();
517                 String value = (String) System.getProperties().getProperty(name);
518                 log("System.property = " + name + " " + value, Project.MSG_DEBUG);
519             }
520             */

521             
522             Class JavaDoc[] parameterClasses = {String JavaDoc.class, inputsAndOutputs};
523             invokeCLI = adminMain.getDeclaredMethod(METHOD_INVOKE_CLI, parameterClasses);
524
525             Object JavaDoc[] parameters = {command, null};
526
527             if (executeCommand) {
528                 invokeCLI.invoke(adminMain, parameters);
529             }
530         } catch (ClassNotFoundException JavaDoc e) {
531             final String JavaDoc msg = lsm.getString("ClassCouldNotBeFound", new Object JavaDoc[] {e.getMessage()});
532             throw new BuildException(msg, getLocation());
533         } catch (NoSuchMethodException JavaDoc e) {
534             final String JavaDoc msg = lsm.getString("CouldNotFindInvokeCLI", new Object JavaDoc[] {e.getMessage()});
535             throw new BuildException(msg, getLocation());
536         } catch (InvocationTargetException JavaDoc e) {
537             final String JavaDoc msg = lsm.getString("ExceptionOccuredRunningTheCommand", new Object JavaDoc[] {e.getTargetException().getMessage()});
538             throw new BuildException(msg, getLocation());
539         } catch (IllegalAccessException JavaDoc e) {
540             final String JavaDoc msg = lsm.getString("ExceptionOccuredInvokeingCLI", new Object JavaDoc[] {e.getMessage()});
541             throw new BuildException(msg, getLocation());
542         } catch (Exception JavaDoc e) {
543             throw new BuildException(e.getMessage(), getLocation());
544         }
545         
546         
547     }
548
549     /**
550      * This inner class is used to represent administration instances of the
551      * Sun ONE Application Server. The administration instance may be sent
552      * commands which enable users to configure their application servers.
553      *
554      * @author Greg Nelson <a HREF="mailto:gn@sun.com">gn@sun.com</a>
555      */

556     public class Server {
557         private Server parent; // Attr values may be inherited from parent
558
private String JavaDoc user; // Username used to logon to admin instance
559
private String JavaDoc password; // Password used to logon to admin instance
560
private String JavaDoc passwordfile; // Passwordfile used to logon to admin instance
561
private String JavaDoc host; // Hostname where admin instance is running
562
private int port; // Port number of admin instance
563
private String JavaDoc secure; // Secure option indiccating communicating with admin server in secured mode.
564
private String JavaDoc instance; // Name of the "target" instance for command
565

566         protected static final String JavaDoc DEFAULT_USER = "admin";
567         protected static final String JavaDoc DEFAULT_HOST = "localhost";
568         protected static final String JavaDoc DEFAULT_PORT = "4848";
569
570         /**
571          * Constructs a new Server object without specifying a parent server
572          * from which values are inherited.
573          */

574         public Server() {
575             this(null);
576         }
577
578         /**
579          * Constructs a new Server object and specifies the parent server from
580          * which attribute values are inherited
581          *
582          * @param server The parent server for this object.
583          */

584         public Server(Server parent) {
585             this.parent = parent;
586         }
587
588         /**
589          * Sets the parent server for this server. If attribute values are not
590          * explicitly set for this object, they may be inherited from the parent
591          * object.
592          *
593          * @param parent The parent server for this object.
594          */

595         public void setParent(Server parent) {
596             this.parent = parent;
597         }
598
599         /**
600          * Gets the parent server for this server. If attribute values are not
601          * explicitly set for this object, they may be inherited from the parent
602          * object.
603          *
604          * @return The parent server for this object.
605          */

606         public Server getParent() {
607             return parent;
608         }
609
610         /**
611          * Sets the username used when logging into the application server
612          * administration instance.
613          *
614          * @param user The username used to logon
615          */

616         public void setUser(String JavaDoc user) {
617             this.user = user;
618         }
619
620         /**
621          * Returns the username used when logging into the application server
622          * administration instance.
623          *
624          * @return The username used to logon
625          */

626         protected String JavaDoc getUser() {
627             if (user == null) {
628                 return (parent == null) ? DEFAULT_USER : parent.getUser();
629             }
630             return user;
631         }
632
633         /**
634          * Sets the password used when logging into the application server
635          * administration instance.
636          *
637          * @param password The password used to logon
638          */

639         public void setPassword(String JavaDoc password) {
640             this.password = password;
641         }
642
643         /**
644          * Returns the password or password command
645          * e.g.
646          * " --password admin123 "
647          * " --passwordfile /somefile "
648          * @return The password command. If no password was specified, return null.
649          */

650         protected String JavaDoc getPasswordCommand() {
651
652             // note that hasPassword() returns false when there is no password anywhere
653
// up the family tree...
654

655             if(!hasPassword())
656                 return null;
657             
658             // PRECEDENCE CHART:
659
// 1. passwordfile in this instance
660
// 2. password in this instance
661
// 3. whatever is eventually returned by parent instances -- which all use the 2 precedence rules above...
662

663             // 1.
664
if(passwordfile != null)
665                 return " --passwordfile " + passwordfile + " ";
666             
667             // 2.
668
if (password != null)
669                 return " --password " + password + " ";
670             
671             // 3.
672
if( parent != null)
673                 return parent.getPasswordCommand();
674             
675             // nobody has a password!!
676
return null;
677         }
678         /**
679          * Returns the password used when logging into the application server
680          * administration instance.
681          *
682          * Only this and the enclosing class have any business getting the password.
683          * Use hasPassword() to see if a password was passed in either directly or in a file
684          * @return The password used to logon
685          */

686         private String JavaDoc getPassword() {
687             if (password == null) {
688                 return (parent == null) ? null : parent.getPassword();
689             }
690             return password;
691         }
692         /**
693          * Returns whether or not a password or passwordfile is available.
694          */

695         protected boolean hasPassword() {
696             
697             if(passwordfile != null || password != null)
698                 return true;
699             
700             if(parent != null)
701                 return parent.hasPassword();
702
703             // no password, no passwordfile, no parent...
704
return false;
705         }
706         
707         /**
708          * Sets the password used when logging into the application server
709          * administration instance.
710          *
711          * @param password The password used to logon
712          */

713         public void setPasswordfile(String JavaDoc passwordfile) {
714                 // need quotes around the passwordfile value since in Windows
715
// the escape character (\) is used as file separator therefore
716
// quotes is used to disambiguate the escape character.
717
this.passwordfile = "\"" + new File JavaDoc(passwordfile).getPath() + "\"";
718         }
719
720         /**
721          * Returns the passwordfile used when logging into the application server
722          * administration instance.
723          *
724          * Outsiders need to call getPasswordCommand to make it transparent how
725          * the password is arriving.
726          * @return The passwordfile used to logon
727          */

728         private String JavaDoc getPasswordfile() {
729             if (passwordfile == null) {
730                 return (parent == null) ? null : parent.getPasswordfile();
731             }
732             return passwordfile;
733         }
734
735         /**
736          * Sets the hostname where the application server administration
737          * instance is running.
738          *
739          * @param host The host name where the administration instance is running
740          */

741         public void setHost(String JavaDoc host) {
742             this.host = host;
743         }
744
745         /**
746          * Returns the hostname where the application server administration
747          * instance is running.
748          *
749          * @return The host name where the administration instance is running
750          */

751         protected String JavaDoc getHost() {
752             if (host == null) {
753                 return (parent == null) ? null : parent.getHost();
754             }
755             return host;
756         }
757
758         /**
759          * Sets the port where the application server administration instance is
760          * running.
761          *
762          * @param port The port number of the administration instance
763          */

764         public void setPort(int port) {
765             this.port = port;
766         }
767
768         /**
769          * Returns the port where the application server administration instance
770          * is running.
771          *
772          * @return The port number of the administration instance
773          */

774         protected int getPort() {
775             if (port == 0) {
776                 return (parent == null) ? 0 : parent.getPort();
777             }
778             return port;
779         }
780
781         /**
782          * Sets secure option indication if communicating th appserver in
783          * secured mode.
784          *
785          * @param secure - true if communicating with appserver in secured mode
786          */

787         public void setSecure(String JavaDoc secure) {
788             this.secure = secure;
789         }
790
791         /**
792          * Returns the secure option.
793          *
794          * @return secure
795          */

796         protected String JavaDoc getSecure() {
797             if (secure == null) {
798                 return (parent == null) ? null : parent.getSecure();
799             }
800             return secure;
801         }
802
803         /**
804          * Sets the application server instance name. Typically, this is the
805          * "target" for administrative commands.
806          *
807          * @param instance The name of the application server instance
808          */

809         public void setInstance(String JavaDoc instance) {
810             this.instance = instance;
811         }
812
813         /**
814          * Returns the application server instance name. Typically, this is the
815          * "target" for administrative commands.
816          *
817          * @return The name of the application server instance
818          */

819         protected String JavaDoc getInstance() {
820             if (instance == null) {
821                 return (parent == null) ? null : parent.getInstance();
822             }
823             return instance;
824         }
825
826         /**
827          * Builds the server-related command-line parameters. This includes the
828          * --user, --password, --host, --port, and (optionally) --instance
829          * parameters.
830          *
831          * @param includeInstance Indicates if the --instance parameter should
832          * be include in the command string. Some admin
833          * commands do not allow the --instance parameter.
834          * @return String representation of the server-related command-line
835          * parameters.
836          */

837         protected String JavaDoc getCommandParameters(boolean includeInstance) {
838             StringBuffer JavaDoc cmdString = new StringBuffer JavaDoc();
839             cmdString.append(" --user ").append(getUser());
840             
841             // note: you definitely don't want the overhead of calling getPasswordCommand()
842
// twice like the other lines below.
843
// So we call it once and save it in a String...
844
String JavaDoc pwc = getPasswordCommand();
845             
846             if(pwc != null){
847                 cmdString.append(pwc);
848             }
849             
850             if (getHost() != null) {
851                 cmdString.append(" --host ").append(getHost());
852             }
853             if (getPort() != 0) {
854                 cmdString.append(" --port ").append(getPort());
855             }
856             if (includeInstance && (getInstance() != null)) {
857                 cmdString.append(" --instance ").append(getInstance());
858             }
859             if (getSecure() != null) {
860                 cmdString.append(" --secure=").append(getSecure());
861             }
862             
863
864             return cmdString.toString();
865         }
866
867         /**
868          * Returns a string representation of the object.
869          *
870          * @return a string representation of the object.
871          */

872         public String JavaDoc toString() {
873             StringBuffer JavaDoc sb;
874             sb = new StringBuffer JavaDoc((getHost() == null) ? DEFAULT_HOST : getHost());
875             sb.append(':');
876             sb.append((getPort() == 0) ? DEFAULT_PORT : String.valueOf(getPort()));
877             return sb.toString();
878         }
879     }; // End of Server inner class
880
}
881
Popular Tags