1 45 package org.openejb.alt.assembler.classic; 46 47 import org.openejb.Container; 48 import org.openejb.OpenEJBException; 49 import org.openejb.core.ContainerSystem; 50 import org.openejb.core.DeploymentInfo; 51 import org.openejb.spi.SecurityService; 52 import org.openejb.spi.TransactionService; 53 import org.openejb.util.Messages; 54 import org.openejb.util.SafeToolkit; 55 import org.openejb.util.proxy.ProxyFactory; 56 import org.openejb.util.proxy.ProxyManager; 57 58 import javax.naming.InitialContext ; 59 import javax.resource.spi.ConnectionManager ; 60 import javax.resource.spi.ManagedConnectionFactory ; 61 import java.lang.reflect.Method ; 62 import java.util.HashMap ; 63 import java.util.HashSet ; 64 import java.util.List ; 65 import java.util.Properties ; 66 import java.util.Vector ; 67 68 86 public class AssemblerTool { 87 88 public static final Class PROXY_FACTORY = org.openejb.util.proxy.ProxyFactory.class; 89 public static final Class SECURITY_SERVICE = org.openejb.spi.SecurityService.class; 90 public static final Class TRANSACTION_SERVICE = org.openejb.spi.TransactionService.class; 91 public static final Class CONNECTION_MANAGER = javax.resource.spi.ConnectionManager .class; 92 public static final Class CONNECTOR = javax.resource.spi.ManagedConnectionFactory .class; 93 94 97 protected static final Messages messages = new Messages("org.openejb.util.resources"); 98 protected static final SafeToolkit toolkit = SafeToolkit.getToolkit("AssemblerTool"); 99 protected static final HashMap codebases = new HashMap (); 100 101 protected Properties props; 102 103 static { 104 ClassLoader cl = ClassLoader.getSystemClassLoader(); 105 codebases.put("CLASSPATH", cl); 106 107 System.setProperty("noBanner", "true"); 108 } 109 110 124 public void assembleContainers(ContainerSystem containerSystem, ContainerSystemInfo containerSystemInfo) throws Exception { 125 126 ContainerBuilder containerBuilder = new ContainerBuilder(containerSystemInfo, this.props); 127 List containers = (List ) containerBuilder.build(); 128 for (int i = 0; i < containers.size(); i++) { 129 Container container = (Container) containers.get(i); 130 containerSystem.addContainer(container.getContainerID(), container); 131 org.openejb.DeploymentInfo[] deployments = container.deployments(); 132 for (int j = 0; j < deployments.length; j++) { 133 containerSystem.addDeployment((org.openejb.core.DeploymentInfo) deployments[j]); 134 } 135 } 136 } 137 138 143 public InitialContext assembleRemoteJndiContext(JndiContextInfo context) 144 throws org.openejb.OpenEJBException { 145 try { 146 InitialContext ic = new InitialContext (context.properties); 147 return ic; 148 } catch (javax.naming.NamingException ne) { 149 throw new org.openejb.OpenEJBException("The remote JNDI EJB references for remote-jndi-contexts = " + context.jndiContextId + "+ could not be resolved.", ne); 152 } 153 } 154 155 163 public ConnectionManager assembleConnectionManager(ConnectionManagerInfo cmInfo) 164 throws OpenEJBException, java.lang.Exception { 165 169 Class managerClass = SafeToolkit.loadClass(cmInfo.className, cmInfo.codebase); 170 171 checkImplementation(CONNECTION_MANAGER, managerClass, "ConnectionManager", cmInfo.connectionManagerId); 172 173 ConnectionManager connectionManager = (ConnectionManager ) toolkit.newInstance(managerClass); 174 175 if (cmInfo.properties != null) { 177 Properties clonedProps = (Properties ) (this.props.clone()); 178 clonedProps.putAll(cmInfo.properties); 179 applyProperties(connectionManager, clonedProps); 180 } 181 182 return connectionManager; 183 } 184 185 193 public ManagedConnectionFactory assembleManagedConnectionFactory(ManagedConnectionFactoryInfo mngedConFactInfo) 194 throws org.openejb.OpenEJBException, java.lang.Exception { 195 196 ManagedConnectionFactory managedConnectionFactory = null; 197 try { 198 Class factoryClass = SafeToolkit.loadClass(mngedConFactInfo.className, mngedConFactInfo.codebase); 199 checkImplementation(CONNECTOR, factoryClass, "Connector", mngedConFactInfo.id); 200 201 managedConnectionFactory = (ManagedConnectionFactory ) toolkit.newInstance(factoryClass); 202 } catch (Exception e) { 203 throw new OpenEJBException("Could not instantiate Connector '" + mngedConFactInfo.id + "'.", e); 204 } 205 206 207 try { 208 if (mngedConFactInfo.properties != null) { 210 Properties clonedProps = (Properties ) (this.props.clone()); 211 clonedProps.putAll(mngedConFactInfo.properties); 212 applyProperties(managedConnectionFactory, clonedProps); 213 } 214 } catch (java.lang.reflect.InvocationTargetException ite) { 215 throw new OpenEJBException("Could not initialize Connector '" + mngedConFactInfo.id + "'.", ite.getTargetException()); 216 } catch (Exception e) { 217 throw new OpenEJBException("Could not initialize Connector '" + mngedConFactInfo.id + "'.", e); 219 } 220 221 return managedConnectionFactory; 222 } 223 224 232 public SecurityService assembleSecurityService(SecurityServiceInfo securityInfo) 233 throws org.openejb.OpenEJBException, java.lang.Exception { 234 238 Class serviceClass = SafeToolkit.loadClass(securityInfo.factoryClassName, securityInfo.codebase); 239 240 checkImplementation(SECURITY_SERVICE, serviceClass, "SecurityService", securityInfo.serviceName); 241 242 SecurityService securityService = (SecurityService) toolkit.newInstance(serviceClass); 243 244 if (securityInfo.properties != null) 246 applyProperties(securityService, securityInfo.properties); 247 248 return securityService; 249 } 250 251 260 public javax.transaction.TransactionManager assembleTransactionManager(TransactionServiceInfo txInfo) 261 throws org.openejb.OpenEJBException, java.lang.Exception { 262 266 267 Class serviceClass = SafeToolkit.loadClass(txInfo.factoryClassName, txInfo.codebase); 268 269 checkImplementation(TRANSACTION_SERVICE, serviceClass, "TransactionService", txInfo.serviceName); 270 271 TransactionService txService = (TransactionService) toolkit.newInstance(serviceClass); 272 273 if (txInfo.properties != null) 275 applyProperties(txService, txInfo.properties); 276 277 278 return (javax.transaction.TransactionManager ) (new org.openejb.core.TransactionManagerWrapper(txService.getTransactionManager())); 281 } 282 283 293 public void applyProxyFactory(IntraVmServerInfo ivmInfo) throws OpenEJBException { 294 Class factoryClass = SafeToolkit.loadClass(ivmInfo.proxyFactoryClassName, ivmInfo.codebase); 295 296 checkImplementation(PROXY_FACTORY, factoryClass, "ProxyFactory", ivmInfo.factoryName); 297 298 ProxyFactory factory = (ProxyFactory) toolkit.newInstance(factoryClass); 299 300 factory.init(ivmInfo.properties); 301 ProxyManager.registerFactory("ivm_server", factory); 302 ProxyManager.setDefaultFactory("ivm_server"); 303 304 } 305 306 319 public void applyProperties(Object target, Properties props) 320 throws java.lang.reflect.InvocationTargetException , 321 java.lang.IllegalAccessException , java.lang.NoSuchMethodException { 322 if (props != null ) { 323 Method method = target.getClass().getMethod("init", new Class []{Properties .class}); 324 method.invoke(target, new Object []{props}); 325 } 326 } 327 328 341 public void applyTransactionAttributes(DeploymentInfo deploymentInfo, MethodTransactionInfo[] mtis) { 342 346 for (int i = 0; i < mtis.length; i++) { 347 MethodTransactionInfo transInfo = mtis[i]; 348 MethodInfo[] mis = transInfo.methods; 349 350 for (int z = 0; z < mis.length; z++) { 351 MethodInfo methodInfo = mis[z]; 352 if (mis[z].ejbDeploymentId == null || mis[z].ejbDeploymentId.equals(deploymentInfo.getDeploymentID())) { 354 if (!deploymentInfo.isBeanManagedTransaction()) { 355 Vector methodVect = new Vector (); 357 358 if (methodInfo.methodIntf == null) { 359 resolveMethods(methodVect, deploymentInfo.getRemoteInterface(), methodInfo); 361 resolveMethods(methodVect, deploymentInfo.getHomeInterface(), methodInfo); 362 } else if (methodInfo.methodIntf.equals("Home")) { 363 resolveMethods(methodVect, deploymentInfo.getHomeInterface(), methodInfo); 364 } else if (methodInfo.methodIntf.equals("Remote")) { 365 resolveMethods(methodVect, deploymentInfo.getRemoteInterface(), methodInfo); 366 } else { 367 } 369 370 for (int x = 0; x < methodVect.size(); x++) { 371 Method method = (Method ) methodVect.elementAt(x); 372 373 if ((method.getDeclaringClass() == javax.ejb.EJBObject .class || 375 method.getDeclaringClass() == javax.ejb.EJBHome .class) && 376 method.getName().equals("remove") == false) { 377 continue; } 379 deploymentInfo.setMethodTransactionAttribute(method, transInfo.transAttribute); 380 } 381 } 382 } 383 } 384 } 385 386 } 387 388 401 public void applySecurityRoleReference(DeploymentInfo deployment, EnterpriseBeanInfo beanInfo, AssemblerTool.RoleMapping roleMapping) { 402 if (beanInfo.securityRoleReferences != null) { 403 for (int l = 0; l < beanInfo.securityRoleReferences.length; l++) { 404 SecurityRoleReferenceInfo roleRef = beanInfo.securityRoleReferences[l]; 405 String [] physicalRoles = roleMapping.getPhysicalRoles(roleRef.roleLink); 406 deployment.addSecurityRoleReference(roleRef.roleName, physicalRoles); 407 } 408 } 409 } 410 411 426 public void applyMethodPermissions(DeploymentInfo deployment, MethodPermissionInfo[] permissions) { 427 432 for (int a = 0; a < permissions.length; a++) { 433 MethodPermissionInfo methodPermission = permissions[a]; 434 for (int b = 0; b < methodPermission.methods.length; b++) { 435 MethodInfo methodInfo = methodPermission.methods[b]; 436 437 if (methodInfo.ejbDeploymentId == null || methodInfo.ejbDeploymentId.equals(deployment.getDeploymentID())) { 439 java.lang.reflect.Method [] methods = resolveMethodInfo(methodInfo, deployment); 441 for (int c = 0; c < methods.length; c++) { 443 deployment.appendMethodPermissions(methods[c], methodPermission.roleNames); 444 } 445 } 446 447 } 448 } 449 } 450 451 468 public void applyMethodPermissions(DeploymentInfo deployment, MethodPermissionInfo[] permissions, AssemblerTool.RoleMapping roleMapping) { 469 474 for (int i = 0; i < permissions.length; i++) { 475 permissions[i] = applyRoleMappings(permissions[i], roleMapping); 476 } 477 applyMethodPermissions(deployment, permissions); 478 } 479 480 491 public MethodPermissionInfo applyRoleMappings(MethodPermissionInfo methodPermission, 492 AssemblerTool.RoleMapping roleMapping) { 493 498 499 HashSet physicalRoles = new HashSet (); 500 501 for (int z = 0; z < methodPermission.roleNames.length; z++) { 502 String [] physicals = roleMapping.getPhysicalRoles(methodPermission.roleNames[z]); 503 if (physicals != null) { 504 for (int x = 0; x < physicals.length; x++) { 505 physicalRoles.add(physicals[x]); 506 } 507 } else { 509 physicalRoles.add(methodPermission.roleNames[z]); 510 } 511 } 512 methodPermission.roleNames = new String [physicalRoles.size()]; 513 physicalRoles.toArray(methodPermission.roleNames); 514 return methodPermission; 515 } 516 517 526 public static class RoleMapping { 527 private HashMap map = new HashMap (); 528 529 535 public RoleMapping(RoleMappingInfo[] roleMappingInfos) { 536 for (int i = 0; i < roleMappingInfos.length; i++) { 537 RoleMappingInfo mapping = roleMappingInfos[i]; 538 for (int z = 0; z < mapping.logicalRoleNames.length; z++) { 539 map.put(mapping.logicalRoleNames[z], mapping.physicalRoleNames); 540 } 541 } 542 } 543 544 550 public String [] logicalRoles() { 551 return (String []) map.keySet().toArray(); 552 } 553 554 561 public String [] getPhysicalRoles(String logicalRole) { 562 String [] roles = (String []) map.get(logicalRole); 563 return roles != null ? (String []) roles.clone() : null; 564 } 565 566 } 567 568 569 575 576 577 578 579 580 586 protected java.lang.reflect.Method [] resolveMethodInfo(MethodInfo methodInfo, org.openejb.core.DeploymentInfo di) { 587 592 593 Vector methodVect = new Vector (); 594 595 Class remote = di.getRemoteInterface(); 596 Class home = di.getHomeInterface(); 597 if (methodInfo.methodIntf == null) { 598 resolveMethods(methodVect, remote, methodInfo); 599 resolveMethods(methodVect, home, methodInfo); 600 } else if (methodInfo.methodIntf.equals("Remote")) { 601 resolveMethods(methodVect, remote, methodInfo); 602 } else { 603 resolveMethods(methodVect, home, methodInfo); 604 } 605 return (java.lang.reflect.Method []) methodVect.toArray(new java.lang.reflect.Method [methodVect.size()]); 606 } 607 608 613 protected static void resolveMethods(Vector methods, Class intrface, MethodInfo mi) 614 throws SecurityException { 615 619 620 if (mi.methodName.equals("*")) { 621 Method [] mthds = intrface.getMethods(); 622 for (int i = 0; i < mthds.length; i++) 623 methods.add(mthds[i]); 624 } else if (mi.methodParams != null) { try { 626 Class [] params = new Class [mi.methodParams.length]; 627 ClassLoader cl = intrface.getClassLoader(); 628 for (int i = 0; i < params.length; i++) { 629 try { 630 params[i] = getClassForParam(mi.methodParams[i], cl); 631 } catch (ClassNotFoundException cnfe) { 632 } 634 } 635 Method m = intrface.getMethod(mi.methodName, params); 636 methods.add(m); 637 } catch (NoSuchMethodException nsme) { 638 643 } 644 } else { Method [] ms = intrface.getMethods(); 646 for (int i = 0; i < ms.length; i++) { 647 if (ms[i].getName().equals(mi.methodName)) 648 methods.add(ms[i]); 649 } 650 } 651 652 } 653 654 protected void checkImplementation(Class intrfce, Class factory, String serviceType, String serviceName) throws OpenEJBException { 655 if (!intrfce.isAssignableFrom(factory)) { 656 handleException("init.0100", serviceType, serviceName, factory.getName(), intrfce.getName()); 657 } 658 } 659 660 664 private static java.lang.Class getClassForParam(java.lang.String className, ClassLoader cl) throws ClassNotFoundException { 665 if (cl == null) { 666 cl = ClassLoader.getSystemClassLoader(); 667 } 668 669 if (className.equals("int")) { 671 return java.lang.Integer.TYPE; 672 } else if (className.equals("double")) { 673 return java.lang.Double.TYPE; 674 } else if (className.equals("long")) { 675 return java.lang.Long.TYPE; 676 } else if (className.equals("boolean")) { 677 return java.lang.Boolean.TYPE; 678 } else if (className.equals("float")) { 679 return java.lang.Float.TYPE; 680 } else if (className.equals("char")) { 681 return java.lang.Character.TYPE; 682 } else if (className.equals("short")) { 683 return java.lang.Short.TYPE; 684 } else if (className.equals("byte")) { 685 return java.lang.Byte.TYPE; 686 } else 687 return cl.loadClass(className); 688 689 } 690 691 692 693 694 695 public void handleException(String errorCode, Object arg0, Object arg1, Object arg2, Object arg3) throws OpenEJBException { 696 throw new OpenEJBException(messages.format(errorCode, arg0, arg1, arg2, arg3)); 697 } 698 699 public void handleException(String errorCode, Object arg0, Object arg1, Object arg2) throws OpenEJBException { 700 throw new OpenEJBException(messages.format(errorCode, arg0, arg1, arg2)); 701 } 702 703 public void handleException(String errorCode, Object arg0, Object arg1) throws OpenEJBException { 704 throw new OpenEJBException(messages.format(errorCode, arg0, arg1)); 705 } 706 707 public void handleException(String errorCode, Object arg0) throws OpenEJBException { 708 throw new OpenEJBException(messages.format(errorCode, arg0)); 709 } 710 711 public void handleException(String errorCode) throws OpenEJBException { 712 throw new OpenEJBException(messages.format(errorCode)); 713 } 714 715 716 717 718 719 720 public void logWarning(String errorCode, Object arg0, Object arg1, Object arg2, Object arg3) { 721 System.out.println("Warning: " + messages.format(errorCode, arg0, arg1, arg2, arg3)); 722 } 723 724 public void logWarning(String errorCode, Object arg0, Object arg1, Object arg2) { 725 System.out.println("Warning: " + messages.format(errorCode, arg0, arg1, arg2)); 726 } 727 728 public void logWarning(String errorCode, Object arg0, Object arg1) { 729 System.out.println("Warning: " + messages.format(errorCode, arg0, arg1)); 730 } 731 732 public void logWarning(String errorCode, Object arg0) { 733 System.out.println("Warning: " + messages.format(errorCode, arg0)); 734 } 735 736 public void logWarning(String errorCode) { 737 System.out.println("Warning: " + messages.format(errorCode)); 738 } 739 } | Popular Tags |