Search in sources :

Example 11 with Interceptor$JAXB.writeInterceptor

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

the class CheckInvalidInterceptorTest method test1.

@Keys({ @Key(value = "interceptor.unused", count = 2, type = KeyType.WARNING), @Key("aroundInvoke.invalidArguments") })
public EjbJar test1() {
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new StatelessBean(BarBean.class));
    final Interceptor unused = ejbJar.addInterceptor(new org.apache.openejb.jee.Interceptor(UnusedInterceptor.class));
    final Interceptor unused1 = ejbJar.addInterceptor(new org.apache.openejb.jee.Interceptor(UnusedInterceptor1.class));
    final Interceptor used = ejbJar.addInterceptor(new org.apache.openejb.jee.Interceptor(UsedInterceptor.class));
    final List<InterceptorBinding> interceptorBindings = ejbJar.getAssemblyDescriptor().getInterceptorBinding();
    final InterceptorBinding binding = new InterceptorBinding("BarBean", used);
    interceptorBindings.add(binding);
    return ejbJar;
}
Also used : InterceptorBinding(org.apache.openejb.jee.InterceptorBinding) StatelessBean(org.apache.openejb.jee.StatelessBean) Interceptor(org.apache.openejb.jee.Interceptor) Interceptor(org.apache.openejb.jee.Interceptor) EjbJar(org.apache.openejb.jee.EjbJar)

Example 12 with Interceptor$JAXB.writeInterceptor

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

the class Interceptors$JAXB method _read.

public static final Interceptors _read(final XoXMLStreamReader reader, RuntimeContext context) throws Exception {
    // Check for xsi:nil
    if (reader.isXsiNil()) {
        return null;
    }
    if (context == null) {
        context = new RuntimeContext();
    }
    final Interceptors interceptors = new Interceptors();
    context.beforeUnmarshal(interceptors, LifecycleCallback.NONE);
    ArrayList<Text> descriptions = null;
    ArrayList<Interceptor> interceptor = null;
    // Check xsi:type
    final QName xsiType = reader.getXsiType();
    if (xsiType != null) {
        if (("interceptorsType" != xsiType.getLocalPart()) || ("http://java.sun.com/xml/ns/javaee" != xsiType.getNamespaceURI())) {
            return context.unexpectedXsiType(reader, Interceptors.class);
        }
    }
    // Read attributes
    for (final Attribute attribute : reader.getAttributes()) {
        if (("id" == attribute.getLocalName()) && (("" == attribute.getNamespace()) || (attribute.getNamespace() == null))) {
            // ATTRIBUTE: id
            final String id = Adapters.collapsedStringAdapterAdapter.unmarshal(attribute.getValue());
            context.addXmlId(reader, id, interceptors);
            interceptors.id = id;
        } else if (XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI != attribute.getNamespace()) {
            context.unexpectedAttribute(attribute, new QName("", "id"));
        }
    }
    // Read elements
    for (final XoXMLStreamReader elementReader : reader.getChildElements()) {
        if (("description" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: descriptions
            final Text descriptionsItem = readText(elementReader, context);
            if (descriptions == null) {
                descriptions = new ArrayList<Text>();
            }
            descriptions.add(descriptionsItem);
        } else if (("interceptor" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
            // ELEMENT: interceptor
            final Interceptor interceptorItem = readInterceptor(elementReader, context);
            if (interceptor == null) {
                interceptor = new ArrayList<Interceptor>();
            }
            interceptor.add(interceptorItem);
        } else {
            context.unexpectedElement(elementReader, new QName("http://java.sun.com/xml/ns/javaee", "description"), new QName("http://java.sun.com/xml/ns/javaee", "interceptor"));
        }
    }
    if (descriptions != null) {
        try {
            interceptors.setDescriptions(descriptions.toArray(new Text[descriptions.size()]));
        } catch (final Exception e) {
            context.setterError(reader, Interceptors.class, "setDescriptions", Text[].class, e);
        }
    }
    if (interceptor != null) {
        try {
            interceptors.setInterceptor(interceptor.toArray(new Interceptor[interceptor.size()]));
        } catch (final Exception e) {
            context.setterError(reader, Interceptors.class, "setInterceptor", Interceptor[].class, e);
        }
    }
    context.afterUnmarshal(interceptors, LifecycleCallback.NONE);
    return interceptors;
}
Also used : Attribute(org.metatype.sxc.util.Attribute) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) 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) XoXMLStreamReader(org.metatype.sxc.util.XoXMLStreamReader)

Example 13 with Interceptor$JAXB.writeInterceptor

use of org.apache.openejb.jee.Interceptor$JAXB.writeInterceptor 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 14 with Interceptor$JAXB.writeInterceptor

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

the class StatefulInterceptorTest method buildTestApp.

