KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > commands > VersionCommand


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 com.sun.enterprise.cli.commands;
25
26 import com.sun.enterprise.cli.framework.*;
27 import com.sun.appserv.server.util.Version;
28 import javax.management.MBeanServerConnection JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import java.io.IOException JavaDoc;
31
32
33 /**
34  * This command will get the version of the application server
35  * @version $Revision: 1.4 $
36  */

37 public class VersionCommand extends S1ASCommand
38 {
39     private static final String JavaDoc VERBOSE = "verbose";
40     private static final String JavaDoc FULL_VERSION_ATTR = "applicationServerFullVersion";
41     private static final String JavaDoc SHORT_VERSION_ATTR = "applicationServerVersion";
42
43     /**
44      * An abstract method that validates the options
45      * on the specification in the xml properties file
46      * This method verifies for the correctness of number of
47      * operands and if all the required options are supplied by the client.
48      * @return boolean returns true if success else returns false
49      */

50     public boolean validateOptions() throws CommandValidationException
51     {
52         return super.validateOptions();
53     }
54
55     /**
56      * An abstract method that Executes the command
57      * @throws CommandException
58      */

59     public void runCommand() throws CommandException, CommandValidationException
60     {
61         //if validateOptions is false, then it must be that --help option
62
//is provided and there is no need to execute the command since
63
//either manpage or usage text is displayed
64
if (!validateOptions())
65             return;
66
67         String JavaDoc objectName = getObjectName();
68         try
69         {
70             //use http connector
71
MBeanServerConnection JavaDoc mbsc = getMBeanServerConnection(getHost(), getPort(),
72                                                                   getUser(), getPassword());
73
74             //if (System.getProperty("Debug") != null)
75
// printDebug(mbsc, objectName);
76
String JavaDoc attribute;
77             if (getBooleanOption("verbose"))
78                 attribute = FULL_VERSION_ATTR;
79             else
80                 attribute = SHORT_VERSION_ATTR;
81
82             Object JavaDoc returnValue = mbsc.getAttribute(new ObjectName JavaDoc(objectName),
83                                              attribute);
84             // handleReturnValue(returnValue);
85
CLILogger.getInstance().printMessage(getLocalizedString("Version",
86                                                  new Object JavaDoc[] {returnValue}));
87             CLILogger.getInstance().printDetailMessage(getLocalizedString(
88                                                        "CommandSuccessful",
89                                                        new Object JavaDoc[] {name}));
90         }
91         catch (IOException JavaDoc ioe)
92         {
93             displayVersionLocally();
94         }
95         catch (CommandValidationException cve)
96         {
97             displayVersionLocally();
98         }
99         catch(Exception JavaDoc e)
100         {
101             if (e.getLocalizedMessage() != null)
102                 CLILogger.getInstance().printDetailMessage(e.getLocalizedMessage());
103             throw new CommandException(getLocalizedString("CommandUnSuccessful",
104                                                      new Object JavaDoc[] {name} ), e);
105         }
106     }
107
108     /**
109      * This method gets the Version locally
110      */

111     private void displayVersionLocally()
112     {
113         //if there is problem communicating with admin server, then get version
114
//locally
115
CLILogger.getInstance().printDetailMessage(getLocalizedString("UnableToCommunicateWithAdminServer"));
116         
117         if (getBooleanOption("verbose"))
118             CLILogger.getInstance().printMessage(getLocalizedString("Version",
119                                                  new Object JavaDoc[] {Version.getFullVersion()}));
120         else
121             CLILogger.getInstance().printMessage(getLocalizedString("Version",
122                                                  new Object JavaDoc[] {Version.getVersion()}));
123         CLILogger.getInstance().printDetailMessage(getLocalizedString(
124                                                        "CommandSuccessful",
125                                                        new Object JavaDoc[] {name}));
126     }
127     
128
129     /**
130      * This method prints the objecName info for debugging purpose
131      */

132     private void printDebug(MBeanServerConnection JavaDoc mbsc, String JavaDoc objectName)
133         throws Exception JavaDoc
134     {
135         CLILogger.getInstance().printDebugMessage("********** getMBeanInfo **********");
136         final javax.management.MBeanInfo JavaDoc mbinfo = mbsc.getMBeanInfo(new ObjectName JavaDoc(objectName));
137         CLILogger.getInstance().printDebugMessage("Description = " + mbinfo.getDescription());
138         CLILogger.getInstance().printDebugMessage("Classname = " + mbinfo.getClassName());
139         final javax.management.MBeanOperationInfo JavaDoc[] mboinfo = mbinfo.getOperations();
140         for (int ii=0; ii<mboinfo.length; ii++)
141         {
142             CLILogger.getInstance().printDebugMessage("("+ii+") Description = " +
143                                                       mboinfo[ii].getDescription());
144             CLILogger.getInstance().printDebugMessage("("+ii+") Name = " +
145                                                       mboinfo[ii].getName());
146             CLILogger.getInstance().printDebugMessage("****** TYPE *****");
147             final javax.management.MBeanParameterInfo JavaDoc[] mbpi = mboinfo[ii].getSignature();
148             for (int kk=0; kk<mbpi.length; kk++)
149             {
150                 CLILogger.getInstance().printDebugMessage("type = " + mbpi[kk].getType());
151             }
152             CLILogger.getInstance().printDebugMessage("returnType = " + mboinfo[ii].getReturnType());
153
154
155         }
156     }
157
158     
159     /*
160      * Returns the user option value
161      * @return user returns user option value
162      * @throws CommandValidationException if the following is true:
163      * 1. --user option not on command line
164      * 2. user option not specified in environment
165      * 3. user option not specified in ASADMINPREFS file
166      * 4. user option not specified in .asadminpass file
167      */

168     protected String JavaDoc getUser() throws CommandValidationException
169     {
170         String JavaDoc userValue = getOption(USER);
171         if (userValue == null)
172         {
173             // read from .asadminpass
174
userValue = getUserFromASADMINPASS();
175             
176             // read from .asadminprefs
177
if (userValue == null)
178                 userValue= getValuesFromASADMINPREFS(USER);
179             if (userValue != null)
180             {
181                 CLILogger.getInstance().printDebugMessage(
182                                 "user value read from " + ASADMINPREFS);
183             }
184         }
185         return userValue;
186     }
187
188     
189     /**
190      * Returns the password option value. This is used by all asadmin commands
191      * that accept the --password option.
192      * @return password returns password option value
193      */

194     protected String JavaDoc getPassword() throws CommandValidationException, CommandException
195     {
196         //getPassword(optionName, allowedOnCommandLine, readPrefsFile, readPasswordOptionFromPrefs, readMasterPasswordFile, mgr, config,
197
//promptUser, confirm, validate)
198
return getPassword(PASSWORD, "AdminPasswordPrompt", "AdminPasswordConfirmationPrompt",
199                             true, true, false, false, null, null, false, false, false, true);
200     }
201     
202      
203 }
204
Popular Tags