1 22 package org.jboss.system; 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import javax.management.ObjectName ; 29 30 import org.jboss.logging.Logger; 31 import org.jboss.mx.interceptor.AbstractInterceptor; 32 import org.jboss.mx.interceptor.DynamicInterceptor; 33 import org.jboss.mx.interceptor.Interceptor; 34 import org.jboss.mx.server.Invocation; 35 36 52 public abstract class InterceptorServiceMBeanSupport extends ServiceMBeanSupport 53 implements InterceptorServiceMBean 54 { 55 57 58 private List interceptables; 59 60 61 private Interceptor interceptor; 62 63 65 68 public InterceptorServiceMBeanSupport() 69 { 70 super(); 71 } 72 73 80 public InterceptorServiceMBeanSupport(final Class type) 81 { 82 super(type); 83 } 84 85 92 public InterceptorServiceMBeanSupport(final String category) 93 { 94 super(category); 95 } 96 97 104 public InterceptorServiceMBeanSupport(final Logger log) 105 { 106 super(log); 107 } 108 109 111 public void setInterceptables(List interceptables) 112 { 113 if (interceptables != null) 115 { 116 this.interceptables = new ArrayList (interceptables); 117 } 118 } 119 120 public List getInterceptables() 121 { 122 if (interceptables != null) 124 { 125 return new ArrayList (interceptables); 126 } 127 return null; 128 } 129 130 132 139 protected void attach() throws Exception 140 { 141 if (interceptor == null) 142 { 143 attach(new XMBeanInterceptor()); 144 } 145 } 146 147 153 protected void attach(Interceptor interceptor) throws Exception 154 { 155 if (interceptor == null) 156 { 157 throw new IllegalArgumentException ("Null interceptor"); 158 } 159 160 if (this.interceptor != null) 162 { 163 throw new IllegalStateException ("Interceptor already attached"); 164 } 165 166 log.debug("Attaching interceptor: " + interceptor.getName()); 167 168 this.interceptor = interceptor; 170 171 if (interceptables != null) 175 { 176 Object [] params = new Object [] { interceptor }; 177 String [] signature = new String [] { Interceptor.class.getName() }; 178 179 for (Iterator i = interceptables.iterator(); i.hasNext(); ) 180 { 181 ObjectName target = (ObjectName )i.next(); 182 super.server.invoke(target, 183 DynamicInterceptor.ADD_INTERCEPTOR, 184 params, 185 signature); 186 187 log.debug("Interceptor attached to: '" + target + "'"); 188 } 189 } 190 } 191 192 195 protected void detach() 196 { 197 if (interceptor != null) 198 { 199 log.debug("Detaching interceptor: " + interceptor.getName()); 200 if (interceptables != null) 201 { 202 Object [] params = new Object [] { interceptor }; 203 String [] signature = new String [] { Interceptor.class.getName() }; 204 205 for (Iterator i = interceptables.iterator(); i.hasNext(); ) 206 { 207 ObjectName target = (ObjectName )i.next(); 208 try 209 { 210 super.server.invoke(target, 211 DynamicInterceptor.REMOVE_INTERCEPTOR, 212 params, 213 signature); 214 215 log.debug("Interceptor detached from: '" + target + "'"); 216 } 217 catch (Exception e) 218 { 219 log.debug("Caught exception while removing interceptor from '" + 220 target + "'", e); 221 } 222 } 223 } 224 interceptor = null; 225 } 226 } 227 228 231 protected Object invokeNext(Invocation invocation) throws Throwable 232 { 233 Interceptor next = invocation.nextInterceptor(); 236 if (next != null) 237 { 238 return next.invoke(invocation); 239 } 240 else 241 { 242 return invocation.dispatch(); 243 } 244 } 245 246 248 251 protected Object invoke(Invocation invocation) throws Throwable 252 { 253 return invokeNext(invocation); 254 } 255 256 258 262 private class XMBeanInterceptor extends AbstractInterceptor 263 { 264 public XMBeanInterceptor() 265 { 266 super("XMBeanInterceptor('" + InterceptorServiceMBeanSupport.this.getServiceName() + "')"); 267 } 268 269 public Object invoke(Invocation invocation) throws Throwable 270 { 271 return InterceptorServiceMBeanSupport.this.invoke(invocation); 273 } 274 } 275 } 276 | Popular Tags |