KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webjmx > api > JMXProcessor


1 /*
2  * Copyright (C) WebJMX.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the WebJMX License version 2.0.
6  * See the terms of the WebJMX License in the documentation provided with this software.
7  */

8 /*
9  * JMXProcessor.java
10  *
11  * Created on October 6, 2002
12  */

13  
14 package org.webjmx.api;
15
16 import java.lang.reflect.*;
17 import java.io.*;
18 import java.util.*;
19 import javax.management.*;
20
21 import org.webjmx.tags.*;
22
23 /** This class receives input from JMX taglib formMBean tags. It
24  * processes the posted info and makes setAttribute, invoke, createMBean or unregister
25  * calls on an MBeanServer.
26  *
27  * @author John Aronson
28  */

29
30 public class JMXProcessor
31     implements JMXTaglibConstants
32 {
33     private static final Class JavaDoc STRING_ARG_LIST[] = new Class JavaDoc[] { String JavaDoc.class };
34
35     /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
36      * @param input contains data posted via HTTP from the WebJMX tags
37      * @throws Exception
38      */

39     public static void handleRequest(Map input)
40         throws Exception JavaDoc
41     {
42         //update MBean
43
String JavaDoc locator = getValue(input, TAGLIB_SERVER);
44         String JavaDoc objName = getValue(input, TAGLIB_NAME);
45         
46         MBeanServer server = GetServerTag.getMBeanServer(locator);
47         if(server == null)
48             throw new Exception JavaDoc("Server not found");
49
50         if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("invoke: " +getValue(input, TAGLIB_INVOKE));
51         if(getValue(input, TAGLIB_INVOKE) != null)
52             invokeMethod(server, objName, input);
53         else if(getValue(input, TAGLIB_UNREGISTER) != null)
54             unregisterMBean(server, objName);
55         else if(getValue(input, TAGLIB_CLASSNAME) != null)
56             createMBean(server, objName, input);
57         else
58             writeAttributes(server, objName, input);
59     }
60
61     /** Calls setAttribute for all the MBean parameters encoded into the request.
62      * @param server MBeanServer which hosts the MBean to be updated
63      * @param objName ObjectName of the MBean to be updated
64      * @param input holds the new attribute values
65      * @throws Exception
66      */

67     public static void writeAttributes(MBeanServer server, String JavaDoc objName, Map input)
68         throws Exception JavaDoc
69     {
70         ObjectName name = null;
71         Set set = server.queryNames(new ObjectName(objName), null);
72         Iterator it = set.iterator();
73         name = (ObjectName)it.next();
74
75         it = input.keySet().iterator();
76         while(it.hasNext())
77         {
78             String JavaDoc pName = it.next().toString();
79             if(pName.startsWith(TAGLIB_PREFIX))
80                 continue;
81
82             Object JavaDoc param = null;
83             if(getValue(input, TAGLIB_TYPE_PREFIX +pName) != null)
84             {
85                 Class JavaDoc cls = Class.forName(getValue(input, TAGLIB_TYPE_PREFIX +pName));
86                 Constructor cons = cls.getConstructor(STRING_ARG_LIST);
87                 param = cons.newInstance(new String JavaDoc[] { getValue(input, pName) });
88             }else
89                 param = getValue(input, pName);
90             if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("writing attribute [" +pName +", " +param.getClass() +", " +param +"]");
91             server.setAttribute(name, new Attribute(pName, param));
92         }
93     }
94
95     /** Calls unregister on the MBeanServer for the ObjectName
96      * @param server MBeanServer which hosts the MBean to be updated
97      * @param objName ObjectName of the MBean to be updated
98      * @throws Exception
99      */

100     public static void unregisterMBean(MBeanServer server, String JavaDoc objName)
101         throws Exception JavaDoc
102     {
103         ObjectName name = null;
104             Set set = server.queryNames(new ObjectName(objName), null);
105             Iterator it = set.iterator();
106             name = (ObjectName)it.next();
107             
108             server.unregisterMBean(name);
109     }
110
111     /** Calls invoke on the MBeanServer for the MBean with the given params
112      * @param server MBeanServer which hosts the MBean to be updated
113      * @param objName ObjectName of the MBean to be updated
114      * @param input holds the new attribute values
115      * @throws Exception
116      */

117     public static void invokeMethod(MBeanServer server, String JavaDoc objName, Map input)
118         throws Exception JavaDoc
119     {
120         ObjectName name = null;
121         Set set = server.queryNames(new ObjectName(objName), null);
122         Iterator it = set.iterator();
123         name = (ObjectName)it.next();
124         String JavaDoc opName = getValue(input, TAGLIB_INVOKE);
125
126         List l = new ArrayList(), names = getParameterNames(input), l2 = new ArrayList();
127         Collections.sort(names);
128
129         if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("opName: " +opName);
130         if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("paramNames: " +names);
131
132         //get a list of the possible signatures
133
/*MBeanInfo info = server.getMBeanInfo(name);
134         MBeanOperationInfo ops[] = info.getOperations();
135         List sigs = new ArrayList();
136         for(int i = 0; i < ops.length; i++)
137         {
138             if(ops[i].getName().equals(opName))
139                 sigs.add(ops[i].getSignature());
140         }
141         MBeanParameterInfo sig[] = getIntendedSignature(names, sigs);
142         
143         if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("opName: " +opName);
144         if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("paramNames: " +names);
145         if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("operations: " +Arrays.asList(ops));
146         if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("signature count: " +sig.length);
147         if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("parameters: " +Arrays.asList(getParameters(request, sig)));
148         if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("types: " +Arrays.asList(getSignatureTypes(sig)));
149
150         server.invoke(name, opName, getParameters(request, sig), getSignatureTypes(sig));
151          */

152         for(int i = 0; i < names.size(); i++)
153         {
154             String JavaDoc type = getValue(input, TAGLIB_TYPE_PREFIX +names.get(i));
155             if(type == null)
156                 continue;
157             l2.add(type);
158             l.add(convertParameter(new MBeanParameterInfo((String JavaDoc)names.get(i), type, null), input));
159         }
160         Object JavaDoc params[] = l.toArray();
161         String JavaDoc types[] = (String JavaDoc[])l2.toArray(new String JavaDoc[0]);
162
163         if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("parameters: " +Arrays.asList(params));
164         if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("types: " +Arrays.asList(types));
165
166         server.invoke(name, opName, params, types);
167     }
168
169     /** Calls createMBean on the MBeanServer with the given params
170      * @param server MBeanServer which hosts the MBean to be updated
171      * @param objName ObjectName of the MBean to be updated
172      * @param request holds the new attribute values
173      * @throws Exception
174      */

175     public static void createMBean(MBeanServer server, String JavaDoc objName, Map input)
176         throws Exception JavaDoc
177     {
178         ObjectName name = new ObjectName(objName);
179         String JavaDoc className = getValue(input, TAGLIB_CLASSNAME);
180         ObjectName loaderName = null;
181         if(getValue(input, TAGLIB_LOADER) != null)
182             loaderName = new ObjectName(getValue(input, TAGLIB_LOADER));
183
184         List l = new ArrayList(), names = getParameterNames(input), l2 = new ArrayList();
185         Collections.sort(names);
186         for(int i = 0; i < names.size(); i++)
187         {
188             if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("checking parameter: " +names.get(i));
189             String JavaDoc type = getValue(input, TAGLIB_TYPE_PREFIX +names.get(i));
190             if(type == null)
191                 continue;
192             l2.add(type);
193             l.add(convertParameter(new MBeanParameterInfo((String JavaDoc)names.get(i), type, null), input));
194         }
195         Object JavaDoc params[] = l.toArray();
196         String JavaDoc types[] = (String JavaDoc[])l2.toArray(new String JavaDoc[0]);
197             
198         //name might be null?
199
if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("createMBean() call");
200         if(loaderName == null && params == null)
201             server.createMBean(className, name);
202         else if(loaderName == null && params != null)
203             server.createMBean(className, name, params, types);
204         else if(loaderName != null && params == null)
205             server.createMBean(className, name, loaderName);
206         else
207             server.createMBean(className, name, loaderName, params, types);
208     }
209     
210     // filter the non-system request parameter names from the request
211
static private List getParameterNames(Map input)
212     {
213         List l = new ArrayList();
214         Iterator it = input.keySet().iterator();
215         while(it.hasNext())
216         {
217             String JavaDoc pName = it.next().toString();
218             if(pName.startsWith(TAGLIB_PREFIX))
219                 continue;
220             l.add(pName);
221         }
222         return l;
223     }
224     
225     /* Utility method which examines a list of signatures and finds the signature which matches the given list of parameter names
226      * @param paramNames List of paramter names
227      * @param signatures List of signatures [method or constructor]
228      * @throws ServletException
229      * @return a signature with a matching list of parameter names or null
230     public static MBeanParameterInfo[] getIntendedSignature(List paramNames, List signatures)
231         throws ServletException
232     {
233         int count = paramNames.size();
234         for(int i = 0; i < signatures.size(); i++)
235         {
236             MBeanParameterInfo sig[] = (MBeanParameterInfo[])signatures.get(i);
237             if(sig.length != paramNames.size())
238                 continue;
239             for(int j = 0; j < sig.length; j++)
240             {
241                 if(paramNames.indexOf(sig[j].getName()) < 0)
242                     continue;
243             }
244             
245             // if we made this far, then it must be a match
246             return sig;
247         }
248         throw new ServletException("failed to locate valid signature with names: " +paramNames);
249     }
250     
251     static private Object[] getParameters(ServletRequest request, MBeanParameterInfo signature[])
252         throws Exception
253     {
254         ArrayList l = new ArrayList();
255         for(int i = 0; i < signature.length; i++)
256             l.add(convertParameter(signature[i], request));
257         return l.toArray();
258     }
259      */

260     
261     static protected Object JavaDoc convertParameter(MBeanParameterInfo param, Map input)
262         throws Exception JavaDoc
263     {
264         /*if(Boolean.getBoolean(DEBUG_PROP)) System.out.println("param name: " +param.getName());
265         if(Boolean.getBoolean(DEBUG_PROP)) System.out.println(",\trequest value: " +request.getParameter(param.getName()));
266         if(Boolean.getBoolean(DEBUG_PROP)) System.out.println(",\tparam type: " +param.getType());*/

267
268         if(param.getType().equals(Boolean JavaDoc.class.getName()))
269             return new Boolean JavaDoc(getValue(input, param.getName()));
270         else if(param.getType().equals(Integer JavaDoc.class.getName()))
271             return new Integer JavaDoc(getValue(input, param.getName()));
272         else if(param.getType().equals(Long JavaDoc.class.getName()))
273             return new Long JavaDoc(getValue(input, param.getName()));
274         else if(param.getType().equals(Double JavaDoc.class.getName()))
275             return new Double JavaDoc(getValue(input, param.getName()));
276         else if(param.getType().equals(String JavaDoc.class.getName()))
277             return getValue(input, param.getName());
278         else
279         {
280             //the type needs to be directly convertable from a string in the constructor
281
Constructor cons = Class.forName(param.getType()).getConstructor(STRING_ARG_LIST);
282             return cons.newInstance(new Object JavaDoc[] { getValue(input, param.getName()) });
283         }
284     }
285     
286     /** Converts a signature into a list of parameter types
287      * @param signature a list of parameters for an MBeanServer call
288      * @return an array of string which contains the parameter types of the signature
289      */

290     static protected String JavaDoc[] getSignatureTypes(MBeanParameterInfo signature[])
291     {
292         if(signature.length == 0)
293             return null;
294         
295         ArrayList l = new ArrayList();
296         for(int i = 0; i < signature.length; i++)
297             l.add(signature[i].getType());
298         return (String JavaDoc[])l.toArray(new String JavaDoc[0]);
299     }
300     
301     //get the [assumed] single parameter value from the input Map with the given name
302
private static String JavaDoc getValue(Map input, String JavaDoc name)
303     {
304          Object JavaDoc o = input.get(name);
305          if(o == null)
306              return null;
307          if(o instanceof String JavaDoc[])
308          {
309             String JavaDoc values[] = (String JavaDoc[])o;
310             if(values.length == 1)
311                 return values[0];
312             throw new RuntimeException JavaDoc("input map key: " +name +"; unexpected multiple parameter values: " +values.length);
313          }
314          throw new RuntimeException JavaDoc("input map key: " +name +"; bad input map value class: " +o.getClass());
315     }
316 }
317
Popular Tags