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

MBeanAttributeSetter

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
logger
private final MBeanServer
mbs
private final ObjectName
on
private static final Map
P2W
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();
                attNames.add(an);
                if (an.equals(name)) {
                    type = mai.getType();
                    break;
                }
            }
        } catch (final Exception e) { //TODO
            //even though one can land here, it is okay to squelch this exception
            // Log it and move on
            logger.info(CMBStrings.get("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);
            logger.warning(mesg);
            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(c.equals(Character.class)){
                if(value.length() != 1){
                    String mesg = CMBStrings.get("cmb.badCharAttribute", name, value);
                    logger.warning(mesg);
                    throw new CustomMBeanException(mesg);
                }
                valueObject = Character.valueOf(value.charAt(0));
            }
            else{
                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) ||
                            long.class.getName().equals(type);
        return ( primitive );
    
public voidsetIt(java.lang.String name, java.lang.String value)

        
        try
        {
            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) );