1 22 package org.jboss.ejb; 23 24 import java.lang.reflect.Method ; 25 import java.util.HashMap ; 26 import java.util.Hashtable ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 30 import javax.ejb.CreateException ; 31 import javax.ejb.EJBMetaData ; 32 import javax.ejb.EJBObject ; 33 import javax.ejb.Handle ; 34 import javax.ejb.HomeHandle ; 35 import javax.ejb.RemoveException ; 36 import javax.ejb.TimedObject ; 37 import javax.ejb.Timer ; 38 import javax.ejb.EJBException ; 39 import javax.management.ObjectName ; 40 41 import org.jboss.invocation.Invocation; 42 import org.jboss.metadata.MessageDrivenMetaData; 43 import org.jboss.util.NullArgumentException; 44 45 58 public class MessageDrivenContainer 59 extends Container 60 implements EJBProxyFactoryContainer, InstancePoolContainer, MessageDrivenContainerMBean 61 { 62 66 protected Map beanMapping; 67 68 69 protected InstancePool instancePool; 70 71 75 protected Interceptor interceptor; 76 77 protected long messageCount; 78 79 public LocalProxyFactory getLocalProxyFactory() 80 { 81 return localProxyFactory; 82 } 83 84 public void setInstancePool(final InstancePool instancePool) 85 { 86 if (instancePool == null) 87 throw new NullArgumentException("instancePool"); 88 89 this.instancePool = instancePool; 90 this.instancePool.setContainer(this); 91 } 92 93 public InstancePool getInstancePool() 94 { 95 return instancePool; 96 } 97 98 public void addInterceptor(Interceptor in) 99 { 100 if (interceptor == null) 101 { 102 interceptor = in; 103 } 104 else 105 { 106 Interceptor current = interceptor; 107 108 while (current.getNext() != null) 109 { 110 current = current.getNext(); 111 } 112 113 current.setNext(in); 114 } 115 } 116 117 public Interceptor getInterceptor() 118 { 119 return interceptor; 120 } 121 122 126 public long getMessageCount() 127 { 128 return messageCount; 129 } 130 131 135 public Class getHomeClass() 136 { 137 return null; 139 } 140 141 public Class getRemoteClass() 142 { 143 return null; 145 } 146 147 public Class getLocalClass() 148 { 149 return null; 150 } 151 152 public Class getLocalHomeClass() 153 { 154 return null; 156 } 157 158 160 protected void createService() throws Exception 161 { 162 ClassLoader oldCl = SecurityActions.getContextClassLoader(); 164 SecurityActions.setContextClassLoader(getClassLoader()); 165 166 try 167 { 168 super.createService(); 170 171 Map map = new HashMap (); 173 MessageDrivenMetaData mdMetaData = (MessageDrivenMetaData)metaData; 174 Class clazz = getClassLoader().loadClass(mdMetaData.getMessagingType()); 175 Method [] methods = clazz.getDeclaredMethods(); 176 for (int i = 0; i < methods.length; i++) 177 { 178 Method m = methods[i]; 179 map.put(m, beanClass.getMethod(m.getName(), m.getParameterTypes())); 180 log.debug("Mapped " + m.getName() + " " + m.hashCode() + " to " + map.get(m)); 181 } 182 if( TimedObject .class.isAssignableFrom( beanClass ) ) { 183 map.put( 185 TimedObject .class.getMethod( "ejbTimeout", new Class [] { Timer .class } ), 186 beanClass.getMethod( "ejbTimeout", new Class [] { Timer .class } ) 187 ); 188 } 189 beanMapping = map; 190 191 try 193 { 194 ObjectName containerName = super.getJmxName(); 195 Hashtable props = containerName.getKeyPropertyList(); 196 props.put("plugin", "pool"); 197 ObjectName poolName = new ObjectName (containerName.getDomain(), props); 198 server.registerMBean(instancePool, poolName); 199 } 200 catch(Throwable t) 201 { 202 log.debug("Failed to register pool as mbean", t); 203 } 204 instancePool.create(); 206 207 for (Iterator it = proxyFactories.keySet().iterator(); it.hasNext();) 208 { 209 String invokerBinding = (String ) it.next(); 210 EJBProxyFactory ci = (EJBProxyFactory) proxyFactories.get(invokerBinding); 211 try 213 { 214 ObjectName containerName = super.getJmxName(); 215 Hashtable props = containerName.getKeyPropertyList(); 216 props.put("plugin", "invoker"); 217 props.put("binding", invokerBinding); 218 ObjectName invokerName = new ObjectName (containerName.getDomain(), props); 219 server.registerMBean(ci, invokerName); 220 } 221 catch(Throwable t) 222 { 223 log.debug("Failed to register invoker binding as mbean", t); 224 } 225 ci.create(); 226 } 227 228 Interceptor in = interceptor; 230 while (in != null) 231 { 232 in.setContainer(this); 233 in.create(); 234 in = in.getNext(); 235 } 236 237 } 238 finally 239 { 240 SecurityActions.setContextClassLoader(oldCl); 242 } 243 } 244 245 protected void startService() throws Exception 246 { 247 ClassLoader oldCl = SecurityActions.getContextClassLoader(); 249 SecurityActions.setContextClassLoader(getClassLoader()); 250 251 try 252 { 253 super.startService(); 255 256 instancePool.start(); 258 259 Interceptor in = interceptor; 261 while (in != null) 262 { 263 in.start(); 264 in = in.getNext(); 265 } 266 267 for (Iterator it = proxyFactories.keySet().iterator(); it.hasNext();) 269 { 270 String invokerBinding = (String ) it.next(); 271 EJBProxyFactory ci = (EJBProxyFactory) proxyFactories.get(invokerBinding); 272 ci.start(); 273 } 274 275 restoreTimers(); 277 } 278 finally 279 { 280 SecurityActions.setContextClassLoader(oldCl); 282 } 283 } 284 285 protected void stopService() throws Exception 286 { 287 ClassLoader oldCl = SecurityActions.getContextClassLoader(); 289 SecurityActions.setContextClassLoader(getClassLoader()); 290 291 try 292 { 293 super.stopService(); 295 296 for (Iterator it = proxyFactories.keySet().iterator(); it.hasNext();) 298 { 299 String invokerBinding = (String ) it.next(); 300 EJBProxyFactory ci = (EJBProxyFactory) proxyFactories.get(invokerBinding); 301 ci.stop(); 302 } 303 304 instancePool.stop(); 306 307 Interceptor in = interceptor; 309 while (in != null) 310 { 311 in.stop(); 312 in = in.getNext(); 313 } 314 } 315 finally 316 { 317 SecurityActions.setContextClassLoader(oldCl); 319 } 320 } 321 322 protected void destroyService() throws Exception 323 { 324 ClassLoader oldCl = SecurityActions.getContextClassLoader(); 326 SecurityActions.setContextClassLoader(getClassLoader()); 327 328 try 329 { 330 for (Iterator it = proxyFactories.keySet().iterator(); it.hasNext();) 332 { 333 String invokerBinding = (String ) it.next(); 334 EJBProxyFactory ci = (EJBProxyFactory) proxyFactories.get(invokerBinding); 335 ci.destroy(); 336 ci.setContainer(null); 337 try 338 { 339 ObjectName containerName = super.getJmxName(); 340 Hashtable props = containerName.getKeyPropertyList(); 341 props.put("plugin", "invoker"); 342 props.put("binding", invokerBinding); 343 ObjectName invokerName = new ObjectName (containerName.getDomain(), props); 344 server.unregisterMBean(invokerName); 345 } 346 catch(Throwable ignore) 347 { 348 } 349 } 350 351 instancePool.destroy(); 353 instancePool.setContainer(null); 354 try 355 { 356 ObjectName containerName = super.getJmxName(); 357 Hashtable props = containerName.getKeyPropertyList(); 358 props.put("plugin", "pool"); 359 ObjectName poolName = new ObjectName (containerName.getDomain(), props); 360 server.unregisterMBean(poolName); 361 } 362 catch(Throwable ignore) 363 { 364 } 365 366 Interceptor in = interceptor; 368 while (in != null) 369 { 370 in.destroy(); 371 in.setContainer(null); 372 in = in.getNext(); 373 } 374 375 super.destroyService(); 377 } 378 finally 379 { 380 SecurityActions.setContextClassLoader(oldCl); 382 } 383 } 384 385 388 public Object internalInvokeHome(Invocation mi) 389 throws Exception 390 { 391 throw new Error ("invokeHome not valid for MessageDriven beans"); 392 } 393 394 399 public Object internalInvoke(Invocation mi) throws Exception 400 { 401 return getInterceptor().invoke(mi); 403 } 404 405 406 408 public EJBObject createHome() 409 throws java.rmi.RemoteException , CreateException 410 { 411 throw new Error ("createHome not valid for MessageDriven beans"); 412 } 413 414 415 public void removeHome(Handle handle) 416 throws java.rmi.RemoteException , RemoveException 417 { 418 throw new Error ("removeHome not valid for MessageDriven beans"); 419 } 421 422 public void removeHome(Object primaryKey) 423 throws java.rmi.RemoteException , RemoveException 424 { 425 throw new Error ("removeHome not valid for MessageDriven beans"); 426 } 428 429 public EJBMetaData getEJBMetaDataHome() 430 throws java.rmi.RemoteException 431 { 432 throw new Error ("getEJBMetaDataHome not valid for MessageDriven beans"); 435 } 436 437 public HomeHandle getHomeHandleHome() 438 throws java.rmi.RemoteException 439 { 440 throw new Error ("getHomeHandleHome not valid for MessageDriven beans"); 443 } 444 445 Interceptor createContainerInterceptor() 446 { 447 return new ContainerInterceptor(); 448 } 449 450 453 class ContainerInterceptor 454 extends AbstractContainerInterceptor 455 { 456 459 public Object invokeHome(Invocation mi) throws Exception 460 { 461 throw new Error ("invokeHome not valid for MessageDriven beans"); 462 } 463 464 469 public Object invoke(Invocation mi) 470 throws Exception 471 { 472 EnterpriseContext ctx = (EnterpriseContext) mi.getEnterpriseContext(); 473 474 if (ctx.getTransaction() == null) 477 { 478 ctx.setTransaction(mi.getTransaction()); 479 } 480 481 Method m = (Method ) beanMapping.get(mi.getMethod()); 483 if( m == null ) 484 { 485 String msg = MessageDrivenContainer.this.getBeanMetaData().getEjbName() 487 + " Invalid invocation, check your deployment packaging, interfaces, method=" + mi.getMethod(); 488 throw new EJBException (msg); 489 } 490 491 try 493 { 494 messageCount++; 495 return mi.performCall(ctx.getInstance(), m, mi.getArguments()); 496 } 497 catch (Exception e) 498 { 499 rethrow(e); 500 } 501 502 throw new org.jboss.util.UnreachableStatementException(); 504 } 505 } 506 } 507 | Popular Tags |