MBeanAttributeSetter.javaAPI DocGlassfish v2 API8376Fri May 04 22:24:10 BST 2007com.sun.enterprise.admin.mbeans.custom.loading


public class MBeanAttributeSetter extends Object
Class to set the attributes of given MBean taking into account the types of the attributes.

Fields Summary
private static final Logger
private final MBeanServer
private final ObjectName
private static final Map
Constructors Summary
public MBeanAttributeSetter(MBeanServer mbs, ObjectName on)

        this.mbs    = mbs;
        this.on     = on;
Methods Summary
private java.lang.StringgetAttributeType(java.lang.String name)

 //gets it from MBeanInfo
        String type = null;
        List<String> attNames = new ArrayList<String>(); // just in case we need to log this info in the event of an error...
        try {
            MBeanAttributeInfo[] mais = mbs.getMBeanInfo(on).getAttributes();
            for (MBeanAttributeInfo mai : mais) {
                final String an = mai.getName();
                if (an.equals(name)) {
                    type = mai.getType();
        } catch (final Exception e) { //TODO
            //even though one can land here, it is okay to squelch this exception
            // Log it and move on
  "getAttributeTypeNonFatal", name));
        if(type == null){
            // this is an error!  They probably misspelled the Attribute Name.
            // this code is NEVER called unless we are expecting to *find* the attribute
            String names = new String();
            for(int i = 0; i < attNames.size(); i++){
                if(i != 0)
                    names += ", ";
                names += attNames.get(i);
            String mesg = CMBStrings.get("cmb.badAttribute", name, names);
            throw new CustomMBeanException(mesg);
        return ( type );
private java.lang.ObjectgetAttributeValue(java.lang.String type, java.lang.String name, java.lang.String value)
Method to construct an instance of a type from its String representation. Note that for this method to succeed, the type class has to have such a constructor that accepts a String and creates the instance of that type. Most of the primitive wrappers have such a constructor. This requirement has to be satisfied because the attributes of an MBean will be persisted to a config file and can only be read back as Strings.

        Object valueObject = null;
        try {
            if (isPrimitive(type)) {
                //recursive call
                return ( getAttributeValue(toWrapper(type), name, value) );
            final Class c                   = Class.forName(type);
            final Object[] params           = new Object[]{value};
            // special case...
                if(value.length() != 1){
                    String mesg = CMBStrings.get("cmb.badCharAttribute", name, value);
                    throw new CustomMBeanException(mesg);
                valueObject = Character.valueOf(value.charAt(0));
                final Constructor ctor          = c.getConstructor(new Class[]{java.lang.String.class});
                valueObject                     = ctor.newInstance(params);
        } catch (final Throwable t) {
            final String msg = CMBStrings.get("attributeValueError", name, type, value);
            throw new CustomMBeanException (msg, t);
        return ( valueObject );
private booleanisPrimitive(java.lang.String type)

        boolean primitive = int.class.getName().equals(type) ||
			    char.class.getName().equals(type) ||
			    short.class.getName().equals(type) ||
			    byte.class.getName().equals(type) ||
			    boolean.class.getName().equals(type) ||
			    double.class.getName().equals(type) ||
			    float.class.getName().equals(type) ||
        return ( primitive );
public voidsetIt(java.lang.String name, java.lang.String value)

            final String at         = getAttributeType(name);
            final Object atv        = getAttributeValue(at, name, value); // construct the attribute from "String" value
            final Attribute atr     = new Attribute(name, atv);
            mbs.setAttribute(on, atr);
        catch(CustomMBeanException cmbe)
            throw cmbe;
        catch(Exception e)
            throw new CustomMBeanException(e);
private java.lang.StringtoWrapper(java.lang.String primitive)

        return ( P2W.get(primitive) );