Package org.apache.openejb.jee

Examples of org.apache.openejb.jee.AssemblyDescriptor


    public void applicationExceptionInheritanceTest() throws Exception {
        EjbModule ejbModule = testModule();
        AnnotationDeployer.DiscoverAnnotatedBeans discvrAnnBeans = new AnnotationDeployer.DiscoverAnnotatedBeans();
        ejbModule = discvrAnnBeans.deploy(ejbModule);

        AssemblyDescriptor assemblyDescriptor = ejbModule.getEjbJar().getAssemblyDescriptor();
        org.apache.openejb.jee.ApplicationException appEx =
                assemblyDescriptor.getApplicationException(BusinessException.class);
        assertThat(appEx, notNullValue());
        assertThat(appEx.getExceptionClass(), is(BusinessException.class.getName()));
        assertThat(appEx.isRollback(), is(true));

        //inheritance is now handled at runtime, only explicitly mentioned exceptions are in the assembly descriptor
        appEx = assemblyDescriptor.getApplicationException(ValueRequiredException.class);
        assertThat(appEx, nullValue());
    }
View Full Code Here


    public static EjbModule buildTestApp() throws Exception {
        EjbJar ejbJar = new EjbJar();
        ejbJar.setId(StatelessInterceptorTest.class.getName());
       
        AssemblyDescriptor ad = ejbJar.getAssemblyDescriptor();

        ejbJar.addEnterpriseBean(new StatelessBean(Target2Bean.class));

        EnterpriseBean bean = ejbJar.addEnterpriseBean(new StatelessBean(TargetBean.class));

        Interceptor interceptor;

        interceptor = ejbJar.addInterceptor(new Interceptor(DefaultInterceptor.class));
        ad.addInterceptorBinding(new InterceptorBinding("*", interceptor));

        {
            interceptor = ejbJar.addInterceptor(new Interceptor(EchoMethodInterceptorViaDD.class));
            InterceptorBinding binding = ad.addInterceptorBinding(new InterceptorBinding(bean, interceptor));
            binding.setMethod(new NamedMethod(TargetBean.class.getMethod("echo", List.class)));
        }

        {
            interceptor = ejbJar.addInterceptor(new Interceptor(EchoMethodInterceptorViaDD.class));
            InterceptorBinding binding = ad.addInterceptorBinding(new InterceptorBinding(bean, interceptor));
            binding.setMethod(new NamedMethod(TargetBean.class.getMethod("echo", int.class)));
        }

        {
            interceptor = ejbJar.addInterceptor(new Interceptor(EchoMethodInterceptorViaDD.class));
            InterceptorBinding binding = ad.addInterceptorBinding(new InterceptorBinding(bean, interceptor));
            binding.setMethod(new NamedMethod(TargetBean.class.getMethod("echo", boolean.class)));
        }

        EnterpriseBean bean3 = ejbJar.addEnterpriseBean(new StatelessBean(Target3Bean.class));
        InterceptorBinding binding = ad.addInterceptorBinding(new InterceptorBinding(bean3));
        binding.setExcludeDefaultInterceptors(true);
        binding.setExcludeClassInterceptors(true);

        return new EjbModule(ejbJar);
    }