public EjbModule buildTestApp() {
    final EjbJar ejbJar = new EjbJar();
    final AssemblyDescriptor ad = ejbJar.getAssemblyDescriptor();
    final EnterpriseBean bean = ejbJar.addEnterpriseBean(new StatefulBean(TargetBean.class));
    Interceptor interceptor;
    interceptor = ejbJar.addInterceptor(new Interceptor(ClassInterceptor.class));
    ad.addInterceptorBinding(new InterceptorBinding(bean, interceptor));
    interceptor = ejbJar.addInterceptor(new Interceptor(DefaultInterceptor.class));
    ad.addInterceptorBinding(new InterceptorBinding("*", interceptor));
    interceptor = ejbJar.addInterceptor(new Interceptor(EchoMethodInterceptor.class));
    final InterceptorBinding binding = ad.addInterceptorBinding(new InterceptorBinding(bean, interceptor));
    binding.setMethod(new NamedMethod("echo"));
    return new EjbModule(this.getClass().getClassLoader(), this.getClass().getSimpleName(), "test", ejbJar, null);
}
Also used : InterceptorBinding(org.apache.openejb.jee.InterceptorBinding) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) StatefulBean(org.apache.openejb.jee.StatefulBean) EjbModule(org.apache.openejb.config.EjbModule) AssemblyDescriptor(org.apache.openejb.jee.AssemblyDescriptor) NamedMethod(org.apache.openejb.jee.NamedMethod) Interceptor(org.apache.openejb.jee.Interceptor) EjbJar(org.apache.openejb.jee.EjbJar)

Example 15 with Interceptor$JAXB.writeInterceptor

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

the class StatefulSessionSynchronizationTest method test.

