KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > util > MBeanUtil


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 /*
25  * MBeanUtil.java
26  *
27  * Created on April 1, 2003, 11:48 AM
28  */

29
30 package com.sun.enterprise.tools.admingui.util;
31
32 import javax.management.MBeanException JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.management.AttributeList JavaDoc;
35 import javax.management.Attribute JavaDoc;
36 import javax.management.MBeanServerConnection JavaDoc;
37
38 import com.iplanet.jato.RequestContext;
39 import com.iplanet.jato.RequestManager;
40 import com.iplanet.jato.util.NonSyncStringBuffer;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.Set JavaDoc;
44 import java.util.HashMap JavaDoc;
45
46 import com.sun.enterprise.admin.common.MBeanServerFactory;
47 import com.sun.enterprise.admin.config.MBeanConfigInstanceNotFoundException;
48
49 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
50
51 public class MBeanUtil {
52     
53     private static MBeanServerConnection JavaDoc remoteConnection = null;
54     
55     // FIXME: this needs to be dynamic.
56
//private static final boolean localConnection = true;
57

58     public static Object JavaDoc invoke(String JavaDoc objectName, String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature) {
59     try {
60         return invoke(
61         new ObjectName JavaDoc(objectName), operationName, params, signature);
62     } catch (Exception JavaDoc ex) {
63         if (Util.isLoggableFINE()) {
64                 NonSyncStringBuffer buf = new NonSyncStringBuffer();
65                 buf.append(ex.getMessage());
66                 buf.append("MBUtil.invoke failed:\n");
67                 buf.append(" OBJECT NAME: "+objectName+"\n");
68                 buf.append(" OPERATION NAME: "+operationName+"\n");
69                 Util.logFINE(buf.toString());
70             }
71         throw new FrameworkException(ex);
72     }
73     }
74     
75     public static AttributeList JavaDoc getAttributes(String JavaDoc objectName, String JavaDoc[] attributeNames) {
76     try {
77         return getMBeanServer().getAttributes(new ObjectName JavaDoc(objectName), attributeNames);
78     } catch (Exception JavaDoc ex) {
79         throw new FrameworkException(ex);
80     }
81     }
82     
83     public static Object JavaDoc getAttribute(ObjectName JavaDoc objectName, String JavaDoc attributeName) {
84     try {
85         return getMBeanServer().getAttribute(objectName, attributeName);
86     } catch (Exception JavaDoc ex) {
87         if (Util.isLoggableFINE()) {
88                 NonSyncStringBuffer buf = new NonSyncStringBuffer();
89                 buf.append(ex.getMessage());
90                 buf.append("Attribute not found on the given Object:\n");
91                 buf.append(" OBJECT NAME: "+objectName+"\n");
92                 buf.append(" ATTRIBUTE NAME: "+attributeName+"\n");
93                 Util.logFINE(buf.toString());
94             }
95         throw new FrameworkException(ex);
96     }
97     }
98     
99     public static Object JavaDoc getAttribute(String JavaDoc objectName, String JavaDoc attributeName) {
100     try {
101         return getMBeanServer().getAttribute(new ObjectName JavaDoc(objectName), attributeName);
102     } catch (Exception JavaDoc ex) {
103         if (Util.isLoggableFINE()) {
104                 NonSyncStringBuffer buf = new NonSyncStringBuffer();
105                 buf.append(ex.getMessage());
106                 buf.append("Attribute not found on the given Object:\n");
107                 buf.append(" OBJECT NAME: "+objectName+"\n");
108                 buf.append(" ATTRIBUTE NAME: "+attributeName+"\n");
109                 Util.logFINE(buf.toString());
110             }
111         throw new FrameworkException(ex);
112     }
113     }
114     
115     public static void setAttribute(String JavaDoc objectName, Attribute JavaDoc attributeName) {
116     try {
117         setAttribute(new ObjectName JavaDoc(objectName), attributeName);
118     } catch (Exception JavaDoc ex) {
119         throw new FrameworkException(ex);
120     }
121     }
122     
123     public static void setAttribute(ObjectName JavaDoc objectName, Attribute JavaDoc attributeName) {
124     try {
125         getMBeanServer().setAttribute(objectName, attributeName);
126     } catch (Exception JavaDoc ex) {
127         throw new FrameworkException(ex);
128     }
129     }
130         
131     public static AttributeList JavaDoc setAttributes(String JavaDoc objectName, AttributeList JavaDoc attributes) {
132     try {
133         return getMBeanServer().setAttributes(new ObjectName JavaDoc(objectName), attributes);
134     } catch (Exception JavaDoc ex) {
135         throw new FrameworkException(ex);
136     }
137     }
138     
139     public static Object JavaDoc invoke(ObjectName JavaDoc objectName, String JavaDoc operationName, Object JavaDoc[] params, String JavaDoc[] signature) {
140     if (Util.isLoggableFINE()) {
141         // Log some trace info
142
NonSyncStringBuffer buf = new NonSyncStringBuffer();
143         buf.append("***** Calling MBean Server *****\n");
144         buf.append("objectName = "+objectName+"\n");
145         buf.append("operationName = "+operationName+"\n");
146         if (params != null) {
147         for (int i = 0; i < params.length; i++) {
148             buf.append("params["+i+"] = "+params[i]);
149         }
150         }
151         if (signature != null) {
152         for (int i = 0; i < signature.length; i++) {
153             buf.append("types["+i+"] = "+signature[i]);
154         }
155         }
156         buf.append("params = "+params);
157         buf.append("signature = "+signature);
158         Util.logFINE(buf.toString());
159     }
160
161     try {
162             return getMBeanServer().invoke(objectName, operationName, params, signature);
163         } catch (Exception JavaDoc ex) {
164             // this exception is being handled in a special way - this
165
// means that the backend could not find a particular mbean.
166
if (ex.getCause() instanceof MBeanConfigInstanceNotFoundException) {
167                 return null;
168             }
169             throw new FrameworkException(ex);
170         }
171     }
172     
173     
174     public static boolean isValidMBean(String JavaDoc objectName) {
175         boolean valid = false;
176         try {
177             Set JavaDoc beans = getMBeanServer().queryMBeans(new ObjectName JavaDoc(objectName), null);
178             if (beans.size() > 0)
179                 valid = true;
180         } catch (Exception JavaDoc ex) {
181             // ignore
182
}
183         return valid;
184     }
185     /*
186     private static Map initEnvironment() {
187         final Map env = new HashMap();
188         final String PKGS = "com.sun.enterprise.admin.jmx.remote.protocol";
189
190         env.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, PKGS);
191         // FIXME: these values shouldn't be hard coded.
192         env.put(DefaultConfiguration.ADMIN_USER_ENV_PROPERTY_NAME, "admin");
193         env.put(DefaultConfiguration.ADMIN_PASSWORD_ENV_PROPERTY_NAME, "password");
194         env.put(DefaultConfiguration.HTTP_AUTH_PROPERTY_NAME,
195                 DefaultConfiguration.DEFAULT_HTTP_AUTH_SCHEME);
196         return ( env );
197     }
198     
199     private static MBeanServerConnection getRemoteConnection( String host, int port ) {
200         MBeanServerConnection mbeanServer = null;
201         try {
202             JMXServiceURL url =
203                 new JMXServiceURL("service:jmx:s1ashttp://" + host + ":" + port );
204
205             JMXConnector conn = JMXConnectorFactory.connect(url, initEnvironment());
206             mbeanServer = conn.getMBeanServerConnection();
207
208         } catch (Exception ex) {
209             System.out.println("Remote Connect Failed!!!");
210             System.out.println(ex.getMessage());
211             throw new RuntimeException(ex);
212         }
213         return mbeanServer;
214     }
215     */

216     public static MBeanServerConnection JavaDoc getMBeanServer() {
217 // if (localConnection) {
218
return MBeanServerFactory.getMBeanServer();
219 // } else {
220
// // FIXME: should not hard code host and port number.
221
// if (remoteConnection == null)
222
// remoteConnection = getRemoteConnection( "localhost", 2906 );
223
// return remoteConnection;
224
// }
225
}
226     
227     /* if types = null and params !=null, make all types Strings. Otherwise
228      just return an array from arraylist
229      */

230     public static Object JavaDoc[] getParamsAndTypes(ArrayList JavaDoc paramsList, ArrayList JavaDoc typesList) {
231         String JavaDoc[] types = null;
232         Object JavaDoc[] params = null;
233         
234         if (paramsList == null)
235             return new Object JavaDoc[]{null, null};
236         
237         if (typesList == null) {
238             types = new String JavaDoc[paramsList.size()];
239             for (int i = 0; i < types.length; i++) {
240                 types[i] = "java.lang.String";
241             }
242             if (paramsList != null) {
243                 params = paramsList.toArray();
244             }
245             return new Object JavaDoc[] {params, types};
246         }
247 // for (int i = 0; i < typesList.size(); i++) {
248
// System.out.println(typesList.get(i));
249
// }
250
// for (int i = 0; i < paramsList.size(); i++) {
251
// System.out.println(paramsList.get(i));
252
// }
253
for (int i = 0; i < typesList.size(); i++) {
254             Object JavaDoc type = typesList.get(i);
255             Object JavaDoc param = paramsList.get(i);
256
257             if (type instanceof ArrayList JavaDoc) {
258                 ArrayList JavaDoc typeArrayList = (ArrayList JavaDoc)type;
259                 String JavaDoc[] typesArray = (String JavaDoc[])typeArrayList.toArray(new String JavaDoc[typeArrayList.size()]);
260                 typesList.remove(i);
261                 for (int j = 0; j < typesArray.length; j++) {
262                     typesList.add(i+j, typesArray[j]);
263                 }
264                 if (param != null) {
265                     ArrayList JavaDoc paramArrayList = (ArrayList JavaDoc)param;
266                     Object JavaDoc[] paramsArray = paramArrayList.toArray();
267                     paramsList.remove(i);
268                     for (int j = 0; j < paramsArray.length; j++) {
269                         paramsList.add(i+j, paramsArray[j]);
270                     }
271                 }
272             }
273         }
274         //System.out.println("got paramsAndTypes");
275
types = (String JavaDoc[])typesList.toArray(new String JavaDoc[typesList.size()]);
276         params = paramsList.toArray();
277         for(int i = 0; i < types.length; i++) {
278             if(types[i].equals("boolean") || types[i].equals("java.lang.Boolean")) {
279                 params[i] = new Boolean JavaDoc(params[i].toString());
280             }
281             else if(types[i].equals("java.lang.Integer")) {
282                 params[i] = new Integer JavaDoc(params[i].toString());
283             }
284         }
285         return new Object JavaDoc[] {params, types};
286     }
287     
288     public static Object JavaDoc[] getParams(ArrayList JavaDoc params, String JavaDoc[] types) {
289         if (params == null)
290             return null;
291         
292         if (types == null)
293             return null;
294         
295         return params.toArray();
296     }
297     
298     
299     public static void main(String JavaDoc _args[]) {
300         ArrayList JavaDoc typesList = new ArrayList JavaDoc();
301         ArrayList JavaDoc paramsList = new ArrayList JavaDoc();
302         typesList.add("java.lang.String");
303         paramsList.add(null);
304         Object JavaDoc[] paramArray = null;
305         String JavaDoc[] typesArray = null;
306         Object JavaDoc[] paramsAndTypes = getParamsAndTypes(paramsList, typesList);
307         System.out.println(paramsAndTypes[0]);
308     }
309 }
310
Popular Tags