View Full Code Here

    EnterpriseBean enterpriseBean = new StatefulBean();
    enterpriseBean.setEjbName("TestBean"); //$NON-NLS-1$
    enterpriseBean.setEjbClass("test.TestBean"); //$NON-NLS-1$
    enterpriseBean.setTransactionType(TransactionType.BEAN);
   
    AssemblyDescriptor descriptor = new AssemblyDescriptor();
   
    addMethodTransactionToDescriptor(descriptor, "TestBean", "*", new String[0]); //$NON-NLS-1$ //$NON-NLS-2$
    addMethodTransactionToDescriptor(descriptor, "TestBean", "test", new String[] { "java.lang.String" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
   
    // execute
View Full Code Here

    context.assertIsSatisfied();
  }

  private void addMethodToEjbJarDescriptor(EjbJar ejbJar, String beanName,
      String methodName, String[] rolesAllowed, boolean unchecked, boolean deny) {
    AssemblyDescriptor descriptor = ejbJar.getAssemblyDescriptor();
    List<MethodPermission> methodPermissions = descriptor.getMethodPermission();
    MethodPermission methodPermission = new MethodPermission();
    List<Method> methods = methodPermission.getMethod();
    methodPermission.getRoleName().addAll(Arrays.asList(rolesAllowed));
    methodPermission.setUnchecked(unchecked);
    methodPermissions.add(methodPermission);
    Method method = new Method();
    method.setEjbName(beanName);
    method.setMethodName(methodName);
    method.setMethodParams(new MethodParams());
    methods.add(method);
   
    if (deny) {
      descriptor.getExcludeList().addMethod(method);
    }
  }
View Full Code Here

      annotationHelper.addClassAnnotation(ejbClass, Stateful.class, null);
    }
  }

  public void processMethodPermissions(EjbJar ejbJar) {
    AssemblyDescriptor descriptor = ejbJar.getAssemblyDescriptor();

    List<MethodPermission> methodPermissions = descriptor.getMethodPermission();
    Iterator<MethodPermission> iterator = methodPermissions.iterator();

    while (iterator.hasNext()) {
      MethodPermission methodPermission = (MethodPermission) iterator.next();
      List<String> roles = methodPermission.getRoleName();

      if (roles == null || roles.size() == 0) {
        continue;
      }

      String[] roleList = roles.toArray(new String[0]);
      Map<String, Object> roleProps = new HashMap<String, Object>();
      roleProps.put("value", roleList); //$NON-NLS-1$


      List<Method> methods = methodPermission.getMethod();
      Iterator<Method> methodIter = methods.iterator();

      while (methodIter.hasNext()) {
        Method method = (Method) methodIter.next();
        EnterpriseBean enterpriseBean = ejbJar.getEnterpriseBean(method.getEjbName());
        try {
          MethodParams methodParams = method.getMethodParams();
          String[] params = methodParams.getMethodParam().toArray(new String[0]);
          if ((!"*".equals(method.getMethodName())) && descriptor.getExcludeList().getMethod().contains(method)) { //$NON-NLS-1$
            annotationHelper.addMethodAnnotation(enterpriseBean.getEjbClass(), method.getMethodName(), params, DenyAll.class, null);
            continue;
          }
          if (methodPermission.getUnchecked()) {
            if ("*".equals(method.getMethodName())) { //$NON-NLS-1$
View Full Code Here

                            bean.setTransactionType(TransactionType.CONTAINER);
                            break;
                    }
                }

                final AssemblyDescriptor assemblyDescriptor = ejbModule.getEjbJar().getAssemblyDescriptor();

                /*
                 * @ApplicationException
                 */
                processApplicationExceptions(clazz, assemblyDescriptor);

                /*
                 * TransactionAttribute
                 */
                if (bean.getTransactionType() == TransactionType.CONTAINER) {
                    processAttributes(new TransactionAttributeHandler(assemblyDescriptor, ejbName), clazz, annotationFinder);
                } else {
                    if (finder == null) {
                        finder = annotationFinder.select(clazz.getName());
                    }
                    checkAttributes(new TransactionAttributeHandler(assemblyDescriptor, ejbName), ejbName, ejbModule, finder, "invalidTransactionAttribute");
                }

                /*
                 * @RolesAllowed
                 * @PermitAll
                 * @DenyAll
                 * @RunAs
                 * @DeclareRoles
                 */
                processSecurityAnnotations(clazz, ejbName, ejbModule, annotationFinder, bean);

                /*
                 * @Schedule
                 * @Schedules
                 */
                processSchedules(bean, annotationFinder);

                /*
                 * Add any interceptors they may have referenced in xml but did not declare
                 */
                for (final InterceptorBinding binding : assemblyDescriptor.getInterceptorBinding()) {
                    final EjbJar ejbJar = ejbModule.getEjbJar();

                    final List<String> list = new ArrayList<String>(binding.getInterceptorClass());

                    if (binding.getInterceptorOrder() != null) {
                        list.clear();
                        list.addAll(binding.getInterceptorOrder().getInterceptorClass());
                    }

                    for (final String interceptor : list) {
                        if (ejbJar.getInterceptor(interceptor) == null) {
                            logger.debug("Adding '<ejb-jar><interceptors><interceptor>' entry for undeclared interceptor " + interceptor);
                            ejbJar.addInterceptor(new Interceptor(interceptor));
                        }
                    }
                }

                /*
                 * @Interceptors
                 */
                final List<Annotated<Class<?>>> annotatedClasses = sortClasses(annotationFinder.findMetaAnnotatedClasses(Interceptors.class));
                for (final Annotated<Class<?>> interceptorsAnnotatedClass : annotatedClasses) {
                    final Interceptors interceptors = interceptorsAnnotatedClass.getAnnotation(Interceptors.class);
                    final EjbJar ejbJar = ejbModule.getEjbJar();
                    for (final Class interceptor : interceptors.value()) {
                        if (ejbJar.getInterceptor(interceptor.getName()) == null) {
                            ejbJar.addInterceptor(new Interceptor(interceptor.getName()));
                        }
                    }

                    final InterceptorBinding binding = new InterceptorBinding(bean);
                    assemblyDescriptor.getInterceptorBinding().add(0, binding);

                    for (final Class interceptor : interceptors.value()) {
                        binding.getInterceptorClass().add(interceptor.getName());
                    }
                }

                final List<Annotated<Method>> annotatedMethods = sortMethods(annotationFinder.findMetaAnnotatedMethods(Interceptors.class));
                for (final Annotated<Method> method : annotatedMethods) {
                    final Interceptors interceptors = method.getAnnotation(Interceptors.class);
                    if (interceptors != null) {
                        final EjbJar ejbJar = ejbModule.getEjbJar();
                        for (final Class interceptor : interceptors.value()) {
                            if (ejbJar.getInterceptor(interceptor.getName()) == null) {
                                ejbJar.addInterceptor(new Interceptor(interceptor.getName()));
                            }
                        }

                        final InterceptorBinding binding = new InterceptorBinding(bean);
                        assemblyDescriptor.getInterceptorBinding().add(0, binding);

                        for (final Class interceptor : interceptors.value()) {
                            binding.getInterceptorClass().add(interceptor.getName());
                        }

                        binding.setMethod(new NamedMethod(method.get()));
                    }
                }

                /*
                 * @ExcludeDefaultInterceptors
                 */
                final ExcludeDefaultInterceptors excludeDefaultInterceptors = clazz.getAnnotation(ExcludeDefaultInterceptors.class);
                if (excludeDefaultInterceptors != null) {
                    final InterceptorBinding binding = assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(bean));
                    binding.setExcludeDefaultInterceptors(true);
                }

                for (final Annotated<Method> method : annotationFinder.findMetaAnnotatedMethods(ExcludeDefaultInterceptors.class)) {
                    final InterceptorBinding binding = assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(bean));
                    binding.setExcludeDefaultInterceptors(true);
                    binding.setMethod(new NamedMethod(method.get()));
                }

                for (final Annotated<Method> method : sortMethods(annotationFinder.findMetaAnnotatedMethods(ExcludeClassInterceptors.class))) {
                    final InterceptorBinding binding = assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(bean));
                    binding.setExcludeClassInterceptors(true);
                    binding.setMethod(new NamedMethod(method.get()));
                }

                /**
 
View Full Code Here

            }
            return value;
        }

        private void processSecurityAnnotations(final Class<?> beanClass, final String ejbName, final EjbModule ejbModule, final AnnotationFinder annotationFinder, final EnterpriseBean bean) {
            final AssemblyDescriptor assemblyDescriptor = ejbModule.getEjbJar().getAssemblyDescriptor();

            final List<String> classPermissions = getDeclaredClassPermissions(assemblyDescriptor, ejbName);

            for (final Class<?> clazzz : Classes.ancestors(beanClass)) {
                final MetaAnnotatedClass<?> clazz = new MetaAnnotatedClass(clazzz);
                /*
                 * Process annotations at the class level
                 */
                if (!classPermissions.contains("*") || !classPermissions.contains(clazz.getName())) {

                    final RolesAllowed rolesAllowed = clazz.getAnnotation(RolesAllowed.class);
                    final PermitAll permitAll = clazz.getAnnotation(PermitAll.class);
                    final DenyAll denyAll = clazz.getAnnotation(DenyAll.class);

                    /*
                     * @RolesAllowed
                     */
                    if ((rolesAllowed != null && permitAll != null)
                        || (rolesAllowed != null && denyAll != null)
                        || (permitAll != null && denyAll != null)) {
                        ejbModule.getValidation().fail(ejbName, "permitAllAndRolesAllowedOnClass", clazz.getName());
                    }

                    if (rolesAllowed != null) {
                        final MethodPermission methodPermission = new MethodPermission();
                        methodPermission.getRoleName().addAll(asList(rolesAllowed.value()));
                        methodPermission.getMethod().add(new org.apache.openejb.jee.Method(ejbName, clazz.getName(), "*"));
                        assemblyDescriptor.getMethodPermission().add(methodPermission);

                        // Automatically add a role ref for any role listed in RolesAllowed
                        if (RemoteBean.class.isInstance(bean)) {
                            final RemoteBean remoteBean = RemoteBean.class.cast(bean);
                            final List<SecurityRoleRef> securityRoleRefs = remoteBean.getSecurityRoleRef();
                            for (final String role : rolesAllowed.value()) {
                                securityRoleRefs.add(new SecurityRoleRef(role));
                            }
                        }
                    }

                    /*
                     * @PermitAll
                     */
                    if (permitAll != null) {
                        final MethodPermission methodPermission = new MethodPermission();
                        methodPermission.setUnchecked(true);
                        methodPermission.getMethod().add(new org.apache.openejb.jee.Method(ejbName, clazz.getName(), "*"));
                        assemblyDescriptor.getMethodPermission().add(methodPermission);
                    }

                    /**
                     * @DenyAll
                     */
                    if (denyAll != null) {
                        assemblyDescriptor.getExcludeList()
                            .addMethod(new org.apache.openejb.jee.Method(ejbName, clazz.getName(), "*"));
                    }
                }

                /*
                 * @RunAs
                 */
                final RunAs runAs = clazz.getAnnotation(RunAs.class);
                if (runAs != null && bean.getSecurityIdentity() == null) {
                    final SecurityIdentity securityIdentity = new SecurityIdentity();
                    securityIdentity.setRunAs(runAs.value());
                    bean.setSecurityIdentity(securityIdentity);
                }

                /*
                 * @DeclareRoles
                 */
                final DeclareRoles declareRoles = clazz.getAnnotation(DeclareRoles.class);
                if (declareRoles != null && bean instanceof RemoteBean) {
                    final RemoteBean remoteBean = (RemoteBean) bean;
                    final List<SecurityRoleRef> securityRoleRefs = remoteBean.getSecurityRoleRef();
                    for (final String role : declareRoles.value()) {
                        securityRoleRefs.add(new SecurityRoleRef(role));
                    }
                }
            }

            /*
             * Process annotations at the method level
             */
            final List<Method> seen = new ArrayList<Method>();

            /*
             * @RolesAllowed
             */
            for (final Annotated<Method> method : annotationFinder.findMetaAnnotatedMethods(RolesAllowed.class)) {
                checkConflictingSecurityAnnotations(method, ejbName, ejbModule, seen);
                final RolesAllowed rolesAllowed = method.getAnnotation(RolesAllowed.class);
                final MethodPermission methodPermission = new MethodPermission();
                methodPermission.getRoleName().addAll(asList(rolesAllowed.value()));
                methodPermission.getMethod().add(new org.apache.openejb.jee.Method(ejbName, method.get()));
                assemblyDescriptor.getMethodPermission().add(methodPermission);

                // Automatically add a role ref for any role listed in RolesAllowed
                if (RemoteBean.class.isInstance(bean)) {
                    final RemoteBean remoteBean = RemoteBean.class.cast(bean);
                    final List<SecurityRoleRef> securityRoleRefs = remoteBean.getSecurityRoleRef();
                    for (final String role : rolesAllowed.value()) {
                        securityRoleRefs.add(new SecurityRoleRef(role));
                    }
                }
            }

            /*
             * @PermitAll
             */
            for (final Annotated<Method> method : annotationFinder.findMetaAnnotatedMethods(PermitAll.class)) {
                checkConflictingSecurityAnnotations(method, ejbName, ejbModule, seen);
                final MethodPermission methodPermission = new MethodPermission();
                methodPermission.setUnchecked(true);
                methodPermission.getMethod().add(new org.apache.openejb.jee.Method(ejbName, method.get()));
                assemblyDescriptor.getMethodPermission().add(methodPermission);
            }

            /*
             * @DenyAll
             */
            for (final Annotated<Method> method : annotationFinder.findMetaAnnotatedMethods(DenyAll.class)) {
                checkConflictingSecurityAnnotations(method, ejbName, ejbModule, seen);
                final ExcludeList excludeList = assemblyDescriptor.getExcludeList();
                excludeList.addMethod(new org.apache.openejb.jee.Method(ejbName, method.get()));
            }

        }
