// READ THIS INTO YOUR MAIN CLASS (anywhere), then search and replace: // "TEMPLATE" --> Your class name /** * Primary structure holding data pertaining to each managed * EasyBean implementation (subclass). */ private HandledBean hbean = new HandledBean(); private class HandledBean { String className = null; MBeanInfo mBeanInfo = null; HashMap methodHash = new HashMap(); HashSet getterSet = new HashSet(); HashSet setterSet = new HashSet(); HashSet operationSet = new HashSet(); HashSet isSet = new HashSet(); HashSet constructorSet = new HashSet(); /** * Constructor. inClass and desc are passed here when * implementing classes call static method EasyBean.publish(). * * @param desc JMX description of our class * @return HandledBean For use by EasyBean */ HandledBean() { HashMap mbaiHash = new HashMap(); HashMap mboiHash = new HashMap(); Method method; Constructor cons; Descriptor da[]; ConsDescriptor cda[]; MBeanAttributeInfo mbai; try { className = thisClass.getName(); Method[] methods = thisClass.getMethods(); for (int i = 0; i < methods.length; i++) { method = methods[i]; if (Modifier.isStatic(method.getModifiers())) continue; methodHash.put(method.getName(), method); } da = null; try { da = (Descriptor[]) (thisClass.getDeclaredField("setterDescriptors")). get(null); } catch (NoSuchFieldException nsfe) {}; if (da != null) { for (int i = 0; i < da.length; i++) { setterSet.add(da[i].name); method = (Method) methodHash.get( TEMPLATE.makeSetterName(da[i].name)); if (method == null) throw new Exception("No setter method for '" + da[i].name + "'"); if (mbaiHash.containsKey(da[i].name)) { mbai = (MBeanAttributeInfo) mbaiHash.get( da[i].name); mbai = new MBeanAttributeInfo( mbai.getName(), mbai.getType(), ((da[i].description == null) ? mbai.getDescription() : da[i].description ), true, mbai.isWritable(), mbai.isIs()); } else { mbai = new MBeanAttributeInfo( da[i].name, ((method.getParameterTypes())[0]). getName().toString(), da[i].description, false, true, false); } mbaiHash.put(da[i].name, mbai); } } da = null; try { da = (Descriptor[]) (thisClass.getDeclaredField("getterDescriptors")). get(null); } catch (NoSuchFieldException nsfe) {}; if (da != null) { for (int i = 0; i < da.length; i++) { getterSet.add(da[i].name); method = (Method) methodHash.get( TEMPLATE.makeGetterName(da[i].name)); if (method == null) throw new Exception("No getter method for '" + da[i].name + "'"); if (mbaiHash.containsKey(da[i].name)) { mbai = (MBeanAttributeInfo) mbaiHash.get( da[i].name); mbai = new MBeanAttributeInfo( mbai.getName(), mbai.getType(), ((da[i].description == null) ? mbai.getDescription() : da[i].description ), true, mbai.isWritable(), mbai.isIs()); } else { mbai = new MBeanAttributeInfo( da[i].name, method.getReturnType().getName().toString(), da[i].description, true, false, false); } mbaiHash.put(da[i].name, mbai); } } da = null; try { da = (Descriptor[]) (thisClass.getDeclaredField("isDescriptors")).get(null); } catch (NoSuchFieldException nsfe) {}; if (da != null) { for (int i = 0; i < da.length; i++) { isSet.add(da[i].name); method = (Method) methodHash.get( TEMPLATE.makeIsName(da[i].name)); if (method == null) throw new Exception("No is method for '" + da[i].name + "'"); if (mbaiHash.containsKey(da[i].name)) { mbai = (MBeanAttributeInfo) mbaiHash.get(da[i].name); mbai = new MBeanAttributeInfo( mbai.getName(), mbai.getType(), ((da[i].description == null) ? mbai.getDescription() : da[i].description ), true, mbai.isWritable(), true); } else { mbai = new MBeanAttributeInfo( da[i].name, method.getReturnType().getName().toString(), da[i].description, true, false, true); } mbaiHash.put(da[i].name, mbai); } } da = null; try { da = (Descriptor[]) (thisClass.getDeclaredField("operationDescriptors")). get(null); } catch (NoSuchFieldException nsfe) {}; if (da != null) { for (int i = 0; i < da.length; i++) { operationSet.add(da[i].name); method = (Method) methodHash.get(da[i].name); if (method == null) throw new Exception("No operation method for '" + da[i].name + "'"); mboiHash.put(da[i].name, new MBeanOperationInfo( da[i].description, method)); } } cda = null; try { cda = (ConsDescriptor[]) (thisClass.getDeclaredField("consDescriptors")). get(null); } catch (NoSuchFieldException nsfe) {}; MBeanConstructorInfo[] mbciArray = null; if (cda != null) { mbciArray = new MBeanConstructorInfo[cda.length]; for (int i = 0; i < cda.length; i++) { constructorSet.add(cda[i].classes); cons = (Constructor) thisClass.getConstructor(cda[i].classes); if (cons == null) throw new Exception("No constructor for '" + cda[i].classes + "'"); mbciArray[i] = new MBeanConstructorInfo(cda[i].description, cons); } } Iterator it; int i; i = 0; MBeanAttributeInfo[] mbaiArray = new MBeanAttributeInfo[mbaiHash.size()]; it = mbaiHash.values().iterator(); while (it.hasNext()) mbaiArray[i++] = (MBeanAttributeInfo) it.next(); i = 0; MBeanOperationInfo[] mboiArray = new MBeanOperationInfo[mboiHash.size()]; it = mboiHash.values().iterator(); while (it.hasNext()) mboiArray[i++] = (MBeanOperationInfo) it.next(); mBeanInfo = new MBeanInfo(className, classDescription, mbaiArray, mbciArray, mboiArray, null); } catch (Exception e) { System.err.println("Classes which extend TEMPLATE must set " + "static variables according " + "to the EasyBean API. " + e + '\n'); e.printStackTrace(); } } } /** * Convenience method * * @param inString * @return inString with initial cap */ static private String capString(String inString) throws Exception { if (inString == null || inString.length() == 0) throw new Exception("make*Name called with empty base name"); return Character.toUpperCase(inString.charAt(0)) + ((inString.length() > 1) ? inString.substring(1) : ""); } /** * Convenience method * * @param inString Base name. * @return A setter method name, like setBase */ static public String makeSetterName(String inString) throws Exception { return "set" + capString(inString); } /** * Convenience method * * @param inString Base name. * @return A setter method name, like getBase */ static public String makeGetterName(String inString) throws Exception { return "get" + capString(inString); } /** * Convenience method * * @param inString Base name. * @return An is method name, like isBase */ static public String makeIsName(String inString) throws Exception { return "is" + capString(inString); } /* * IMPLEMENTATION OF DynamicMBean * * The remaining methods provide implementation of the DyanmicBean * interface. */ /** @see javax.management.DynamicBean interface */ public Object getAttribute(String inString) throws AttributeNotFoundException, MBeanException { if (!hbean.getterSet.contains(inString)) throw new AttributeNotFoundException(inString); try { return ((Method) (hbean.methodHash.get(makeGetterName(inString)))). invoke(this, null); } catch (Exception e) { throw new MBeanException(e); } } /** @see javax.management.DynamicBean interface */ public AttributeList getAttributes(String[] inSA) { AttributeList al = new AttributeList(); for (int i = 0; i < inSA.length; i++) try { al.add(new Attribute(inSA[i], getAttribute(inSA[i]))); } catch (AttributeNotFoundException anfe) { } catch (MBeanException anfe) {} return al; } /** @see javax.management.DynamicBean interface */ public MBeanInfo getMBeanInfo() { return hbean.mBeanInfo; } /** @see javax.management.DynamicBean interface */ public Object invoke(String inString, Object[] inParams, String[] inSign) throws MBeanException { if (!hbean.operationSet.contains(inString)) throw new MBeanException(new Exception(inString)); try { return ((Method) hbean.methodHash.get(inString)). invoke(this, inParams); } catch (IllegalAccessException iae) { throw new MBeanException(iae); } catch (InvocationTargetException ite) { throw new MBeanException(ite); } } /** @see javax.management.DynamicBean interface */ public void setAttribute(Attribute inAttribute) throws AttributeNotFoundException, MBeanException { String attName = inAttribute.getName(); if (!hbean.setterSet.contains(attName)) throw new AttributeNotFoundException(attName); Object[] oa = { inAttribute.getValue() }; try { ((Method) hbean.methodHash.get(makeSetterName(attName))). invoke(this, oa); } catch (Exception e) { throw new MBeanException(e); } } /** @see javax.management.DynamicBean interface */ public AttributeList setAttributes(AttributeList inAL) { AttributeList al = new AttributeList(); Attribute att; for (int i = 0; i < inAL.size(); i++) { try { att = (Attribute) inAL.get(i); setAttribute(att); al.add(att); } catch (AttributeNotFoundException anfe) { } catch (MBeanException anfe) {} } return al; } }