Search in sources :

Example 1 with MethodCallJournalData

use of jp.ossc.nimbus.service.journal.editor.MethodCallJournalData 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 2 with MethodCallJournalData

use of jp.ossc.nimbus.service.journal.editor.MethodCallJournalData in project nimbus by nimbus-org.

the class MethodJournalInterceptorServiceTest method test4.

public void test4() throws Throwable {
    AttributeMetaData attr = new AttributeMetaData(interceptorServiceData);
    attr.setName("BushingCallBlock");
    attr.setValue("true");
    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 Interceptor() {

        public Object invoke(InvocationContext ctx, InterceptorChain chain) throws Throwable {
            ctx.setAttribute("chain", chain);
            return chain.invokeNext(ctx);
        }
    } }), new Invoker() {

        public Object invoke(InvocationContext ctx) throws Throwable {
            String reqId = (String) context.get(ThreadContextKey.REQUEST_ID);
            if ("001".equals(reqId)) {
                context.put(ThreadContextKey.REQUEST_ID, "002");
                final InterceptorChain chain = (InterceptorChain) ctx.getAttribute("chain");
                final InterceptorChain newChain = chain.cloneChain();
                newChain.setCurrentInterceptorIndex(0);
                return newChain.invokeNext(ctx);
            } else {
                MethodInvocationContext mctx = (MethodInvocationContext) ctx;
                return mctx.getTargetMethod().invoke(mctx.getTargetObject(), mctx.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) AttributeMetaData(jp.ossc.nimbus.core.AttributeMetaData) HashMap(java.util.HashMap) MethodCallJournalData(jp.ossc.nimbus.service.journal.editor.MethodCallJournalData) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain) InterceptorChain(jp.ossc.nimbus.service.aop.InterceptorChain) 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) Interceptor(jp.ossc.nimbus.service.aop.Interceptor) DefaultInterceptorChain(jp.ossc.nimbus.service.aop.DefaultInterceptorChain)

Example 3 with MethodCallJournalData

use of jp.ossc.nimbus.service.journal.editor.MethodCallJournalData in project nimbus by nimbus-org.

the class MethodJournalInterceptorServiceTest method test2.

public void test2() 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));
    try {
        new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {

            public Object invoke(InvocationContext context) throws Throwable {
                throw new IllegalArgumentException();
            }
        }).invokeNext(new DefaultMethodInvocationContext(target, HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
        fail();
    } catch (IllegalArgumentException e) {
    }
    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]);
    assertTrue(retData.getReturnValue() instanceof IllegalArgumentException);
    assertTrue(retData instanceof MethodThrowJournalData);
    assertTrue(((MethodThrowJournalData) retData).getThrowable() instanceof IllegalArgumentException);
}
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) HashMap(java.util.HashMap) MethodCallJournalData(jp.ossc.nimbus.service.journal.editor.MethodCallJournalData) Invoker(jp.ossc.nimbus.service.aop.Invoker) MethodThrowJournalData(jp.ossc.nimbus.service.journal.editor.MethodThrowJournalData) 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)

Aggregations

HashMap (java.util.HashMap)3 Map (java.util.Map)3 DefaultInterceptorChain (jp.ossc.nimbus.service.aop.DefaultInterceptorChain)3 DefaultInterceptorChainList (jp.ossc.nimbus.service.aop.DefaultInterceptorChainList)3 DefaultMethodInvocationContext (jp.ossc.nimbus.service.aop.DefaultMethodInvocationContext)3 InvocationContext (jp.ossc.nimbus.service.aop.InvocationContext)3 Invoker (jp.ossc.nimbus.service.aop.Invoker)3 MethodInvocationContext (jp.ossc.nimbus.service.aop.MethodInvocationContext)3 Context (jp.ossc.nimbus.service.context.Context)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 AttributeMetaData (jp.ossc.nimbus.core.AttributeMetaData)1 Interceptor (jp.ossc.nimbus.service.aop.Interceptor)1 InterceptorChain (jp.ossc.nimbus.service.aop.InterceptorChain)1 MethodThrowJournalData (jp.ossc.nimbus.service.journal.editor.MethodThrowJournalData)1