Search in sources :

Example 16 with Interceptor

use of org.apache.openejb.jee.Interceptor in project tomee by apache.

the class Interceptors$JAXB method _write.

public static final void _write(final XoXMLStreamWriter writer, final Interceptors interceptors, RuntimeContext context) throws Exception {
    if (interceptors == null) {
        writer.writeXsiNil();
        return;
    }
    if (context == null) {
        context = new RuntimeContext();
    }
    final String prefix = writer.getUniquePrefix("http://java.sun.com/xml/ns/javaee");
    if (Interceptors.class != interceptors.getClass()) {
        context.unexpectedSubclass(writer, interceptors, Interceptors.class);
        return;
    }
    context.beforeMarshal(interceptors, LifecycleCallback.NONE);
    // ATTRIBUTE: id
    final String idRaw = interceptors.id;
    if (idRaw != null) {
        String id = null;
        try {
            id = Adapters.collapsedStringAdapterAdapter.marshal(idRaw);
        } catch (final Exception e) {
            context.xmlAdapterError(interceptors, "id", CollapsedStringAdapter.class, String.class, String.class, e);
        }
        writer.writeAttribute("", "", "id", id);
    }
    // ELEMENT: descriptions
    Text[] descriptions = null;
    try {
        descriptions = interceptors.getDescriptions();
    } catch (final Exception e) {
        context.getterError(interceptors, "descriptions", Interceptors.class, "getDescriptions", e);
    }
    if (descriptions != null) {
        for (final Text descriptionsItem : descriptions) {
            if (descriptionsItem != null) {
                writer.writeStartElement(prefix, "description", "http://java.sun.com/xml/ns/javaee");
                writeText(writer, descriptionsItem, context);
                writer.writeEndElement();
            } else {
                context.unexpectedNullValue(interceptors, "descriptions");
            }
        }
    }
    // ELEMENT: interceptor
    Interceptor[] interceptor = null;
    try {
        interceptor = interceptors.getInterceptor();
    } catch (final Exception e) {
        context.getterError(interceptors, "interceptor", Interceptors.class, "getInterceptor", e);
    }
    if (interceptor != null) {
        for (final Interceptor interceptorItem : interceptor) {
            if (interceptorItem != null) {
                writer.writeStartElement(prefix, "interceptor", "http://java.sun.com/xml/ns/javaee");
                writeInterceptor(writer, interceptorItem, context);
                writer.writeEndElement();
            } else {
                context.unexpectedNullValue(interceptors, "interceptor");
            }
        }
    }
    context.afterMarshal(interceptors, LifecycleCallback.NONE);
}
Also used : CollapsedStringAdapter(javax.xml.bind.annotation.adapters.CollapsedStringAdapter) Text$JAXB.readText(org.apache.openejb.jee.Text$JAXB.readText) Text$JAXB.writeText(org.apache.openejb.jee.Text$JAXB.writeText) RuntimeContext(org.metatype.sxc.jaxb.RuntimeContext) Interceptor$JAXB.writeInterceptor(org.apache.openejb.jee.Interceptor$JAXB.writeInterceptor) Interceptor$JAXB.readInterceptor(org.apache.openejb.jee.Interceptor$JAXB.readInterceptor)

Example 17 with Interceptor

use of org.apache.openejb.jee.Interceptor in project tomee by apache.

the class SystemApps method getSystemModule.

