package javax.management.modelmbean;
import org.jboss.mx.modelmbean.ModelMBeanConstants;
import org.jboss.mx.util.Serialization;
import javax.management.Descriptor;
import javax.management.DescriptorAccess;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import javax.management.RuntimeOperationsException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.StreamCorruptedException;
import java.lang.reflect.Method;
public class ModelMBeanOperationInfo
extends MBeanOperationInfo
implements DescriptorAccess
{
private Descriptor descriptor = null;
private transient String operationRole;
private static final long serialVersionUID;
private static final ObjectStreamField[] serialPersistentFields;
static
{
switch (Serialization.version)
{
case Serialization.V1R0:
serialVersionUID = 9087646304346171239L;
serialPersistentFields = new ObjectStreamField[]
{
new ObjectStreamField("operDescriptor", Descriptor.class)
};
break;
default:
serialVersionUID = 6532732096650090465L;
serialPersistentFields = new ObjectStreamField[]
{
new ObjectStreamField("operationDescriptor", Descriptor.class)
};
}
}
public ModelMBeanOperationInfo(String description, Method operationMethod)
{
super(description, operationMethod);
this.operationRole = getOperationRole();
setDescriptor(createDefaultDescriptor());
}
public ModelMBeanOperationInfo(String description, Method operationMethod, Descriptor descriptor)
{
super(description, operationMethod);
this.operationRole = getOperationRole();
setDescriptor(descriptor);
}
public ModelMBeanOperationInfo(String name, String description, MBeanParameterInfo[] signature,
String type, int impact)
{
super(name, description, signature, type, impact);
this.operationRole = getOperationRole();
setDescriptor(createDefaultDescriptor());
}
public ModelMBeanOperationInfo(String name, String description, MBeanParameterInfo[] signature,
String type, int impact, Descriptor descriptor)
{
super(name, description, signature, type, impact);
this.operationRole = getOperationRole();
setDescriptor(descriptor);
}
public ModelMBeanOperationInfo(ModelMBeanOperationInfo info)
{
this(info.getName(), info.getDescription(), info.getSignature(),
info.getReturnType(), info.getImpact(), info.getDescriptor());
}
private String getOperationRole()
{
if (getName().startsWith("get") && getReturnType().equals("void") == false && getSignature().length == 0)
return ModelMBeanConstants.ROLE_GETTER;
else if (getName().startsWith("is") && (getReturnType().equals("boolean") || getReturnType().equals("java.lang.Boolean")) && getSignature().length == 0)
return ModelMBeanConstants.ROLE_GETTER;
else if (getName().startsWith("set") && getReturnType().equals("void") && getSignature().length == 1)
return ModelMBeanConstants.ROLE_SETTER;
else
return ModelMBeanConstants.ROLE_OPERATION;
}
public Descriptor getDescriptor()
{
return (Descriptor)descriptor.clone();
}
public void setDescriptor(Descriptor inDescriptor)
{
if (inDescriptor == null)
inDescriptor = createDefaultDescriptor();
if (inDescriptor.isValid() && isOperationDescriptorValid(inDescriptor))
this.descriptor = inDescriptor;
}
private boolean isOperationDescriptorValid(Descriptor inDescriptor)
{
String name = (String)inDescriptor.getFieldValue(ModelMBeanConstants.NAME);
if (name.equals(getName()) == false)
throw new RuntimeOperationsException (new IllegalArgumentException("Invalid name, expected '" + getName() + "' but got: " + name));
String descriptorType = (String)inDescriptor.getFieldValue(ModelMBeanConstants.DESCRIPTOR_TYPE);
if (ModelMBeanConstants.OPERATION_DESCRIPTOR.equalsIgnoreCase(descriptorType) == false)
throw new RuntimeOperationsException (new IllegalArgumentException("Invalid descriptorType, for operation '" + name + "' expected 'operation' but got: " + descriptorType));
String role = (String) inDescriptor.getFieldValue(ModelMBeanConstants.ROLE);
if (role != null && operationRole.equals(role) == false)
throw new RuntimeOperationsException(new IllegalArgumentException("Invalid role, for operation '" + name + "' expected '" + operationRole + "' but got: " + role));
return true;
}
public synchronized Object clone()
{
ModelMBeanOperationInfo clone = (ModelMBeanOperationInfo)super.clone();
clone.descriptor = (Descriptor)this.descriptor.clone();
return clone;
}
public String toString()
{
return "ModelMBeanOperationInfo[" +
getReturnType() + " " + getName() + getSignatureString() +
",Impact=" + getImpactString() +
",Descriptor(" + getDescriptor() + ")]";
}
private String getSignatureString()
{
StringBuffer sbuf = new StringBuffer(400);
sbuf.append("(");
MBeanParameterInfo[] sign = getSignature();
if (sign.length > 0)
{
for (int i = 0; i < sign.length; ++i)
{
sbuf.append(sign[i].getType());
sbuf.append(" ");
sbuf.append(sign[i].getName());
sbuf.append(",");
}
sbuf.delete(sbuf.length() - 1, sbuf.length());
}
sbuf.append(")");
return sbuf.toString();
}
private String getImpactString()
{
int impact = getImpact();
if (impact == MBeanOperationInfo.ACTION)
return "ACTION";
else if (impact == MBeanOperationInfo.INFO)
return "INFO";
else if (impact == MBeanOperationInfo.ACTION_INFO)
return "ACTION_INFO";
else
return "UNKNOWN";
}
private Descriptor createDefaultDescriptor()
{
DescriptorSupport descr = new DescriptorSupport();
descr.setField(ModelMBeanConstants.NAME, super.getName());
descr.setField(ModelMBeanConstants.DISPLAY_NAME, super.getName());
descr.setField(ModelMBeanConstants.DESCRIPTOR_TYPE, ModelMBeanConstants.OPERATION_DESCRIPTOR);
descr.setField(ModelMBeanConstants.ROLE, operationRole);
return descr;
}
private void readObject(ObjectInputStream ois)
throws IOException, ClassNotFoundException
{
ObjectInputStream.GetField getField = ois.readFields();
switch (Serialization.version)
{
case Serialization.V1R0:
descriptor = (Descriptor) getField.get("operDescriptor", null);
break;
default:
descriptor = (Descriptor) getField.get("operationDescriptor", null);
}
if (descriptor == null)
throw new StreamCorruptedException("Null descriptor?");
}
private void writeObject(ObjectOutputStream oos)
throws IOException
{
ObjectOutputStream.PutField putField = oos.putFields();
switch (Serialization.version)
{
case Serialization.V1R0:
putField.put("operDescriptor", descriptor);
break;
default:
putField.put("operationDescriptor", descriptor);
}
oos.writeFields();
}
}