1 8 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 29 30 public class JMXProcessor 31 implements JMXTaglibConstants 32 { 33 private static final Class STRING_ARG_LIST[] = new Class [] { String .class }; 34 35 39 public static void handleRequest(Map input) 40 throws Exception 41 { 42 String locator = getValue(input, TAGLIB_SERVER); 44 String objName = getValue(input, TAGLIB_NAME); 45 46 MBeanServer server = GetServerTag.getMBeanServer(locator); 47 if(server == null) 48 throw new Exception ("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 67 public static void writeAttributes(MBeanServer server, String objName, Map input) 68 throws Exception 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 pName = it.next().toString(); 79 if(pName.startsWith(TAGLIB_PREFIX)) 80 continue; 81 82 Object param = null; 83 if(getValue(input, TAGLIB_TYPE_PREFIX +pName) != null) 84 { 85 Class cls = Class.forName(getValue(input, TAGLIB_TYPE_PREFIX +pName)); 86 Constructor cons = cls.getConstructor(STRING_ARG_LIST); 87 param = cons.newInstance(new String [] { 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 100 public static void unregisterMBean(MBeanServer server, String objName) 101 throws Exception 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 117 public static void invokeMethod(MBeanServer server, String objName, Map input) 118 throws Exception 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 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 152 for(int i = 0; i < names.size(); i++) 153 { 154 String 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 )names.get(i), type, null), input)); 159 } 160 Object params[] = l.toArray(); 161 String types[] = (String [])l2.toArray(new String [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 175 public static void createMBean(MBeanServer server, String objName, Map input) 176 throws Exception 177 { 178 ObjectName name = new ObjectName(objName); 179 String 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 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 )names.get(i), type, null), input)); 194 } 195 Object params[] = l.toArray(); 196 String types[] = (String [])l2.toArray(new String [0]); 197 198 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 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 pName = it.next().toString(); 218 if(pName.startsWith(TAGLIB_PREFIX)) 219 continue; 220 l.add(pName); 221 } 222 return l; 223 } 224 225 260 261 static protected Object convertParameter(MBeanParameterInfo param, Map input) 262 throws Exception 263 { 264 267 268 if(param.getType().equals(Boolean .class.getName())) 269 return new Boolean (getValue(input, param.getName())); 270 else if(param.getType().equals(Integer .class.getName())) 271 return new Integer (getValue(input, param.getName())); 272 else if(param.getType().equals(Long .class.getName())) 273 return new Long (getValue(input, param.getName())); 274 else if(param.getType().equals(Double .class.getName())) 275 return new Double (getValue(input, param.getName())); 276 else if(param.getType().equals(String .class.getName())) 277 return getValue(input, param.getName()); 278 else 279 { 280 Constructor cons = Class.forName(param.getType()).getConstructor(STRING_ARG_LIST); 282 return cons.newInstance(new Object [] { getValue(input, param.getName()) }); 283 } 284 } 285 286 290 static protected String [] 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 [])l.toArray(new String [0]); 299 } 300 301 private static String getValue(Map input, String name) 303 { 304 Object o = input.get(name); 305 if(o == null) 306 return null; 307 if(o instanceof String []) 308 { 309 String values[] = (String [])o; 310 if(values.length == 1) 311 return values[0]; 312 throw new RuntimeException ("input map key: " +name +"; unexpected multiple parameter values: " +values.length); 313 } 314 throw new RuntimeException ("input map key: " +name +"; bad input map value class: " +o.getClass()); 315 } 316 } 317 | Popular Tags |