Skip to content

Commit d62da2a

Browse files
committed
Make RPC naming convention clear for RPC users, add handler and event dispatchers
1 parent 17f2af4 commit d62da2a

15 files changed

Lines changed: 325 additions & 59 deletions
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
119
package org.apache.cloudstack.framework.messaging;
220

321
public interface ComponentContainer {
4-
ComponentEndpoint wireComponent(ComponentEndpoint endpoint, String predefinedAddress);
22+
ComponentEndpoint wire(ComponentEndpoint endpoint, String predefinedAddress);
523
}

‎framework/ipc/src/org/apache/cloudstack/framework/messaging/ComponentEndpoint.java‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
*/
1919
package org.apache.cloudstack.framework.messaging;
2020

21-
public class ComponentEndpoint implements RpcEndpoint, Subscriber {
21+
public class ComponentEndpoint implements RpcServiceEndpoint, Subscriber {
22+
2223
private TransportEndpoint transportEndpoint;
2324
private RpcProvider rpcProvider;
2425

@@ -42,17 +43,19 @@ public void setRpcProvider(RpcProvider rpcProvider) {
4243
}
4344

4445
public void initialize() {
45-
rpcProvider.registerRpcEndpoint(this);
46+
rpcProvider.registerRpcServiceEndpoint(this);
4647
}
4748

4849
@Override
49-
public void onCallReceive(RpcServerCall call) {
50-
// TODO Auto-generated method stub
51-
// implement annotation based call dispatching
50+
public boolean onCallReceive(RpcServerCall call) {
51+
return RpcServiceDispatcher.dispatch(this, call);
5252
}
5353

5454
@Override
5555
public void onPublishEvent(String subject, String senderAddress, Object args) {
56-
// TODO
56+
try {
57+
EventDispatcher.dispatch(this, subject, senderAddress, args);
58+
} catch(RuntimeException e) {
59+
}
5760
}
5861
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.cloudstack.framework.messaging;
20+
21+
import java.lang.reflect.InvocationTargetException;
22+
import java.lang.reflect.Method;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
public class EventDispatcher {
27+
private static Map<Class<?>, Method> s_handlerCache = new HashMap<Class<?>, Method>();
28+
29+
public static boolean dispatch(Object target, String subject, String senderAddress, Object args) {
30+
assert(subject != null);
31+
assert(target != null);
32+
33+
Method handler = resolveHandler(target.getClass(), subject);
34+
if(handler == null)
35+
return false;
36+
37+
try {
38+
handler.invoke(target, subject, senderAddress, args);
39+
} catch (IllegalArgumentException e) {
40+
throw new RuntimeException("IllegalArgumentException when invoking event handler for subject: " + subject);
41+
} catch (IllegalAccessException e) {
42+
throw new RuntimeException("IllegalAccessException when invoking event handler for subject: " + subject);
43+
} catch (InvocationTargetException e) {
44+
throw new RuntimeException("InvocationTargetException when invoking event handler for subject: " + subject);
45+
}
46+
47+
return true;
48+
}
49+
50+
public static Method resolveHandler(Class<?> handlerClz, String subject) {
51+
synchronized(s_handlerCache) {
52+
Method handler = s_handlerCache.get(handlerClz);
53+
if(handler != null)
54+
return handler;
55+
56+
for(Method method : handlerClz.getMethods()) {
57+
EventHandler annotation = method.getAnnotation(EventHandler.class);
58+
if(annotation != null) {
59+
if(match(annotation.topic(), subject)) {
60+
s_handlerCache.put(handlerClz, method);
61+
return method;
62+
}
63+
}
64+
}
65+
}
66+
67+
return null;
68+
}
69+
70+
private static boolean match(String expression, String param) {
71+
return param.matches(expression);
72+
}
73+
}

‎framework/ipc/src/org/apache/cloudstack/framework/messaging/EventHandler.java‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
// Licensed to the Apache Software Foundation (ASF) under one
2-
// or more contributor license agreements. See the NOTICE file
3-
// distributed with this work for additional information
4-
// regarding copyright ownership. The ASF licenses this file
5-
// to you under the Apache License, Version 2.0 (the
6-
// "License"); you may not use this file except in compliance
7-
// the License. You may obtain a copy of the License at
8-
//
9-
// http://www.apache.org/licenses/LICENSE-2.0
10-
//
11-
// Unless required by applicable law or agreed to in writing,
12-
// software distributed under the License is distributed on an
13-
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14-
// KIND, either express or implied. See the License for the
15-
// specific language governing permissions and limitations
16-
// under the License.
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
1719
package org.apache.cloudstack.framework.messaging;
1820

1921
import java.lang.annotation.ElementType;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.cloudstack.framework.messaging;
20+
21+
import java.lang.reflect.InvocationTargetException;
22+
import java.lang.reflect.Method;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
public class RpcCallbackDispatcher {
27+
28+
private static Map<Class<?>, Method> s_handlerCache = new HashMap<Class<?>, Method>();
29+
30+
public static boolean dispatch(Object target, RpcClientCall clientCall) {
31+
assert(clientCall != null);
32+
assert(target != null);
33+
34+
Method handler = resolveHandler(target.getClass(), clientCall.getCommand());
35+
if(handler == null)
36+
return false;
37+
38+
try {
39+
handler.invoke(target, clientCall);
40+
} catch (IllegalArgumentException e) {
41+
throw new RpcException("IllegalArgumentException when invoking RPC callback for command: " + clientCall.getCommand());
42+
} catch (IllegalAccessException e) {
43+
throw new RpcException("IllegalAccessException when invoking RPC callback for command: " + clientCall.getCommand());
44+
} catch (InvocationTargetException e) {
45+
throw new RpcException("InvocationTargetException when invoking RPC callback for command: " + clientCall.getCommand());
46+
}
47+
48+
return true;
49+
}
50+
51+
public static Method resolveHandler(Class<?> handlerClz, String command) {
52+
synchronized(s_handlerCache) {
53+
Method handler = s_handlerCache.get(handlerClz);
54+
if(handler != null)
55+
return handler;
56+
57+
for(Method method : handlerClz.getMethods()) {
58+
RpcCallbackHandler annotation = method.getAnnotation(RpcCallbackHandler.class);
59+
if(annotation != null) {
60+
if(annotation.command().equals(command)) {
61+
s_handlerCache.put(handlerClz, method);
62+
return method;
63+
}
64+
}
65+
}
66+
}
67+
68+
return null;
69+
}
70+
}

framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcServerCallHandler.java renamed to framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcCallbackHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525

2626
@Retention(RetentionPolicy.RUNTIME)
2727
@Target(ElementType.METHOD)
28-
public @interface RpcServerCallHandler {
28+
public @interface RpcCallbackHandler {
2929
String command();
3030
}

‎framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcClientCall.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import java.util.concurrent.TimeUnit;
2020

2121
public interface RpcClientCall {
22+
String getCommand();
2223
RpcClientCall setCommand(String cmd);
23-
RpcClientCall setPipeline(String pipeline);
2424
RpcClientCall setTimeout(TimeUnit timeout);
2525

2626
RpcClientCall setCommandArg(Object arg);
@@ -34,5 +34,8 @@ public interface RpcClientCall {
3434
void apply();
3535
void cancel();
3636

37+
/**
38+
* @return the result object, it may also throw RpcException to indicate RPC failures
39+
*/
3740
<T> T get();
3841
}

‎framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcClientCallHandler.java‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎framework/ipc/src/org/apache/cloudstack/framework/messaging/RpcProvider.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public interface RpcProvider extends TransportMultiplexier {
2222
void setMessageSerializer(MessageSerializer messageSerializer);
2323
MessageSerializer getMessageSerializer();
2424

25-
void registerRpcEndpoint(RpcEndpoint rpcEndpoint);
26-
void unregisteRpcEndpoint(RpcEndpoint rpcEndpoint);
25+
void registerRpcServiceEndpoint(RpcServiceEndpoint rpcEndpoint);
26+
void unregisteRpcServiceEndpoint(RpcServiceEndpoint rpcEndpoint);
2727

2828
RpcClientCall target(String target);
2929
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.cloudstack.framework.messaging;
20+
21+
import java.lang.reflect.InvocationTargetException;
22+
import java.lang.reflect.Method;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
public class RpcServiceDispatcher {
27+
28+
private static Map<Class<?>, Method> s_handlerCache = new HashMap<Class<?>, Method>();
29+
30+
public static boolean dispatch(Object target, RpcServerCall serviceCall) {
31+
assert(serviceCall != null);
32+
assert(target != null);
33+
34+
Method handler = resolveHandler(target.getClass(), serviceCall.getCommand());
35+
if(handler == null)
36+
return false;
37+
38+
try {
39+
handler.invoke(target, serviceCall);
40+
} catch (IllegalArgumentException e) {
41+
throw new RpcException("IllegalArgumentException when invoking RPC service command: " + serviceCall.getCommand());
42+
} catch (IllegalAccessException e) {
43+
throw new RpcException("IllegalAccessException when invoking RPC service command: " + serviceCall.getCommand());
44+
} catch (InvocationTargetException e) {
45+
throw new RpcException("InvocationTargetException when invoking RPC service command: " + serviceCall.getCommand());
46+
}
47+
48+
return true;
49+
}
50+
51+
public static Method resolveHandler(Class<?> handlerClz, String command) {
52+
synchronized(s_handlerCache) {
53+
Method handler = s_handlerCache.get(handlerClz);
54+
if(handler != null)
55+
return handler;
56+
57+
for(Method method : handlerClz.getMethods()) {
58+
RpcServiceHandler annotation = method.getAnnotation(RpcServiceHandler.class);
59+
if(annotation != null) {
60+
if(annotation.command().equals(command)) {
61+
s_handlerCache.put(handlerClz, method);
62+
return method;
63+
}
64+
}
65+
}
66+
}
67+
68+
return null;
69+
}
70+
}

0 commit comments

Comments
 (0)