public static EjbModule getSystemModule() {
    final EjbModule module = new EjbModule(new EjbJar("openejb"), new OpenejbJar());
    final OpenejbJar openejbJar = module.getOpenejbJar();
    final EjbJar ejbJar = module.getEjbJar();
    // 
    // 
    // DONT MODIFY IT WITHOUT VALIDATING org.apache.openejb.config.SystemAppInfo.preComputedInfo()
    // 
    // 
    ejbJar.addEnterpriseBean(singleton(DeployerEjb.class));
    ejbJar.addEnterpriseBean(singleton(ConfigurationInfoEjb.class));
    ejbJar.addEnterpriseBean(singleton(MEJBBean.class));
    ejbJar.addInterceptor(new Interceptor(InternalSecurityInterceptor.class));
    ejbJar.getAssemblyDescriptor().addInterceptorBinding(new InterceptorBinding("*", InternalSecurityInterceptor.class.getName()));
    module.getMbeans().add(JMXDeployer.class.getName());
    final SingletonBean bean = ejbJar.addEnterpriseBean(new SingletonBean("openejb/WebappDeployer", "org.apache.tomee.catalina.deployer.WebappDeployer"));
    final EjbDeployment deployment = openejbJar.addEjbDeployment(bean);
    deployment.getProperties().put("openejb.jndiname.format", "{deploymentId}{interfaceType.annotationName}");
    final SingletonBean exceptionManager = ejbJar.addEnterpriseBean(new SingletonBean("openejb/ExceptionManagerFacade", "org.apache.tomee.catalina.facade.ExceptionManagerFacadeBean"));
    final EjbDeployment exceptionMgr = openejbJar.addEjbDeployment(exceptionManager);
    exceptionMgr.getProperties().put("openejb.jndiname.format", "{deploymentId}{interfaceType.annotationName}");
    openejbJar.getProperties().put("openejb.deploymentId.format", "{ejbName}");
    openejbJar.getProperties().put("openejb.jndiname.format", "{deploymentId}{interfaceType.openejbLegacyName}");
    return module;
}
Also used : MEJBBean(org.apache.openejb.mgmt.MEJBBean) InternalSecurityInterceptor(org.apache.openejb.security.internal.InternalSecurityInterceptor) InterceptorBinding(org.apache.openejb.jee.InterceptorBinding) SingletonBean(org.apache.openejb.jee.SingletonBean) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) DeployerEjb(org.apache.openejb.assembler.DeployerEjb) JMXDeployer(org.apache.openejb.assembler.monitoring.JMXDeployer) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) ConfigurationInfoEjb(org.apache.openejb.assembler.classic.cmd.ConfigurationInfoEjb) Interceptor(org.apache.openejb.jee.Interceptor) InternalSecurityInterceptor(org.apache.openejb.security.internal.InternalSecurityInterceptor) EjbJar(org.apache.openejb.jee.EjbJar)

Example 18 with Interceptor

use of org.apache.openejb.jee.Interceptor in project tomee by apache.

the class CheckAssemblyBindings method checkUnusedInterceptors.

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<>(interceptors.size());
    for (final Interceptor interceptor : interceptors) {
        interceptorClassNames.add(interceptor.getInterceptorClass());
    }
    final Set<String> interceptorClassNamesUsedInBindings = new HashSet<>();
    for (final InterceptorBinding binding : assembly.getInterceptorBinding()) {
        final List<String> interceptorClass = binding.getInterceptorClass();
        interceptorClassNamesUsedInBindings.addAll(interceptorClass);
    }
    final Set<String> unusedInterceptors = new HashSet<>();
    for (final String clazz : interceptorClassNames) {
        if (!interceptorClassNamesUsedInBindings.contains(clazz)) {
            unusedInterceptors.add(clazz);
        }
    }
    for (final String clazz : unusedInterceptors) {
        warn("Interceptors", "interceptor.unused", clazz);
    }
}
Also used : InterceptorBinding(org.apache.openejb.jee.InterceptorBinding) AssemblyDescriptor(org.apache.openejb.jee.AssemblyDescriptor) Interceptor(org.apache.openejb.jee.Interceptor) HashSet(java.util.HashSet)

Example 19 with Interceptor

use of org.apache.openejb.jee.Interceptor in project tomee by apache.

the class CheckCallbacks method validate.

