Search in sources :

Example 21 with Interceptor

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

the class MethodMappingInterceptorServiceTest method test3.

public void test3() 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(*)=#Interceptor2\n" + "java\\.util\\..*#.*(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(interceptor2, new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor1 }), null).invokeNext(new DefaultMethodInvocationContext(null, ArrayList.class.getMethod("get", new Class[] { Integer.TYPE }), 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 : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) AttributeMetaData(jp.ossc.nimbus.core.AttributeMetaData) HashMap(java.util.HashMap) ServiceMetaData(jp.ossc.nimbus.core.ServiceMetaData) ArrayList(java.util.ArrayList) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) 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 22 with Interceptor

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

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

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

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

the class MethodMetricsInterceptorServiceTest method test6.

public void test6() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    MethodMetricsInterceptorService interceptor = new MethodMetricsInterceptorService();
    ServiceManagerFactory.registerService("Test", "MethodMetricsInterceptor", interceptor);
    class TestCategory implements Category {

        public Object elements;

        public boolean isEnabled() {
            return true;
        }

        public void setEnabled(boolean enable) {
        }

        public void write(Object elements) {
            this.elements = elements;
            synchronized (this) {
                this.notify();
            }
        }
    }
    ;
    TestCategory category1 = new TestCategory();
    ServiceManagerFactory.registerService("Test", "Category1", category1);
    TestCategory category2 = new TestCategory();
    ServiceManagerFactory.registerService("Test", "Category2", category2);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        Properties mapping = new Properties();
        mapping.setProperty("java.util.HashMap#get(java.lang.Object)", "Test#Category1");
        mapping.setProperty("java.util.HashMap#put(java.lang.Object,java.lang.Object)", "Test#Category2");
        interceptor.setMethodAndCategoryServiceNameMapping(mapping);
        interceptor.setDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
        interceptor.setOutputInterval(1000L);
        interceptor.setOutputCount(true);
        interceptor.setOutputExceptionCount(true);
        interceptor.setOutputErrorCount(true);
        interceptor.setOutputLastTime(true);
        interceptor.setOutputLastExceptionTime(true);
        interceptor.setOutputLastErrorTime(true);
        interceptor.setOutputBestPerformance(true);
        interceptor.setOutputBestPerformanceTime(true);
        interceptor.setOutputWorstPerformance(true);
        interceptor.setOutputWorstPerformanceTime(true);
        interceptor.setOutputAveragePerformance(true);
        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)));
        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("put", new Class[] { Object.class, Object.class }), null));
            fail();
        } catch (IllegalArgumentException e) {
        }
        synchronized (category1) {
            if (category1.elements == null) {
                category1.wait(2000);
            }
        }
        assertNotNull(category1.elements);
        assertTrue(category1.elements instanceof Map);
        Map record = (Map) category1.elements;
        assertEquals(12, record.size());
        assertEquals("java.util.HashMap#get(java.lang.Object)", record.get(MethodMetricsInterceptorService.RECORD_KEY_METHOD));
        assertEquals(new Long(1), record.get(MethodMetricsInterceptorService.RECORD_KEY_COUNT));
        assertEquals(new Long(0), record.get(MethodMetricsInterceptorService.RECORD_KEY_EXCEPTION_COUNT));
        assertEquals(new Long(0), record.get(MethodMetricsInterceptorService.RECORD_KEY_ERROR_COUNT));
        assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_TIME));
        assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_TIME)) > 0);
        assertNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_EXCEPTION_TIME));
        assertNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_ERROR_TIME));
        assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE));
        assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE)) < 0);
        assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE_TIME));
        assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE_TIME)) > 0);
        assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE));
        assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE)) < 0);
        assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE_TIME));
        assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE_TIME)) > 0);
        assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_AVERAGE_PERFORMANCE));
        assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_AVERAGE_PERFORMANCE)) < 0);
        synchronized (category2) {
            if (category2.elements == null) {
                category2.wait(2000);
            }
        }
        assertNotNull(category2.elements);
        assertTrue(category2.elements instanceof Map);
        record = (Map) category2.elements;
        assertEquals(12, record.size());
        assertEquals("java.util.HashMap#put(java.lang.Object,java.lang.Object)", record.get(MethodMetricsInterceptorService.RECORD_KEY_METHOD));
        assertEquals(new Long(0), record.get(MethodMetricsInterceptorService.RECORD_KEY_COUNT));
        assertEquals(new Long(1), record.get(MethodMetricsInterceptorService.RECORD_KEY_EXCEPTION_COUNT));
        assertEquals(new Long(0), record.get(MethodMetricsInterceptorService.RECORD_KEY_ERROR_COUNT));
        assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_TIME));
        assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_TIME)) > 0);
        assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_EXCEPTION_TIME));
        assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_EXCEPTION_TIME)) > 0);
        assertNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_LAST_ERROR_TIME));
        assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE));
        assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE)) < 0);
        assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE_TIME));
        assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_BEST_PERFORMANCE_TIME)) > 0);
        assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE));
        assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE)) < 0);
        assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE_TIME));
        assertTrue(new Date().compareTo((Date) record.get(MethodMetricsInterceptorService.RECORD_KEY_WORST_PERFORMANCE_TIME)) > 0);
        assertNotNull(record.get(MethodMetricsInterceptorService.RECORD_KEY_AVERAGE_PERFORMANCE));
        assertTrue(new Long(0).compareTo((Long) record.get(MethodMetricsInterceptorService.RECORD_KEY_AVERAGE_PERFORMANCE)) < 0);
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) Category(jp.ossc.nimbus.service.writer.Category) HashMap(java.util.HashMap) Properties(java.util.Properties) Date(java.util.Date) 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) HashMap(java.util.HashMap) Map(java.util.Map) 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