Search in sources :

Example 1 with DefaultContextService

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

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

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

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

the class ContextImportInterceptorServiceTest 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 {
            Map importContext = new HashMap();
            importContext.put("A", "100");
            importContext.put("B", "200");
            context.setAttribute("Context", importContext);
            return chain.invokeNext(context);
        }
    };
    ContextImportInterceptorService interceptor2 = new ContextImportInterceptorService();
    ServiceManagerFactory.registerService("Test", "ContextImportInterceptor", interceptor2);
    Interceptor interceptor3 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            Context ctx = (Context) ServiceManagerFactory.getServiceObject("Test", "Context");
            assertEquals(2, ctx.size());
            assertEquals("100", ctx.get("A"));
            assertEquals("200", ctx.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) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) DefaultInvocationContext(jp.ossc.nimbus.service.aop.DefaultInvocationContext) HashMap(java.util.HashMap) 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) HashMap(java.util.HashMap) Map(java.util.Map) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 5 with DefaultContextService

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

the class ContextImportInterceptorServiceTest 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 {
            Map importContext = new HashMap();
            importContext.put("A", "100");
            importContext.put("B", "200");
            context.setAttribute(ContextImportInterceptorService.DEFAULT_ATTRIBUTE_NAME, importContext);
            return chain.invokeNext(context);
        }
    };
    ContextImportInterceptorService interceptor2 = new ContextImportInterceptorService();
    ServiceManagerFactory.registerService("Test", "ContextImportInterceptor", interceptor2);
    Interceptor interceptor3 = new Interceptor() {

        public Object invoke(InvocationContext context, InterceptorChain chain) throws Throwable {
            Context ctx = (Context) ServiceManagerFactory.getServiceObject("Test", "Context");
            assertEquals(2, ctx.size());
            assertEquals("100", ctx.get("A"));
            assertEquals("200", ctx.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) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) DefaultInterceptorChainList(jp.ossc.nimbus.service.aop.DefaultInterceptorChainList) DefaultInvocationContext(jp.ossc.nimbus.service.aop.DefaultInvocationContext) HashMap(java.util.HashMap) 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) HashMap(java.util.HashMap) Map(java.util.Map) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Aggregations

DefaultInterceptorChain (jp.ossc.nimbus.service.aop.DefaultInterceptorChain)5 DefaultInterceptorChainList (jp.ossc.nimbus.service.aop.DefaultInterceptorChainList)5 Interceptor (jp.ossc.nimbus.service.aop.Interceptor)5 InvocationContext (jp.ossc.nimbus.service.aop.InvocationContext)5 Context (jp.ossc.nimbus.service.context.Context)5 DefaultContextService (jp.ossc.nimbus.service.context.DefaultContextService)5 Map (java.util.Map)4 DefaultInvocationContext (jp.ossc.nimbus.service.aop.DefaultInvocationContext)4 InterceptorChain (jp.ossc.nimbus.service.aop.InterceptorChain)4 HashMap (java.util.HashMap)3 ServiceName (jp.ossc.nimbus.core.ServiceName)2 AttributeMetaData (jp.ossc.nimbus.core.AttributeMetaData)1 ServiceMetaData (jp.ossc.nimbus.core.ServiceMetaData)1 DefaultMethodInvocationContext (jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext)1