Search in sources :

Example 1 with Interceptor

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

the class FlowControlInterceptorServiceTest method test2.

public void test2() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    final ServiceMetaData interceptorServiceData = new ServiceMetaData();
    interceptorServiceData.setCode(FlowControlInterceptorService.class.getName());
    interceptorServiceData.setName("FlowControlInterceptor");
    interceptorServiceData.addDepends(interceptorServiceData.createDependsMetaData("Test", "Semaphore"));
    ServiceManagerFactory.registerService("Test", interceptorServiceData);
    FlowControlInterceptorService interceptor = (FlowControlInterceptorService) ServiceManagerFactory.getService("Test", "FlowControlInterceptor");
    final DefaultSemaphoreService semaphore = new DefaultSemaphoreService();
    ServiceManagerFactory.registerService("Test", "Semaphore", semaphore);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setSemaphoreServiceName(new ServiceName("Test", "Semaphore"));
        interceptor.setTimeout(200l);
        semaphore.setResourceCapacity(1);
        ServiceManagerFactory.findManager("Test").startAllService();
        final InterceptorChain chain = new DefaultThreadLocalInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                Thread.sleep(500);
                return "test";
            }
        });
        Runnable runner = new Runnable() {

            public void run() {
                try {
                    chain.invokeNext(new DefaultMethodInvocationContext());
                } catch (Throwable th) {
                }
            }
        };
        Thread thread = new Thread(runner);
        thread.start();
        Thread.sleep(100);
        try {
            chain.invokeNext(new DefaultMethodInvocationContext());
            fail();
        } catch (FailToObtainSemaphoreException e) {
        }
        thread.join();
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) ServiceMetaData(jp.ossc.nimbus.core.ServiceMetaData) DefaultThreadLocalInterceptorChain(jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain) DefaultThreadLocalInterceptorChain(jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) Invoker(jp.ossc.nimbus.service.aop.Invoker) ServiceName(jp.ossc.nimbus.core.ServiceName) DefaultSemaphoreService(jp.ossc.nimbus.service.semaphore.DefaultSemaphoreService) 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)

Example 2 with Interceptor

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

the class FlowControlInterceptorServiceTest method test1.

public void test1() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    final ServiceMetaData interceptorServiceData = new ServiceMetaData();
    interceptorServiceData.setCode(FlowControlInterceptorService.class.getName());
    interceptorServiceData.setName("FlowControlInterceptor");
    interceptorServiceData.addDepends(interceptorServiceData.createDependsMetaData("Test", "Semaphore"));
    ServiceManagerFactory.registerService("Test", interceptorServiceData);
    FlowControlInterceptorService interceptor = (FlowControlInterceptorService) ServiceManagerFactory.getService("Test", "FlowControlInterceptor");
    final DefaultSemaphoreService semaphore = new DefaultSemaphoreService();
    ServiceManagerFactory.registerService("Test", "Semaphore", semaphore);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setSemaphoreServiceName(new ServiceName("Test", "Semaphore"));
        semaphore.setResourceCapacity(2);
        ServiceManagerFactory.findManager("Test").startAllService();
        class Counter {

            public volatile int count;

            public boolean isAssertFail;
        }
        final Counter counter = new Counter();
        final InterceptorChain chain = new DefaultThreadLocalInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                try {
                    synchronized (counter) {
                        counter.count++;
                    }
                    Thread.sleep(new Random().nextInt(100));
                    return "test";
                } finally {
                    synchronized (counter) {
                        counter.count--;
                    }
                }
            }
        });
        Runnable runner = new Runnable() {

            public void run() {
                for (int i = 0; i < 10; i++) {
                    counter.isAssertFail |= counter.count > semaphore.getResourceCapacity();
                    try {
                        chain.invokeNext(new DefaultMethodInvocationContext());
                    } catch (Throwable th) {
                    }
                    counter.isAssertFail |= counter.count > semaphore.getResourceCapacity();
                }
            }
        };
        Thread[] threads = new Thread[10];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(runner);
        }
        Random random = new Random();
        for (int i = 0; i < threads.length; i++) {
            threads[i].start();
            Thread.sleep(random.nextInt(100));
        }
        for (int i = 0; i < threads.length; i++) {
            threads[i].join();
        }
        assertFalse(counter.isAssertFail);
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) ServiceMetaData(jp.ossc.nimbus.core.ServiceMetaData) DefaultThreadLocalInterceptorChain(jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain) DefaultThreadLocalInterceptorChain(jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) Invoker(jp.ossc.nimbus.service.aop.Invoker) Random(java.util.Random) ServiceName(jp.ossc.nimbus.core.ServiceName) DefaultSemaphoreService(jp.ossc.nimbus.service.semaphore.DefaultSemaphoreService) 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)

Example 3 with Interceptor

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

the class FlowControlInterceptorServiceTest method test3.

