Search in sources :

Example 26 with DefaultInterceptorChain

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

the class MethodMappingInterceptorServiceTest method test4.

public void test4() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    ServiceMetaData serviceData = new ServiceMetaData();
    serviceData.setCode(MethodMappingInterceptorService.class.getName());
    serviceData.setName("MethodMappingInterceptor");
    AttributeMetaData attr = new AttributeMetaData(serviceData);
    attr.setName("TargetMethodReturnMapping");
    attr.setValue("java.util.HashMap#get(java.lang.Object)=A\n" + "java.util.HashMap#put(java.lang.Object, java.lang.Object)=B");
    serviceData.addAttribute(attr);
    attr = new AttributeMetaData(serviceData);
    attr.setName("ContextServiceName");
    attr.setValue("#Context");
    serviceData.addAttribute(attr);
    ServiceManagerFactory.registerService("Test", serviceData);
    Interceptor interceptor1 = (Interceptor) ServiceManagerFactory.getServiceObject("Test", "MethodMappingInterceptor");
    Context context = new DefaultContextService();
    ServiceManagerFactory.registerService("Test", "Context", context);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        ServiceManagerFactory.findManager("Test").startAllService();
        context.put("A", new Integer(100));
        context.put("B", new Integer(200));
        assertEquals(new Integer(100), new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor1 }), null).invokeNext(new DefaultMethodInvocationContext(null, HashMap.class.getMethod("get", new Class[] { Object.class }), null)));
        assertEquals(new Integer(200), 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 : InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) Context(jp.ossc.nimbus.service.context.Context) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) AttributeMetaData(jp.ossc.nimbus.core.AttributeMetaData) HashMap(java.util.HashMap) DefaultContextService(jp.ossc.nimbus.service.context.DefaultContextService) ServiceMetaData(jp.ossc.nimbus.core.ServiceMetaData) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 27 with DefaultInterceptorChain

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

the class MethodMappingInterceptorServiceTest method test1.

public void test1() 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.HashMap#get(java.lang.Object)=#Interceptor2\n" + "java.util.HashMap#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 28 with DefaultInterceptorChain

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

the class MethodMetricsInterceptorServiceTest method test2.

public void test2() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    MethodMetricsInterceptorService interceptor = new MethodMetricsInterceptorService();
    ServiceManagerFactory.registerService("Test", "MethodMetricsInterceptor", interceptor);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        ServiceManagerFactory.findManager("Test").startAllService();
        try {
            new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

                public Object invoke(InvocationContext context) throws Throwable {
                    Thread.sleep(new Random().nextInt(400) + 100);
                    throw new IllegalArgumentException();
                }
            }).invokeNext(new DefaultMethodInvocationContext(null, HashMap.class.getMethod("get", new Class[] { Object.class }), null));
            fail();
        } catch (IllegalArgumentException e) {
        }
        MetricsInfo info = interceptor.getMetricsInfo(HashMap.class.getMethod("get", new Class[] { Object.class }));
        assertNotNull(info);
        assertEquals("java.util.HashMap#get(java.lang.Object)", info.getKey());
        assertEquals(1L, info.getTotalCount());
        assertEquals(0L, info.getCount());
        assertTrue(info.getLastTime() > 0);
        assertEquals(1L, info.getExceptionCount());
        assertTrue(info.getLastExceptionTime() > 0);
        assertEquals(0L, info.getErrorCount());
        assertEquals(0L, info.getLastErrorTime());
        assertTrue(info.getBestPerformance() > 0);
        assertTrue(info.getBestPerformanceTime() > 0);
        assertTrue(info.getWorstPerformance() > 0);
        assertTrue(info.getWorstPerformanceTime() > 0);
        assertTrue(info.getAveragePerformance() > 0);
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) Invoker(jp.ossc.nimbus.service.aop.Invoker) Random(java.util.Random) HashMap(java.util.HashMap) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 29 with DefaultInterceptorChain

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

the class MethodMetricsInterceptorServiceTest method test1.

public void test1() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    MethodMetricsInterceptorService interceptor = new MethodMetricsInterceptorService();
    ServiceManagerFactory.registerService("Test", "MethodMetricsInterceptor", interceptor);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        ServiceManagerFactory.findManager("Test").startAllService();
        assertEquals("test", new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                Thread.sleep(new Random().nextInt(400) + 100);
                return "test";
            }
        }).invokeNext(new DefaultMethodInvocationContext(null, HashMap.class.getMethod("get", new Class[] { Object.class }), null)));
        MetricsInfo info = interceptor.getMetricsInfo(HashMap.class.getMethod("get", new Class[] { Object.class }));
        assertNotNull(info);
        assertEquals("java.util.HashMap#get(java.lang.Object)", info.getKey());
        assertEquals(1L, info.getTotalCount());
        assertEquals(1L, info.getCount());
        assertTrue(info.getLastTime() > 0);
        assertEquals(0L, info.getExceptionCount());
        assertEquals(0L, info.getLastExceptionTime());
        assertEquals(0L, info.getErrorCount());
        assertEquals(0L, info.getLastErrorTime());
        assertTrue(info.getBestPerformance() > 0);
        assertTrue(info.getBestPerformanceTime() > 0);
        assertTrue(info.getWorstPerformance() > 0);
        assertTrue(info.getWorstPerformanceTime() > 0);
        assertTrue(info.getAveragePerformance() > 0);
        interceptor.reset();
        info = interceptor.getMetricsInfo(HashMap.class.getMethod("get", new Class[] { Object.class }));
        assertNull(info);
    } 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) Invoker(jp.ossc.nimbus.service.aop.Invoker) Random(java.util.Random) 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 30 with DefaultInterceptorChain

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

the class MethodMetricsInterceptorServiceTest method test4.

public void test4() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    MethodMetricsInterceptorService interceptor = new MethodMetricsInterceptorService();
    ServiceManagerFactory.registerService("Test", "MethodMetricsInterceptor", interceptor);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setCalculateOnlyNormal(true);
        ServiceManagerFactory.findManager("Test").startAllService();
        try {
            new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

                public Object invoke(InvocationContext context) throws Throwable {
                    Thread.sleep(new Random().nextInt(400) + 100);
                    throw new IllegalArgumentException();
                }
            }).invokeNext(new DefaultMethodInvocationContext(null, HashMap.class.getMethod("get", new Class[] { Object.class }), null));
            fail();
        } catch (IllegalArgumentException e) {
        }
        MetricsInfo info = interceptor.getMetricsInfo(HashMap.class.getMethod("get", new Class[] { Object.class }));
        assertNotNull(info);
        assertEquals("java.util.HashMap#get(java.lang.Object)", info.getKey());
        assertEquals(1L, info.getTotalCount());
        assertEquals(0L, info.getCount());
        assertTrue(info.getLastTime() > 0);
        assertEquals(1L, info.getExceptionCount());
        assertTrue(info.getLastExceptionTime() > 0);
        assertEquals(0L, info.getErrorCount());
        assertEquals(0L, info.getLastErrorTime());
        assertEquals(Long.MAX_VALUE, info.getBestPerformance());
        assertEquals(0L, info.getBestPerformanceTime());
        assertEquals(Long.MIN_VALUE, info.getWorstPerformance());
        assertEquals(0L, info.getWorstPerformanceTime());
        assertEquals(0L, info.getAveragePerformance());
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) Invoker(jp.ossc.nimbus.service.aop.Invoker) Random(java.util.Random) HashMap(java.util.HashMap) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) 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