1 23 24 package com.sun.enterprise.admin.mbeans.custom; 25 26 import com.sun.enterprise.admin.common.MBeanServerFactory; 27 import com.sun.enterprise.admin.common.constant.AdminConstants; 28 import com.sun.enterprise.admin.event.AdminEventListenerException; 29 import com.sun.enterprise.admin.event.MBeanElementChangeEvent; 30 import com.sun.enterprise.admin.event.MBeanElementChangeEventListener; 31 import com.sun.enterprise.admin.mbeans.custom.loading.CustomMBeanRegistrationImpl; 32 import com.sun.enterprise.admin.mbeans.custom.loading.MBeanAttributeSetter; 33 import com.sun.enterprise.admin.server.core.CustomMBeanRegistration; 34 import com.sun.enterprise.config.ConfigAdd; 35 import com.sun.enterprise.config.ConfigChange; 36 import com.sun.enterprise.config.ConfigContext; 37 import com.sun.enterprise.config.ConfigDelete; 38 import com.sun.enterprise.config.serverbeans.ElementProperty; 39 import com.sun.enterprise.config.serverbeans.Mbean; 40 import com.sun.enterprise.config.serverbeans.ApplicationRef; 41 import com.sun.enterprise.config.serverbeans.ApplicationHelper; 42 import com.sun.enterprise.config.serverbeans.Server; 43 import java.util.ArrayList ; 44 import java.util.logging.Logger ; 45 import javax.management.MBeanServer ; 46 import javax.management.ObjectName ; 47 import javax.management.JMException ; 48 import com.sun.enterprise.config.ConfigException; 49 50 public class InProcessMBeanElementChangeEventListenerImpl implements MBeanElementChangeEventListener { 51 52 private static final Logger logger = Logger.getLogger(AdminConstants.kLoggerName); 53 private final MBeanServer mbs = MBeanServerFactory.getMBeanServer(); 54 public InProcessMBeanElementChangeEventListenerImpl() { 55 } 56 57 public void handleUpdate(final MBeanElementChangeEvent event) throws AdminEventListenerException { 58 63 try { 64 final ConfigContext rcc = event.getConfigContext(); 65 final ArrayList <ConfigChange> changes = event.getConfigChangeList(); 66 for (ConfigChange change : changes) { 67 handleUpdate(rcc, change); 68 } 69 } catch (final AdminEventListenerException ae) { 70 throw ae; 71 } catch (final Exception e) { 72 throw new AdminEventListenerException(e); 73 } 74 } 75 76 public void handleCreate(final MBeanElementChangeEvent event) throws AdminEventListenerException { 77 try { 78 final ConfigContext rcc = event.getConfigContext(); 79 final ArrayList <ConfigAdd> additions = event.getConfigChangeList(); 80 for (ConfigAdd added : additions) { 82 final String xp = added.getXPath(); 83 if (xp != null) { 84 Object co = rcc.exactLookup(xp); 85 if (co instanceof ApplicationRef) { 86 co = ApplicationHelper.findApplication(rcc, 87 ((ApplicationRef)co).getRef()); 88 if (co instanceof Mbean) { 89 Mbean mb = (Mbean)co; 90 if(mb.isEnabled()) 91 register(mb); 92 } 93 } 94 } 95 } 96 } catch (final Exception e) { 97 throw new AdminEventListenerException(e); 98 } 99 } 100 101 public void handleDelete(final MBeanElementChangeEvent event) throws AdminEventListenerException { 102 try { 103 final ConfigContext occ = event.getOldConfigContext(); 104 final ArrayList <ConfigDelete> deletions = event.getConfigChangeList(); 105 for (ConfigDelete deleted : deletions) { 107 final String xp = deleted.getXPath(); 108 if (xp != null) { 109 Object co = occ.exactLookup(xp); 110 if (co instanceof ApplicationRef) { 111 co = ApplicationHelper.findApplication(occ, 112 ((ApplicationRef)co).getRef()); 113 if (co instanceof Mbean) { 114 final Mbean mbean = (Mbean)co; 115 unregister(mbean); 116 logger.info(CMBStrings.get("cmb.successfulDelete", mbean.getName())); 117 } 118 } 119 } 120 } 121 } catch (final Exception e) { 122 throw new AdminEventListenerException(e); 123 } 124 } 125 126 protected void register(final Mbean mbean) throws Exception { 127 final ObjectName on = CustomMBeanRegistrationImpl.getCascadingAwareObjectName(mbean); 128 if (mbs.isRegistered(on)) { 129 logger.info(CMBStrings.get("cmb.unsuccessfulRegistration", on)); 130 } 131 else { 132 final CustomMBeanRegistration cmr = new CustomMBeanRegistrationImpl(mbs); 133 cmr.registerMBean(mbean); 134 logger.info(CMBStrings.get("cmb.successfulRegistration", mbean.getName())); 135 } 136 } 137 138 protected void unregister(final Mbean mbean) throws Exception { 139 final ObjectName on = CustomMBeanRegistrationImpl.getCascadingAwareObjectName(mbean); 143 if (mbs.isRegistered(on)) { 144 mbs.unregisterMBean(on); 145 logger.info(CMBStrings.get("cmb.successfulUnRegistration", on)); 146 } 147 else { 148 logger.info(CMBStrings.get("cmb.unsuccessfulUnRegistration", on)); 149 } 150 } 151 private void handleUpdate(ConfigContext rcc, ConfigChange change) throws JMException , ConfigException, AdminEventListenerException { 154 final String xp = change.getXPath(); 155 156 if (xp == null) 157 return; 158 159 logger.fine(CMBStrings.get("cmb.gotConfigChange", xp)); 160 161 final Object changedObject = rcc.exactLookup(xp); 162 163 final String parentPath = xp.substring(0, xp.lastIndexOf("/")); 164 Object parent = null; 165 try { 166 parent = rcc.exactLookup(parentPath); 167 } catch(Exception e) { 168 } 170 171 if (changedObject instanceof Mbean) { 172 handleUpdate((Mbean)changedObject); 173 } else if (changedObject instanceof ElementProperty) { 174 handleUpdate(parent, (ElementProperty)changedObject); 175 } else if (changedObject instanceof ApplicationRef) { 176 handleUpdate(rcc, (ApplicationRef)changedObject); 177 } 178 else { 179 throw new AdminEventListenerException(CMBStrings.get("InternalError", "Can't handle this: ", changedObject.getClass())); 180 } 181 } 182 183 private void handleUpdate(Mbean mbean){ 184 logger.info(CMBStrings.get("cmb.illegalHandleUpdate", mbean.getName())); 185 } 186 187 private void handleUpdate(Object parent, ElementProperty ep) throws JMException { 188 final Mbean mbean = (Mbean) parent; 189 if (! mbean.isEnabled()) { 190 logger.info(CMBStrings.get("cmb.mbeanIsDisabled", mbean.getName())); 191 return; 192 } 193 final ObjectName on = new ObjectName (mbean.getObjectName()); 194 final MBeanAttributeSetter mas = new MBeanAttributeSetter(mbs, on); 195 mas.setIt(ep.getName(), ep.getValue()); 196 } 197 198 private void handleUpdate(ConfigContext rcc, ApplicationRef ref) throws JMException , ConfigException, AdminEventListenerException{ 199 String mbeanName = ref.getRef(); 200 Mbean mbean = (Mbean)ApplicationHelper.findApplication(rcc, mbeanName); 201 202 207 try { 208 if(ref.isEnabled()) { 209 register(mbean); 210 } 211 else { 212 unregister(mbean); 213 } 214 }catch(Exception e) { 215 throw new AdminEventListenerException(e); 216 } 217 } 218 } | Popular Tags |