Search in sources :

Example 1 with Context

use of jp.ossc.nimbus.service.context.Context 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 2 with Context

use of jp.ossc.nimbus.service.context.Context 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 3 with Context

use of jp.ossc.nimbus.service.context.Context in project nimbus by nimbus-org.

the class ContextExportInterceptorServiceTest method test1.

public void test1() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    ServiceManagerFactory.registerService("Test", "Context", new DefaultContextService());
    Interceptor interceptor1 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            Context ctx = (Context) ServiceManagerFactory.getServiceObject("Test", "Context");
            ctx.put("A", "100");
            ctx.put("B", "200");
            return chain.invokeNext(context);
        }
    };
    ContextExportInterceptorService interceptor2 = new ContextExportInterceptorService();
    ServiceManagerFactory.registerService("Test", "ContextExportInterceptor", interceptor2);
    Interceptor interceptor3 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            Map exportedContext = (Map) context.getAttribute(ContextExportInterceptorService.DEFAULT_ATTRIBUTE_NAME);
            assertEquals(2, exportedContext.size());
            assertEquals("100", exportedContext.get("A"));
            assertEquals("200", exportedContext.get("B"));
            return chain.invokeNext(context);
        }
    };
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor2.setContextServiceName(new ServiceName("Test", "Context"));
        ServiceManagerFactory.findManager("Test").startAllService();
        new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor1, interceptor2, interceptor3 }), null).invokeNext(new DefaultInvocationContext());
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInvocationContext(jp.ossc.nimbus.service.aop.DefaultInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) Context(jp.ossc.nimbus.service.context.Context) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) DefaultInvocationContext(jp.ossc.nimbus.service.aop.DefaultInvocationContext) ServiceName(jp.ossc.nimbus.core.ServiceName) DefaultContextService(jp.ossc.nimbus.service.context.DefaultContextService) DefaultInvocationContext(jp.ossc.nimbus.service.aop.DefaultInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) Map(java.util.Map) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 4 with Context

use of jp.ossc.nimbus.service.context.Context in project nimbus by nimbus-org.

the class ContextExportInterceptorServiceTest method test2.

public void test2() throws Throwable {
    ServiceManagerFactory.registerManager("Test");
    DefaultContextService contextService = new DefaultContextService();
    ServiceManagerFactory.registerService("Test", "Context", contextService);
    Interceptor interceptor1 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            Context ctx = (Context) ServiceManagerFactory.getServiceObject("Test", "Context");
            ctx.put("A", "100");
            ctx.put("B", "200");
            ctx.put("C", "300");
            return chain.invokeNext(context);
        }
    };
    ContextExportInterceptorService interceptor2 = new ContextExportInterceptorService();
    ServiceManagerFactory.registerService("Test", "ContextExportInterceptor", interceptor2);
    Interceptor interceptor3 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            Map exportedContext = (Map) context.getAttribute("Context");
            assertEquals(2, exportedContext.size());
            assertEquals("100", exportedContext.get("A"));
            assertEquals("200", exportedContext.get("B"));
            return chain.invokeNext(context);
        }
    };
    try {
        ServiceManagerFactory.findManager("Test").createAllService();
        interceptor2.setContext(contextService);
        interceptor2.setAttributeName("Context");
        interceptor2.setContextKeys(new String[] { "A", "B" });
        ServiceManagerFactory.findManager("Test").startAllService();
        new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor1, interceptor2, interceptor3 }), null).invokeNext(new DefaultInvocationContext());
    } finally {
        ServiceManagerFactory.findManager("Test").stopAllService();
        ServiceManagerFactory.findManager("Test").destroyAllService();
        ServiceManagerFactory.unregisterManager("Test");
    }
}
Also used : DefaultInvocationContext(jp.ossc.nimbus.service.aop.DefaultInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) Context(jp.ossc.nimbus.service.context.Context) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) DefaultInvocationContext(jp.ossc.nimbus.service.aop.DefaultInvocationContext) DefaultContextService(jp.ossc.nimbus.service.context.DefaultContextService) DefaultInvocationContext(jp.ossc.nimbus.service.aop.DefaultInvocationContext) InvocationContext(jp.ossc.nimbus.service.aop.InvocationContext) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) Map(java.util.Map) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 5 with Context

use of jp.ossc.nimbus.service.context.Context in project nimbus by nimbus-org.

the class DefaultFacadeCallService method setHeaderFromThreadContext.

protected void setHeaderFromThreadContext(FacadeValue value) {
    if (threadContextServiceName != null) {
        final Context context = (Context) ServiceManagerFactory.getServiceObject(threadContextServiceName);
        if (threadContextKeys == null) {
            final Iterator keys = context.keySet().iterator();
            while (keys.hasNext()) {
                final String key = (String) keys.next();
                value.putHeader(key, context.get(key));
            }
        } else {
            for (int i = 0; i < threadContextKeys.length; i++) {
                final String key = threadContextKeys[i];
                value.putHeader(key, context.get(key));
            }
        }
    }
}
Also used : Context(jp.ossc.nimbus.service.context.Context)

Aggregations

Context (jp.ossc.nimbus.service.context.Context)14 InvocationContext (jp.ossc.nimbus.service.aop.InvocationContext)10 DefaultInterceptorChain (jp.ossc.nimbus.service.aop.DefaultInterceptorChain)9 DefaultInterceptorChainList (jp.ossc.nimbus.service.aop.DefaultInterceptorChainList)9 Map (java.util.Map)8 HashMap (java.util.HashMap)7 Interceptor (jp.ossc.nimbus.service.aop.Interceptor)6 DefaultMethodInvocationContext (jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext)5 InterceptorChain (jp.ossc.nimbus.service.aop.InterceptorChain)5 MethodInvocationContext (jp.ossc.nimbus.service.aop.MethodInvocationContext)5 DefaultContextService (jp.ossc.nimbus.service.context.DefaultContextService)5 DefaultInvocationContext (jp.ossc.nimbus.service.aop.DefaultInvocationContext)4 Invoker (jp.ossc.nimbus.service.aop.Invoker)4 AttributeMetaData (jp.ossc.nimbus.core.AttributeMetaData)3 MethodCallJournalData (jp.ossc.nimbus.service.journal.editor.MethodCallJournalData)3 MethodReturnJournalData (jp.ossc.nimbus.service.journal.editor.MethodReturnJournalData)3 WritableElement (jp.ossc.nimbus.service.writer.WritableElement)3 ServiceName (jp.ossc.nimbus.core.ServiceName)2 Iterator (java.util.Iterator)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1