Search in sources :

Example 1 with DefaultInterceptorChain

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

the class MethodAsynchronousInterceptorServiceTest method test2.

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

                public Object invoke(InvocationContext context) throws Throwable {
                    Thread.sleep(500);
                    return "test";
                }
            }).invokeNext(new DefaultMethodInvocationContext(new Integer(100), Integer.class.getMethod("toString", (Class[]) null), null));
            fail();
        } catch (AsynchronousTimeoutException e) {
        }
    } 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) 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 2 with DefaultInterceptorChain

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

the class MethodAsynchronousInterceptorServiceTest method test4.

public void test4() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    final ServiceMetaData interceptorServiceData = new ServiceMetaData();
    interceptorServiceData.setCode(MethodAsynchronousInterceptorService.class.getName());
    interceptorServiceData.setName("MethodAsynchronousInterceptor");
    interceptorServiceData.addDepends(interceptorServiceData.createDependsMetaData("Test", "Queue"));
    ServiceManagerFactory.registerService("Test", interceptorServiceData);
    MethodAsynchronousInterceptorService interceptor = (MethodAsynchronousInterceptorService) ServiceManagerFactory.getService("Test", "MethodAsynchronousInterceptor");
    final ServiceMetaData queueServiceData = new ServiceMetaData();
    queueServiceData.setCode(DefaultQueueService.class.getName());
    queueServiceData.setName("Queue");
    queueServiceData.setInstance(ServiceMetaData.INSTANCE_TYPE_THREADLOCAL);
    ServiceManagerFactory.registerService("Test", queueServiceData);
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor.setResponseQueueServiceName(new ServiceName("Test", "Queue"));
        interceptor.setInvokerThreadSize(3);
        interceptor.setReturnResponse(false);
        ServiceManagerFactory.findManager("Test").startAllService();
        for (int i = 0; i < 3; i++) {
            Object ret = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

                public Object invoke(InvocationContext context) throws Throwable {
                    Thread.sleep(500);
                    return "test";
                }
            }).invokeNext(new DefaultMethodInvocationContext(new Integer(i), Integer.class.getMethod("toString", (Class[]) null), null));
            assertNull(ret);
        }
        Queue queue = (Queue) ServiceManagerFactory.getServiceObject("Test", "Queue");
        for (int i = 0; i < 3; i++) {
            AsynchronousResponse response = (AsynchronousResponse) queue.get();
            assertNotNull(response);
            assertEquals("test", response.getReturnObject());
        }
        assertNull(queue.get(500));
    } 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) DefaultQueueService(jp.ossc.nimbus.service.queue.DefaultQueueService) Invoker(jp.ossc.nimbus.service.aop.Invoker) ServiceName(jp.ossc.nimbus.core.ServiceName) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Queue(jp.ossc.nimbus.service.queue.Queue) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 3 with DefaultInterceptorChain

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

the class MethodJournalInterceptorServiceTest method test3.

public void test3() throws Throwable {
    AttributeMetaData attr = new AttributeMetaData(interceptorServiceData);
    attr.setName("Enabled");
    attr.setValue("false");
    interceptorServiceData.addAttribute(attr);
    ServiceManagerFactory.registerService("Test", interceptorServiceData);
    interceptor = (MethodJournalInterceptorService) ServiceManagerFactory.getService("Test", "MethodJournalInterceptor");
    ServiceManagerFactory.findManager("Test").createAllService();
    ServiceManagerFactory.findManager("Test").startAllService();
    final Context context = (Context) ServiceManagerFactory.getServiceObject("Test", "Context");
    context.put(ThreadContextKey.REQUEST_ID, "001");
    Map target = new HashMap();
    target.put("A", new Integer(100));
    Object ret = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

        public Object invoke(InvocationContext context) throws Throwable {
            MethodInvocationContext ctx = (MethodInvocationContext) context;
            return ctx.getTargetMethod().invoke(ctx.getTargetObject(), ctx.getParameters());
        }
    }).invokeNext(new DefaultMethodInvocationContext(target, HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
    assertEquals(ret, new Integer(100));
    synchronized (writer) {
        if (writer.record == null) {
            writer.wait(1000);
        }
    }
    assertNull(writer.record);
}
Also used : InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) MethodInvocationContext(jp.ossc.nimbus.service.aop.MethodInvocationContext) Context(jp.ossc.nimbus.service.context.Context) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) MethodInvocationContext(jp.ossc.nimbus.service.aop.MethodInvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) Invoker(jp.ossc.nimbus.service.aop.Invoker) AttributeMetaData(jp.ossc.nimbus.core.AttributeMetaData) HashMap(java.util.HashMap) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) MethodInvocationContext(jp.ossc.nimbus.service.aop.MethodInvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) HashMap(java.util.HashMap) Map(java.util.Map) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 4 with DefaultInterceptorChain

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

