Skip to content

Commit e6ac3fd

Browse files
committed
Add chain support to AsyncCallDispatcher
1 parent c1ed9a9 commit e6ac3fd

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

framework/ipc/src/org/apache/cloudstack/framework/async/AsyncCallbackDispatcher.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,33 @@
2121

2222
import java.lang.reflect.InvocationTargetException;
2323
import java.lang.reflect.Method;
24-
import java.util.HashMap;
25-
import java.util.Map;
2624

2725
import net.sf.cglib.proxy.Enhancer;
2826
import net.sf.cglib.proxy.MethodInterceptor;
2927
import net.sf.cglib.proxy.MethodProxy;
3028

3129
@SuppressWarnings("rawtypes")
3230
public class AsyncCallbackDispatcher<T> implements AsyncCompletionCallback {
31+
private AsyncCallbackDispatcher _parent;
32+
3333
private Method _callbackMethod;
3434
private T _targetObject;
3535
private Object _contextObject;
3636
private Object _resultObject;
3737
private AsyncCallbackDriver _driver = new InplaceAsyncCallbackDriver();
3838

39-
public AsyncCallbackDispatcher(T target) {
39+
private AsyncCallbackDispatcher(T target) {
40+
assert(target != null);
41+
_targetObject = target;
42+
}
43+
44+
private AsyncCallbackDispatcher(T target, AsyncCallbackDispatcher parent) {
4045
assert(target != null);
4146
_targetObject = target;
47+
_parent = parent;
4248
}
4349

44-
public AsyncCallbackDispatcher attachDriver(AsyncCallbackDriver driver) {
50+
public AsyncCallbackDispatcher<T> attachDriver(AsyncCallbackDriver driver) {
4551
assert(driver != null);
4652
_driver = driver;
4753

@@ -64,11 +70,11 @@ public Object intercept(Object arg0, Method arg1, Object[] arg2,
6470
});
6571
}
6672

67-
public AsyncCallbackDispatcher setCallback(Object useless) {
73+
public AsyncCallbackDispatcher<T> setCallback(Object useless) {
6874
return this;
6975
}
7076

71-
public AsyncCallbackDispatcher setContext(Object context) {
77+
public AsyncCallbackDispatcher<T> setContext(Object context) {
7278
_contextObject = context;
7379
return this;
7480
}
@@ -82,16 +88,36 @@ public void complete(Object resultObject) {
8288
_resultObject = resultObject;
8389
_driver.performCompletionCallback(this);
8490
}
91+
92+
public void deepComplete(Object resultObject) {
93+
complete(resultObject);
94+
if(_parent != null)
95+
_parent.deepComplete(resultObject);
96+
}
8597

8698
@SuppressWarnings("unchecked")
8799
public <R> R getResult() {
88100
return (R)_resultObject;
89101
}
90-
91-
public Object getTargetObject() {
102+
103+
// for internal use
104+
Object getTargetObject() {
92105
return _targetObject;
93106
}
94107

108+
public static <P> AsyncCallbackDispatcher<P> create(P target) {
109+
return new AsyncCallbackDispatcher<P>(target);
110+
}
111+
112+
public <P> AsyncCallbackDispatcher<P> chainToCreate(P target) {
113+
return new AsyncCallbackDispatcher<P>(target, this);
114+
}
115+
116+
@SuppressWarnings("unchecked")
117+
public <P> AsyncCallbackDispatcher<P> getParent() {
118+
return (AsyncCallbackDispatcher<P>)_parent;
119+
}
120+
95121
public static boolean dispatch(Object target, AsyncCallbackDispatcher callback) {
96122
assert(callback != null);
97123
assert(target != null);

framework/ipc/test/org/apache/cloudstack/framework/codestyle/AsyncSampleEventDrivenStyleCaller.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class AsyncSampleEventDrivenStyleCaller {
2828
@SuppressWarnings("unchecked")
2929
public void MethodThatWillCallAsyncMethod() {
3030
String vol = new String("Hello");
31-
AsyncCallbackDispatcher<AsyncSampleEventDrivenStyleCaller> caller = new AsyncCallbackDispatcher<AsyncSampleEventDrivenStyleCaller>(this);
31+
AsyncCallbackDispatcher<AsyncSampleEventDrivenStyleCaller> caller = AsyncCallbackDispatcher.create(this);
3232
_ds.createVolume(vol, caller
3333
.setCallback(caller.getTarget().HandleVolumeCreateAsyncCallback(null, null))
3434
.setContext(vol)

0 commit comments

Comments
 (0)