public void test() 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();
    final Interceptor interceptor = new Interceptor(SimpleInterceptor.class);
    ejbJar.addInterceptor(interceptor);
    // Test SessionSynchronization interface
    final StatefulBean subBeanA = new StatefulBean(SubBeanA.class);
    ejbJar.addEnterpriseBean(subBeanA);
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanA, interceptor));
    // Test configure session synchronization callback methods in deployment plan
    final StatefulBean subBeanB = new StatefulBean(SubBeanB.class);
    subBeanB.setAfterBeginMethod(new NamedMethod(SubBeanB.class.getDeclaredMethod("afterBegin")));
    subBeanB.setBeforeCompletionMethod(new NamedMethod(SubBeanB.class.getDeclaredMethod("beforeCompletion")));
    subBeanB.setAfterCompletionMethod(new NamedMethod(SubBeanB.class.getDeclaredMethod("afterCompletion", boolean.class)));
    ejbJar.addEnterpriseBean(subBeanB);
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanB, interceptor));
    // Test session synchronization methods via annotations
    final StatefulBean subBeanC = new StatefulBean(SubBeanC.class);
    ejbJar.addEnterpriseBean(subBeanC);
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanC, interceptor));
    // Test override the annotations by deployment plan
    final StatefulBean subBeanD = new StatefulBean(SubBeanD.class);
    subBeanD.setAfterBeginMethod(new NamedMethod(SubBeanD.class.getDeclaredMethod("afterBeginNew")));
    subBeanD.setBeforeCompletionMethod(new NamedMethod(SubBeanD.class.getDeclaredMethod("beforeCompletionNew")));
    subBeanD.setAfterCompletionMethod(new NamedMethod(SubBeanD.class.getDeclaredMethod("afterCompletionNew", boolean.class)));
    ejbJar.addEnterpriseBean(subBeanD);
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanD, interceptor));
    // Test only one session synchronization method @AfterBegin
    final StatefulBean subBeanE = new StatefulBean(SubBeanE.class);
    ejbJar.addEnterpriseBean(subBeanE);
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanE, interceptor));
    // Test only one session synchronization method @AfterCompletion
    final StatefulBean subBeanF = new StatefulBean(SubBeanF.class);
    ejbJar.addEnterpriseBean(subBeanF);
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanF, interceptor));
    // Test only one session synchronization method @BeforeCompletion
    final StatefulBean subBeanG = new StatefulBean(SubBeanG.class);
    ejbJar.addEnterpriseBean(subBeanG);
    assemblyDescriptor.addInterceptorBinding(new InterceptorBinding(subBeanG, interceptor));
    // Test SessionSynchronization interface but methods are in the parent class
    // Interceptor is declared on the bean method
    final StatefulBean subBeanH = new StatefulBean(SubBeanH.class);
    ejbJar.addEnterpriseBean(subBeanH);
    // Test SessionSynchronization interface but methods are in the parent class
    // using @LocalBean
    final StatefulBean subBeanI = new StatefulBean(SubBeanI.class);
    ejbJar.addEnterpriseBean(subBeanI);
    final EjbJarInfo ejbJarInfo = config.configureApplication(ejbJar);
    assembler.createApplication(ejbJarInfo);
    final InitialContext context = new InitialContext();
    final List<Call> expectedResult = Arrays.asList(Call.INTERCEPTOR_AFTER_BEGIN, Call.BEAN_AFTER_BEGIN, Call.INTERCEPTOR_AROUND_INVOKE_BEGIN, Call.BEAN_AROUND_INVOKE_BEGIN, Call.BEAN_METHOD, Call.BEAN_AROUND_INVOKE_AFTER, Call.INTERCEPTOR_AROUND_INVOKE_AFTER, Call.INTERCEPTOR_BEFORE_COMPLETION, Call.BEAN_BEFORE_COMPLETION, Call.INTERCEPTOR_AFTER_COMPLETION, Call.BEAN_AFTER_COMPLETION);
    {
        final BeanInterface beanA = (BeanInterface) context.lookup("SubBeanALocal");
        beanA.simpleMethod();
        assertEquals(expectedResult, result);
        result.clear();
    }
    {
        final BeanInterface beanB = (BeanInterface) context.lookup("SubBeanBLocal");
        beanB.simpleMethod();
        assertEquals(expectedResult, result);
        result.clear();
    }
    {
        final BeanInterface beanC = (BeanInterface) context.lookup("SubBeanCLocal");
        beanC.simpleMethod();
        assertEquals(expectedResult, result);
        result.clear();
    }
    {
        final BeanInterface beanD = (BeanInterface) context.lookup("SubBeanDLocal");
        beanD.simpleMethod();
        assertEquals(expectedResult, result);
        result.clear();
    }
    {
        final BeanInterface beanE = (BeanInterface) context.lookup("SubBeanELocal");
        beanE.simpleMethod();
        assertEquals(Arrays.asList(Call.INTERCEPTOR_AFTER_BEGIN, Call.BEAN_AFTER_BEGIN, Call.INTERCEPTOR_AROUND_INVOKE_BEGIN, Call.BEAN_AROUND_INVOKE_BEGIN, Call.BEAN_METHOD, Call.BEAN_AROUND_INVOKE_AFTER, Call.INTERCEPTOR_AROUND_INVOKE_AFTER, Call.INTERCEPTOR_BEFORE_COMPLETION, Call.INTERCEPTOR_AFTER_COMPLETION), result);
        result.clear();
    }
    {
        final BeanInterface beanF = (BeanInterface) context.lookup("SubBeanFLocal");
        beanF.simpleMethod();
        assertEquals(Arrays.asList(Call.INTERCEPTOR_AFTER_BEGIN, Call.INTERCEPTOR_AROUND_INVOKE_BEGIN, Call.BEAN_AROUND_INVOKE_BEGIN, Call.BEAN_METHOD, Call.BEAN_AROUND_INVOKE_AFTER, Call.INTERCEPTOR_AROUND_INVOKE_AFTER, Call.INTERCEPTOR_BEFORE_COMPLETION, Call.INTERCEPTOR_AFTER_COMPLETION, Call.BEAN_AFTER_COMPLETION), result);
        result.clear();
    }
    {
        final BeanInterface beanG = (BeanInterface) context.lookup("SubBeanGLocal");
        beanG.simpleMethod();
        assertEquals(Arrays.asList(Call.INTERCEPTOR_AFTER_BEGIN, Call.INTERCEPTOR_AROUND_INVOKE_BEGIN, Call.BEAN_AROUND_INVOKE_BEGIN, Call.BEAN_METHOD, Call.BEAN_AROUND_INVOKE_AFTER, Call.INTERCEPTOR_AROUND_INVOKE_AFTER, Call.INTERCEPTOR_BEFORE_COMPLETION, Call.BEAN_BEFORE_COMPLETION, Call.INTERCEPTOR_AFTER_COMPLETION), result);
        result.clear();
    }
    final List<Call> synchAndArroundInvokeResult = Arrays.asList(Call.BEAN_AFTER_BEGIN, Call.INTERCEPTOR_AROUND_INVOKE_BEGIN, Call.BEAN_AROUND_INVOKE_BEGIN, Call.BEAN_METHOD, Call.BEAN_AROUND_INVOKE_AFTER, Call.INTERCEPTOR_AROUND_INVOKE_AFTER, Call.BEAN_BEFORE_COMPLETION, Call.BEAN_AFTER_COMPLETION);
    {
        final BeanInterface beanH = (BeanInterface) context.lookup("SubBeanHLocal");
        beanH.simpleMethod();
        assertEquals(synchAndArroundInvokeResult, result);
        result.clear();
    }
    {
        final BeanInterface beanI = (BeanInterface) context.lookup("SubBeanILocalBean");
        beanI.simpleMethod();
        assertEquals(synchAndArroundInvokeResult, result);
        result.clear();
    }
}
Also used : StatefulBean(org.apache.openejb.jee.StatefulBean) LocalInitialContextFactory(org.apache.openejb.core.LocalInitialContextFactory) NamedMethod(org.apache.openejb.jee.NamedMethod) InitialContext(javax.naming.InitialContext) ProxyFactoryInfo(org.apache.openejb.assembler.classic.ProxyFactoryInfo) InterceptorBinding(org.apache.openejb.jee.InterceptorBinding) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) 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)20 EjbJar (org.apache.openejb.jee.EjbJar)16 InterceptorBinding (org.apache.openejb.jee.InterceptorBinding)15 AssemblyDescriptor (org.apache.openejb.jee.AssemblyDescriptor)11 StatelessBean (org.apache.openejb.jee.StatelessBean)10 EnterpriseBean (org.apache.openejb.jee.EnterpriseBean)6 NamedMethod (org.apache.openejb.jee.NamedMethod)5 ArrayList (java.util.ArrayList)3 Module (org.apache.openejb.testing.Module)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