Search in sources :

Example 11 with DefaultInterceptorChain

use of jp.ossc.nimbus.service.aop.DefaultInterceptorChain in project nimbus by nimbus-org.

the class ContextExportInterceptorServiceTest method test1.

public void test1() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    ServiceManagerFactory.registerService("Test", "Context", new DefaultContextService());
    Interceptor interceptor1 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            Context ctx = (Context) ServiceManagerFactory.getServiceObject("Test", "Context");
            ctx.put("A", "100");
            ctx.put("B", "200");
            return chain.invokeNext(context);
        }
    };
    ContextExportInterceptorService interceptor2 = new ContextExportInterceptorService();
    ServiceManagerFactory.registerService("Test", "ContextExportInterceptor", interceptor2);
    Interceptor interceptor3 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            Map exportedContext = (Map) context.getAttribute(ContextExportInterceptorService.DEFAULT_ATTRIBUTE_NAME);
            assertEquals(2, exportedContext.size());
            assertEquals("100", exportedContext.get("A"));
            assertEquals("200", exportedContext.get("B"));
            return chain.invokeNext(context);
        }
    };
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor2.setContextServiceName(new ServiceName("Test", "Context"));
        ServiceManagerFactory.findManager("Test").startAllService();
        new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor1, interceptor2, interceptor3 }), null).invokeNext(new DefaultInvocationContext());
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInvocationContext(jp.ossc.nimbus.service.aop.DefaultInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) Context(jp.ossc.nimbus.service.context.Context) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) DefaultInvocationContext(jp.ossc.nimbus.service.aop.DefaultInvocationContext) ServiceName(jp.ossc.nimbus.core.ServiceName) DefaultContextService(jp.ossc.nimbus.service.context.DefaultContextService) DefaultInvocationContext(jp.ossc.nimbus.service.aop.DefaultInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) Map(java.util.Map) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 12 with DefaultInterceptorChain

use of jp.ossc.nimbus.service.aop.DefaultInterceptorChain in project nimbus by nimbus-org.

the class ContextExportInterceptorServiceTest method test2.

public void test2() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    DefaultContextService contextService = new DefaultContextService();
    ServiceManagerFactory.registerService("Test", "Context", contextService);
    Interceptor interceptor1 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            Context ctx = (Context) ServiceManagerFactory.getServiceObject("Test", "Context");
            ctx.put("A", "100");
            ctx.put("B", "200");
            ctx.put("C", "300");
            return chain.invokeNext(context);
        }
    };
    ContextExportInterceptorService interceptor2 = new ContextExportInterceptorService();
    ServiceManagerFactory.registerService("Test", "ContextExportInterceptor", interceptor2);
    Interceptor interceptor3 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            Map exportedContext = (Map) context.getAttribute("Context");
            assertEquals(2, exportedContext.size());
            assertEquals("100", exportedContext.get("A"));
            assertEquals("200", exportedContext.get("B"));
            return chain.invokeNext(context);
        }
    };
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor2.setContext(contextService);
        interceptor2.setAttributeName("Context");
        interceptor2.setContextKeys(new String[] { "A", "B" });
        ServiceManagerFactory.findManager("Test").startAllService();
        new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor1, interceptor2, interceptor3 }), null).invokeNext(new DefaultInvocationContext());
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInvocationContext(jp.ossc.nimbus.service.aop.DefaultInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) Context(jp.ossc.nimbus.service.context.Context) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) DefaultInvocationContext(jp.ossc.nimbus.service.aop.DefaultInvocationContext) DefaultContextService(jp.ossc.nimbus.service.context.DefaultContextService) DefaultInvocationContext(jp.ossc.nimbus.service.aop.DefaultInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) Map(java.util.Map) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 13 with DefaultInterceptorChain

use of jp.ossc.nimbus.service.aop.DefaultInterceptorChain in project nimbus by nimbus-org.

the class ExceptionConsumeInterceptorServiceTest method test4.