public void test3() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    final ServiceMetaData interceptorServiceData = new ServiceMetaData();
    interceptorServiceData.setCode(FlowControlInterceptorService.class.getName());
    interceptorServiceData.setName("FlowControlInterceptor");
    interceptorServiceData.addDepends(interceptorServiceData.createDependsMetaData("Test", "Semaphore"));
    ServiceManagerFactory.registerService("Test", interceptorServiceData);
    FlowControlInterceptorService interceptor = (FlowControlInterceptorService) ServiceManagerFactory.getService("Test", "FlowControlInterceptor");
    final DefaultSemaphoreService semaphore = new DefaultSemaphoreService();
    ServiceManagerFactory.registerService("Test", "Semaphore", semaphore);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setSemaphoreServiceName(new ServiceName("Test", "Semaphore"));
        interceptor.setTimeout(200l);
        interceptor.setFailToObtainSemaphore(false);
        semaphore.setResourceCapacity(1);
        ServiceManagerFactory.findManager("Test").startAllService();
        final InterceptorChain chain = new DefaultThreadLocalInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                Thread.sleep(500);
                return "test";
            }
        });
        Runnable runner = new Runnable() {

            public void run() {
                try {
                    chain.invokeNext(new DefaultMethodInvocationContext());
                } catch (Throwable th) {
                }
            }
        };
        Thread thread = new Thread(runner);
        thread.start();
        Thread.sleep(100);
        assertNull(chain.invokeNext(new DefaultMethodInvocationContext()));
        thread.join();
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) ServiceMetaData(jp.ossc.nimbus.core.ServiceMetaData) DefaultThreadLocalInterceptorChain(jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain) DefaultThreadLocalInterceptorChain(jp.ossc.nimbus.service.aop.DefaultThreadLocalInterceptorChain) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) Invoker(jp.ossc.nimbus.service.aop.Invoker) ServiceName(jp.ossc.nimbus.core.ServiceName) DefaultSemaphoreService(jp.ossc.nimbus.service.semaphore.DefaultSemaphoreService) 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)

Example 4 with Interceptor

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

the class MethodMappingInterceptorServiceTest method test2.

public void test2() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    ServiceMetaData serviceData = new ServiceMetaData();
    serviceData.setCode(MethodMappingInterceptorService.class.getName());
    serviceData.setName("MethodMappingInterceptor");
    AttributeMetaData attr = new AttributeMetaData(serviceData);
    attr.setName("TargetMethodMapping");
    attr.setValue("java\\.util\\..*#get(java.lang.Object)=#Interceptor2\n" + "java\\.util\\..*#put(java.lang.Object, java.lang.Object)=#Interceptor3");
    serviceData.addAttribute(attr);
    ServiceManagerFactory.registerService("Test", serviceData);
    Interceptor interceptor1 = (Interceptor) ServiceManagerFactory.getServiceObject("Test", "MethodMappingInterceptor");
    Interceptor interceptor2 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            return this;
        }
    };
    ServiceManagerFactory.registerService("Test", "Interceptor2", interceptor2);
    Interceptor interceptor3 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            return this;
        }
    };
    ServiceManagerFactory.registerService("Test", "Interceptor3", interceptor3);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        ServiceManagerFactory.findManager("Test").startAllService();
        assertEquals(interceptor2, new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor1 }), null).invokeNext(new DefaultMethodInvocationContext(null, HashMap.class.getMethod("get", new Class[] { Object.class }), null)));
        assertEquals(interceptor3, new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor1 }), null).invokeNext(new DefaultMethodInvocationContext(null, HashMap.class.getMethod("put", new Class[] { Object.class, Object.class }), null)));
    } 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) AttributeMetaData(jp.ossc.nimbus.core.AttributeMetaData) HashMap(java.util.HashMap) ServiceMetaData(jp.ossc.nimbus.core.ServiceMetaData) 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 5 with Interceptor

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

the class MethodSynchronizeInterceptorServiceTest method test2.

public void test2() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    MethodSynchronizeInterceptorService interceptor = new MethodSynchronizeInterceptorService();
    ServiceManagerFactory.registerService("Test", "MethodSynchronizeInterceptor", interceptor);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setScope(MethodSynchronizeInterceptorService.SCOPE_CLASS);
        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";
            }
        });
        Thread thread = new Thread() {

            public void run() {
                try {
                    chain1.invokeNext(new DefaultMethodInvocationContext(new HashMap(), HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
                } catch (Throwable th) {
                }
            }
        };
        thread.start();
        Thread.sleep(100);
        chain2.invokeNext(new DefaultMethodInvocationContext(new ArrayList(), ArrayList.class.getMethod("add", new Class[] { Object.class }), new Object[] { "A" }));
        chain3.invokeNext(new DefaultMethodInvocationContext(new HashMap(), HashMap.class.getMethod("put", new Class[] { Object.class, Object.class }), new Object[] { "A", new Integer(1) }));
    } 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) ArrayList(java.util.ArrayList) 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)

Aggregations

DefaultInterceptorChainList (jp.ossc.nimbus.service.aop.DefaultInterceptorChainList)37 Interceptor (jp.ossc.nimbus.service.aop.Interceptor)37 InvocationContext (jp.ossc.nimbus.service.aop.InvocationContext)35 DefaultInterceptorChain (jp.ossc.nimbus.service.aop.DefaultInterceptorChain)34 DefaultMethodInvocationContext (jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext)32 InterceptorChain (jp.ossc.nimbus.service.aop.InterceptorChain)31 Invoker (jp.ossc.nimbus.service.aop.Invoker)25 HashMap (java.util.HashMap)14 ServiceName (jp.ossc.nimbus.core.ServiceName)8 Map (java.util.Map)7 ServiceMetaData (jp.ossc.nimbus.core.ServiceMetaData)7 Properties (java.util.Properties)6 Context (jp.ossc.nimbus.service.context.Context)6 Method (java.lang.reflect.Method)5 AttributeMetaData (jp.ossc.nimbus.core.AttributeMetaData)5 DefaultInvocationContext (jp.ossc.nimbus.service.aop.DefaultInvocationContext)5 DefaultContextService (jp.ossc.nimbus.service.context.DefaultContextService)5 Random (java.util.Random)4 MethodReflectionCallInvokerService (jp.ossc.nimbus.service.aop.invoker.MethodReflectionCallInvokerService)4 ArrayList (java.util.ArrayList)3