package javax.management.modelmbean;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.management.Descriptor;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanOperationInfo;
import javax.management.RuntimeOperationsException;
import org.jboss.mx.modelmbean.ModelMBeanConstants;
import org.jboss.mx.util.Serialization;
public class ModelMBeanInfoSupport
extends MBeanInfo
implements ModelMBeanInfo, Serializable
{
private Descriptor mbeanDescriptor = null;
private static final long serialVersionUID;
private static final ObjectStreamField[] serialPersistentFields;
private ModelMBeanAttributeInfo[] modelAttributes = null;
private ModelMBeanConstructorInfo[] modelConstructors = null;
private ModelMBeanOperationInfo[] modelOperations = null;
private ModelMBeanNotificationInfo[] modelNotifications = null;
static
{
switch (Serialization.version)
{
case Serialization.V1R0:
serialVersionUID = -3944083498453227709L;
serialPersistentFields = new ObjectStreamField[]
{
new ObjectStreamField("mmbAttributes", new MBeanAttributeInfo[0].getClass()),
new ObjectStreamField("mmbConstructors", new MBeanConstructorInfo[0].getClass()),
new ObjectStreamField("mmbNotifications", new MBeanNotificationInfo[0].getClass()),
new ObjectStreamField("mmbOperations", new MBeanOperationInfo[0].getClass()),
new ObjectStreamField("modelMBeanDescriptor", Descriptor.class)
};
break;
default:
serialVersionUID = -1935722590756516193L;
serialPersistentFields = new ObjectStreamField[]
{
new ObjectStreamField("modelMBeanAttributes", new MBeanAttributeInfo[0].getClass()),
new ObjectStreamField("modelMBeanConstructors", new MBeanConstructorInfo[0].getClass()),
new ObjectStreamField("modelMBeanNotifications", new MBeanNotificationInfo[0].getClass()),
new ObjectStreamField("modelMBeanOperations", new MBeanOperationInfo[0].getClass()),
new ObjectStreamField("modelMBeanDescriptor", Descriptor.class)
};
}
}
public ModelMBeanInfoSupport(ModelMBeanInfo mbi)
{
super(mbi.getClassName(), mbi.getDescription(), mbi.getAttributes(),
mbi.getConstructors(), mbi.getOperations(), mbi.getNotifications());
modelAttributes = (ModelMBeanAttributeInfo[]) mbi.getAttributes();
modelConstructors = (ModelMBeanConstructorInfo[]) mbi.getConstructors();
modelOperations = (ModelMBeanOperationInfo[]) mbi.getOperations();
modelNotifications = (ModelMBeanNotificationInfo[]) mbi.getNotifications();
try
{
setMBeanDescriptor(mbi.getMBeanDescriptor());
}
catch (MBeanException e)
{
throw new RuntimeException("Cannot set MBean descriptor", e);
}
}
public ModelMBeanInfoSupport(String className, String description,
ModelMBeanAttributeInfo[] modelAttributes,
ModelMBeanConstructorInfo[] modelConstructors,
ModelMBeanOperationInfo[] modelOperations,
ModelMBeanNotificationInfo[] modelNotifications)
{
super(className, description,
(null == modelAttributes) ? new ModelMBeanAttributeInfo[0] : modelAttributes,
(null == modelConstructors) ? new ModelMBeanConstructorInfo[0] : modelConstructors,
(null == modelOperations) ? new ModelMBeanOperationInfo[0] : modelOperations,
(null == modelNotifications) ? new ModelMBeanNotificationInfo[0] : modelNotifications);
this.modelAttributes = (ModelMBeanAttributeInfo[]) super.getAttributes();
this.modelConstructors = (ModelMBeanConstructorInfo[]) super.getConstructors();
this.modelOperations = (ModelMBeanOperationInfo[]) super.getOperations();
this.modelNotifications = (ModelMBeanNotificationInfo[]) super.getNotifications();
try
{
setMBeanDescriptor(createDefaultDescriptor());
}
catch(MBeanException e)
{
throw new RuntimeException("Cannot set MBean descriptor", e);
}
}
public ModelMBeanInfoSupport(String className, String description,
ModelMBeanAttributeInfo[] modelAttributes,
ModelMBeanConstructorInfo[] modelConstructors,
ModelMBeanOperationInfo[] modelOperations,
ModelMBeanNotificationInfo[] modelNotifications, Descriptor mbeandescriptor)
throws RuntimeOperationsException
{
super(className, description,
(null == modelAttributes) ? new ModelMBeanAttributeInfo[0] : modelAttributes,
(null == modelConstructors) ? new ModelMBeanConstructorInfo[0] : modelConstructors,
(null == modelOperations) ? new ModelMBeanOperationInfo[0] : modelOperations,
(null == modelNotifications) ? new ModelMBeanNotificationInfo[0] : modelNotifications);
this.modelAttributes = (ModelMBeanAttributeInfo[]) super.getAttributes();
this.modelConstructors = (ModelMBeanConstructorInfo[]) super.getConstructors();
this.modelOperations = (ModelMBeanOperationInfo[]) super.getOperations();
this.modelNotifications = (ModelMBeanNotificationInfo[]) super.getNotifications();
try
{
setMBeanDescriptor(mbeandescriptor);
}
catch(MBeanException e)
{
throw new RuntimeException("Cannot set MBean descriptor", e);
}
}
public Descriptor[] getDescriptors(String descrType) throws MBeanException
{
if (descrType == null)
{
List list = new ArrayList(100);
list.add(mbeanDescriptor);
list.addAll(getAttributeDescriptors().values());
list.addAll(getOperationDescriptors().values());
list.addAll(getNotificationDescriptors().values());
list.addAll(getConstructorDescriptors().values());
return (Descriptor[])list.toArray(new Descriptor[0]);
}
else if (descrType.equalsIgnoreCase(ModelMBeanConstants.MBEAN_DESCRIPTOR))
return new Descriptor[] { mbeanDescriptor };
else if (descrType.equalsIgnoreCase(ModelMBeanConstants.ATTRIBUTE_DESCRIPTOR))
return (Descriptor[])getAttributeDescriptors().values().toArray(new Descriptor[0]);
else if (descrType.equalsIgnoreCase(ModelMBeanConstants.OPERATION_DESCRIPTOR))
return (Descriptor[])getOperationDescriptors().values().toArray(new Descriptor[0]);
else if (descrType.equalsIgnoreCase(ModelMBeanConstants.NOTIFICATION_DESCRIPTOR))
return (Descriptor[])getNotificationDescriptors().values().toArray(new Descriptor[0]);
else if (descrType.equalsIgnoreCase(ModelMBeanConstants.CONSTRUCTOR_DESCRIPTOR))
return (Descriptor[])getConstructorDescriptors().values().toArray(new Descriptor[0]);
throw new IllegalArgumentException("unknown descriptor type: " + descrType);
}
public Descriptor getDescriptor(String descrName, String descrType) throws MBeanException
{
if (descrType == null)
throw new RuntimeOperationsException(new IllegalArgumentException("null descriptor type"));
if (descrType.equalsIgnoreCase(ModelMBeanConstants.MBEAN_DESCRIPTOR))
return mbeanDescriptor;
else if (descrType.equalsIgnoreCase(ModelMBeanConstants.ATTRIBUTE_DESCRIPTOR))
return getAttributeDescriptor(descrName);
else if (descrType.equalsIgnoreCase(ModelMBeanConstants.OPERATION_DESCRIPTOR))
return getOperationDescriptor(descrName);
else if (descrType.equalsIgnoreCase(ModelMBeanConstants.CONSTRUCTOR_DESCRIPTOR))
return getConstructorDescriptor(descrName);
else if (descrType.equalsIgnoreCase(ModelMBeanConstants.NOTIFICATION_DESCRIPTOR))
return getNotificationDescriptor(descrName);
throw new RuntimeOperationsException(new IllegalArgumentException("unknown descriptor type: " + descrType));
}
public void setDescriptors(Descriptor[] inDescriptors) throws MBeanException
{
for (int i = 0; i < inDescriptors.length; ++i)
{
if (inDescriptors[i] != null && inDescriptors[i].isValid())
{
setDescriptor(
inDescriptors[i],
(String)inDescriptors[i].getFieldValue(ModelMBeanConstants.DESCRIPTOR_TYPE)
);
}
}
}
public void setDescriptor(Descriptor descr, String descrType) throws MBeanException
{
if (descr == null)
throw new RuntimeOperationsException(new IllegalArgumentException("null descriptor"));
if (!descr.isValid())
throw new RuntimeOperationsException(new IllegalArgumentException("not a valid descriptor"));
if (descrType == null)
descrType = (String)descr.getFieldValue(ModelMBeanConstants.DESCRIPTOR_TYPE);
if (descrType.equalsIgnoreCase(ModelMBeanConstants.MBEAN_DESCRIPTOR))
{
setMBeanDescriptor(descr);
}
else if (descrType.equalsIgnoreCase(ModelMBeanConstants.ATTRIBUTE_DESCRIPTOR))
{
ModelMBeanAttributeInfo info = getAttribute((String)descr.getFieldValue(ModelMBeanConstants.NAME));
info.setDescriptor(descr);
}
else if (descrType.equalsIgnoreCase(ModelMBeanConstants.OPERATION_DESCRIPTOR))
{
ModelMBeanOperationInfo info = getOperation((String)descr.getFieldValue(ModelMBeanConstants.NAME));
info.setDescriptor(descr);
}
else if (descrType.equalsIgnoreCase(ModelMBeanConstants.CONSTRUCTOR_DESCRIPTOR))
{
ModelMBeanConstructorInfo info = getConstructor((String)descr.getFieldValue(ModelMBeanConstants.NAME));
info.setDescriptor(descr);
}
else if (descrType.equalsIgnoreCase(ModelMBeanConstants.NOTIFICATION_DESCRIPTOR))
{
ModelMBeanNotificationInfo info = getNotification((String)descr.getFieldValue(ModelMBeanConstants.NAME));
info.setDescriptor(descr);
}
else
throw new RuntimeOperationsException(new IllegalArgumentException("unknown descriptor type: " + descrType));
}
public ModelMBeanAttributeInfo getAttribute(String inName) throws MBeanException
{
if (inName == null)
throw new RuntimeOperationsException(new IllegalArgumentException("Null attribute name"));
for (int i = 0; i < modelAttributes.length; ++i)
{
if (modelAttributes[i].getName().equals(inName))
return modelAttributes[i];
}
return null;
}
public ModelMBeanOperationInfo getOperation(String inName) throws MBeanException
{
if (inName == null)
throw new RuntimeOperationsException(new IllegalArgumentException("Null operation name"));
for (int i = 0; i < modelOperations.length; ++i)
if (modelOperations[i].getName().equals(inName))
return modelOperations[i];
return null;
}
public ModelMBeanConstructorInfo getConstructor(String inName) throws MBeanException
{
if (inName == null)
throw new RuntimeOperationsException(new IllegalArgumentException("Null constructor name"));
for (int i = 0; i < modelConstructors.length; ++i)
if (modelConstructors[i].getName().equals(inName))
return modelConstructors[i];
return null;
}
public ModelMBeanNotificationInfo getNotification(String inName) throws MBeanException
{
if (inName == null)
throw new RuntimeOperationsException(new IllegalArgumentException("Null notification name"));
for (int i = 0; i < modelNotifications.length; ++i)
if (modelNotifications[i].getName().equals(inName))
return modelNotifications[i];
return null;
}
public MBeanAttributeInfo[] getAttributes()
{
return super.getAttributes();
}
public MBeanOperationInfo[] getOperations()
{
return super.getOperations();
}
public MBeanConstructorInfo[] getConstructors()
{
return super.getConstructors();
}
public MBeanNotificationInfo[] getNotifications()
{
return super.getNotifications();
}
public Descriptor getMBeanDescriptor() throws MBeanException
{
return mbeanDescriptor;
}
public void setMBeanDescriptor(Descriptor inDescriptor)
throws MBeanException, RuntimeOperationsException
{
if (inDescriptor == null)
inDescriptor = createDefaultDescriptor();
if (inDescriptor.isValid() && isMBeanDescriptorValid(inDescriptor))
{
addDefaultMBeanDescriptorFields(inDescriptor);
this.mbeanDescriptor = inDescriptor;
}
}
private boolean isMBeanDescriptorValid(Descriptor inDescriptor)
{
String name = (String)inDescriptor.getFieldValue(ModelMBeanConstants.NAME);
if (name.equals(getClassName()) == false)
throw new RuntimeOperationsException (new IllegalArgumentException("Invalid name, expected '" + getClassName() + "' but got: " + name));
String descriptorType = (String)inDescriptor.getFieldValue(ModelMBeanConstants.DESCRIPTOR_TYPE);
if (ModelMBeanConstants.MBEAN_DESCRIPTOR.equalsIgnoreCase(descriptorType) == false)
throw new RuntimeOperationsException (new IllegalArgumentException("Invalid descriptorType, for mbean '" + name + "' expected 'MBean' but got: " + descriptorType));
return true;
}
public Descriptor getDescriptor(String descrName) throws MBeanException
{
if (descrName.equals(mbeanDescriptor.getFieldValue(ModelMBeanConstants.NAME)))
return mbeanDescriptor;
Descriptor descr = null;
descr = (Descriptor)getAttributeDescriptors().get(descrName);
if (descr != null)
return descr;
descr = (Descriptor)getOperationDescriptors().get(descrName);
if (descr != null)
return descr;
descr = (Descriptor)getNotificationDescriptors().get(descrName);
if (descr != null)
return descr;
descr = (Descriptor)getConstructorDescriptors().get(descrName);
if (descr != null)
return descr;
return null;
}
public synchronized Object clone()
{
ModelMBeanInfoSupport clone = (ModelMBeanInfoSupport)super.clone();
clone.mbeanDescriptor = (Descriptor)mbeanDescriptor.clone();
return clone;
}
private void addDefaultMBeanDescriptorFields(Descriptor descr)
{
if (descr.getFieldValue(ModelMBeanConstants.NAME) == null || descr.getFieldValue(ModelMBeanConstants.NAME).equals(""))
descr.setField(ModelMBeanConstants.NAME, getClassName());
if (descr.getFieldValue(ModelMBeanConstants.DESCRIPTOR_TYPE) == null)
descr.setField(ModelMBeanConstants.DESCRIPTOR_TYPE, ModelMBeanConstants.MBEAN_DESCRIPTOR);
if (!(((String)descr.getFieldValue(ModelMBeanConstants.DESCRIPTOR_TYPE)).equalsIgnoreCase(ModelMBeanConstants.MBEAN_DESCRIPTOR)))
descr.setField(ModelMBeanConstants.DESCRIPTOR_TYPE, ModelMBeanConstants.MBEAN_DESCRIPTOR);
if (descr.getFieldValue(ModelMBeanConstants.DISPLAY_NAME) == null)
descr.setField(ModelMBeanConstants.DISPLAY_NAME, getClassName());
if (descr.getFieldValue(ModelMBeanConstants.PERSIST_POLICY) == null)
descr.setField(ModelMBeanConstants.PERSIST_POLICY, ModelMBeanConstants.PP_NEVER);
if (descr.getFieldValue(ModelMBeanConstants.LOG) == null)
descr.setField(ModelMBeanConstants.LOG, "F");
if (descr.getFieldValue(ModelMBeanConstants.VISIBILITY) == null)
descr.setField(ModelMBeanConstants.VISIBILITY, ModelMBeanConstants.HIGH_VISIBILITY);
}
private Descriptor createDefaultDescriptor()
{
return new DescriptorSupport(new String[] {
ModelMBeanConstants.NAME + "=" + getClassName() ,
ModelMBeanConstants.DESCRIPTOR_TYPE + "=" + ModelMBeanConstants.MBEAN_DESCRIPTOR,
ModelMBeanConstants.DISPLAY_NAME + "=" + getClassName(),
ModelMBeanConstants.PERSIST_POLICY + "=" + ModelMBeanConstants.PP_NEVER,
ModelMBeanConstants.LOG + "=" + "F",
ModelMBeanConstants.VISIBILITY + "=" + ModelMBeanConstants.HIGH_VISIBILITY
});
}
private Map getAttributeDescriptors()
{
Map map = new HashMap();
for (int i = 0; i < modelAttributes.length; ++i)
map.put(modelAttributes[i].getName(), (modelAttributes[i]).getDescriptor());
return map;
}
private Descriptor getAttributeDescriptor(String descrName)
{
for (int i = 0; i < modelAttributes.length; ++i)
if (modelAttributes[i].getName().equals(descrName))
return modelAttributes[i].getDescriptor();
return null;
}
private Map getOperationDescriptors()
{
Map map = new HashMap();
for (int i = 0; i < modelOperations.length; ++i)
map.put(modelOperations[i].getName(), (modelOperations[i]).getDescriptor());
return map;
}
private Descriptor getOperationDescriptor(String descrName)
{
for (int i = 0; i < modelOperations.length; ++i)
if (modelOperations[i].getName().equals(descrName))
return modelOperations[i].getDescriptor();
return null;
}
private Map getConstructorDescriptors()
{
Map map = new HashMap();
for (int i = 0; i < modelConstructors.length; ++i)
map.put(modelConstructors[i].getName(), (modelConstructors[i]).getDescriptor());
return map;
}
private Descriptor getConstructorDescriptor(String descrName)
{
for (int i = 0; i < modelConstructors.length; ++i)
if (modelConstructors[i].getName().equals(descrName))
return modelConstructors[i].getDescriptor();
return null;
}
private Map getNotificationDescriptors()
{
Map map = new HashMap();
for (int i = 0; i < modelNotifications.length; ++i)
map.put(modelNotifications[i].getName(), (modelNotifications[i]).getDescriptor());
return map;
}
private Descriptor getNotificationDescriptor(String descrName)
{
for (int i = 0; i < modelNotifications.length; ++i)
if (modelNotifications[i].getName().equals(descrName))
return modelNotifications[i].getDescriptor();
return null;
}
private void readObject(ObjectInputStream ois)
throws IOException, ClassNotFoundException
{
ModelMBeanAttributeInfo[] attrInfo;
ModelMBeanConstructorInfo[] consInfo;
ModelMBeanOperationInfo[] operInfo;
ModelMBeanNotificationInfo[] notifyInfo;
Descriptor desc;
ObjectInputStream.GetField getField = ois.readFields();
switch (Serialization.version)
{
case Serialization.V1R0:
attrInfo = (ModelMBeanAttributeInfo[]) getField.get("mmbAttributes", null);
consInfo = (ModelMBeanConstructorInfo[]) getField.get("mmbConstructors", null);
notifyInfo = (ModelMBeanNotificationInfo[]) getField.get("mmbNotifications", null);
operInfo = (ModelMBeanOperationInfo[]) getField.get("mmbOperations", null);
break;
default:
attrInfo = (ModelMBeanAttributeInfo[]) getField.get("modelMBeanAttributes", null);
consInfo = (ModelMBeanConstructorInfo[]) getField.get("modelMBeanConstructors", null);
notifyInfo = (ModelMBeanNotificationInfo[]) getField.get("modelMBeanNotifications", null);
operInfo = (ModelMBeanOperationInfo[]) getField.get("modelMBeanOperations", null);
}
desc = (Descriptor) getField.get("modelMBeanDescriptor", null);
if (desc == null)
throw new StreamCorruptedException("Null descriptor?");
this.modelAttributes = (null == attrInfo) ? new ModelMBeanAttributeInfo[0]
: attrInfo;
this.modelConstructors = (null == consInfo) ? new ModelMBeanConstructorInfo[0]
: consInfo;
this.modelOperations = (null == operInfo) ? new ModelMBeanOperationInfo[0]
: operInfo;
this.modelNotifications = (null == notifyInfo) ? new ModelMBeanNotificationInfo[0]
: notifyInfo;
try
{
setMBeanDescriptor(createDefaultDescriptor());
}
catch(MBeanException ignore)
{
}
}
private void writeObject(ObjectOutputStream oos)
throws IOException
{
ObjectOutputStream.PutField putField = oos.putFields();
switch (Serialization.version)
{
case Serialization.V1R0:
putField.put("mmbAttributes", modelAttributes);
putField.put("mmbConstructors", modelConstructors);
putField.put("mmbNotifications", modelNotifications);
putField.put("mmbOperations", modelOperations);
break;
default:
putField.put("modelMBeanAttributes", modelAttributes);
putField.put("modelMBeanConstructors", modelConstructors);
putField.put("modelMBeanNotifications", modelNotifications);
putField.put("modelMBeanOperations", modelOperations);
}
putField.put("modelMBeanDescriptor", mbeanDescriptor);
oos.writeFields();
}
}