Search in sources :

Example 11 with Invoker

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

the class MethodSynchronizeInterceptorServiceTest method test4.

public void test4() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    MethodSynchronizeInterceptorService interceptor = new MethodSynchronizeInterceptorService();
    ServiceManagerFactory.registerService("Test", "MethodSynchronizeInterceptor", interceptor);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setScope(MethodSynchronizeInterceptorService.SCOPE_INSTANCE);
        ServiceManagerFactory.findManager("Test").startAllService();
        class Counter {

            public volatile int count;
        }
        final Counter counter = new Counter();
        final InterceptorChain chain1 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                try {
                    counter.count++;
                    Thread.sleep(500);
                    return "test";
                } finally {
                    counter.count--;
                }
            }
        });
        final InterceptorChain chain2 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                assertEquals(1, counter.count);
                return "test";
            }
        });
        final InterceptorChain chain3 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                assertEquals(0, counter.count);
                return "test";
            }
        });
        final HashMap target = new HashMap();
        Thread thread = new Thread() {

            public void run() {
                try {
                    chain1.invokeNext(new DefaultMethodInvocationContext(target, HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
                } catch (Throwable th) {
                }
            }
        };
        thread.start();
        Thread.sleep(100);
        chain2.invokeNext(new DefaultMethodInvocationContext(new HashMap(), HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
        chain3.invokeNext(new DefaultMethodInvocationContext(target, HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) HashMap(java.util.HashMap) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) 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 12 with Invoker

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

the class NoCalledMethodMetricsInterceptorServiceTest method test1.

public void test1() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    NoCalledMethodMetricsInterceptorService interceptor = new NoCalledMethodMetricsInterceptorService();
    ServiceManagerFactory.registerService("Test", "NoCalledMethodMetricsInterceptor", interceptor);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setOutputSystemOut(false);
        interceptor.setTargetClassName("jp\\.ossc\\.nimbus\\..*");
        ServiceManagerFactory.findManager("Test").startAllService();
        final InterceptorChain chain = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                return null;
            }
        });
        Method method = StringOperator.class.getMethod("makeSpace", new Class[] { Integer.TYPE });
        assertNotNull(interceptor.getNoCalledMethodSet());
        assertTrue(interceptor.getNoCalledMethodSet().contains(method));
        chain.invokeNext(new DefaultMethodInvocationContext(null, method, new Object[] { new Integer(1) }));
        assertNotNull(interceptor.getNoCalledMethodSet());
        assertFalse(interceptor.getNoCalledMethodSet().contains(method));
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) Invoker(jp.ossc.nimbus.service.aop.Invoker) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Method(java.lang.reflect.Method) 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 13 with Invoker

use of jp.ossc.nimbus.service.aop.Invoker 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 Invoker

use of jp.ossc.nimbus.service.aop.Invoker 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 Invoker

use of jp.ossc.nimbus.service.aop.Invoker 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

DefaultInterceptorChainList (jp.ossc.nimbus.service.aop.DefaultInterceptorChainList)36 InvocationContext (jp.ossc.nimbus.service.aop.InvocationContext)36 Invoker (jp.ossc.nimbus.service.aop.Invoker)36 DefaultMethodInvocationContext (jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext)35 DefaultInterceptorChain (jp.ossc.nimbus.service.aop.DefaultInterceptorChain)33 Interceptor (jp.ossc.nimbus.service.aop.Interceptor)25 InterceptorChain (jp.ossc.nimbus.service.aop.InterceptorChain)20 HashMap (java.util.HashMap)14 Random (java.util.Random)7 Map (java.util.Map)6 Properties (java.util.Properties)6 ServiceName (jp.ossc.nimbus.core.ServiceName)5 ServiceMetaData (jp.ossc.nimbus.core.ServiceMetaData)4 MethodInvocationContext (jp.ossc.nimbus.service.aop.MethodInvocationContext)4 Context (jp.ossc.nimbus.service.context.Context)4 Date (java.util.Date)3 DefaultThreadLocalInterceptorChain (jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain)3 MethodCallJournalData (jp.ossc.nimbus.service.journal.editor.MethodCallJournalData)3 MethodReturnJournalData (jp.ossc.nimbus.service.journal.editor.MethodReturnJournalData)3 DefaultSemaphoreService (jp.ossc.nimbus.service.semaphore.DefaultSemaphoreService)3