1 17 18 package org.apache.geronimo.console.jmxmanager; 19 20 import java.net.URI ; 21 import java.net.URL ; 22 import java.text.DateFormat ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.TreeMap ; 31 32 import javax.management.ObjectName ; 33 34 import org.apache.geronimo.gbean.AbstractName; 35 import org.apache.geronimo.gbean.AbstractNameQuery; 36 import org.apache.geronimo.gbean.GAttributeInfo; 37 import org.apache.geronimo.gbean.GBeanInfo; 38 import org.apache.geronimo.gbean.GOperationInfo; 39 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory; 40 import org.apache.geronimo.kernel.GBeanNotFoundException; 41 import org.apache.geronimo.kernel.Kernel; 42 import org.apache.geronimo.kernel.KernelRegistry; 43 44 47 public class JMXManagerHelper { 48 49 private static final String ALL_MBEANS = "AllMBeans"; 50 private static final String GBEANINFO_NAME = "GBeanInfo"; 51 private static final String SERVICEMODULE_KEY = "ServiceModule"; 52 53 private final Kernel kernel; 54 55 58 public JMXManagerHelper() { 59 kernel = KernelRegistry.getSingleKernel(); 60 } 61 62 65 public Collection listByDomain(String domain) { 66 Collection result = new ArrayList (); 67 if ((domain == null) || (domain.trim().length() == 0)) { 68 return result; 69 } 70 71 return listByPattern(domain + ":*"); 72 } 73 74 77 public Collection listBySubstring(String substring) { 78 Collection result = new ArrayList (); 79 if ((substring == null) || (substring.trim().length() == 0)) { 80 return result; 81 } 82 83 Collection abstractNames = getAbstractNames(substring); 84 for (Iterator it = abstractNames.iterator(); it.hasNext();) { 85 AbstractName aname = (AbstractName) it.next(); 86 ObjectName oname = aname.getObjectName(); 87 String [] pair = { aname.toString(), oname.toString() }; 88 result.add(pair); 89 } 90 91 return result; 92 } 93 94 97 public Collection listByPattern(String pattern) { 98 Collection result = new ArrayList (); 99 if ((pattern == null) || (pattern.trim().length() == 0)) { 100 return result; 101 } 102 103 try { 104 Map abstractNames = getAbstractNames(); 109 ObjectName onamePattern = new ObjectName (pattern); 110 Set beans = kernel.listGBeans(onamePattern); 111 for (Iterator it = beans.iterator(); it.hasNext();) { 112 ObjectName oname = (ObjectName ) it.next(); 113 AbstractName aname = (AbstractName) abstractNames.get(oname); 114 String [] pair = { aname.toString(), oname.toString() }; 115 result.add(pair); 116 } 117 } catch (Exception e) { 118 } 120 121 return result; 122 } 123 124 127 public Collection listByJ2EEType(String type) { 128 Collection result = new ArrayList (); 129 Map m = null; 130 131 if ((type == null) || (type.trim().length() == 0)) { 132 return result; 133 } else { 134 if (ALL_MBEANS.equalsIgnoreCase(type)) { 135 m = Collections.EMPTY_MAP; 136 } else { 137 m = Collections.singletonMap(NameFactory.J2EE_TYPE, type); 138 } 139 } 140 141 AbstractNameQuery query = new AbstractNameQuery(null, m, 142 Collections.EMPTY_SET); 143 Set beans = kernel.listGBeans(query); 144 for (Iterator it = beans.iterator(); it.hasNext();) { 145 AbstractName abstractName = (AbstractName) it.next(); 146 ObjectName objectName = abstractName.getObjectName(); 147 String [] pair = { abstractName.toString(), objectName.toString() }; 148 result.add(pair); 149 } 150 151 return result; 152 } 153 154 157 public Collection getServiceModules() { 158 Map svcModules = new TreeMap (); 159 Collection svcModuleMBeans = getAbstractNames(SERVICEMODULE_KEY + '='); 160 for (Iterator it = svcModuleMBeans.iterator(); it.hasNext();) { 161 AbstractName aname = (AbstractName) it.next(); 162 String svcModule = aname.getNameProperty(SERVICEMODULE_KEY); 163 if (!svcModules.containsKey(svcModule)) { 164 svcModules.put(svcModule, null); 165 } 166 } 167 168 return svcModules.keySet(); 169 } 170 171 174 private Collection getAbstractNames(String substring) { 175 Collection result = new ArrayList (); 176 if ((substring == null) || (substring.trim().length() == 0)) { 177 return result; 178 } 179 180 Map abstractNames = getAbstractNames(); 181 for (Iterator it = abstractNames.keySet().iterator(); it.hasNext();) { 182 ObjectName oname = (ObjectName ) it.next(); 183 if (oname.toString().indexOf(substring) > 0) { 184 AbstractName aname = (AbstractName) abstractNames.get(oname); 185 result.add(aname); 186 } 187 } 188 189 return result; 190 } 191 192 195 private Map getAbstractNames() { 196 Map abstractNames = new HashMap (); 197 AbstractNameQuery query = new AbstractNameQuery(null, 199 Collections.EMPTY_MAP, Collections.EMPTY_SET); 200 Set allBeans = kernel.listGBeans(query); 201 for (Iterator it = allBeans.iterator(); it.hasNext();) { 202 AbstractName abstractName = (AbstractName) it.next(); 203 ObjectName objectName = abstractName.getObjectName(); 204 abstractNames.put(objectName, abstractName); 205 } 206 207 return abstractNames; 208 } 209 210 213 public Collection getAttributes(String abstractName) { 214 Map attributes = new TreeMap (); 215 try { 216 AbstractName aname = new AbstractName(URI.create(abstractName)); 217 GBeanInfo info = kernel.getGBeanInfo(aname); 218 Set attribs = info.getAttributes(); 219 for (Iterator i = attribs.iterator(); i.hasNext();) { 220 GAttributeInfo attribInfo = (GAttributeInfo) i.next(); 221 String attribName = attribInfo.getName(); 223 if (!GBEANINFO_NAME.equals(attribName)) { 224 Map attribInfoMap = getAttribInfoAsMap(aname, attribInfo); 225 attributes.put(attribName, attribInfoMap); 226 } 227 } 228 } catch (GBeanNotFoundException e) { 229 } 231 232 return attributes.values(); 233 } 234 235 238 private Map getAttribInfoAsMap(AbstractName abstractName, 239 GAttributeInfo attribInfo) { 240 Map map = new TreeMap (); 241 String attribName = attribInfo.getName(); 242 map.put("name", attribName); 243 map.put("getterName", attribInfo.getGetterName()); 244 map.put("setterName", attribInfo.getSetterName()); 245 map.put("type", attribInfo.getType()); 246 map.put("manageable", String.valueOf(attribInfo.isManageable())); 247 map.put("persistent", String.valueOf(attribInfo.isPersistent())); 248 map.put("readable", String.valueOf(attribInfo.isReadable())); 249 map.put("writable", String.valueOf(attribInfo.isWritable())); 250 if (attribInfo.isReadable()) { 251 String attribValue = ""; 252 try { 253 Object value = kernel.getAttribute(abstractName, attribName); 254 if (value != null) { 255 attribValue = value.toString(); 256 } 257 } catch (Exception e) { 258 attribValue = "** EXCEPTION: " + e; 260 } 261 map.put("value", attribValue); 262 } 263 return map; 264 } 265 266 269 public Collection getOperations(String abstractName) { 270 Map operations = new TreeMap (); 271 try { 272 AbstractName aname = new AbstractName(URI.create(abstractName)); 273 GBeanInfo info = kernel.getGBeanInfo(aname); 274 Set opers = info.getOperations(); 275 for (Iterator i = opers.iterator(); i.hasNext();) { 276 GOperationInfo operInfo = (GOperationInfo) i.next(); 277 Map operInfoMap = getOperInfoAsMap(operInfo); 278 String operName = (String ) operInfoMap.get("name"); 279 operations.put(operName, operInfoMap); 280 } 281 } catch (Exception e) { 282 } 284 285 return operations.values(); 286 } 287 288 291 private Map getOperInfoAsMap(GOperationInfo operInfo) { 292 Map map = new TreeMap (); 293 map.put("methodName", operInfo.getMethodName()); 294 map.put("name", operInfo.getName()); 295 map.put("parameterList", operInfo.getParameterList()); 296 return map; 297 } 298 299 302 public Collection getMBeanInfo(String abstractName) { 303 Collection info = new ArrayList (); 304 try { 305 AbstractName aname = new AbstractName(URI.create(abstractName)); 306 info.add(new String [] { "abstractName", aname.toString() }); 307 ObjectName oname = aname.getObjectName(); 308 info.add(new String [] { "objectName", oname.toString() }); 309 GBeanInfo beanInfo = kernel.getGBeanInfo(aname); 310 String className = beanInfo.getClassName(); 311 info.add(new String [] { "className", className }); 312 String domain = oname.getDomain(); 313 info.add(new String [] { "domain", domain }); 314 String j2eeType = beanInfo.getJ2eeType(); 315 info.add(new String [] { "j2eeType", j2eeType }); 316 } catch (Exception e) { 319 } 321 322 return info; 323 } 324 325 328 public String [] invokeOperWithArgs(String abstractName, String methodName, 329 String [] args, String [] types) { 330 String [] result = new String [2]; result[0] = methodName + "(...)"; 332 333 try { 334 Object [] newArgs = processOperArgs(args, types); 335 AbstractName aname = new AbstractName(URI.create(abstractName)); 336 Object res = kernel.invoke(aname, methodName, newArgs, types); 337 if (result != null) { 338 result[1] = res.toString(); 339 } else { 340 result[1] = "<null>"; 341 } 342 } catch (Exception e) { 343 result[1] = e.toString(); 344 } 345 346 return result; 347 } 348 349 352 public String [] invokeOperNoArgs(String abstractName, String methodName) { 353 String [] result = new String [2]; result[0] = methodName + "()"; 355 356 try { 357 AbstractName aname = new AbstractName(URI.create(abstractName)); 358 Object res = kernel.invoke(aname, methodName); 359 if (result != null) { 360 result[1] = res.toString(); 361 } else { 362 result[1] = "<null>"; 363 } 364 } catch (Exception e) { 365 result[1] = e.toString(); 366 } 367 368 return result; 369 } 370 371 374 private Object [] processOperArgs(String [] args, String [] types) 375 throws Exception { 376 Object [] newArgs = new Object [args.length]; 378 for (int i = 0; i < args.length; i++) { 379 String type = types[i]; 380 String arg = args[i]; 381 newArgs[i] = createObject(arg, type); 382 } 383 384 return newArgs; 385 } 386 387 390 private Object createObject(String arg, String type) throws Exception { 391 Object newArg = new Object (); 392 if ("byte".equals(type) || "java.lang.Byte".equals(type)) { 393 newArg = new Byte (arg); 394 } else if ("short".equals(type) || "java.lang.Short".equals(type)) { 395 newArg = new Short (arg); 396 } else if ("int".equals(type) || "java.lang.Integer".equals(type)) { 397 newArg = new Integer (arg); 398 } else if ("long".equals(type) || "java.lang.Long".equals(type)) { 399 newArg = new Long (arg); 400 } else if ("float".equals(type) || "java.lang.Float".equals(type)) { 401 newArg = new Float (arg); 402 } else if ("double".equals(type) || "java.lang.Double".equals(type)) { 403 newArg = new Double (arg); 404 } else if ("char".equals(type) || "java.lang.Character".equals(type)) { 405 newArg = new Character (arg.charAt(0)); 406 } else if ("boolean".equals(type) || "java.lang.Boolean".equals(type)) { 407 newArg = new Boolean (arg); 408 } else if ("java.lang.String".equals(type)) { 409 newArg = arg; 410 } else if ("java.lang.Object".equals(type)) { 411 newArg = arg; 412 } else if ("java.util.Date".equals(type)) { 413 newArg = DateFormat.getInstance().parse(arg); 414 } else if ("java.net.URL".equals(type)) { 415 newArg = new URL (arg); 416 } else if ("java.net.URI".equals(type)) { 417 newArg = new URI (arg); 418 } else if ("javax.management.ObjectName".equals(type)) { 419 newArg = new ObjectName (arg); 420 } else if ("org.apache.geronimo.gbean.AbstractName".equals(type)) { 421 newArg = new AbstractName(URI.create(arg)); 422 } else { 423 String errorMsg = "Can't create instance of '" + type + "' using '" 425 + arg + "'."; 426 throw new IllegalArgumentException (errorMsg); 427 } 428 429 return newArg; 430 } 431 432 435 public String [] setAttribute(String abstractName, String attribName, 436 String attribValue, String attribType) { 437 String [] result = new String [2]; result[0] = attribName; 439 result[1] = "<SUCCESS>"; 440 441 try { 442 AbstractName aname = new AbstractName(URI.create(abstractName)); 443 Object newAttribValue = createObject(attribValue, attribType); 444 kernel.setAttribute(aname, attribName, newAttribValue); 445 } catch (Exception e) { 446 result[1] = e.toString(); 447 } 448 449 return result; 450 } 451 452 } 453 | Popular Tags |