1 23 24 25 package com.sun.enterprise.admin.mbeans.custom.loading; 26 27 import com.sun.enterprise.admin.mbeans.custom.CMBStrings; 28 import com.sun.enterprise.admin.mbeans.custom.CustomMBeanException; 29 import java.lang.reflect.Constructor ; 30 import java.util.*; 31 import com.sun.enterprise.admin.common.constant.AdminConstants; 32 import javax.management.Attribute ; 33 import javax.management.AttributeNotFoundException ; 34 import javax.management.InstanceNotFoundException ; 35 import javax.management.InvalidAttributeValueException ; 36 import javax.management.MBeanAttributeInfo ; 37 import javax.management.MBeanException ; 38 import javax.management.MBeanServer ; 39 import javax.management.ObjectName ; 40 import javax.management.ReflectionException ; 41 import java.util.logging.Logger ; 42 43 46 public class MBeanAttributeSetter { 47 private static final Logger logger = Logger.getLogger(AdminConstants.kLoggerName); 48 private final MBeanServer mbs; 49 private final ObjectName on; 50 public MBeanAttributeSetter(final MBeanServer mbs, final ObjectName on) { 51 this.mbs = mbs; 52 this.on = on; 53 } 54 public void setIt(final String name, final String value) throws InstanceNotFoundException , AttributeNotFoundException , 55 InvalidAttributeValueException , MBeanException, ReflectionException { 56 final String at = getAttributeType(name); 57 final Object atv = getAttributeValue(at, name, value); final Attribute atr = new Attribute (name, atv); 59 mbs.setAttribute(on, atr); 60 } 61 62 private String getAttributeType(final String name) { String type = null; 65 List<String > attNames = new ArrayList<String >(); try { 67 MBeanAttributeInfo [] mais = mbs.getMBeanInfo(on).getAttributes(); 68 for (MBeanAttributeInfo mai : mais) { 69 final String an = mai.getName(); 70 attNames.add(an); 71 if (an.equals(name)) { 72 type = mai.getType(); 73 break; 74 } 75 } 76 } catch (final Exception e) { } 80 if(type == null){ 81 String names = new String (); 84 for(int i = 0; i < attNames.size(); i++){ 85 if(i != 0) 86 names += ", "; 87 88 names += attNames.get(i); 89 } 90 91 String mesg = CMBStrings.get("cmb.badAttribute", name, names); 92 logger.warning(mesg); 93 throw new CustomMBeanException(mesg); 94 } 95 return ( type ); 96 } 97 104 private Object getAttributeValue(final String type, final String name, final String value) { 105 Object valueObject = null; 106 try { 107 if (isPrimitive(type)) { 108 return ( getAttributeValue(toWrapper(type), name, value) ); 110 } 111 final Class c = Class.forName(type); 112 final Object [] params = new Object []{value}; 113 114 if(c.equals(Character .class)){ 116 if(value.length() != 1){ 117 String mesg = CMBStrings.get("cmb.badCharAttribute", name, value); 118 logger.warning(mesg); 119 throw new CustomMBeanException(mesg); 120 } 121 valueObject = new Character (value.charAt(0)); 122 } 123 else{ 124 final Constructor ctor = c.getConstructor(new Class []{java.lang.String .class}); 125 valueObject = ctor.newInstance(params); 126 } 127 128 } catch (final Throwable t) { 129 final String msg = CMBStrings.get("attributeValueError", name, type, value); 130 throw new RuntimeException (msg, t); 131 } 132 return ( valueObject ); 133 } 134 135 private boolean isPrimitive(final String type) { 136 boolean primitive = int.class.getName().equals(type) || 137 char.class.getName().equals(type) || 138 short.class.getName().equals(type) || 139 byte.class.getName().equals(type) || 140 boolean.class.getName().equals(type) || 141 double.class.getName().equals(type) || 142 float.class.getName().equals(type) || 143 long.class.getName().equals(type); 144 return ( primitive ); 145 } 146 private String toWrapper(final String primitive) { 147 return ( P2W.get(primitive) ); 148 149 } 150 private static final Map<String , String > P2W = new HashMap<String , String >(); 151 static { 152 P2W.put(int.class.getName(), java.lang.Integer .class.getName()); 153 P2W.put(char.class.getName(), java.lang.Character .class.getName()); 154 P2W.put(short.class.getName(), java.lang.Short .class.getName()); 155 P2W.put(byte.class.getName(), java.lang.Byte .class.getName()); 156 P2W.put(boolean.class.getName(), java.lang.Boolean .class.getName()); 157 P2W.put(double.class.getName(), java.lang.Double .class.getName()); 158 P2W.put(float.class.getName(), java.lang.Float .class.getName()); 159 P2W.put(long.class.getName(), java.lang.Long .class.getName()); 160 } 161 } | Popular Tags |