1 23 24 25 package com.sun.enterprise.admin.mbeans.custom.loading; 26 import java.util.logging.Logger ; 27 import javax.management.MBeanServer ; 28 import javax.management.InstanceAlreadyExistsException ; 29 import javax.management.MBeanRegistrationException ; 30 import javax.management.NotCompliantMBeanException ; 31 import javax.management.ObjectInstance ; 32 import javax.management.ObjectName ; 33 34 import com.sun.enterprise.admin.mbeans.custom.CMBStrings; 35 import com.sun.enterprise.admin.mbeans.custom.ObjectNameSelectionAlgorithm; 36 import com.sun.enterprise.config.serverbeans.Mbean; 37 import com.sun.enterprise.config.serverbeans.ElementProperty; 38 import com.sun.enterprise.admin.common.constant.AdminConstants; 39 import com.sun.enterprise.admin.mbeans.custom.CustomMBeanConstants; 40 import com.sun.enterprise.admin.server.core.CustomMBeanRegistration; 41 import com.sun.enterprise.util.SystemPropertyConstants; 42 import java.util.Hashtable ; 43 import java.util.List ; 44 import javax.management.AttributeNotFoundException ; 45 import javax.management.InstanceNotFoundException ; 46 import javax.management.InvalidAttributeValueException ; 47 import javax.management.MBeanException ; 48 import javax.management.ReflectionException ; 49 import javax.management.RuntimeOperationsException ; 50 51 57 public final class CustomMBeanRegistrationImpl implements CustomMBeanRegistration { 58 59 60 private final MBeanServer mbs; 61 private ClassLoader cl; 62 private static final Logger logger = Logger.getLogger(AdminConstants.kLoggerName); 63 64 public CustomMBeanRegistrationImpl(final MBeanServer mbs) throws IllegalArgumentException { 65 this.mbs = mbs; 66 this.cl = new MBeanClassLoader(); 67 } 68 69 70 public void registerMBeans(final List <Mbean> mbeans, final boolean continueReg) throws RuntimeException { 71 if (mbeans == null) 72 throw new IllegalArgumentException ( 73 CMBStrings.get("InternalError", "registerMBeans() received a null mbeans argument")); 74 for (Mbean mbean : mbeans) { 75 try { 76 registerMBean(mbean); 77 } catch(final Throwable t) { 78 if (continueReg) { 79 logger.info(CMBStrings.get("cmb.registerError", mbean.getName())); 80 } 81 else { 82 throw new RuntimeException (t); 83 } 84 } 85 } 86 } 87 88 public void registerMBeans(final List <Mbean> mbeans) throws RuntimeException { 89 this.registerMBeans(mbeans, true); 90 } 91 public void setClassLoader(final ClassLoader cl) throws IllegalArgumentException { 92 if (cl == null) 93 throw new IllegalArgumentException (CMBStrings.get("InternalError", "setClassLoader() received a null argument")); 94 this.cl = cl; 95 } 96 public ObjectName registerMBean(final Mbean mbean) throws RuntimeException { 97 99 if (mbean == null) 100 throw new IllegalArgumentException (CMBStrings.get("InternalError", "registerMBean() received a null argument")); 101 102 ObjectName ron = null; 103 try { 104 logger.fine(CMBStrings.get("cmb.loadingMBean1", mbean.getName())); 105 final ObjectName mon = getCascadingAwareObjectName(mbean); 106 logger.fine(CMBStrings.get("cmb.loadingMBean2", mon.toString())); 107 final Class mc = loadIt(mbean.getImplClassName()); 108 final Object mo = newIt(mc); 109 ron = registerIt(mo, mon); 110 111 112 if(!ObjectNameSelectionAlgorithm.implementsMBeanRegistrationInterface(mbean.getImplClassName())) 115 { 116 if(!mon.equals(ron)) 117 throw new RuntimeException (CMBStrings.get("objNameMismatch", mon, ron)); 118 } 119 initIt(mbean, ron); 120 121 if(!mbean.isEnabled()) 128 unregisterIt(mbean, ron); 129 return ( ron ); 130 } catch (final ClassNotFoundException cnfe) { 131 logger.info(CMBStrings.get("cmb.loadingMBean3", mbean.getImplClassName(), cl.getClass().getName())); 132 throw new RuntimeException (cnfe); 133 } catch (final NoClassDefFoundError ncdfe) { 134 logger.info(CMBStrings.get("cmb.loadingMBean4", mbean.getImplClassName(), cl.getClass().getName())); 135 throw new RuntimeException (ncdfe); 136 } catch (final InstantiationException ie) { 137 logger.info(CMBStrings.get("cmb.loadingMBean5", mbean.getImplClassName())); 138 throw new RuntimeException (ie); 139 } catch (final IllegalAccessException iae) { 140 logger.info(CMBStrings.get("cmb.loadingMBean6", mbean.getImplClassName())); 141 throw new RuntimeException (iae); 142 } catch (final ExceptionInInitializerError eie) { 143 logger.info(CMBStrings.get("cmb.loadingMBean7", mbean.getImplClassName())); 144 throw new RuntimeException (eie); 145 } 146 catch (final Throwable e) { 147 if (ron != null) 150 unregisterIt(mbean, ron); 151 throw new RuntimeException (e); 152 } 153 } 154 155 public static ObjectName getCascadingAwareObjectName(final Mbean mbean) throws RuntimeException { 156 try { 157 final ObjectName configON = new ObjectName (mbean.getObjectName()); 158 return (getCascadingAwareObjectName(configON) ); 159 } catch(final Exception e) { 160 throw new RuntimeException (e); 161 } 162 } 163 public static ObjectName getCascadingAwareObjectName(final ObjectName configON) throws RuntimeException { 164 try { 165 final String serverNameKey = CustomMBeanConstants.SERVER_KEY; 166 final String serverNameVal = System.getProperty(SystemPropertyConstants.SERVER_NAME); 167 final Hashtable properties = configON.getKeyPropertyList(); 168 properties.put(serverNameKey, serverNameVal); 169 final ObjectName ron = new ObjectName (configON.getDomain(), properties); 170 return ( ron ); 171 } catch(final Exception e) { 172 throw new RuntimeException (e); 173 } 174 } 175 public static ObjectName getCascadingUnawareObjectName(final ObjectName cascadeON) throws RuntimeException { 176 try { 177 if (cascadeON == null) { 178 throw new IllegalArgumentException (CMBStrings.get("InternalError", "getCascadingUnawareObjectName() received a null argument")); 179 } 180 ObjectName ron = cascadeON; 181 final String serverNameKey = CustomMBeanConstants.SERVER_KEY; 182 final Hashtable properties = cascadeON.getKeyPropertyList(); if (properties.containsKey(serverNameKey)) { 184 final Hashtable np = new Hashtable (properties); 185 np.remove(serverNameKey); 186 ron = new ObjectName (cascadeON.getDomain(), np); 187 } 188 return ( ron ); 189 } catch(final Exception e) { 190 throw new RuntimeException (e); 191 } 192 } 193 private Class loadIt(final String classname) throws ClassNotFoundException , NoClassDefFoundError { 195 final Class c = cl.loadClass(classname); 196 logger.fine(CMBStrings.get("cmb.loadingMBean8", c.getName(), cl.getClass().getName(), c.getClassLoader().getClass().getName())); 197 return ( c ); 198 } 199 private Object newIt(final Class c) throws IllegalAccessException , InstantiationException , 200 ExceptionInInitializerError { 201 return ( c.newInstance() ); 202 } 203 private ObjectName registerIt(final Object mo, final ObjectName on) throws InstanceAlreadyExistsException , 204 MBeanRegistrationException , NotCompliantMBeanException , RuntimeOperationsException { 205 if(mo == null) 206 throw new RuntimeException (CMBStrings.get("objNameNull")); 207 final ObjectInstance oi = mbs.registerMBean(mo, on); 208 return ( oi.getObjectName() ); 209 } 210 private void initIt(final Mbean mbc, final ObjectName on) throws InstanceNotFoundException , AttributeNotFoundException , 211 InvalidAttributeValueException , MBeanException , ReflectionException { 212 final MBeanAttributeSetter mas = new MBeanAttributeSetter(mbs, on); 213 final ElementProperty[] ats = mbc.getElementProperty(); 214 for (ElementProperty p : ats) { 215 mas.setIt(p.getName(), p.getValue()); 216 logger.fine(CMBStrings.get("cmb.initMBean", p.getName(), mbc.getName())); 217 } 218 } 219 private void unregisterIt(final Mbean m, final ObjectName ron) { 220 try { 222 if (mbs.isRegistered(ron)) 223 mbs.unregisterMBean(ron); 224 } catch (final Throwable e) { 225 logger.warning(CMBStrings.get("cmb.unloadMBeanError", m.getName())); 226 } 227 } 228 } | Popular Tags |