View Full Code Here

public class CheckAssemblyBindings extends ValidationBase {
    public void validate(final EjbModule ejbModule) {
        checkUnusedInterceptors(ejbModule);
        final Map<String, EnterpriseBean> ejbsByName = ejbModule.getEjbJar().getEnterpriseBeansByEjbName();

        final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();

        if (assembly == null) {
            return;
        }

        for (final InterceptorBinding binding : assembly.getInterceptorBinding()) {
            final List<String> interceptorClasses = binding.getInterceptorClass();
            if (binding.getInterceptorOrder() != null) {
                interceptorClasses.addAll(binding.getInterceptorOrder().getInterceptorClass());
            }

            if (binding.getEjbName() != null && !binding.getEjbName().equals("*") && !ejbsByName.containsKey(binding.getEjbName())) {
                fail("InterceptorBinding", "interceptorBinding.noSuchEjbName", binding.getEjbName(), join(",", interceptorClasses));
            }

            if (binding.getMethod() != null) {
                if (binding.getEjbName() == null) {
                    fail("InterceptorBinding", "interceptorBinding.ejbNameRequiredWithMethod", binding.getMethod().getMethodName(), join(",", interceptorClasses));
                }
            }
        }

        for (final MethodPermission permission : assembly.getMethodPermission()) {
            for (final Method method : permission.getMethod()) {
                if (method.getEjbName() == null) {
                    fail("MethodPermission", "methodPermission.ejbNameRequired", method.getMethodName(), join(",", permission.getRoleName()));
                } else if (method.getEjbName().equals("*")) { //NOPMD
                    // no-op. Just continue the loop.
                } else if (!ejbsByName.containsKey(method.getEjbName())) {
                    fail("MethodPermission", "methodPermission.noSuchEjbName", method.getEjbName(), method.getMethodName(), join(",", permission.getRoleName()));
                }
            }
        }

        for (final ContainerTransaction transaction : assembly.getContainerTransaction()) {
            for (final Method method : transaction.getMethod()) {
                if (method.getEjbName() == null) {
                    fail("ContainerTransaction", "containerTransaction.ejbNameRequired", method.getMethodName(), transaction.getTransAttribute());
                } else if (method.getEjbName().equals("*")) { //NOPMD
                    // no-op. Just continue the loop.
View Full Code Here

            }
        }
    }

    private void checkUnusedInterceptors(final EjbModule ejbModule) {
        final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
        final Interceptor[] interceptorsArray = ejbModule.getEjbJar().getInterceptors();
        final List<Interceptor> interceptors = Arrays.asList(interceptorsArray);
        final Set<String> interceptorClassNames = new HashSet<String>(interceptors.size());
        for (final Interceptor interceptor : interceptors) {
            interceptorClassNames.add(interceptor.getInterceptorClass());
        }
        final Set<String> interceptorClassNamesUsedInBindings = new HashSet<String>();
        for (final InterceptorBinding binding : assembly.getInterceptorBinding()) {
            final List<String> interceptorClass = binding.getInterceptorClass();
            interceptorClassNamesUsedInBindings.addAll(interceptorClass);
        }
        final Set<String> unusedInterceptors = new HashSet<String>();
        for (final String clazz : interceptorClassNames) {
View Full Code Here

                    messageBean.setEjbClass(beanClass.get());
                }
                LegacyProcessor.process(beanClass.get(), messageBean);
            }

            AssemblyDescriptor assemblyDescriptor = ejbModule.getEjbJar().getAssemblyDescriptor();
            if (assemblyDescriptor == null) {
                assemblyDescriptor = new AssemblyDescriptor();
                ejbModule.getEjbJar().setAssemblyDescriptor(assemblyDescriptor);
            }

            startupLogger.debug("Searching for annotated application exceptions (see OPENEJB-980)");
            final List<Class<?>> appExceptions = finder.findAnnotatedClasses(ApplicationException.class);
            for (final Class<?> exceptionClass : appExceptions) {
                startupLogger.debug("...handling " + exceptionClass);
                final ApplicationException annotation = exceptionClass.getAnnotation(ApplicationException.class);
                if (assemblyDescriptor.getApplicationException(exceptionClass) == null) {
                    startupLogger.debug("...adding " + exceptionClass + " with rollback=" + annotation.rollback());
                    assemblyDescriptor.addApplicationException(exceptionClass, annotation.rollback(), annotation.inherited());
                } else {
                    mergeApplicationExceptionAnnotation(assemblyDescriptor, exceptionClass, annotation);
                }
            }
View Full Code Here

TOP

Related Classes of org.apache.openejb.jee.AssemblyDescriptor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.