public void validate(final EjbModule module) {
    for (final EnterpriseBean bean : module.getEjbJar().getEnterpriseBeans()) {
        Class ejbClass = null;
        try {
            ejbClass = loadClass(bean.getEjbClass());
        } catch (final OpenEJBException e) {
            continue;
        }
        if (bean instanceof Invokable) {
            final Invokable invokable = (Invokable) bean;
            for (final AroundInvoke aroundInvoke : invokable.getAroundInvoke()) {
                checkAroundInvoke(ejbClass, aroundInvoke, bean.getEjbName());
            }
            for (final AroundTimeout aroundTimeout : invokable.getAroundTimeout()) {
                checkAroundTimeout(ejbClass, aroundTimeout, bean.getEjbName());
            }
        }
        for (final LifecycleCallback callback : bean.getPostConstruct()) {
            checkCallback(ejbClass, "PostConstruct", callback, bean);
        }
        for (final LifecycleCallback callback : bean.getPreDestroy()) {
            checkCallback(ejbClass, "PreDestroy", callback, bean);
        }
        final ClassFinder finder = new ClassFinder(ejbClass);
        if (bean instanceof Session) {
            final SessionBean session = (SessionBean) bean;
            if (session.getSessionType() == SessionType.STATEFUL) {
                for (final LifecycleCallback callback : session.getPrePassivate()) {
                    checkCallback(ejbClass, "PrePassivate", callback, bean);
                }
                for (final LifecycleCallback callback : session.getPostActivate()) {
                    checkCallback(ejbClass, "PostActivate", callback, bean);
                }
                checkSessionSynchronization(ejbClass, session);
                for (final LifecycleCallback callback : session.getAfterBegin()) {
                    checkCallback(ejbClass, "AfterBegin", callback, bean);
                }
                for (final LifecycleCallback callback : session.getBeforeCompletion()) {
                    checkCallback(ejbClass, "BeforeCompletion", callback, bean);
                }
                for (final LifecycleCallback callback : session.getAfterCompletion()) {
                    checkCallback(ejbClass, "AfterCompletion", callback, bean, boolean.class);
                }
                for (final AroundTimeout aroundTimeout : session.getAroundTimeout()) {
                    ignoredMethodAnnotation("AroundTimeout", bean, bean.getEjbClass(), aroundTimeout.getMethodName(), SessionType.STATEFUL.getName());
                }
                for (final Timer timer : session.getTimer()) {
                    ignoredMethodAnnotation("Schedule/Schedules", bean, bean.getEjbClass(), timer.getTimeoutMethod().getMethodName(), SessionType.STATEFUL.getName());
                }
            } else {
                for (final LifecycleCallback callback : session.getAfterBegin()) {
                    ignoredMethodAnnotation("AfterBegin", bean, bean.getEjbClass(), callback.getMethodName(), session.getSessionType().getName());
                }
                for (final LifecycleCallback callback : session.getBeforeCompletion()) {
                    ignoredMethodAnnotation("BeforeCompletion", bean, bean.getEjbClass(), callback.getMethodName(), session.getSessionType().getName());
                }
                for (final LifecycleCallback callback : session.getAfterCompletion()) {
                    ignoredMethodAnnotation("AfterCompletion", bean, bean.getEjbClass(), callback.getMethodName(), session.getSessionType().getName());
                }
                for (final LifecycleCallback callback : session.getPrePassivate()) {
                    ignoredMethodAnnotation("PrePassivate", bean, bean.getEjbClass(), callback.getMethodName(), session.getSessionType().getName());
                }
                for (final LifecycleCallback callback : session.getPostActivate()) {
                    ignoredMethodAnnotation("PostActivate", bean, bean.getEjbClass(), callback.getMethodName(), session.getSessionType().getName());
                }
                for (final RemoveMethod method : session.getRemoveMethod()) {
                    ignoredMethodAnnotation("Remove", bean, bean.getEjbClass(), method.getBeanMethod().getMethodName(), session.getSessionType().getName());
                }
                for (final InitMethod method : session.getInitMethod()) {
                    ignoredMethodAnnotation("Init", bean, bean.getEjbClass(), method.getBeanMethod().getMethodName(), session.getSessionType().getName());
                }
            }
        } else {
            for (final Method method : finder.findAnnotatedMethods(PrePassivate.class)) {
                ignoredMethodAnnotation("PrePassivate", bean, bean.getEjbClass(), method.getName(), bean.getClass().getSimpleName());
            }
            for (final Method method : finder.findAnnotatedMethods(PostActivate.class)) {
                ignoredMethodAnnotation("PostActivate", bean, bean.getEjbClass(), method.getName(), bean.getClass().getSimpleName());
            }
            for (final Method method : finder.findAnnotatedMethods(Remove.class)) {
                ignoredMethodAnnotation("Remove", bean, bean.getEjbClass(), method.getName(), bean.getClass().getSimpleName());
            }
            for (final Method method : finder.findAnnotatedMethods(Init.class)) {
                ignoredMethodAnnotation("Init", bean, bean.getEjbClass(), method.getName(), bean.getClass().getSimpleName());
            }
            for (final Method method : finder.findAnnotatedMethods(AfterBegin.class)) {
                ignoredMethodAnnotation("AfterBegin", bean, bean.getEjbClass(), method.getName(), bean.getClass().getSimpleName());
            }
            for (final Method method : finder.findAnnotatedMethods(BeforeCompletion.class)) {
                ignoredMethodAnnotation("BeforeCompletion", bean, bean.getEjbClass(), method.getName(), bean.getClass().getSimpleName());
            }
            for (final Method method : finder.findAnnotatedMethods(AfterCompletion.class)) {
                ignoredMethodAnnotation("AfterCompletion", bean, bean.getEjbClass(), method.getName(), bean.getClass().getSimpleName());
            }
        }
    // if (bean instanceof TimerConsumer) {
    // TimerConsumer timerConsumer = (TimerConsumer) bean;
    // checkTimeOut(ejbClass, timerConsumer.getTimeoutMethod(), bean);
    // 
    // List<Method> timeoutMethods = finder.findAnnotatedMethods(Timeout.class);
    // if (timeoutMethods.size() > 1) {
    // fail(timerConsumer.getTimerConsumerName(), "timeout.tooManyMethods", timeoutMethods.size(), Join.join(",", timeoutMethods));
    // }
    // 
    // for(Timer timer : ((TimerConsumer) bean).getTimer()) {
    // checkTimeOut(ejbClass, timer.getTimeoutMethod(), bean);
    // }
    // }
    }
    for (final Interceptor interceptor : module.getEjbJar().getInterceptors()) {
        Class interceptorClass = null;
        try {
            interceptorClass = loadClass(interceptor.getInterceptorClass());
        } catch (final OpenEJBException e) {
            continue;
        }
        for (final AroundInvoke aroundInvoke : interceptor.getAroundInvoke()) {
            checkAroundInvoke(interceptorClass, aroundInvoke, "Interceptor");
        }
        for (final AroundTimeout aroundTimeout : interceptor.getAroundTimeout()) {
            checkAroundTimeout(interceptorClass, aroundTimeout, "Interceptor");
        }
        for (final LifecycleCallback callback : interceptor.getPostConstruct()) {
            checkCallback(interceptorClass, "PostConstruct", callback, interceptor);
        }
        for (final LifecycleCallback callback : interceptor.getPreDestroy()) {
            checkCallback(interceptorClass, "PreDestroy", callback, interceptor);
        }
        for (final LifecycleCallback callback : interceptor.getPrePassivate()) {
            checkCallback(interceptorClass, "PrePassivate", callback, interceptor);
        }
        for (final LifecycleCallback callback : interceptor.getPostActivate()) {
            checkCallback(interceptorClass, "PostActivate", callback, interceptor);
        }
        for (final LifecycleCallback callback : interceptor.getAfterBegin()) {
            checkCallback(interceptorClass, "AfterBegin", callback, interceptor);
        }
        for (final LifecycleCallback callback : interceptor.getBeforeCompletion()) {
            checkCallback(interceptorClass, "BeforeCompletion", callback, interceptor);
        }
        for (final LifecycleCallback callback : interceptor.getAfterCompletion()) {
            checkCallback(interceptorClass, "AfterCompletion", callback, interceptor);
        }
    }
}
Also used : InitMethod(org.apache.openejb.jee.InitMethod) OpenEJBException(org.apache.openejb.OpenEJBException) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) AroundTimeout(org.apache.openejb.jee.AroundTimeout) RemoveMethod(org.apache.openejb.jee.RemoveMethod) Method(java.lang.reflect.Method) InitMethod(org.apache.openejb.jee.InitMethod) CallbackMethod(org.apache.openejb.jee.CallbackMethod) SessionBean(org.apache.openejb.jee.SessionBean) Timer(org.apache.openejb.jee.Timer) Invokable(org.apache.openejb.jee.Invokable) ClassFinder(org.apache.xbean.finder.ClassFinder) RemoveMethod(org.apache.openejb.jee.RemoveMethod) LifecycleCallback(org.apache.openejb.jee.LifecycleCallback) AroundInvoke(org.apache.openejb.jee.AroundInvoke) Interceptor(org.apache.openejb.jee.Interceptor) Session(org.apache.openejb.jee.Session)

