KickJava   Java API By Example, From Geeks To Geeks.

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


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.MBeanServerConnection JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import com.sun.appserv.management.util.misc.ExceptionUtil;
32 import com.sun.appserv.management.base.Util;
33 import javax.management.MalformedObjectNameException JavaDoc;
34 import com.sun.appserv.management.config.ResourceRefConfig;
35 import com.sun.appserv.management.config.ResourceConfig;
36 import com.sun.appserv.management.base.XTypes;
37 import java.lang.NullPointerException JavaDoc;
38 import javax.management.RuntimeOperationsException JavaDoc;
39
40 import java.util.Map JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.Set JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.Vector JavaDoc;
45 import java.lang.IllegalArgumentException JavaDoc;
46 import java.io.File JavaDoc;
47 import java.io.IOException JavaDoc;
48
49
50
51 /**
52  * @version $Revision: 1.3 $
53  */

54 public class AMXListResourcesCommand extends S1ASCommand
55 {
56     public final static String JavaDoc DOMAIN_CONFIG_OBJECT_NAME = "amx:j2eeType=X-DomainConfig,name=na";
57     public final static String JavaDoc SERVER_CONFIG_OBJECT_NAME = "amx:j2eeType=X-StandaloneServerConfig,name=";
58     public final static String JavaDoc CLUSTER_CONFIG_OBJECT_NAME = "amx:j2eeType=X-ClusterConfig,name=";
59     public final static String JavaDoc TARGET_NAME = "target";
60
61     /**
62      * An abstract method that validates the options
63      * on the specification in the xml properties file
64      * This method verifies for the correctness of number of
65      * operands and if all the required options are supplied by the client.
66      * @return boolean returns true if success else returns false
67      */

68     public boolean validateOptions() throws CommandValidationException
69     {
70         return super.validateOptions();
71     }
72    
73     /**
74      * An abstract method that Executes the command
75      * @throws CommandException
76      */

77     public void runCommand() throws CommandException, CommandValidationException
78     {
79         if (!validateOptions())
80             throw new CommandValidationException("Validation is false");
81
82
83         //use http connector
84
MBeanServerConnection JavaDoc mbsc = getMBeanServerConnection(getHost(), getPort(),
85                                                               getUser(), getPassword());
86         final Vector JavaDoc vTargetName = getOperands();
87         final String JavaDoc targetName = (vTargetName.size()>0)?(String JavaDoc)vTargetName.get(0):null;
88         
89             //if targetName is not null, then try to get the Config ObjectName of the
90
//target.
91
ObjectName JavaDoc targetON = (targetName!=null && !targetName.equals(DOMAIN))?
92                               getTargetConfigObjectName(mbsc, targetName):null;
93         
94         final Object JavaDoc[] params = getParamsInfo();
95         final String JavaDoc operationName = getOperationName();
96         final String JavaDoc[] types = getTypesInfo();
97         final String JavaDoc j2eeType = (String JavaDoc) ((Vector JavaDoc) getProperty(PARAMS)).get(0);
98
99         try {
100             Object JavaDoc resources = mbsc.invoke(Util.newObjectName(DOMAIN_CONFIG_OBJECT_NAME),
101                                            operationName,
102                                            params, types);
103
104             Map JavaDoc candidates = (Map JavaDoc)resources;
105
106             if (targetON != null ) {
107                 candidates = getResourcesFromTarget(mbsc, targetON, candidates);
108             }
109             displayMap("", (Map JavaDoc)candidates);
110             CLILogger.getInstance().printDetailMessage(getLocalizedString(
111                                                        "CommandSuccessful",
112                                                        new Object JavaDoc[] {name}));
113         }
114         catch (Exception JavaDoc e) {
115             displayExceptionMessage(e);
116         }
117         
118
119     }
120
121
122         /**
123          * Given the Server/Cluster ObjectName, get all the referenced resources.
124          * Then loop through the given resources in the DomainConfig to
125          * determine the referenced resources in the ObjectName.
126          * @param mbsc
127          * @param targetON
128          * @return Map of the referenced resources
129          */

130     private Map JavaDoc getResourcesFromTarget(MBeanServerConnection JavaDoc mbsc,
131                                       ObjectName JavaDoc targetON,
132                                       Map JavaDoc candidates)
133         throws Exception JavaDoc
134     {
135         Object JavaDoc resourceRefs = mbsc.invoke(targetON,
136                                           "getContaineeObjectNameMap",
137                                           new Object JavaDoc[]{new String JavaDoc(XTypes.RESOURCE_REF_CONFIG)},
138                                           new String JavaDoc[]{"java.lang.String"});
139
140         final Set JavaDoc resourceKeySet = ((Map JavaDoc)resourceRefs).keySet();
141         final Iterator JavaDoc resourceKeyIter = resourceKeySet.iterator();
142         Map JavaDoc resMap = new HashMap JavaDoc();
143         while (resourceKeyIter.hasNext()) {
144             final String JavaDoc valueName = (String JavaDoc)resourceKeyIter.next();
145             CLILogger.getInstance().printDebugMessage("Candidate = " + valueName );
146             if (candidates.containsKey(valueName)) {
147                 resMap.put(valueName, candidates.get(valueName));
148             }
149         }
150         return resMap;
151     }
152     
153
154         /**
155          * This routine will display the exception message if the option
156          * --terse is given. This routine will get the root of the exception
157          * and display the message. It will then wrap it with CommaneException
158          * and throw the exception to be handled by CLI framework.
159          * @param e
160          * @throws CommandException
161          */

162     public void displayExceptionMessage(Exception JavaDoc e) throws CommandException
163     {
164             //get the root cause of the exception
165
Throwable JavaDoc rootException = ExceptionUtil.getRootCause(e);
166         
167         if (rootException.getLocalizedMessage() != null)
168             CLILogger.getInstance().printDetailMessage(rootException.getLocalizedMessage());
169         throw new CommandException(getLocalizedString("CommandUnSuccessful",
170                                                       new Object JavaDoc[] {name} ), e);
171
172     }
173
174
175         /**
176          * This routine will get the StandaloneServerConfig or ClusterConfig
177          * by the given target name.
178          * @param MBeanServerConnection
179          * @param targetName
180          * @return ObjectName
181          */

182     private ObjectName JavaDoc getTargetConfigObjectName(final MBeanServerConnection JavaDoc mbsc,
183                                                  final String JavaDoc targetName)
184         throws CommandException
185     {
186         try {
187             ObjectName JavaDoc scON = Util.newObjectName(SERVER_CONFIG_OBJECT_NAME+targetName);
188             if (!mbsc.isRegistered(scON))
189                 scON = Util.newObjectName(CLUSTER_CONFIG_OBJECT_NAME+targetName);
190             if (!mbsc.isRegistered(scON))
191                 throw new CommandException(getLocalizedString("InvalidTargetName"));
192         
193             return scON;
194         }
195         catch (RuntimeOperationsException JavaDoc roe)
196         {
197             throw new CommandException(roe);
198         }
199         catch (IOException JavaDoc ioe)
200         {
201             throw new CommandException(ioe);
202         }
203     }
204
205         
206     /**
207         Display a Map to System.out.
208      */

209     private void displayMap(final String JavaDoc msg, final Map JavaDoc m)
210     {
211         final Set JavaDoc keySet = m.keySet();
212         if (keySet.isEmpty()) {
213             CLILogger.getInstance().printDetailMessage(
214                 getLocalizedString("NoElementsToList"));
215             return;
216         }
217         final Iterator JavaDoc keyIter = keySet.iterator();
218         
219         while (keyIter.hasNext()) {
220             final String JavaDoc valueName = (String JavaDoc)keyIter.next();
221             CLILogger.getInstance().printMessage(msg + " " + valueName );
222         }
223     }
224
225 }
226
Popular Tags