package javax.management.modelmbean;
import java.util.ArrayList;
import java.util.Arrays;
import javax.management.Attribute;
import javax.management.AttributeChangeNotification;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.InvalidAttributeValueException;
import javax.management.ListenerNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
import javax.management.Notification;
import javax.management.NotificationEmitter;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.RuntimeOperationsException;
import javax.management.loading.ClassLoaderRepository;
import org.jboss.mx.modelmbean.RequiredModelMBeanInstantiator;
public class RequiredModelMBean
implements ModelMBean, MBeanRegistration, NotificationEmitter
{
private ModelMBean delegate = null;
private MBeanRegistration registrationInterface = null;
private Boolean isRegistered = new Boolean(false);
public RequiredModelMBean()
throws MBeanException, RuntimeOperationsException
{
delegate = RequiredModelMBeanInstantiator.instantiate();
if (delegate instanceof MBeanRegistration)
registrationInterface = (MBeanRegistration)delegate;
}
public RequiredModelMBean(ModelMBeanInfo info)
throws MBeanException, RuntimeOperationsException
{
if (info == null)
throw new RuntimeOperationsException(new IllegalArgumentException("Null model mbean info"));
delegate = RequiredModelMBeanInstantiator.instantiate(info);
if (delegate instanceof MBeanRegistration)
registrationInterface = (MBeanRegistration)delegate;
}
public void setModelMBeanInfo(ModelMBeanInfo info)
throws MBeanException, RuntimeOperationsException
{
if (isRegistered.booleanValue())
throw new RuntimeOperationsException(new IllegalStateException("Cannot set ModelMBeanInfo, mbean already registered"));
delegate.setModelMBeanInfo(info);
}
public void setManagedResource(Object mr, String mr_type)
throws MBeanException, RuntimeOperationsException,
InstanceNotFoundException, InvalidTargetObjectTypeException
{
delegate.setManagedResource(mr, mr_type);
}
public void load()
throws MBeanException, RuntimeOperationsException, InstanceNotFoundException
{
delegate.load();
}
public void store()
throws MBeanException, RuntimeOperationsException, InstanceNotFoundException
{
delegate.store();
}
public MBeanInfo getMBeanInfo()
{
return delegate.getMBeanInfo();
}
public Object invoke(String opName, Object[] opArgs, String[] sig)
throws MBeanException, ReflectionException
{
return delegate.invoke(opName, opArgs, sig);
}
public Object getAttribute(String attrName)
throws AttributeNotFoundException, MBeanException, ReflectionException
{
return delegate.getAttribute(attrName);
}
public AttributeList getAttributes(String[] attrNames)
{
return delegate.getAttributes(attrNames);
}
public void setAttribute(Attribute attribute) throws AttributeNotFoundException,
InvalidAttributeValueException, MBeanException, ReflectionException
{
delegate.setAttribute(attribute);
}
public AttributeList setAttributes(AttributeList attributes)
{
return delegate.setAttributes(attributes);
}
public void addNotificationListener(NotificationListener inlistener,
NotificationFilter infilter, Object inhandback)
throws IllegalArgumentException
{
delegate.addNotificationListener(inlistener, infilter, inhandback);
}
public void removeNotificationListener(NotificationListener inlistener)
throws ListenerNotFoundException
{
delegate.removeNotificationListener(inlistener);
}
public void removeNotificationListener(NotificationListener listener,
NotificationFilter filter,
Object handback)
throws ListenerNotFoundException
{
if (delegate instanceof NotificationEmitter)
((NotificationEmitter)delegate).removeNotificationListener(listener, filter, handback);
else
throw new UnsupportedOperationException("Delegate is not a NotificationEmitter " + delegate.getClass());
}
public void sendNotification(Notification ntfyObj)
throws MBeanException, RuntimeOperationsException
{
delegate.sendNotification(ntfyObj);
}
public void sendNotification(String ntfyText)
throws MBeanException, RuntimeOperationsException
{
delegate.sendNotification(ntfyText);
}
public MBeanNotificationInfo[] getNotificationInfo()
{
MBeanNotificationInfo generic = null;
MBeanNotificationInfo attrChange = null;
MBeanNotificationInfo[] resourceInfo = delegate.getNotificationInfo();
for(int n = 0; n < resourceInfo.length; n ++)
{
MBeanNotificationInfo notify = resourceInfo[n];
String name = notify.getName();
if( name.equals("GENERIC") )
generic = notify;
else if( name.equals("ATTRIBUTE_CHANGE") )
attrChange = notify;
}
if( generic == null || attrChange == null )
{
ArrayList tmp = new ArrayList(Arrays.asList(resourceInfo));
if( attrChange == null )
{
String[] types = {"jmx.attribute.change"};
ModelMBeanNotificationInfo n = new ModelMBeanNotificationInfo(types,
"ATTRIBUTE_CHANGE", "Attribute change notfication");
n.getDescriptor().setField("log", "T");
n.getDescriptor().setField("displayName", "jmx.attribute.change");
tmp.add(0, n);
}
if( generic == null )
{
String[] types = {"jmx.modelmbean.generic"};
ModelMBeanNotificationInfo n = new ModelMBeanNotificationInfo(types,
"GENERIC", "Generic text msg");
n.getDescriptor().setField("log", "T");
n.getDescriptor().setField("displayName", "jmx.modelmbean.generic");
tmp.add(0, n);
}
resourceInfo = new MBeanNotificationInfo[tmp.size()];
tmp.toArray(resourceInfo);
}
return resourceInfo;
}
public void addAttributeChangeNotificationListener(NotificationListener inlistener,
String inAttributeName, Object inhandback) throws MBeanException, RuntimeOperationsException, IllegalArgumentException
{
delegate.addAttributeChangeNotificationListener(inlistener, inAttributeName, inhandback);
}
public void removeAttributeChangeNotificationListener(NotificationListener inlistener, String inAttributeName)
throws MBeanException, RuntimeOperationsException, ListenerNotFoundException
{
delegate.removeAttributeChangeNotificationListener(inlistener, inAttributeName);
}
public void sendAttributeChangeNotification(AttributeChangeNotification ntfyObj) throws MBeanException, RuntimeOperationsException
{
delegate.sendAttributeChangeNotification(ntfyObj);
}
public void sendAttributeChangeNotification(Attribute inOldVal, Attribute inNewVal)
throws MBeanException, RuntimeOperationsException
{
delegate.sendAttributeChangeNotification(inOldVal, inNewVal);
}
public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception
{
if (registrationInterface != null)
return registrationInterface.preRegister(server, name);
return name;
}
public void postRegister(Boolean registrationDone)
{
if (registrationInterface != null)
registrationInterface.postRegister(registrationDone);
isRegistered = registrationDone;
}
public void preDeregister() throws Exception
{
if (registrationInterface != null)
registrationInterface.preDeregister();
}
public void postDeregister()
{
isRegistered = new Boolean(false);
if (registrationInterface != null)
registrationInterface.postDeregister();
}
protected ClassLoaderRepository getClassLoaderRepository()
{
return null;
}
}