the class MethodJournalInterceptorServiceTest method test1.

public void test1() throws Throwable {
    ServiceManagerFactory.registerService("Test", interceptorServiceData);
    interceptor = (MethodJournalInterceptorService) ServiceManagerFactory.getService("Test", "MethodJournalInterceptor");
    ServiceManagerFactory.findManager("Test").createAllService();
    ServiceManagerFactory.findManager("Test").startAllService();
    final Context context = (Context) ServiceManagerFactory.getServiceObject("Test", "Context");
    context.put(ThreadContextKey.REQUEST_ID, "001");
    Map target = new HashMap();
    target.put("A", new Integer(100));
    Object ret = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

        public Object invoke(InvocationContext context) throws Throwable {
            MethodInvocationContext ctx = (MethodInvocationContext) context;
            return ctx.getTargetMethod().invoke(ctx.getTargetObject(), ctx.getParameters());
        }
    }).invokeNext(new DefaultMethodInvocationContext(target, HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
    assertEquals(ret, new Integer(100));
    synchronized (writer) {
        if (writer.record == null) {
            writer.wait(1000);
        }
    }
    assertNotNull(writer.record);
    assertEquals(3, writer.record.getElementMap().size());
    WritableElement element = (WritableElement) writer.record.getElementMap().get("REQUEST_ID");
    assertNotNull(element);
    assertEquals("001", element.toObject());
    element = (WritableElement) writer.record.getElementMap().get("CALL");
    assertNotNull(element);
    MethodCallJournalData callData = (MethodCallJournalData) element.toObject();
    assertNotNull(callData);
    assertEquals(HashMap.class, callData.getOwnerClass());
    assertEquals("get", callData.getName());
    assertNotNull(callData.getParameterTypes());
    assertEquals(1, callData.getParameterTypes().length);
    assertEquals(Object.class, callData.getParameterTypes()[0]);
    assertNotNull(callData.getParameters());
    assertEquals(1, callData.getParameters().length);
    assertEquals("A", callData.getParameters()[0]);
    element = (WritableElement) writer.record.getElementMap().get("RETURN");
    assertNotNull(element);
    MethodReturnJournalData retData = (MethodReturnJournalData) element.toObject();
    assertNotNull(retData);
    assertEquals(HashMap.class, retData.getOwnerClass());
    assertEquals("get", retData.getName());
    assertNotNull(retData.getParameterTypes());
    assertEquals(1, retData.getParameterTypes().length);
    assertEquals(Object.class, retData.getParameterTypes()[0]);
    assertEquals(new Integer(100), retData.getReturnValue());
}
Also used : InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) MethodInvocationContext(jp.ossc.nimbus.service.aop.MethodInvocationContext) Context(jp.ossc.nimbus.service.context.Context) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) MethodInvocationContext(jp.ossc.nimbus.service.aop.MethodInvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) HashMap(java.util.HashMap) MethodCallJournalData(jp.ossc.nimbus.service.journal.editor.MethodCallJournalData) Invoker(jp.ossc.nimbus.service.aop.Invoker) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) WritableElement(jp.ossc.nimbus.service.writer.WritableElement) MethodReturnJournalData(jp.ossc.nimbus.service.journal.editor.MethodReturnJournalData) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) MethodInvocationContext(jp.ossc.nimbus.service.aop.MethodInvocationContext) DefaultMethodInvocationContext(jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext) HashMap(java.util.HashMap) Map(java.util.Map) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 5 with DefaultInterceptorChain

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

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