| 1 25 26 package org.objectweb.jonas_ejb.container; 27 28 import java.io.File ; 29 import java.lang.reflect.Method ; 30 import java.util.Collections ; 31 import java.util.Enumeration ; 32 import java.util.HashMap ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.Properties ; 36 import java.util.Set ; 37 38 import javax.ejb.AccessLocalException ; 39 import javax.ejb.EJBException ; 40 import javax.naming.Context ; 41 import javax.naming.InitialContext ; 42 import javax.naming.LinkRef ; 43 import javax.naming.NamingException ; 44 import javax.naming.NameNotFoundException ; 45 import javax.naming.Reference ; 46 import javax.naming.StringRefAddr ; 47 import javax.resource.spi.ActivationSpec ; 48 import javax.resource.spi.work.WorkManager ; 49 import javax.security.jacc.PolicyContext ; 50 51 import org.objectweb.jonas.common.JProp; 52 import org.objectweb.jonas.resource.Rar; 53 import org.objectweb.jonas.ws.JServiceFactory; 54 import org.objectweb.jonas.ws.JServiceFactoryFinder; 55 import org.objectweb.jonas_ejb.container.jorm.RdbFactory; 56 import org.objectweb.jonas_ejb.deployment.api.BeanDesc; 57 import org.objectweb.jonas_ejb.deployment.api.EntityBmpDesc; 58 import org.objectweb.jonas_ejb.deployment.api.EntityCmpDesc; 59 import org.objectweb.jonas_ejb.deployment.api.EntityDesc; 60 import org.objectweb.jonas_ejb.deployment.api.EntityJdbcCmp1Desc; 61 import org.objectweb.jonas_ejb.deployment.api.EntityJdbcCmp2Desc; 62 import org.objectweb.jonas_ejb.deployment.api.MessageDrivenDesc; 63 import org.objectweb.jonas_ejb.deployment.api.SessionStatefulDesc; 64 import org.objectweb.jonas_ejb.deployment.api.SessionStatelessDesc; 65 import org.objectweb.jonas_ejb.lib.EJBInvocation; 66 67 import org.objectweb.jonas_lib.deployment.api.EjbLocalRefDesc; 68 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc; 69 import org.objectweb.jonas_lib.deployment.api.EnvEntryDesc; 70 import org.objectweb.jonas_lib.deployment.api.MessageDestinationRefDesc; 71 import org.objectweb.jonas_lib.deployment.api.ResourceEnvRefDesc; 72 import org.objectweb.jonas_lib.deployment.api.ResourceRefDesc; 73 import org.objectweb.jonas_lib.naming.ContainerNaming; 74 import org.objectweb.jonas_lib.security.PermissionManagerException; 75 76 import org.objectweb.jonas_jms.api.JmsManager; 77 78 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc; 79 80 import org.objectweb.transaction.jta.TransactionManager; 81 82 import org.objectweb.carol.rmi.exception.NamingExceptionHelper; 83 84 import org.objectweb.util.monolog.api.BasicLevel; 85 86 100 101 public class JContainer implements Container { 102 103 106 public static final String DEFAULT_FACTORY_CLASS_NAME = "org.objectweb.jonas_ejb.container.JEntityFactory"; 107 108 111 private static WorkManager workManager = null; 112 113 116 private HashMap beanList = new HashMap (); 117 118 121 private String earFileName = null; 122 123 126 private String fileName; 127 128 131 private String externalFileName; 132 133 136 private JmsManager jms = null; 137 138 141 private ClassLoader loader = null; 142 143 146 private String myname; 147 148 151 private ContainerNaming naming = null; 152 153 156 private PermissionManager permissionManager = null; 157 158 161 private PrincipalFactory principalFactory = null; 162 163 166 private boolean securityFlag = true; 167 168 171 private Swapper swapper; 172 173 176 private TransactionManager tm = null; 177 178 private String tmpDirName = null; 179 180 184 private static final String DEFAULT_ACTIVATION_SPEC_NAME = "joramActivationSpec"; 185 186 193 public JContainer(String name, String extFileName, String file, ClassLoader ld) { 194 myname = name; 195 externalFileName = extFileName; 196 fileName = file; 197 loader = ld; 198 swapper = new Swapper(this); 199 swapper.start(); 200 if (TraceEjb.isDebugIc()) { 201 TraceEjb.interp.log(BasicLevel.DEBUG, "New Container extFN= " + externalFileName + " filename=" + fileName); 202 } 203 tmpDirName = JProp.getJonasBase() + File.separator + "tmp"; 204 File d = new File (tmpDirName); 205 d.mkdir(); 206 } 207 208 public String getTmpDirName() { 209 return tmpDirName; 210 } 211 212 216 225 public synchronized BeanFactory addBean(BeanDesc dd) { 226 BeanFactory bf = null; 227 String beanName = dd.getEjbName(); 228 if (dd instanceof SessionStatefulDesc) { 229 if (TraceEjb.isDebugIc()) { 230 TraceEjb.interp.log(BasicLevel.DEBUG, "add SessionStatefulBean " + beanName); 231 } 232 bf = new JStatefulFactory((SessionStatefulDesc) dd, this); 233 } else if (dd instanceof SessionStatelessDesc) { 234 if (TraceEjb.isDebugIc()) { 235 TraceEjb.interp.log(BasicLevel.DEBUG, "add SessionStatelessBean " + beanName); 236 } 237 bf = new JStatelessFactory((SessionStatelessDesc) dd, this); 238 } else if (dd instanceof MessageDrivenDesc) { 239 if (TraceEjb.isDebugIc()) { 240 TraceEjb.interp.log(BasicLevel.DEBUG, "add MessageDrivenBean " + beanName); 241 } 242 Object obj = null; 243 InitialContext ictx = null; 244 String destJName = ((MessageDrivenDesc) dd).getDestinationJndiName(); 245 try { 246 ictx = naming.getInitialContext(); 247 obj = ictx.lookup(destJName); 248 } catch (NamingException ex) { 249 if (ex instanceof NameNotFoundException || ex.getCause() instanceof NameNotFoundException ) { 251 if (TraceEjb.isDebugIc()) { 252 TraceEjb.interp.log(BasicLevel.DEBUG, "Can not find destination JNDI name " + destJName, ex); 253 } 254 } else { 255 throw new EJBException (ex); 256 } 257 } 258 if (obj != null && obj instanceof ActivationSpec ) { 259 bf = new JMdbEndpointFactory((MessageDrivenDesc) dd, this, (ActivationSpec ) obj); 260 } else if (getJmsManager() == null) { 261 if (((MessageDrivenDesc) dd).getDestination() != null) { 264 if (TraceEjb.isDebugIc()) { 265 TraceEjb.interp.log(BasicLevel.DEBUG, "JMS service not started and specified ActivationSpec(" + destJName + ") not deployed"); 266 } 267 throw new EJBException ("JMS service not started and specified ActivationSpec(" + destJName + ") not deployed"); 268 } 269 270 String dest = Rar.getDefaultAS(); if (dest == null) { 273 dest = DEFAULT_ACTIVATION_SPEC_NAME; } 275 276 try { 277 obj = ictx.lookup(dest); 278 } catch (Exception ex) { 279 if (TraceEjb.isDebugIc()) { 280 TraceEjb.interp.log(BasicLevel.DEBUG, "JMS service not started and default ActivationSpec(" + dest + ") not deployed"); 281 } 282 throw new EJBException ("JMS service not started and default ActivationSpec(" + dest + ") not deployed", ex); 283 } 284 if (obj != null && obj instanceof ActivationSpec ) { 285 bf = new JMdbEndpointFactory((MessageDrivenDesc) dd, dest, this, (ActivationSpec ) obj); 286 } else { 287 if (TraceEjb.isDebugIc()) { 288 TraceEjb.interp.log(BasicLevel.DEBUG, "Invalid destination: No ActivationSpec deployed matching " + dest); 289 } 290 throw new EJBException ("Invalid destination: No ActivationSpec deployed matching " + dest); 291 } 292 } else { 293 if (((MessageDrivenDesc) dd).getDestination() != null) { 295 if (TraceEjb.isDebugIc()) { 296 TraceEjb.interp.log(BasicLevel.DEBUG, "JMS service started and specified ActivationSpec(" + destJName + ") not deployed"); 297 } 298 throw new EJBException ("JMS service started and specified ActivationSpec(" + destJName + ") not deployed"); 299 } 300 bf = new JMdbFactory((MessageDrivenDesc) dd, this); 301 } 302 } else if (dd instanceof EntityJdbcCmp2Desc) { 303 EntityJdbcCmp2Desc ecd = (EntityJdbcCmp2Desc) dd; 304 if (TraceEjb.isDebugIc()) { 306 TraceEjb.interp.log(BasicLevel.DEBUG, "add CMP2 EntityBean " + beanName); 307 } 308 String cn = null; 310 try { 311 String dsn = ecd.getDatasourceJndiName(); 312 326 InitialContext ictx = naming.getInitialContext(); 327 String mapperName = null; 328 try { 332 Object cls = ictx.lookup(dsn); 333 Method meth = cls.getClass().getMethod("getMapperName", (Class []) null); 334 mapperName = (String ) meth.invoke(cls, (Object []) null); 335 } catch (Exception e1) { 336 if (TraceEjb.isDebugIc()) { 337 TraceEjb.interp.log(BasicLevel.DEBUG, "Cannot read mapper name", e1); 338 } 339 } 340 if (mapperName == null || mapperName.trim().length() == 0) { 341 try { 344 JProp dsProps = JProp.getInstance("mapper"); 345 mapperName = dsProps.getValue(dsn, ""); 346 } catch (Exception e2) { 347 throw new EJBException ("Unable to retrieve mapperName for " + dsn, e2); 348 } 349 if (mapperName == null) { 350 throw new EJBException ("Unable to retrieve mapperName for " + dsn + ". mappername is null."); 351 } 352 } 353 354 cn = ecd.getFactoryClassName(); 355 bf = (JEntityFactory) loader.loadClass(cn).newInstance(); 356 ((RdbFactory) bf).init(ecd, this, mapperName); 357 setSwapTime(((EntityDesc) ecd).getPassivationTimeout()); 358 TraceEjb.interp.log(BasicLevel.INFO, beanName + " is loaded and using " + mapperName); 359 } catch (Exception e) { 360 TraceEjb.interp.log(BasicLevel.ERROR, "Impossible to instanciate the entity factory: " + cn, e); 361 throw new EJBException ("Impossible to instanciate the entity factory: " + cn, e); 362 } 363 } else if (dd instanceof EntityDesc) { 364 if (TraceEjb.isDebugIc()) { 365 TraceEjb.interp.log(BasicLevel.DEBUG, "add EntityBean " + beanName); 366 } 367 String cn = null; 369 try { 370 cn = DEFAULT_FACTORY_CLASS_NAME; 371 bf = (JEntityFactory) loader.loadClass(cn).newInstance(); 372 } catch (Exception e) { 373 throw new EJBException ("Impossible to instanciate the specified entity factory: " + cn, e); 374 } 375 ((JEntityFactory) bf).init((EntityDesc) dd, this); 376 setSwapTime(((EntityDesc) dd).getPassivationTimeout()); 377 } else { 378 throw new EJBException ("Bad Descriptor Type for " + beanName); 379 } 380 beanList.put(beanName, bf); 381 TraceEjb.interp.log(BasicLevel.INFO, beanName + " available"); 382 return bf; 383 } 384 385 395 private Set beansDependence(Enumeration beanFactories, String rName, boolean isResRef) { 396 HashSet result = new HashSet (); 397 BeanFactory bf = null; 398 while (beanFactories.hasMoreElements()) { 399 bf = (BeanFactory) beanFactories.nextElement(); 401 BeanDesc ejbDesc = bf.getDeploymentDescriptor(); 402 403 boolean isDependent = false; 405 String ejbType = null; 407 if (bf instanceof JEntityFactory) { 408 if (ejbDesc instanceof EntityBmpDesc) { 409 ejbType = "ejbbmp"; 410 } 411 if (ejbDesc instanceof EntityCmpDesc) { 412 ejbType = "ejbcmp"; 413 } 414 } else if (bf instanceof JStatefulFactory) { 415 ejbType = "ejbsbf"; 416 } else if (bf instanceof JStatelessFactory) { 417 ejbType = "ejbsbl"; 418 } else if (bf instanceof JMdbFactory || bf instanceof JMdbEndpointFactory) { 419 ejbType = "ejbmdb"; 420 } 421 422 if (isResRef) { 423 if (ejbType.equals("ejbcmp")) { 427 String jndiName = null; 428 if (ejbDesc instanceof EntityJdbcCmp1Desc) { 429 jndiName = ((EntityJdbcCmp1Desc) ejbDesc).getDatasourceJndiName(); 430 } else if (ejbDesc instanceof EntityJdbcCmp2Desc) { 431 jndiName = ((EntityJdbcCmp2Desc) ejbDesc).getDatasourceJndiName(); 432 } 433 isDependent = rName.equals(jndiName); 434 } 435 ResourceRefDesc[] rrDesc = ejbDesc.getResourceRefDesc(); 437 for (int i = 0; i < rrDesc.length; i++) { 438 if (rrDesc[i].getJndiName().equals(rName)) { 440 isDependent = true; 441 break; 442 } 443 } 444 } else { 445 if (ejbType.equals("ejbmdb")) { 446 if (rName.equals(((MessageDrivenDesc) ejbDesc).getDestinationJndiName())) { 447 isDependent = true; 448 } 449 } 450 ResourceEnvRefDesc[] rerDesc = ejbDesc.getResourceEnvRefDesc(); 452 for (int i = 0; i < rerDesc.length; i++) { 453 if (rerDesc[i].getJndiName().equals(rName)) { 455 isDependent = true; 456 break; 457 } 458 } 459 } 460 461 if (isDependent) { 462 Properties toAdd = new Properties (); 463 toAdd.setProperty("type", ejbType); 464 toAdd.setProperty("fname", getFileName()); 465 toAdd.setProperty("name", ejbDesc.getEjbName()); 466 toAdd.setProperty("cname", getName()); 467 String earFileName = getEarFileName(); 468 if (earFileName != null) { 469 toAdd.setProperty("earFileName", earFileName); 470 } 471 result.add(toAdd); 472 } 473 474 } 475 return result; 476 } 477 478 485 protected void checkSecurity(String ejbName, EJBInvocation ejbInv, boolean inRunAs) { 486 String oldContextId = PolicyContext.getContextID(); 487 488 boolean accessIsOk = false; 489 try { 490 if (permissionManager != null) { 492 accessIsOk = permissionManager.checkSecurity(ejbName, ejbInv, inRunAs); 493 } 494 } catch (Exception e) { 495 TraceEjb.security.log(BasicLevel.ERROR, "Error while checking security", e); 496 } finally { 497 PolicyContext.setContextID(oldContextId); 498 } 499 if (!accessIsOk) { 500 StringBuffer errMsg = new StringBuffer ("Access Denied on bean '"); 501 errMsg.append(ejbName); 502 errMsg.append("' with run-as = '"); 503 errMsg.append(inRunAs); 504 errMsg.append("'. "); 505 if (ejbInv != null && ejbInv.methodPermissionSignature != null) { 506 errMsg.append(" Method signature = '"); 507 errMsg.append(ejbInv.methodPermissionSignature); 508 errMsg.append("'."); 509 } 510 throw new AccessLocalException (errMsg.toString()); 511 } 512 513 } 514 515 520 public BeanFactory getBeanFactory(String ejbName) { 521 return (BeanFactory) beanList.get(ejbName); 522 } 523 524 527 public int getBeanNb() { 528 return beanList.size(); 529 } 530 531 534 public ClassLoader getClassLoader() { 535 if (loader == null) { 536 TraceEjb.logger.log(BasicLevel.ERROR, "container has been removed"); 537 return null; 539 } else { 540 return loader; 541 } 542 } 543 544 548 public ContainerNaming getContainerNaming() { 549 return naming; 550 } 551 552 558 public Set getDataSourceDependence(String dsName) { 559 return beansDependence(Collections.enumeration(beanList.values()), dsName, true); 561 } 562 563 567 public String getEarFileName() { 568 return earFileName; 569 } 570 571 574 public int getEntityBMPNb() { 575 BeanFactory bf; 576 int total = 0; 577 Iterator it = beanList.values().iterator(); 578 while (it.hasNext()) { 579 bf = (BeanFactory) it.next(); 580 if (bf.getDeploymentDescriptor() instanceof EntityBmpDesc) { 581 total++; 582 } 583 } 584 return total; 585 } 586 587 590 public int getEntityCMPNb() { 591 BeanFactory bf; 592 int total = 0; 593 Iterator it = beanList.values().iterator(); 594 while (it.hasNext()) { 595 bf = (BeanFactory) it.next(); 596 if (bf.getDeploymentDescriptor() instanceof EntityCmpDesc) { 597 total++; 598 } 599 } 600 return total; 601 } 602 603 606 public String getFileName() { 607 return fileName; 608 } 609 610 613 public String getExternalFileName() { 614 return externalFileName; 615 } 616 617 624 public Set getJmsConnectionFactoryDependence(String cfName) { 625 return beansDependence(Collections.enumeration(beanList.values()), cfName, true); 627 } 628 629 636 public Set getJmsDestinationDependence(String destName) { 637 return beansDependence(Collections.enumeration(beanList.values()), destName, false); 639 } 640 641 645 public JmsManager getJmsManager() { 646 return jms; 647 } 648 649 656 public Set getMailFactoryDependence(String mfName) { 657 return beansDependence(Collections.enumeration(beanList.values()), mfName, true); 659 } 660 661 664 public int getMessageDrivenNb() { 665 BeanFactory bf; 666 int total = 0; 667 Iterator it = beanList.values().iterator(); 668 while (it.hasNext()) { 669 bf = (BeanFactory) it.next(); 670 if (bf.getDeploymentDescriptor() instanceof MessageDrivenDesc) { 671 total++; 672 } 673 } 674 return total; 675 } 676 677 680 public String getName() { 681 return myname; 682 } 683 684 688 public PermissionManager getPermissionManager() { 689 return permissionManager; 690 } 691 692 695 public PrincipalFactory getPrincipalFactory() { 696 return principalFactory; 697 } 698 699 702 public int getStatefulSessionNb() { 703 BeanFactory bf; 704 int total = 0; 705 Iterator it = beanList.values().iterator(); 706 while (it.hasNext()) { 707 bf = (BeanFactory) it.next(); 708 if (bf.getDeploymentDescriptor() instanceof SessionStatefulDesc) { 709 total++; 710 } 711 } 712 return total; 713 } 714 715 718 public int getStatelessSessionNb() { 719 BeanFactory bf; 720 int total = 0; 721 Iterator it = beanList.values().iterator(); 722 while (it.hasNext()) { 723 bf = (BeanFactory) it.next(); 724 if (bf.getDeploymentDescriptor() instanceof SessionStatelessDesc) { 725 total++; 726 &nbs
|