adding in some more test cases which now include the MBeanContainer
This commit is contained in:
parent
33270eced5
commit
6c0bb390ae
|
@ -57,7 +57,11 @@ public class MBeanContainer extends AbstractLifeCycle implements Container.Liste
|
||||||
*/
|
*/
|
||||||
public synchronized ObjectName findMBean(Object object)
|
public synchronized ObjectName findMBean(Object object)
|
||||||
{
|
{
|
||||||
|
|
||||||
ObjectName bean = _beans.get(object);
|
ObjectName bean = _beans.get(object);
|
||||||
|
|
||||||
|
LOG.debug("findMBean {} {}", object, bean );
|
||||||
|
|
||||||
return bean == null ? null : bean;
|
return bean == null ? null : bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,6 +73,8 @@ public class MBeanContainer extends AbstractLifeCycle implements Container.Liste
|
||||||
*/
|
*/
|
||||||
public synchronized Object findBean(ObjectName oname)
|
public synchronized Object findBean(ObjectName oname)
|
||||||
{
|
{
|
||||||
|
LOG.debug("findBean {}", oname );
|
||||||
|
|
||||||
for (Map.Entry<Object, ObjectName> entry : _beans.entrySet())
|
for (Map.Entry<Object, ObjectName> entry : _beans.entrySet())
|
||||||
{
|
{
|
||||||
ObjectName bean = entry.getValue();
|
ObjectName bean = entry.getValue();
|
||||||
|
|
|
@ -237,7 +237,16 @@ public class ObjectMBean implements DynamicMBean
|
||||||
|
|
||||||
// Process Type Annotations
|
// Process Type Annotations
|
||||||
ManagedObject primary = o_class.getAnnotation( ManagedObject.class);
|
ManagedObject primary = o_class.getAnnotation( ManagedObject.class);
|
||||||
desc = primary.value();
|
|
||||||
|
if ( primary != null )
|
||||||
|
{
|
||||||
|
desc = primary.value();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LOG.debug("No @ManagedObject declared on {}", _managed.getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// For each influence
|
// For each influence
|
||||||
for (int i=0;i<influences.size();i++)
|
for (int i=0;i<influences.size();i++)
|
||||||
|
@ -252,49 +261,42 @@ public class ObjectMBean implements DynamicMBean
|
||||||
LOG.debug("Annotations not found for: " + oClass.getCanonicalName() );
|
LOG.debug("Annotations not found for: " + oClass.getCanonicalName() );
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
// Process Field Annotations
|
||||||
{
|
for (Field field : oClass.getDeclaredFields())
|
||||||
// Process Field Annotations
|
|
||||||
for ( Field field : oClass.getDeclaredFields())
|
|
||||||
{
|
|
||||||
LOG.debug("Checking: " + field.getName());
|
|
||||||
ManagedAttribute fieldAnnotation = field.getAnnotation(ManagedAttribute.class);
|
|
||||||
|
|
||||||
if ( fieldAnnotation != null )
|
|
||||||
{
|
|
||||||
LOG.debug("Field Annotation found for: " + field.getName() );
|
|
||||||
attributes.add( defineAttribute(field.getName(), fieldAnnotation));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process Method Annotations
|
|
||||||
|
|
||||||
for ( Method method : oClass.getDeclaredMethods() )
|
|
||||||
{
|
|
||||||
ManagedAttribute methodAttributeAnnotation = method.getAnnotation(ManagedAttribute.class);
|
|
||||||
|
|
||||||
if ( methodAttributeAnnotation != null )
|
|
||||||
{
|
|
||||||
// TODO sort out how a proper name could get here, its a method name as an attribute at this point.
|
|
||||||
LOG.debug("Attribute Annotation found for: " + method.getName() );
|
|
||||||
attributes.add(defineAttribute(method.getName(),methodAttributeAnnotation));
|
|
||||||
}
|
|
||||||
|
|
||||||
ManagedOperation methodOperationAnnotation = method.getAnnotation(ManagedOperation.class);
|
|
||||||
|
|
||||||
if (methodOperationAnnotation != null)
|
|
||||||
{
|
|
||||||
LOG.debug("Method Annotation found for: " + method.getName());
|
|
||||||
operations.add(defineOperation(method,methodOperationAnnotation));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
catch(MissingResourceException e)
|
|
||||||
{
|
{
|
||||||
LOG.ignore(e);
|
LOG.debug("Checking: " + field.getName());
|
||||||
|
ManagedAttribute fieldAnnotation = field.getAnnotation(ManagedAttribute.class);
|
||||||
|
|
||||||
|
if (fieldAnnotation != null)
|
||||||
|
{
|
||||||
|
LOG.debug("Field Annotation found for: " + field.getName());
|
||||||
|
attributes.add(defineAttribute(field.getName(),fieldAnnotation));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Process Method Annotations
|
||||||
|
|
||||||
|
for (Method method : oClass.getDeclaredMethods())
|
||||||
|
{
|
||||||
|
ManagedAttribute methodAttributeAnnotation = method.getAnnotation(ManagedAttribute.class);
|
||||||
|
|
||||||
|
if (methodAttributeAnnotation != null)
|
||||||
|
{
|
||||||
|
// TODO sort out how a proper name could get here, its a method name as an attribute at this point.
|
||||||
|
LOG.debug("Attribute Annotation found for: " + method.getName());
|
||||||
|
attributes.add(defineAttribute(method.getName(),methodAttributeAnnotation));
|
||||||
|
}
|
||||||
|
|
||||||
|
ManagedOperation methodOperationAnnotation = method.getAnnotation(ManagedOperation.class);
|
||||||
|
|
||||||
|
if (methodOperationAnnotation != null)
|
||||||
|
{
|
||||||
|
LOG.debug("Method Annotation found for: " + method.getName());
|
||||||
|
operations.add(defineOperation(method,methodOperationAnnotation));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_info = new MBeanInfo(o_class.getName(),
|
_info = new MBeanInfo(o_class.getName(),
|
||||||
|
@ -575,7 +577,7 @@ public class ObjectMBean implements DynamicMBean
|
||||||
{
|
{
|
||||||
//String name = field.getName();
|
//String name = field.getName();
|
||||||
String description = attributeAnnotation.value();
|
String description = attributeAnnotation.value();
|
||||||
boolean writable = attributeAnnotation.readonly();
|
boolean writable = !attributeAnnotation.readonly();
|
||||||
boolean onMBean = attributeAnnotation.proxied();
|
boolean onMBean = attributeAnnotation.proxied();
|
||||||
boolean convert = attributeAnnotation.managed();
|
boolean convert = attributeAnnotation.managed();
|
||||||
|
|
||||||
|
@ -597,9 +599,6 @@ public class ObjectMBean implements DynamicMBean
|
||||||
{
|
{
|
||||||
if ((methods[m].getModifiers() & Modifier.PUBLIC) == 0)
|
if ((methods[m].getModifiers() & Modifier.PUBLIC) == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
LOG.debug("Declared Getter: {} vs {}", declaredGetter, methods[m].getName());
|
|
||||||
|
|
||||||
|
|
||||||
// Check if it is a declared getter
|
// Check if it is a declared getter
|
||||||
if (methods[m].getName().equals(declaredGetter) && methods[m].getParameterTypes().length == 0)
|
if (methods[m].getName().equals(declaredGetter) && methods[m].getParameterTypes().length == 0)
|
||||||
|
@ -694,20 +693,21 @@ public class ObjectMBean implements DynamicMBean
|
||||||
{
|
{
|
||||||
if (type==null)
|
if (type==null)
|
||||||
{
|
{
|
||||||
LOG.warn("No mbean type for " + name+" on "+_managed.getClass());
|
LOG.warn("No mbean type for {} on {}", name, _managed.getClass());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type.isPrimitive() && !type.isArray())
|
if (type.isPrimitive() && !type.isArray())
|
||||||
{
|
{
|
||||||
LOG.warn("Cannot convert mbean primative " + name);
|
LOG.warn("Cannot convert mbean primative {}", name);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
LOG.debug("passed convert checks {} for type {}", name, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getter == null && setter == null)
|
if (getter == null && setter == null)
|
||||||
{
|
{
|
||||||
LOG.warn("No mbean getter or setters found for " + name+ " in "+oClass);
|
LOG.warn("No mbean getter or setters found for {} in {}", name, oClass);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -721,15 +721,21 @@ public class ObjectMBean implements DynamicMBean
|
||||||
if (convert)
|
if (convert)
|
||||||
{
|
{
|
||||||
_convert.add(name);
|
_convert.add(name);
|
||||||
|
|
||||||
if (type.isArray())
|
if (type.isArray())
|
||||||
|
{
|
||||||
info= new MBeanAttributeInfo(name,OBJECT_NAME_ARRAY_CLASS,description,getter!=null,setter!=null,getter!=null&&getter.getName().startsWith("is"));
|
info= new MBeanAttributeInfo(name,OBJECT_NAME_ARRAY_CLASS,description,getter!=null,setter!=null,getter!=null&&getter.getName().startsWith("is"));
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
info= new MBeanAttributeInfo(name,OBJECT_NAME_CLASS,description,getter!=null,setter!=null,getter!=null&&getter.getName().startsWith("is"));
|
info= new MBeanAttributeInfo(name,OBJECT_NAME_CLASS,description,getter!=null,setter!=null,getter!=null&&getter.getName().startsWith("is"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
info= new MBeanAttributeInfo(name,description,getter,setter);
|
info= new MBeanAttributeInfo(name,description,getter,setter);
|
||||||
|
}
|
||||||
|
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
|
|
@ -24,6 +24,9 @@ public class Derived extends Base implements Signature
|
||||||
@ManagedAttribute(value="The full name of something", getter="getFullName", setter="setFullName")
|
@ManagedAttribute(value="The full name of something", getter="getFullName", setter="setFullName")
|
||||||
String fname="Full Name";
|
String fname="Full Name";
|
||||||
|
|
||||||
|
@ManagedAttribute( value="sample managed object")
|
||||||
|
Managed managedInstance = new Managed();
|
||||||
|
|
||||||
public String getFullName()
|
public String getFullName()
|
||||||
{
|
{
|
||||||
return fname;
|
return fname;
|
||||||
|
@ -50,4 +53,16 @@ public class Derived extends Base implements Signature
|
||||||
{
|
{
|
||||||
return "bad";
|
return "bad";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Managed getManagedInstance()
|
||||||
|
{
|
||||||
|
return managedInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setManagedInstance(Managed managedInstance)
|
||||||
|
{
|
||||||
|
this.managedInstance = managedInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.acme;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.util.annotation.ManagedAttribute;
|
||||||
|
import org.eclipse.jetty.util.annotation.ManagedObject;
|
||||||
|
|
||||||
|
@ManagedObject(value="Managed Object", wrapper="com.acme.jmx.ManagedMBean")
|
||||||
|
public class Managed
|
||||||
|
{
|
||||||
|
@ManagedAttribute("Managed Attribute")
|
||||||
|
String managed = "foo";
|
||||||
|
|
||||||
|
public String getManaged()
|
||||||
|
{
|
||||||
|
return managed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setManaged(String managed)
|
||||||
|
{
|
||||||
|
this.managed = managed;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.acme.jmx;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.jmx.ObjectMBean;
|
||||||
|
import org.eclipse.jetty.util.annotation.ManagedAttribute;
|
||||||
|
import org.eclipse.jetty.util.annotation.ManagedObject;
|
||||||
|
import org.eclipse.jetty.util.annotation.ManagedOperation;
|
||||||
|
|
||||||
|
import com.acme.Derived;
|
||||||
|
|
||||||
|
@ManagedObject("Managed MBean Wrapper")
|
||||||
|
public class ManagedMBean extends ObjectMBean
|
||||||
|
{
|
||||||
|
public ManagedMBean(Object managedObject)
|
||||||
|
{
|
||||||
|
super(managedObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ManagedOperation(value="test of proxy operations", managed=true)
|
||||||
|
public String good()
|
||||||
|
{
|
||||||
|
return "not " + ((Derived)_managed).bad();
|
||||||
|
}
|
||||||
|
|
||||||
|
@ManagedAttribute(value="test of proxy attributes", getter="goop", proxied=true)
|
||||||
|
public String goop()
|
||||||
|
{
|
||||||
|
return "goop";
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,25 +27,54 @@ import junit.framework.Assert;
|
||||||
|
|
||||||
import org.eclipse.jetty.util.log.Log;
|
import org.eclipse.jetty.util.log.Log;
|
||||||
import org.eclipse.jetty.util.log.Logger;
|
import org.eclipse.jetty.util.log.Logger;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.acme.Derived;
|
import com.acme.Derived;
|
||||||
|
import com.acme.Managed;
|
||||||
|
|
||||||
|
|
||||||
public class ObjectMBeanTest
|
public class ObjectMBeanTest
|
||||||
{
|
{
|
||||||
private static final Logger LOG = Log.getLogger(ObjectMBeanTest.class);
|
private static final Logger LOG = Log.getLogger(ObjectMBeanTest.class);
|
||||||
|
|
||||||
|
private static MBeanContainer container;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void beforeClass() throws Exception
|
||||||
|
{
|
||||||
|
container = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
|
||||||
|
container.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void afterClass() throws Exception
|
||||||
|
{
|
||||||
|
container.stop();
|
||||||
|
container = null;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* this test uses the com.acme.Derived test classes
|
* this test uses the com.acme.Derived test classes
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testMbeanInfo() throws Exception
|
public void testMbeanInfo() throws Exception
|
||||||
{
|
{
|
||||||
|
|
||||||
Derived derived = new Derived();
|
Derived derived = new Derived();
|
||||||
ObjectMBean mbean = (ObjectMBean)ObjectMBean.mbeanFor(derived);
|
ObjectMBean mbean = (ObjectMBean)ObjectMBean.mbeanFor(derived);
|
||||||
|
|
||||||
assertTrue(mbean.getMBeanInfo()!=null);
|
ObjectMBean managed = (ObjectMBean)ObjectMBean.mbeanFor(derived.getManagedInstance());
|
||||||
|
mbean.setMBeanContainer(container);
|
||||||
|
managed.setMBeanContainer(container);
|
||||||
|
|
||||||
|
container.addBean(mbean);
|
||||||
|
container.addBean(managed);
|
||||||
|
|
||||||
|
MBeanInfo toss = managed.getMBeanInfo();
|
||||||
|
|
||||||
|
Assert.assertNotNull(mbean.getMBeanInfo());
|
||||||
|
|
||||||
MBeanInfo info = mbean.getMBeanInfo();
|
MBeanInfo info = mbean.getMBeanInfo();
|
||||||
|
|
||||||
|
@ -58,15 +87,15 @@ public class ObjectMBeanTest
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 6 attributes from lifecycle and 1 from Derived and 1 from MBean
|
* 6 attributes from lifecycle and 2 from Derived and 1 from MBean
|
||||||
*/
|
*/
|
||||||
Assert.assertEquals("attribute count does not match", 8, info.getAttributes().length);
|
Assert.assertEquals("attribute count does not match", 9, info.getAttributes().length);
|
||||||
|
|
||||||
Assert.assertEquals("attribute values does not match", "Full Name", mbean.getAttribute("fname") );
|
Assert.assertEquals("attribute values does not match", "Full Name", mbean.getAttribute("fname") );
|
||||||
|
|
||||||
//mbean.setAttribute( new Attribute("fname","Fuller Name"));
|
mbean.setAttribute( new Attribute("fname","Fuller Name"));
|
||||||
|
|
||||||
//Assert.assertEquals("set attribute value does not match", "Fuller Name", mbean.getAttribute("fname") );
|
Assert.assertEquals("set attribute value does not match", "Fuller Name", mbean.getAttribute("fname") );
|
||||||
|
|
||||||
Assert.assertEquals("proxy attribute values do not match", "goop", mbean.getAttribute("goop") );
|
Assert.assertEquals("proxy attribute values do not match", "goop", mbean.getAttribute("goop") );
|
||||||
|
|
||||||
|
@ -112,6 +141,10 @@ public class ObjectMBeanTest
|
||||||
Assert.assertTrue("doodle operation was not not found", doodle);
|
Assert.assertTrue("doodle operation was not not found", doodle);
|
||||||
Assert.assertTrue("good operation was not not found", good);
|
Assert.assertTrue("good operation was not not found", good);
|
||||||
|
|
||||||
|
// TODO sort out why this is not working...something off in Bean vs MBean ism's
|
||||||
|
|
||||||
|
Managed managedInstance = (Managed)mbean.getAttribute("managedInstance");
|
||||||
|
Assert.assertNotNull(managedInstance);
|
||||||
|
Assert.assertEquals("managed instance returning nonsense", "foo", managedInstance.getManaged());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue