use of jp.ossc.nimbus.service.aop.Interceptor in project nimbus by nimbus-org.
the class MethodSynchronizeInterceptorServiceTest method test1.
public void test1() throws Throwable {
ServiceManagerFactory.registerManager("Test");
MethodSynchronizeInterceptorService interceptor = new MethodSynchronizeInterceptorService();
ServiceManagerFactory.registerService("Test", "MethodSynchronizeInterceptor", interceptor);
try {
ServiceManagerFactory.findManager("Test").createAllService();
interceptor.setScope(MethodSynchronizeInterceptorService.SCOPE_VM);
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(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" }));
} finally {
ServiceManagerFactory.findManager("Test").stopAllService();
ServiceManagerFactory.findManager("Test").destroyAllService();
ServiceManagerFactory.unregisterManager("Test");
}
}
use of jp.ossc.nimbus.service.aop.Interceptor in project nimbus by nimbus-org.
the class MethodSynchronizeInterceptorServiceTest method test4.
public void test4() throws Throwable {
ServiceManagerFactory.registerManager("Test");
MethodSynchronizeInterceptorService interceptor = new MethodSynchronizeInterceptorService();
ServiceManagerFactory.registerService("Test", "MethodSynchronizeInterceptor", interceptor);
try {
ServiceManagerFactory.findManager("Test").createAllService();
interceptor.setScope(MethodSynchronizeInterceptorService.SCOPE_INSTANCE);
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";
}
});
final HashMap target = new HashMap();
Thread thread = new Thread() {
public void run() {
try {
chain1.invokeNext(new DefaultMethodInvocationContext(target, HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
} catch (Throwable th) {
}
}
};
thread.start();
Thread.sleep(100);
chain2.invokeNext(new DefaultMethodInvocationContext(new HashMap(), HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
chain3.invokeNext(new DefaultMethodInvocationContext(target, HashMap.class.getMethod("get", new Class[] { Object.class }), new Object[] { "A" }));
} finally {
ServiceManagerFactory.findManager("Test").stopAllService();
ServiceManagerFactory.findManager("Test").destroyAllService();
ServiceManagerFactory.unregisterManager("Test");
}
}
use of jp.ossc.nimbus.service.aop.Interceptor in project nimbus by nimbus-org.
the class NoCalledMethodMetricsInterceptorServiceTest method test1.
public void test1() throws Throwable {
ServiceManagerFactory.registerManager("Test");
NoCalledMethodMetricsInterceptorService interceptor = new NoCalledMethodMetricsInterceptorService();
ServiceManagerFactory.registerService("Test", "NoCalledMethodMetricsInterceptor", interceptor);
try {
ServiceManagerFactory.findManager("Test").createAllService();
interceptor.setOutputSystemOut(false);
interceptor.setTargetClassName("jp\\.ossc\\.nimbus\\..*");
ServiceManagerFactory.findManager("Test").startAllService();
final InterceptorChain chain = new DefaultInterceptorChain(new DefaultInterceptorChainList(new Interceptor[] { interceptor }), new Invoker() {
public Object invoke(InvocationContext context) throws Throwable {
return null;
}
});
Method method = StringOperator.class.getMethod("makeSpace", new Class[] { Integer.TYPE });
assertNotNull(interceptor.getNoCalledMethodSet());
assertTrue(interceptor.getNoCalledMethodSet().contains(method));
chain.invokeNext(new DefaultMethodInvocationContext(null, method, new Object[] { new Integer(1) }));
assertNotNull(interceptor.getNoCalledMethodSet());
assertFalse(interceptor.getNoCalledMethodSet().contains(method));
} finally {
ServiceManagerFactory.findManager("Test").stopAllService();
ServiceManagerFactory.findManager("Test").destroyAllService();
ServiceManagerFactory.unregisterManager("Test");
}
}
use of jp.ossc.nimbus.service.aop.Interceptor 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");
}
}
use of jp.ossc.nimbus.service.aop.Interceptor 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");
}
}
Aggregations