1 23 24 package com.sun.enterprise.admin.mbeans.custom; 25 26 import com.sun.enterprise.admin.mbeans.custom.loading.CustomMBeanRegistrationImpl; 27 import com.sun.enterprise.admin.server.core.CustomMBeanRegistration; 28 import com.sun.enterprise.config.serverbeans.ElementProperty; 29 import com.sun.enterprise.config.serverbeans.Mbean; 30 import com.sun.enterprise.util.SystemPropertyConstants; 31 import java.util.HashMap ; 32 import java.util.Hashtable ; 33 import java.util.Map ; 34 import javax.management.MBeanServer ; 35 import javax.management.MBeanServerFactory ; 36 import javax.management.MalformedObjectNameException ; 37 import javax.management.ObjectName ; 38 39 54 public final class MBeanValidator { 55 56 private final MBeanServer mbs; 57 58 public MBeanValidator() { 59 mbs = MBeanServerFactory.newMBeanServer(); 60 } 61 public ObjectName registerTestMBean(final Map <String , String > params, final Map <String , String > attributes) throws RuntimeException { 62 try { 63 CustomMBeanRegistration cmr = new CustomMBeanRegistrationImpl(mbs); 64 final Mbean m = toMbean(params, attributes, true); 65 final ObjectName ron = cmr.registerMBean(m); 66 return ( ron ); 67 } catch (final Exception e) { 68 throw new RuntimeException (e); 69 } 70 71 } 72 public void unregisterTestMBean(final ObjectName ron) throws RuntimeException { 73 try { 74 if (mbs.isRegistered(ron)) 75 mbs.unregisterMBean(ron); 76 } catch (final Exception e) { 77 throw new RuntimeException (e); 78 } 79 } 80 86 public static final Mbean toMbean(final Map <String , String > params, final Map <String , String > attributes, final boolean enabledNotUsed) { 87 final Mbean cmb = new Mbean(); 88 final String name = params.get(CustomMBeanConstants.NAME_KEY); 89 final String cName = params.get(CustomMBeanConstants.IMPL_CLASS_NAME_KEY); 90 final String on = params.get(CustomMBeanConstants.OBJECT_NAME_KEY); 91 final String ot = params.get(CustomMBeanConstants.OBJECT_TYPE_KEY); 92 final String enabledString = params.get(CustomMBeanConstants.ENABLED_KEY); 93 94 boolean enabled = true; 95 96 if(enabledString != null) 97 enabled = new Boolean (enabledString); 98 99 cmb.setName(name); 100 cmb.setImplClassName(cName); 101 cmb.setObjectName(on); 102 cmb.setObjectType(ot); 103 cmb.setEnabled(enabled); 104 cmb.setElementProperty(map2Properties(attributes)); 105 return ( cmb ); 106 } 107 108 public static ObjectName formDefaultObjectName(final Map <String , String > params) throws MalformedObjectNameException { 109 final String domain = CustomMBeanConstants.CUSTOM_MBEAN_DOMAIN; 110 final Map <String , String > onProperties = new HashMap <String , String > (); 112 if (params.containsKey(CustomMBeanConstants.IMPL_CLASS_NAME_KEY)) 113 onProperties.put(CustomMBeanConstants.IMPL_CLASS_NAME_KEY, params.get(CustomMBeanConstants.IMPL_CLASS_NAME_KEY)); 114 if (params.containsKey(CustomMBeanConstants.NAME_KEY)) 115 onProperties.put(CustomMBeanConstants.NAME_KEY, params.get(CustomMBeanConstants.NAME_KEY)); 116 final ObjectName on = new ObjectName (domain, new Hashtable <String , String >(onProperties)); 117 return ( on ); 118 } 119 120 private static ElementProperty[] map2Properties(final Map <String , String > attributes) { 121 final ElementProperty[] props = new ElementProperty[attributes.size()]; 122 int i = 0; 123 for (String n : attributes.keySet()) { 124 final ElementProperty prop = new ElementProperty(); 125 prop.setName(n); 126 prop.setValue(attributes.get(n)); 127 props[i] = prop; 128 i++; 129 } 130 return ( props ) ; 131 } 132 133 } 134 | Popular Tags |