KickJava   Java API By Example, From Geeks To Geeks.

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


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
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.MBeanServerConnection JavaDoc;
30 import com.sun.enterprise.admin.common.ObjectNames;
31
32 // jdk imports
33
import java.io.File JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 /**
37  * Lists the submodules of a deployed component
38  * @version $Revision: 1.3 $
39  */

40 public class ListSubComponentsCommand extends S1ASCommand
41 {
42     private static final String JavaDoc GET_MODULE_COMPONENTS = "getModuleComponents";
43     private static final String JavaDoc APPNAME_OPTION = "appname";
44     private static final String JavaDoc TYPE_OPTION = "type";
45     private static final String JavaDoc EJBS = "ejbs";
46     private static final String JavaDoc SERVLETS = "servlets";
47     private static final String JavaDoc EJB_SUB_MODULE = "EJBModule|SessionBean|StatelessSessionBean|StatefulSessionBean|MessageDrivenBean|EntityBean";
48     private static final String JavaDoc SERVLET_SUB_MODULE = "WebModule|Servlet";
49     private static final String JavaDoc TYPE_OPTION_VALUES = "ejbs|servlets";
50
51     /**
52      * An abstract method that validates the options
53      * on the specification in the xml properties file
54      * This method verifies for the correctness of number of
55      * operands and if all the required options are supplied by the client.
56      * @return boolean returns true if success else returns false
57      */

58     public boolean validateOptions() throws CommandValidationException
59     {
60         //Check if the type option arguments are valid ejbs|servlets
61
final String JavaDoc typeOption = getOption(TYPE_OPTION);
62         if ((typeOption != null) && !typeOption.matches(TYPE_OPTION_VALUES))
63             throw new CommandValidationException(getLocalizedString(
64                                                      "InvalidTypeOption"));
65         return super.validateOptions();
66     }
67
68     
69     /**
70      * An abstract method that Executes the command
71      * @throws CommandException
72      */

73     public void runCommand()
74             throws CommandException, CommandValidationException
75     {
76     validateOptions();
77
78         final String JavaDoc objectName = getObjectName();
79     final String JavaDoc appname = getOption(APPNAME_OPTION);
80     final String JavaDoc typeOption = getOption(TYPE_OPTION);
81     final Object JavaDoc[] params = getParams(appname);
82         final String JavaDoc[] types = getTypes(appname);
83
84     //use http connector
85
final MBeanServerConnection JavaDoc mbsc = getMBeanServerConnection(getHost(), getPort(),
86                                      getUser(), getPassword());
87         try
88         {
89         // if (System.getProperty("Debug") != null) printDebug(mbsc, objectName);
90

91         Object JavaDoc returnValue = mbsc.invoke(new ObjectName JavaDoc(objectName),
92                          GET_MODULE_COMPONENTS, params, types);
93         printObjectName(returnValue, typeOption);
94         CLILogger.getInstance().printDetailMessage(getLocalizedString(
95                                "CommandSuccessful",
96                                new Object JavaDoc[] {name}));
97         }
98         catch(Exception JavaDoc e)
99         {
100         if (e.getLocalizedMessage() != null)
101         CLILogger.getInstance().printDetailMessage(e.getLocalizedMessage());
102             throw new CommandException(getLocalizedString("CommandUnSuccessful",
103                              new Object JavaDoc[] {name} ), e);
104         }
105     }
106
107
108     /**
109      * Returns the paramters to use to pass into GET_MODULE_COMPONENTS method
110      * @param appname - appname if appname is null then returns the paramter
111      * without appname.
112      * @return the parameter in Object[]
113      */

114     private Object JavaDoc[] getParams(String JavaDoc appname)
115     {
116     if (appname == null)
117     {
118         return new Object JavaDoc[] {(String JavaDoc)getOperands().get(0)};
119     }
120     else
121     {
122         return new Object JavaDoc[] {appname, (String JavaDoc)getOperands().get(0)};
123     }
124     }
125
126
127     /**
128      * Returns the types to use for GET_MODULE_COMPONENTS method
129      * @param appname - appname if appname is null then returns the type
130      * without appname.
131      * @return the type in String[]
132      */

133     private String JavaDoc[] getTypes(String JavaDoc appname)
134     {
135     if (appname == null)
136     {
137             return new String JavaDoc[] {String JavaDoc.class.getName()};
138     }
139     else
140     {
141             return new String JavaDoc[] {String JavaDoc.class.getName(), String JavaDoc.class.getName()};
142     }
143     }
144
145
146     /**
147      * This method prints the object name.
148      * @param return value from operation in mbean
149      */

150     private void printObjectName(Object JavaDoc returnValue, String JavaDoc typeOption)
151                 throws CommandException
152     {
153     if (returnValue == null)
154         {
155                     CLILogger.getInstance().printDetailMessage(
156                                                 getLocalizedString("NoElementsToList"));
157         }
158     try
159     {
160         if (returnValue.getClass().getName().equals(STRING_ARRAY))
161         {
162         final String JavaDoc[] objs = (String JavaDoc[])returnValue;
163                 boolean nothingToList = true;
164
165         for (int ii=0; ii<objs.length; ii++)
166         {
167             CLILogger.getInstance().printDebugMessage("***** " + objs[ii]);
168             final ObjectName JavaDoc on = new ObjectName JavaDoc(objs[ii]);
169                     if (typeOption != null)
170                     {
171                         if ((typeOption.equals(EJBS) &&
172                              on.getKeyProperty("j2eeType").matches(EJB_SUB_MODULE)) ||
173                             (typeOption.equals(SERVLETS) &&
174                              on.getKeyProperty("j2eeType").matches(SERVLET_SUB_MODULE)))
175                         {
176                             printSubComponent(on);
177                             nothingToList=false;
178                         }
179                     }
180                     else
181                     {
182                         printSubComponent(on);
183                         nothingToList=false;
184                     }
185                 }
186             if (nothingToList)
187                 {
188                     CLILogger.getInstance().printDetailMessage(
189                                                 getLocalizedString("NoElementsToList"));
190                 }
191             }
192         }
193     catch (Exception JavaDoc e)
194     {
195         throw new CommandException(e);
196     }
197     }
198
199
200     /**
201      * This method prints the sub component name
202      */

203     private void printSubComponent(ObjectName JavaDoc on)
204     {
205         CLILogger.getInstance().printMessage(on.getKeyProperty("name") + " <"+
206                                              on.getKeyProperty("j2eeType") + ">");
207     }
208
209
210     /**
211      * This method prints the objecName info for debugging purpose
212      */

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