1 22 package org.jboss.mx.interceptor; 23 24 import java.util.Hashtable ; 25 import java.util.Set ; 26 27 import javax.management.InstanceAlreadyExistsException ; 28 import javax.management.MBeanOperationInfo ; 29 import javax.management.MBeanParameterInfo ; 30 import javax.management.MBeanServer ; 31 import javax.management.MalformedObjectNameException ; 32 import javax.management.ObjectName ; 33 import javax.management.modelmbean.ModelMBean ; 34 import javax.management.modelmbean.ModelMBeanInfo ; 35 import javax.management.modelmbean.ModelMBeanInfoSupport ; 36 import javax.management.modelmbean.ModelMBeanOperationInfo ; 37 38 import org.jboss.logging.Logger; 39 import org.jboss.mx.modelmbean.ModelMBeanConstants; 40 import org.jboss.mx.modelmbean.RequiredModelMBeanInstantiator; 41 import org.jboss.mx.server.Invocation; 42 import org.jboss.mx.service.ServiceConstants; 43 import org.jboss.mx.util.AgentID; 44 45 46 56 public abstract class AbstractSharedInterceptor 57 extends AbstractInterceptor 58 implements SharedInterceptor 59 { 60 61 64 protected MBeanServer server = null; 65 66 70 protected ObjectName objectName = null; 71 72 73 75 81 public AbstractSharedInterceptor() {} 82 83 93 public AbstractSharedInterceptor(String name) 94 { 95 super(name); 96 } 97 98 99 101 public ObjectName getObjectName() 102 { 103 return objectName; 104 } 105 106 public MBeanServer getMBeanServer() 107 { 108 return server; 109 } 110 111 138 public synchronized ObjectName register(MBeanServer server) 139 throws InterceptorNameConflictException 140 { 141 142 this.server = server; 144 145 if (log == null) 147 log = Logger.getLogger("JBossMX.Interceptor." + name); 148 149 try 150 { 151 objectName = createObjectName(); 153 154 Set names = server.queryNames(objectName, null ); 156 157 if (names.size() > 0) 159 throw new InterceptorNameConflictException( 160 "A shared interceptor named '" + name + "' already registered " + 161 "to this MBean server (" + AgentID.get(server) + ")" 162 ); 163 164 ModelMBean rmm = RequiredModelMBeanInstantiator.instantiate(); 166 rmm.setManagedResource(this, ModelMBeanConstants.OBJECT_REF); 167 rmm.setModelMBeanInfo(getManagementInterface()); 168 server.registerMBean(rmm, objectName); 169 170 isShared = true; 172 } 173 174 catch (InstanceAlreadyExistsException e) 175 { 176 throw new InterceptorNameConflictException( 180 "A shared interceptor named '" + name + "' already registered " + 181 "to this MBean server (" + AgentID.get(server) + ")" 182 ); 183 } 184 185 catch (Exception e) 186 { 187 if (log != null) 190 log.error(e.toString(), e); 191 } 192 193 return objectName; 194 } 195 196 208 public void init() throws Exception {} 209 210 217 public void start() {} 218 219 231 public void stop() throws Exception {} 232 233 240 public void destroy() {} 241 242 243 245 public ObjectName preRegister(MBeanServer server, ObjectName oname) throws Exception 246 { 247 this.server = server; 248 249 if (oname == null) 250 this.objectName = createObjectName(); 251 else 252 this.objectName = oname; 253 254 init(); 255 256 return objectName; 257 } 258 259 public void postRegister(Boolean registrationSuccesful) { 260 isShared = true; 261 262 start(); 263 } 264 265 public void preDeregister() throws Exception { 266 stop(); 267 268 isShared = false; 269 objectName = null; 270 } 271 272 public void postDeregister() { 273 destroy(); 274 } 275 276 277 279 290 public synchronized void setName(String name) 291 { 292 if (isShared()) 293 throw new IllegalArgumentException ("Cannot change the interceptor name. Already registered."); 294 295 this.name = name; 296 } 297 298 299 301 306 public String toString() 307 { 308 String className = getClass().getName(); 309 int index = className.lastIndexOf('.'); 310 311 return className.substring((index < 0) ? 0 : index) + "[" + 312 "name=" + name + "SHARED " + objectName + "]"; 313 } 314 315 316 318 335 protected ObjectName createObjectName() throws MalformedObjectNameException 336 { 337 Hashtable props = new Hashtable (2); 339 props.put("type", "Interceptor"); 340 props.put("name", name); 341 props.put("ID", "0"); 342 343 return new ObjectName (ServiceConstants.JBOSSMX_DOMAIN, props); 344 } 345 346 347 349 private ModelMBeanInfo getManagementInterface() 350 { 351 return new ModelMBeanInfoSupport ( 352 this.getClass().getName(), "Interceptor invocation interface", 355 null, 357 null, 359 new ModelMBeanOperationInfo [] { 361 new ModelMBeanOperationInfo ( 362 "invoke", "Shared interceptor invoke operation.", new MBeanParameterInfo [] { 366 new MBeanParameterInfo ( 367 "invocation", Invocation.class.getName(), "The invocation object." ) 371 }, 372 Object .class.getName(), MBeanOperationInfo.ACTION_INFO ) 375 }, 376 377 null ); 379 } 380 381 } 382 383 384 | Popular Tags |