1 8 9 package mx4j.server.interceptor; 10 11 import java.util.ArrayList ; 12 13 import javax.management.MBeanException ; 14 import javax.management.MBeanServer ; 15 import javax.management.ObjectName ; 16 17 import mx4j.ImplementationException; 18 19 25 public class MBeanServerInterceptorConfigurator implements MBeanServerInterceptorConfiguratorMBean 26 { 27 public static final String OBJECT_NAME = "JMImplementation:type=MBeanServerInterceptorConfigurator"; 28 29 private final MBeanServer server; 30 private final ArrayList preInterceptors = new ArrayList (); 31 private final ArrayList postInterceptors = new ArrayList (); 32 private final ArrayList clientInterceptors = new ArrayList (); 33 private volatile boolean running; 34 private boolean chainModified; 35 private MBeanServerInterceptor head; 36 37 40 public MBeanServerInterceptorConfigurator(MBeanServer server) 41 { 42 this.server = server; 43 chainModified = true; 44 } 45 46 50 public void addInterceptor(MBeanServerInterceptor interceptor) 51 { 52 synchronized (clientInterceptors) 53 { 54 clientInterceptors.add(interceptor); 55 chainModified = true; 56 } 57 } 58 59 63 public void registerInterceptor(MBeanServerInterceptor interceptor, ObjectName name) throws MBeanException 64 { 65 try 67 { 68 server.registerMBean(interceptor, name); 69 addInterceptor(interceptor); 70 } 71 catch (Exception x) 72 { 73 throw new MBeanException (x, "Could not register interceptor with name " + name); 74 } 75 } 76 77 81 public void clearInterceptors() 82 { 83 synchronized (clientInterceptors) 84 { 85 clientInterceptors.clear(); 86 chainModified = true; 87 } 88 } 89 90 95 public void addPreInterceptor(MBeanServerInterceptor interceptor) 96 { 97 if (isRunning()) throw new ImplementationException(); 98 preInterceptors.add(interceptor); 99 } 100 101 106 public void addPostInterceptor(MBeanServerInterceptor interceptor) 107 { 108 if (isRunning()) throw new ImplementationException(); 109 postInterceptors.add(interceptor); 110 } 111 112 116 public MBeanServerInterceptor getHeadInterceptor() 117 { 118 if (!isRunning()) return null; 119 120 if (chainModified) setupChain(); 121 122 return head; 123 } 124 125 private void setupChain() 126 { 127 chainModified = false; 128 129 int size = clientInterceptors.size(); 130 ArrayList chain = new ArrayList (preInterceptors.size() + size + postInterceptors.size()); 131 chain.addAll(preInterceptors); 132 if (size > 0) 133 { 134 synchronized (clientInterceptors) 135 { 136 chain.addAll(clientInterceptors); 137 } 138 } 139 chain.addAll(postInterceptors); 140 141 MBeanServerInterceptor first = (MBeanServerInterceptor)chain.get(0); 143 first.setChain(chain); 144 145 head = first; 146 } 147 148 153 public void start() 154 { 155 if (!isRunning()) 156 { 157 running = true; 158 } 159 } 160 161 165 public void stop() 166 { 167 if (isRunning()) 168 { 169 running = false; 170 } 171 } 172 173 177 public boolean isRunning() 178 { 179 return running; 180 } 181 } 182 | Popular Tags |