1 22 package org.jboss.ejb3; 23 24 import javassist.bytecode.ClassFile; 25 import org.hibernate.cfg.EJB3DTDEntityResolver; 26 import org.hibernate.ejb.packaging.PersistenceMetadata; 27 import org.hibernate.ejb.packaging.PersistenceXmlLoader; 28 import org.jboss.ejb3.enc.EjbModuleEjbResolver; 29 import org.jboss.ejb3.enc.EjbModulePersistenceUnitResolver; 30 import org.jboss.ejb3.entity.PersistenceUnitDeployment; 31 import org.jboss.logging.Logger; 32 import org.jboss.util.file.ArchiveBrowser; 33 import org.jboss.util.file.ClassFileFilter; 34 import org.jboss.virtual.VirtualFile; 35 import org.jboss.virtual.VFS; 36 37 import javax.management.MBeanServer ; 38 import javax.management.ObjectName ; 39 import javax.naming.InitialContext ; 40 import javax.naming.NameNotFoundException ; 41 import javax.naming.NamingException ; 42 import javax.persistence.Entity; 43 import javax.security.jacc.PolicyConfiguration ; 44 import javax.security.jacc.PolicyConfigurationFactory ; 45 import java.io.DataInputStream ; 46 import java.io.InputStream ; 47 import java.net.URL ; 48 import java.util.ArrayList ; 49 import java.util.HashMap ; 50 import java.util.Iterator ; 51 import java.util.LinkedHashMap ; 52 import java.util.List ; 53 54 60 public abstract class Ejb3Deployment 61 { 62 63 private static final Logger log = Logger.getLogger(Ejb3Deployment.class); 64 public static final String ACTUAL_ENTITY_MANAGER_FACTORY_CONTEXT = "java:/ActualEntityManagerFactories"; 65 public static final String MANAGED_ENTITY_FACTORY_CONTEXT = "java:/managedEntityFactories"; 66 67 protected DeploymentUnit di; 68 69 protected LinkedHashMap ejbContainers = new LinkedHashMap (); 70 71 protected boolean hasEntities; 72 protected List <String > explicitEntityClasses = new ArrayList <String >(); 73 74 protected List <PersistenceUnitDeployment> persistenceUnitDeployments = new ArrayList <PersistenceUnitDeployment>(); 75 ; 76 77 protected String defaultSLSBDomain = "Stateless Bean"; 78 protected String defaultSFSBDomain = "Stateful Bean"; 79 protected String defaultMDBDomain = "Message Driven Bean"; 80 protected String defaultConsumerDomain = "Consumer Bean"; 81 protected String defaultServiceDomain = "Service Bean"; 82 protected InitialContext initialContext; 83 84 protected KernelAbstraction kernelAbstraction; 85 86 protected MBeanServer mbeanServer; 88 89 protected DeploymentScope deploymentScope; 90 protected EjbModuleEjbResolver ejbRefResolver; 91 protected EjbModulePersistenceUnitResolver persistenceUnitResolver; 92 93 PolicyConfiguration pc; 95 96 public Ejb3Deployment(DeploymentUnit di, DeploymentScope deploymentScope) 97 { 98 this.di = di; 99 this.deploymentScope = deploymentScope; 100 try 101 { 102 initialContext = EJB3Util.getInitialContext(di.getJndiProperties()); 103 } 104 catch (NamingException e) 105 { 106 throw new RuntimeException (e); 107 } 108 ejbRefResolver = new EjbModuleEjbResolver(deploymentScope, di.getShortName(), ejbContainers, this); 109 persistenceUnitResolver = new EjbModulePersistenceUnitResolver(persistenceUnitDeployments, deploymentScope, ejbContainers); 110 } 111 112 public DeploymentScope getEar() 113 { 114 return deploymentScope; 115 } 116 117 public KernelAbstraction getKernelAbstraction() 118 { 119 return kernelAbstraction; 120 } 121 122 public MBeanServer getMbeanServer() 123 { 124 return mbeanServer; 125 } 126 127 public void setMbeanServer(MBeanServer mbeanServer) 128 { 129 this.mbeanServer = mbeanServer; 130 } 131 132 public DeploymentUnit getDeploymentUnit() 133 { 134 return di; 135 } 136 137 public String getDefaultSLSBDomain() 138 { 139 return defaultSLSBDomain; 140 } 141 142 143 149 public String getScopeKernelName() 150 { 151 String scopedKernelName = ""; 152 if (deploymentScope != null) scopedKernelName += ",ear=" + deploymentScope.getShortName(); 153 scopedKernelName += ",jar=" + di.getShortName(); 154 return scopedKernelName; 155 } 156 157 162 public void setDefaultSLSBDomain(String defaultSLSBDomain) 163 { 164 this.defaultSLSBDomain = defaultSLSBDomain; 165 } 166 167 public String getDefaultSFSBDomain() 168 { 169 return defaultSFSBDomain; 170 } 171 172 public String getDefaultConsumerDomain() 173 { 174 return defaultConsumerDomain; 175 } 176 177 182 public void setDefaultSFSBDomain(String defaultSFSBDomain) 183 { 184 this.defaultSFSBDomain = defaultSFSBDomain; 185 } 186 187 public String getDefaultMDBDomain() 188 { 189 return defaultMDBDomain; 190 } 191 192 197 public void setDefaultMDBDomain(String defaultMDBDomain) 198 { 199 this.defaultMDBDomain = defaultMDBDomain; 200 } 201 202 public String getDefaultServiceDomain() 203 { 204 return defaultServiceDomain; 205 } 206 207 212 public void setDefaultServiceDomain(String defaultServiceDomain) 213 { 214 this.defaultServiceDomain = defaultServiceDomain; 215 } 216 217 protected String getJaccContextId() 218 { 219 return di.getShortName(); 220 } 221 222 public Container getContainer(ObjectName name) 223 { 224 return (Container) ejbContainers.get(name); 225 } 226 227 public java.util.Map getEjbContainers() 228 { 229 return ejbContainers; 230 } 231 232 233 public PersistenceUnitDeployment getPersistenceUnitDeployment(String unitName) throws NameNotFoundException 234 { 235 return persistenceUnitResolver.getPersistenceUnitDeployment(unitName); 236 } 237 238 public PersistenceUnitDeployment getPersistenceUnitDeploymentInternal(String unitName) 239 { 240 return persistenceUnitResolver.getPersistenceUnitDeploymentInternal(unitName); 241 } 242 243 public List <PersistenceUnitDeployment> getPersistenceUnitDeployments() 244 { 245 return persistenceUnitDeployments; 246 } 247 248 249 public EJBContainer getEjbContainer(String ejbLink, Class businessIntf) 250 { 251 return ejbRefResolver.getEjbContainer(ejbLink, businessIntf); 252 } 253 254 public String getEjbJndiName(String ejbLink, Class businessIntf) 255 { 256 return ejbRefResolver.getEjbJndiName(ejbLink, businessIntf); 257 } 258 259 public EJBContainer getEjbContainer(Ejb3Deployment deployment, Class businessIntf) throws NameNotFoundException 260 { 261 return ejbRefResolver.getEjbContainer(deployment, businessIntf); 262 } 263 264 public EJBContainer getEjbContainer(Class businessIntf) throws NameNotFoundException 265 { 266 return ejbRefResolver.getEjbContainer(businessIntf); 267 } 268 269 public String getEjbJndiName(Class businessIntf) throws NameNotFoundException 270 { 271 return ejbRefResolver.getEjbJndiName(businessIntf); 272 } 273 274 protected void processEJBContainerMetadata(Container container) 275 throws Exception 276 { 277 ObjectName on = container.getObjectName(); 278 ejbContainers.put(on, container); 279 DependencyPolicy policy = createDependencyPolicy(); 280 container.processMetadata(policy); 281 282 } 283 284 protected void registerEJBContainer(Container container) 285 throws Exception 286 { 287 ObjectName on = container.getObjectName(); 288 kernelAbstraction.install(on.getCanonicalName(), container.getDependencyPolicy(), container); 289 log.debug("Bound ejb3 container " + on); 290 } 291 292 protected abstract PolicyConfiguration createPolicyConfiguration() throws Exception ; 293 294 protected abstract void putJaccInService(PolicyConfiguration pc, DeploymentUnit unit); 295 296 297 306 public void create() throws Exception 307 { 308 long start = System.currentTimeMillis(); 309 310 pc = createPolicyConfiguration(); 311 312 deploy(); 313 314 initializePersistenceUnits(); 315 316 log.debug("EJB3 deployment time took: " 317 + (System.currentTimeMillis() - start)); 318 } 319 320 public void start() throws Exception 321 { 322 try 323 { 324 startPersistenceUnits(); 325 326 for (Object o : ejbContainers.values()) 327 { 328 Container con = (Container) o; 329 processEJBContainerMetadata(con); 330 } 331 332 for (Object o : ejbContainers.values()) 333 { 334 Container con = (Container) o; 335 registerEJBContainer(con); 336 } 337 338 putJaccInService(pc, di); 339 } 340 catch (Exception ex) 341 { 342 try 343 { 344 stop(); 345 destroy(); 346 } 347 catch (Exception ignored) 348 { 349 } 350 throw ex; 351 } 352 } 353 354 protected void deploy() throws Exception 355 { 356 Ejb3HandlerFactory factory = Ejb3HandlerFactory.getInstance(this); 357 if (di.getUrl() != null) deployUrl(factory); 358 359 if (di.getClasses() != null) 360 { 361 for (Class explicit : di.getClasses()) 362 { 363 if (explicit.isAnnotationPresent(Entity.class)) 364 { 365 continue; 366 } 367 String name = explicit.getName().replace('.', '/') + ".class"; 368 InputStream stream = explicit.getClassLoader().getResourceAsStream(name); 369 deployElement(stream, factory, initialContext); 370 } 371 } 372 } 373 374 protected void deployUrl(Ejb3HandlerFactory factory) 375 throws Exception 376 { 377 InitialContext ctx = initialContext; 378 List <VirtualFile> classes = di.getResources(new org.jboss.ejb3.ClassFileFilter()); 381 for (VirtualFile classFile : classes) 382 { 383 InputStream stream = classFile.openStream(); 384 deployElement(stream, factory, ctx); 385 } 386 } 387 388 protected void deployElement(InputStream stream, Ejb3HandlerFactory factory, InitialContext ctx) 389 throws Exception 390 { 391 DataInputStream dstream = new DataInputStream (stream); 392 ClassFile cf = null; 393 try 394 { 395 cf = new ClassFile(dstream); 396 } 397 finally 398 { 399 dstream.close(); 400 stream.close(); 401 } 402 403 deployElement(factory, cf, ctx); 404 405 } 406 407 protected void deployElement(Ejb3HandlerFactory factory, ClassFile cf, InitialContext ctx) 408 throws Exception 409 { 410 Ejb3Handler handler = factory.createHandler(cf); 411 handler.setCtxProperties(di.getJndiProperties()); 412 413 if (handler.isEjb() || handler.isJBossBeanType()) 414 { 415 List <Container> containers = handler.getContainers(cf, this); 416 for (Container con : containers) 417 { 418 ((EJBContainer) con).instantiated(); 422 this.ejbContainers.put(con.getObjectName(), con); 423 Ejb3Registry.register(con); 424 } 425 } 426 } 427 428 protected void initializePersistenceUnits() 429 throws Exception 430 { 431 URL persistenceXmlUrl = null; 432 persistenceXmlUrl = di.getPersistenceXml(); 433 434 hasEntities = persistenceXmlUrl != null; 435 436 if (!hasEntities) return; 437 438 if (di.getClasses() != null) 439 { 440 for (Class explicit : di.getClasses()) 441 { 442 if (explicit.isAnnotationPresent(Entity.class)) 443 { 444 explicitEntityClasses.add(explicit.getName()); 445 } 446 } 447 } 448 449 List <PersistenceMetadata> persistenceMetadata = PersistenceXmlLoader.deploy(persistenceXmlUrl, new HashMap (), new EJB3DTDEntityResolver()); 451 for (PersistenceMetadata metadata : persistenceMetadata) 452 { 453 String earShortName = deploymentScope == null ? null : deploymentScope.getShortName(); 454 boolean isScoped = ejbContainers.size() > 0; 455 PersistenceUnitDeployment deployment = new PersistenceUnitDeployment(initialContext, this, explicitEntityClasses, persistenceXmlUrl, metadata, earShortName, di.getShortName(), isScoped); 456 PersistenceUnitRegistry.register(deployment); 457 persistenceUnitDeployments.add(deployment); 458 } 459 } 460 461 public abstract DependencyPolicy createDependencyPolicy(); 462 463 protected void startPersistenceUnits() 464 { 465 if (persistenceUnitDeployments == null) return; 466 467 for (PersistenceUnitDeployment entityDeployment : persistenceUnitDeployments) 468 { 469 if (entityDeployment != null) 470 { 471 DependencyPolicy policy = createDependencyPolicy(); 472 entityDeployment.addDependencies(policy); 473 kernelAbstraction.install(entityDeployment.getKernelName(), policy, entityDeployment); 474 } 475 } 476 } 477 478 protected void stopPersistenceUnits() 479 { 480 if (persistenceUnitDeployments == null) return; 481 482 for (PersistenceUnitDeployment entityDeployment : persistenceUnitDeployments) 483 { 484 try 485 { 486 PersistenceUnitRegistry.unregister(entityDeployment); 487 if (entityDeployment != null) 488 { 489 kernelAbstraction.uninstall(entityDeployment.getKernelName()); 490 } 491 } 492 catch (Exception e) 493 { 494 log.debug("error trying to shut down persistence unit", e); 495 } 496 } 497 498 } 499 500 public void stop() throws Exception 501 { 502 for (Object o : ejbContainers.keySet()) 503 { 504 try 505 { 506 ObjectName on = (ObjectName ) o; 507 kernelAbstraction.uninstall(on.getCanonicalName()); 508 Container container = (Container) ejbContainers.get(on); 509 Ejb3Registry.unregister(container); 510 } 511 catch (Exception e) 512 { 513 log.debug("error trying to shut down ejb container", e); 514 } 515 } 516 stopPersistenceUnits(); 517 } 518 519 public void destroy() throws Exception 520 { 521 PolicyConfigurationFactory pcFactory = PolicyConfigurationFactory.getPolicyConfigurationFactory(); 522 PolicyConfiguration pc = pcFactory.getPolicyConfiguration(getJaccContextId(), true); 523 pc.delete(); 524 } 525 } 526 | Popular Tags |