KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.management.Attribute JavaDoc;
28 import javax.management.AttributeList JavaDoc;
29 import javax.management.remote.JMXServiceURL JavaDoc;
30 import javax.management.remote.JMXConnector JavaDoc;
31 import javax.management.remote.JMXConnectorFactory JavaDoc;
32 import javax.management.MBeanServerConnection JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.management.MalformedObjectNameException JavaDoc;
35 import javax.management.InstanceNotFoundException JavaDoc;
36 import javax.management.IntrospectionException JavaDoc;
37 import javax.management.ReflectionException JavaDoc;
38
39 import java.util.Vector JavaDoc;
40 import java.net.MalformedURLException JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.util.StringTokenizer JavaDoc;
43
44 /**
45  * This class is called for the show-comonent-status command.
46  * It uses the application mbean (object name: ias:type=applications,category=config)
47  * and the getStatus operation. This information is defined in
48  * CLIDescriptor.xml,
49  * <p>
50  * The getStatus returns true if component is enabled else returns false.
51  * </p>
52  * @version $Revision: 1.3 $
53  */

54 public class ShowComponentStatusCommand extends S1ASCommand
55 {
56     /**
57      * An abstract method that validates the options
58      * on the specification in the xml properties file
59      * This method verifies for the correctness of number of
60      * operands and if all the required options are supplied by the client.
61      * @return boolean returns true if success else returns false
62      */

63     public boolean validateOptions() throws CommandValidationException
64     {
65         return super.validateOptions();
66     }
67
68
69     /**
70      * An abstract method that Executes the command
71      * @throws CommandException
72      */

73     public void runCommand() throws CommandException, CommandValidationException
74     {
75         validateOptions();
76         String JavaDoc objectName = getObjectName();
77         Object JavaDoc[] params = getParamsInfo();
78         String JavaDoc operationName = getOperationName();
79         String JavaDoc[] types = getTypesInfo();
80
81     //use http connector
82
MBeanServerConnection JavaDoc mbsc = getMBeanServerConnection(getHost(), getPort(),
83                                      getUser(), getPassword());
84
85         try
86         {
87             final Object JavaDoc returnValue = mbsc.invoke(new ObjectName JavaDoc(objectName),
88                          operationName, params, types);
89             displayComponentStatus(returnValue);
90         CLILogger.getInstance().printDetailMessage(getLocalizedString(
91                                "CommandSuccessful",
92                                new Object JavaDoc[] {name}));
93         }
94         catch(Exception JavaDoc e)
95         {
96         if (e.getLocalizedMessage() != null)
97         CLILogger.getInstance().printDetailMessage(e.getLocalizedMessage());
98             throw new CommandException(getLocalizedString("CommandUnSuccessful",
99                              new Object JavaDoc[] {name} ), e);
100         }
101     }
102
103
104     /**
105      * display the status of the component.
106      * if the returnValue is true, then the component is enabled, else disabled.
107      * @throws CommandException is could not determine the status of the component.
108      */

109     private void displayComponentStatus(Object JavaDoc returnValue)
110     throws CommandException
111     {
112     final String JavaDoc componentName = (String JavaDoc) getOperands().get(0);
113     if (returnValue == null)
114         throw new CommandException(getLocalizedString("UndetermineStatus",
115                               new Object JavaDoc[] {componentName}));
116     else
117     {
118         //the return value must be Boolean
119
final String JavaDoc returnValClassName = returnValue.getClass().getName();
120         if (returnValClassName.equals(BOOLEAN_CLASS))
121         {
122         final boolean status = ((Boolean JavaDoc)returnValue).booleanValue();
123         if (status)
124             CLILogger.getInstance().printMessage(getLocalizedString(
125                  "ComponentIsEnabled", new Object JavaDoc[] {componentName}));
126         else
127             CLILogger.getInstance().printMessage(getLocalizedString(
128                  "ComponentIsDisabled", new Object JavaDoc[] {componentName}));
129         }
130         else
131         throw new CommandException(getLocalizedString("UndetermineStatus",
132                                   new Object JavaDoc[] {componentName}));
133     }
134     }
135
136 }
137
Popular Tags