use of jp.ossc.nimbus.service.aop.InvocationContext 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);
}
use of jp.ossc.nimbus.service.aop.InvocationContext 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());
}
use of jp.ossc.nimbus.service.aop.InvocationContext 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");
}
}
use of jp.ossc.nimbus.service.aop.InvocationContext in project nimbus by nimbus-org.
the class MethodMetricsInterceptorServiceTest method test3.
public void test3() throws Throwable {
ServiceManagerFactory.registerManager("Test");
MethodMetricsInterceptorService interceptor = new MethodMetricsInterceptorService();
ServiceManagerFactory.registerService("Test", "MethodMetricsInterceptor", interceptor);
try {
ServiceManagerFactory.findManager("Test").createAllService();
ServiceManagerFactory.findManager("Test").startAllService();
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 OutOfMemoryError();
}
}).invokeNext(new DefaultMethodInvocationContext(null, HashMap.class.getMethod("get", new Class[] { Object.class }), null));
fail();
} catch (OutOfMemoryError e) {
}
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(0L, info.getCount());
assertTrue(info.getLastTime() > 0);
assertEquals(0L, info.getExceptionCount());
assertEquals(0L, info.getLastExceptionTime());
assertEquals(1L, info.getErrorCount());
assertTrue(info.getLastErrorTime() > 0);
assertTrue(info.getBestPerformance() > 0);
assertTrue(info.getBestPerformanceTime() > 0);
assertTrue(info.getWorstPerformance() > 0);
assertTrue(info.getWorstPerformanceTime() > 0);
assertTrue(info.getAveragePerformance() > 0);
} finally {
ServiceManagerFactory.findManager("Test").stopAllService();
ServiceManagerFactory.findManager("Test").destroyAllService();
ServiceManagerFactory.unregisterManager("Test");
}
}
use of jp.ossc.nimbus.service.aop.InvocationContext in project nimbus by nimbus-org.
the class MethodSynchronizeInterceptorServiceTest method test2.
public void test2() throws Throwable {
ServiceManagerFactory.registerManager("Test");
MethodSynchronizeInterceptorService interceptor = new MethodSynchronizeInterceptorService();
ServiceManagerFactory.registerService("Test", "MethodSynchronizeInterceptor", interceptor);
try {
ServiceManagerFactory.findManager("Test").createAllService();
interceptor.setScope(MethodSynchronizeInterceptorService.SCOPE_CLASS);
ServiceManagerFactory.findManager("Test").startAllService();
class Counter {
public volatile int count;
}
final Counter counter = new Counter();
final InterceptorChain chain1 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {
public Object invoke(InvocationContext context) throws Throwable {
try {
counter.count++;
Thread.sleep(500);
return "test";
} finally {
counter.count--;
}
}
});
final InterceptorChain chain2 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {
public Object invoke(InvocationContext context) throws Throwable {
assertEquals(1, counter.count);
return "test";
}
});
final InterceptorChain chain3 = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {
public Object invoke(InvocationContext context) throws Throwable {
assertEquals(0, counter.count);
return "test";
}
});
Thread thread = new Thread() {
public void run() {
try {
chain1.invokeNext(new DefaultMethodInvocationContext(new HashMap(), HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
} catch (Throwable th) {
}
}
};
thread.start();
Thread.sleep(100);
chain2.invokeNext(new DefaultMethodInvocationContext(new ArrayList(), ArrayList.class.getMethod("add", new Class[] { Object.class }), new Object[] { "A" }));
chain3.invokeNext(new DefaultMethodInvocationContext(new HashMap(), HashMap.class.getMethod("put", new Class[] { Object.class, Object.class }), new Object[] { "A", new Integer(1) }));
} finally {
ServiceManagerFactory.findManager("Test").stopAllService();
ServiceManagerFactory.findManager("Test").destroyAllService();
ServiceManagerFactory.unregisterManager("Test");
}
}
Aggregations