Example 20 with Interceptor

use of org.apache.openejb.jee.Interceptor in project tomee by apache.

the class TimeoutAroundTest method _testTimeoutAround.

public void _testTimeoutAround() throws Exception {
    System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
    final Assembler assembler = new Assembler();
    final ConfigurationFactory config = new ConfigurationFactory();
    assembler.createProxyFactory(config.configureService(ProxyFactoryInfo.class));
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    final EjbJar ejbJar = new EjbJar();
    final AssemblyDescriptor assemblyDescriptor = ejbJar.getAssemblyDescriptor();
    // Configure AroundTimeout by deployment plan
    final Interceptor interceptorA = new Interceptor(SimpleInterceptorA.class);
    interceptorA.getAroundTimeout().add(new org.apache.openejb.jee.AroundTimeout(SimpleInterceptorA.class.getName(), "interceptorTimeoutAround"));
    ejbJar.addInterceptor(interceptorA);
    // Configure AroundTimeout by annotation
    final Interceptor interceptorB = new Interceptor(SimpleInterceptorB.class);
    ejbJar.addInterceptor(interceptorB);
    // Override AroundTimeout annotation by deployment plan
    final Interceptor interceptorC = new Interceptor(SimpleInterceptorC.class);
    interceptorC.getAroundTimeout().add(new org.apache.openejb.jee.AroundTimeout(SimpleInterceptorC.class.getName(), "interceptorTimeoutAround"));
    ejbJar.addInterceptor(interceptorC);
    // Configure aroundTimeout by deployment plan
    final StatelessBean subBeanA = new StatelessBean(SubBeanA.class);
    subBeanA.addAroundTimeout("beanTimeoutAround");
    ejbJar.addEnterpriseBean(subBeanA);
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanA, interceptorA));
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanA, interceptorB));
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanA, interceptorC));
    // Configure aroundTimeout by annotation
    final StatelessBean subBeanB = new StatelessBean(SubBeanB.class);
    ejbJar.addEnterpriseBean(subBeanB);
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanB, interceptorA));
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanB, interceptorB));
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanB, interceptorC));
    // Override aroundTimeout annotation by deployment plan
    final StatelessBean subBeanC = new StatelessBean(SubBeanC.class);
    subBeanC.addAroundTimeout("beanTimeoutAround");
    ejbJar.addEnterpriseBean(subBeanC);
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanC, interceptorA));
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanC, interceptorB));
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanC, interceptorC));
    final EjbJarInfo ejbJarInfo = config.configureApplication(ejbJar);
    assembler.createApplication(ejbJarInfo);
    final InitialContext context = new InitialContext();
    final List<Call> expectedResult = Arrays.asList(Call.INTERCEPTOR_BEFORE_AROUNDTIMEOUT, Call.INTERCEPTOR_BEFORE_AROUNDTIMEOUT, Call.INTERCEPTOR_BEFORE_AROUNDTIMEOUT, Call.BEAN_BEFORE_AROUNDTIMEOUT, Call.BEAN_TIMEOUT, Call.BEAN_AFTER_AROUNDTIMEOUT, Call.INTERCEPTOR_AFTER_AROUNDTIMEOUT, Call.INTERCEPTOR_AFTER_AROUNDTIMEOUT, Call.INTERCEPTOR_AFTER_AROUNDTIMEOUT);
    {
        final BeanInterface beanA = (BeanInterface) context.lookup("SubBeanALocal");
        beanA.simpleMethod();
        Thread.sleep(5000L);
        assertEquals(expectedResult, result);
        result.clear();
    }
    {
        final BeanInterface beanB = (BeanInterface) context.lookup("SubBeanBLocal");
        beanB.simpleMethod();
        Thread.sleep(5000L);
        assertEquals(expectedResult, result);
        result.clear();
    }
    {
        final BeanInterface beanC = (BeanInterface) context.lookup("SubBeanCLocal");
        beanC.simpleMethod();
        Thread.sleep(5000L);
        assertEquals(expectedResult, result);
        result.clear();
    }
}
Also used : LocalInitialContextFactory(org.apache.openejb.core.LocalInitialContextFactory) InitialContext(javax.naming.InitialContext) ProxyFactoryInfo(org.apache.openejb.assembler.classic.ProxyFactoryInfo) InterceptorBinding(org.apache.openejb.jee.InterceptorBinding) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) StatelessBean(org.apache.openejb.jee.StatelessBean) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) Assembler(org.apache.openejb.assembler.classic.Assembler) AssemblyDescriptor(org.apache.openejb.jee.AssemblyDescriptor) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) Interceptor(org.apache.openejb.jee.Interceptor) EjbJarInfo(org.apache.openejb.assembler.classic.EjbJarInfo) EjbJar(org.apache.openejb.jee.EjbJar)