public void test4() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    ExceptionConsumeInterceptorService interceptor1 = new ExceptionConsumeInterceptorService();
    ServiceManagerFactory.registerService("Test", "ExceptionConsumeInterceptor", interceptor1);
    Interceptor interceptor2 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            throw new RuntimeException();
        }
    };
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor1.setExceptionClassNames(new String[] { "java.lang.IllegalArgumentException", "java.lang.UnsupportedOperationException" });
        ServiceManagerFactory.findManager("Test").startAllService();
        try {
            new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor1, interceptor2 }), new Invoker() {

                public Object invoke(InvocationContext context) throws Throwable {
                    return "test";
                }
            }).invokeNext(new DefaultMethodInvocationContext());
            fail();
        } catch (RuntimeException e) {
            assertEquals(RuntimeException.class, e.getClass());
        }
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) Invoker(jp.ossc.nimbus.service.aop.Invoker) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 14 with DefaultInterceptorChain

use of jp.ossc.nimbus.service.aop.DefaultInterceptorChain in project nimbus by nimbus-org.

the class ExceptionConsumeInterceptorServiceTest method test6.

public void test6() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    ExceptionConsumeInterceptorService interceptor1 = new ExceptionConsumeInterceptorService();
    ServiceManagerFactory.registerService("Test", "ExceptionConsumeInterceptor", interceptor1);
    Interceptor interceptor2 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            throw new IllegalArgumentException();
        }
    };
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor1.setExceptionClassNames(new String[] { "java.lang.IllegalArgumentException", "java.lang.UnsupportedOperationException" });
        interceptor1.setReturnValue("-1");
        ServiceManagerFactory.findManager("Test").startAllService();
        Object ret = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor1, interceptor2 }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                return "test";
            }
        }).invokeNext(new DefaultMethodInvocationContext("", String.class.getMethod("indexOf", new Class[] { String.class }), new Object[] { "fuga" }));
        assertEquals(new Integer(-1), ret);
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) Invoker(jp.ossc.nimbus.service.aop.Invoker) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 15 with DefaultInterceptorChain

use of jp.ossc.nimbus.service.aop.DefaultInterceptorChain in project nimbus by nimbus-org.

the class ExceptionConsumeInterceptorServiceTest method test2.

public void test2() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    ExceptionConsumeInterceptorService interceptor1 = new ExceptionConsumeInterceptorService();
    ServiceManagerFactory.registerService("Test", "ExceptionConsumeInterceptor", interceptor1);
    Interceptor interceptor2 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            throw new UnsupportedOperationException();
        }
    };
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor1.setExceptionClassNames(new String[] { "java.lang.IllegalArgumentException", "java.lang.UnsupportedOperationException" });
        ServiceManagerFactory.findManager("Test").startAllService();
        Object ret = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor1, interceptor2 }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                return "test";
            }
        }).invokeNext(new DefaultMethodInvocationContext());
        assertNull(ret);
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) Invoker(jp.ossc.nimbus.service.aop.Invoker) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Aggregations

DefaultInterceptorChain (jp.ossc.nimbus.service.aop.DefaultInterceptorChain)47 DefaultInterceptorChainList (jp.ossc.nimbus.service.aop.DefaultInterceptorChainList)45 InvocationContext (jp.ossc.nimbus.service.aop.InvocationContext)43 DefaultMethodInvocationContext (jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext)40 Interceptor (jp.ossc.nimbus.service.aop.Interceptor)34 Invoker (jp.ossc.nimbus.service.aop.Invoker)33 InterceptorChain (jp.ossc.nimbus.service.aop.InterceptorChain)28 HashMap (java.util.HashMap)20 Map (java.util.Map)10 Context (jp.ossc.nimbus.service.context.Context)9 Properties (java.util.Properties)6 Random (java.util.Random)6 AttributeMetaData (jp.ossc.nimbus.core.AttributeMetaData)6 ServiceName (jp.ossc.nimbus.core.ServiceName)6 MethodInvocationContext (jp.ossc.nimbus.service.aop.MethodInvocationContext)6 Method (java.lang.reflect.Method)5 ServiceMetaData (jp.ossc.nimbus.core.ServiceMetaData)5 DefaultInvocationContext (jp.ossc.nimbus.service.aop.DefaultInvocationContext)5 DefaultContextService (jp.ossc.nimbus.service.context.DefaultContextService)5 MethodReflectionCallInvokerService (jp.ossc.nimbus.service.aop.invoker.MethodReflectionCallInvokerService)4