1 4 package org.oddjob.jmx.server; 5 6 import java.util.ArrayList ; 7 import java.util.LinkedList ; 8 import java.util.List ; 9 10 import javax.management.MBeanAttributeInfo ; 11 import javax.management.MBeanException ; 12 import javax.management.MBeanNotificationInfo ; 13 import javax.management.MBeanOperationInfo ; 14 import javax.management.Notification ; 15 import javax.management.ObjectName ; 16 import javax.management.ReflectionException ; 17 18 import org.apache.log4j.Logger; 19 import org.oddjob.Structural; 20 import org.oddjob.structural.StructuralEvent; 21 import org.oddjob.structural.StructuralListener; 22 23 public class StructuralInfo implements InterfaceInfo { 24 private static final Logger logger = Logger.getLogger(StructuralInfo.class); 25 26 public static final String CHILD_ADDED_NOTIF_TYPE = "org.oddjob.childadded"; 27 28 public static final String CHILD_REMOVED_NOTIF_TYPE = "org.oddjob.childremoved"; 29 30 31 public Class interfaceClass() { 32 return Structural.class; 33 } 34 35 public MBeanAttributeInfo [] getMBeanAttributeInfo() { 36 return new MBeanAttributeInfo [0]; 37 } 38 39 public MBeanOperationInfo [] getMBeanOperationInfo() { 40 return new MBeanOperationInfo [0]; 41 } 42 43 public MBeanNotificationInfo [] getMBeanNotificationInfo() { 44 45 MBeanNotificationInfo [] nInfo = new MBeanNotificationInfo [] { 46 new MBeanNotificationInfo (new String [] { 47 CHILD_ADDED_NOTIF_TYPE, CHILD_REMOVED_NOTIF_TYPE }, 48 Notification .class.getName(), "Structural notification.")}; 49 return nInfo; 50 } 51 52 public InterfaceHandler attach(Object target, 53 OddjobMBean ojmb) { 54 Structural structural = (Structural) target; 55 ServerStructuralHelper structuralHelper = new ServerStructuralHelper (structural, ojmb); 56 structural.addStructuralListener(structuralHelper); 57 return structuralHelper; 58 } 59 60 class ServerStructuralHelper implements StructuralListener, InterfaceHandler { 61 62 private final Structural structural; 63 private final OddjobMBean ojmb; 64 private boolean duplicate; 65 66 67 private final LinkedList children = new LinkedList (); 68 69 private final List lastNotifications = new ArrayList (); 70 71 ServerStructuralHelper(Structural structural, 72 OddjobMBean ojmb) { 73 this.structural = structural; 74 this.ojmb = ojmb; 75 } 76 77 82 public void childAdded(final StructuralEvent e) { 83 final ServerContext childContext = new ServerContext(e.getChild(), 84 ojmb.getContext()); 85 duplicate = childContext.isDuplicate(); 86 if (duplicate) { 87 return; 88 } 89 ojmb.runSynchronized(new Runnable () { 90 public void run() { 91 ObjectName child = ojmb.addChild(e.getChild(), childContext); 92 int index = e.getIndex(); 93 StructuralEvent newEvent = new StructuralEvent(new Object (), 94 child, index); 95 final Notification notification = new Notification ( 96 CHILD_ADDED_NOTIF_TYPE, ojmb, 97 ojmb.getNextNotificationNumber()); 98 notification.setUserData(newEvent); 99 children.add(index, child); 100 ojmb.sendNotification(notification); 101 lastNotifications.add(index, notification); 102 } 103 }); 104 logger.debug("Child added, ObjectName [" + e.getChild().toString() + "], index [" + e.getIndex() + "]"); 105 } 106 107 112 public void childRemoved(final StructuralEvent e) { 113 if (duplicate) { 114 return; 116 } 117 ojmb.runSynchronized(new Runnable () { 118 public void run() { 119 int index = e.getIndex(); 120 ObjectName child = (ObjectName ) children.get(index); 121 StructuralEvent newEvent = new StructuralEvent(new Object (), 122 child, index); 123 Notification notification = new Notification ( 124 CHILD_REMOVED_NOTIF_TYPE, ojmb, 125 ojmb.getNextNotificationNumber()); 126 notification.setUserData(newEvent); 127 children.remove(index); 128 ojmb.removeChild(child); 129 ojmb.sendNotification(notification); 130 lastNotifications.remove(index); 131 } 132 }); 133 ojmb.getContext().removeChild(e.getChild()); 134 logger.debug("Child removed, ObjectName [" + e.getChild().toString() + "], index [" + e.getIndex() + "]"); 135 } 136 137 public Notification [] getLastNotifications() { 138 return (Notification []) lastNotifications.toArray(new Notification [0]); 139 } 140 141 public Object invoke(String actionName, Object [] params, String [] signature) throws MBeanException , ReflectionException { 142 throw new ReflectionException ( 143 new IllegalStateException ("invoked for an unknown method."), 144 actionName); 145 } 146 147 public void destroy() { 148 structural.removeStructuralListener(this); 149 while (children.size() > 0) { 151 StructuralEvent dummyEvent = new StructuralEvent(new Object (), 152 new Object (), children.size() - 1); 153 childRemoved(dummyEvent); 154 } 155 } 156 157 } 158 } 159 | Popular Tags |