Aggregations

Interceptor (org.apache.openejb.jee.Interceptor)21 EjbJar (org.apache.openejb.jee.EjbJar)17 InterceptorBinding (org.apache.openejb.jee.InterceptorBinding)16 AssemblyDescriptor (org.apache.openejb.jee.AssemblyDescriptor)12 StatelessBean (org.apache.openejb.jee.StatelessBean)10 EnterpriseBean (org.apache.openejb.jee.EnterpriseBean)6 NamedMethod (org.apache.openejb.jee.NamedMethod)5 Module (org.apache.openejb.testing.Module)5 ArrayList (java.util.ArrayList)3 InitialContext (javax.naming.InitialContext)2 Assembler (org.apache.openejb.assembler.classic.Assembler)2 EjbJarInfo (org.apache.openejb.assembler.classic.EjbJarInfo)2 ProxyFactoryInfo (org.apache.openejb.assembler.classic.ProxyFactoryInfo)2 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)2 TransactionServiceInfo (org.apache.openejb.assembler.classic.TransactionServiceInfo)2 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)2 EjbModule (org.apache.openejb.config.EjbModule)2 LocalInitialContextFactory (org.apache.openejb.core.LocalInitialContextFactory)2 ContainerTransaction (org.apache.openejb.jee.ContainerTransaction)2 Interceptor$JAXB.readInterceptor (org.apache.openejb.jee.Interceptor$JAXB.readInterceptor)2