KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.sun.appserv.management.client.ProxyFactory;
39 import com.sun.appserv.management.DomainRoot;
40
41
42 import java.util.Vector JavaDoc;
43 import java.net.MalformedURLException JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.util.StringTokenizer JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.Map JavaDoc;
48
49 /**
50  * This class is called for the list-components command.
51  * It uses the application mbean (object name: ias:type=applications,category=config)
52  * and the getAllDeployedComponents() operation. This information is defined in
53  * CLIDescriptor.xml,
54  * <p>
55  * The getAllDeployedComponents returns an array of ObjectName[]. From the ObjectName,
56  * the type can be determined. If the --type option is used, the appropriate type
57  * will be displayed otherwise all deployed module will be displayed.
58  * </p>
59  * @version $Revision: 1.4 $
60  */

61 public class ListComponentsCommand extends S1ASCommand
62 {
63     private static final String JavaDoc TYPE_OPTION = "type";
64     private static final String JavaDoc TYPE_OPTION_REG = "application|web|ejb|connector|webservice";
65     private static final String JavaDoc WEBSERVICE_TYPE = "webservice";
66
67     /**
68      * An abstract method that validates the options
69      * on the specification in the xml properties file
70      * This method verifies for the correctness of number of
71      * operands and if all the required options are supplied by the client.
72      * @return boolean returns true if success else returns false
73      */

74     public boolean validateOptions() throws CommandValidationException
75     {
76         return super.validateOptions();
77     }
78    
79     /**
80      * An abstract method that Executes the command
81      * @throws CommandException
82      */

83     public void runCommand() throws CommandException, CommandValidationException
84     {
85     //if validateOptions is false, then it must be that --help option
86
//is provided and there is no need to execute the command since
87
//either manpage or usage text is displayed
88
if (!validateOptions())
89         return;
90     
91         String JavaDoc objectName = getObjectName();
92         Object JavaDoc[] params = getParamsInfo();
93         String JavaDoc operationName = getOperationName();
94         String JavaDoc[] types = getTypesInfo();
95
96     //use http connector
97
MBeanServerConnection JavaDoc mbsc = getMBeanServerConnection(getHost(), getPort(),
98                                      getUser(), getPassword());
99
100         try
101         {
102         if (System.getProperty("Debug") != null) printDebug(mbsc, objectName);
103         // handleReturnValue(returnValue);
104
String JavaDoc type = getTypeOption();
105             boolean nothingToList = true;
106             if ((type != null) && (type.equals(WEBSERVICE_TYPE)))
107             {
108                 nothingToList = printWebServices(mbsc);
109             }
110             else
111             {
112                 Object JavaDoc returnValue = mbsc.invoke(new ObjectName JavaDoc(objectName),
113                                                  operationName, params, types);
114                 nothingToList = printDeployedComponents(returnValue);
115                 // if type is null, also print the webservices
116
if (type == null)
117                 {
118                     if (printWebServices(mbsc) == false)
119                         nothingToList=false;
120                 }
121             }
122             if (nothingToList)
123             {
124                         CLILogger.getInstance().printDetailMessage(
125                                                     getLocalizedString("NoElementsToList"));
126             }
127         CLILogger.getInstance().printDetailMessage(getLocalizedString(
128                                "CommandSuccessful",
129                                new Object JavaDoc[] {name}));
130         }
131         catch(Exception JavaDoc e)
132         {
133         if (e.getLocalizedMessage() != null)
134         CLILogger.getInstance().printDetailMessage(e.getLocalizedMessage());
135             throw new CommandException(getLocalizedString("CommandUnSuccessful",
136                              new Object JavaDoc[] {name} ), e);
137         }
138     }
139
140     /**
141      * This method prints the object name.
142      * @param return value from operation in mbean
143      */

144     private boolean printDeployedComponents(Object JavaDoc returnValue)
145     throws CommandValidationException
146     {
147     if (returnValue == null) return true;
148         boolean nothingToList = true;
149         if (returnValue.getClass() == new ObjectName JavaDoc[0].getClass())
150         {
151             final ObjectName JavaDoc[] objs = (ObjectName JavaDoc[])returnValue;
152             final String JavaDoc displayType = (String JavaDoc)((Vector JavaDoc)getProperty(DISPLAY_TYPE)).get(0);
153         final String JavaDoc type = getTypeOption();
154             for (int ii=0; ii<objs.length; ii++)
155             {
156         final ObjectName JavaDoc objectName = (ObjectName JavaDoc)objs[ii];
157         CLILogger.getInstance().printDebugMessage("ObjectName = " + objectName);
158         final String JavaDoc componentType = objectName.getKeyProperty("type");
159
160         if ((type == null) || (componentType.indexOf(type) != -1))
161         {
162             CLILogger.getInstance().printMessage(
163                         objectName.getKeyProperty(displayType) +
164                         " <" + componentType + "> " );
165                     nothingToList = false;
166         }
167             }
168         }
169         return nothingToList;
170     }
171
172
173     /**
174      * this method will print the fully qualified webservice endpoints
175      * ex. jaxrpc-simple#jaxrpc-simple.war#HelloIF <webservice>
176      * @return nothingToLis
177      */

178     private boolean printWebServices(MBeanServerConnection JavaDoc mbsc)
179     {
180         DomainRoot domainRoot = ProxyFactory.getInstance(mbsc).getDomainRoot();
181         Map JavaDoc map = domainRoot.getWebServiceMgr().getWebServiceEndpointKeys();
182         Iterator JavaDoc keys = map.keySet().iterator();
183         if (!keys.hasNext())
184             return true;
185         while (keys.hasNext())
186             CLILogger.getInstance().printMessage(keys.next().toString() +
187                                                 " <" + WEBSERVICE_TYPE + "> ");
188         return false;
189     }
190     /**
191      * this method gets the value for type option.
192      * type option must be application, ejb, web or connector
193      * @return value of type option
194      * @throws CommandValidationException if type option is invalid.
195      */

196     private String JavaDoc getTypeOption() throws CommandValidationException
197     {
198         final String JavaDoc type = getOption(TYPE_OPTION);
199     if (type == null) return type;
200     if (type.matches(TYPE_OPTION_REG))
201         return type;
202     throw new CommandValidationException(getLocalizedString("InvalidTypeOption"));
203     }
204
205
206     /**
207      * This method prints the objecName info for debugging purpose
208      */

209     private void printDebug(MBeanServerConnection JavaDoc mbsc, String JavaDoc objectName)
210     throws Exception JavaDoc
211     {
212     CLILogger.getInstance().printDebugMessage("********** getMBeanInfo **********");
213     final javax.management.MBeanInfo JavaDoc mbinfo = mbsc.getMBeanInfo(new ObjectName JavaDoc(objectName));
214     CLILogger.getInstance().printDebugMessage("Description = " + mbinfo.getDescription());
215     CLILogger.getInstance().printDebugMessage("Classname = " + mbinfo.getClassName());
216         final javax.management.MBeanOperationInfo JavaDoc[] mboinfo = mbinfo.getOperations();
217     for (int ii=0; ii<mboinfo.length; ii++)
218     {
219         CLILogger.getInstance().printDebugMessage("("+ii+") Description = " +
220                               mboinfo[ii].getDescription());
221         CLILogger.getInstance().printDebugMessage("("+ii+") Name = " +
222                               mboinfo[ii].getName());
223         CLILogger.getInstance().printDebugMessage("****** TYPE *****");
224         final javax.management.MBeanParameterInfo JavaDoc[] mbpi = mboinfo[ii].getSignature();
225         for (int kk=0; kk<mbpi.length; kk++)
226         {
227         CLILogger.getInstance().printDebugMessage("type = " + mbpi[kk].getType());
228         }
229         CLILogger.getInstance().printDebugMessage("returnType = " + mboinfo[ii].getReturnType());
230
231
232     }
233     }
234
235 }
236
Popular Tags