Debian Codegraph @

src/java/org/astrogrid/samp/web/ClientCallbackOperation.java

raw
1 package org.astrogrid.samp.web;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7 import org.astrogrid.samp.Message;
8 import org.astrogrid.samp.Response;
9 import org.astrogrid.samp.client.CallableClient;
10
11 /**
12 * Represents one of the possible callbacks which may be invoked on a
13 * CallableClient. The {@link #invoke} static method arranges for a Callback
14 * acquired from the hub to be dispatched to a CallableClient.
15 *
16 * @author Mark Taylor
17 * @since 3 Feb 2011
18 */
19 abstract class ClientCallbackOperation {
20
21 private final String fqName_;
22 private final Class[] sampSig_;
23 private static final Map OPERATION_MAP = createOperationMap();
24
25 /**
26 * Constructor.
27 *
28 * @param unqualified callback method name
29 * @param signature of callback; an array of SAMP-friendly classes,
30 * one for each argument
31 */
32 private ClientCallbackOperation( String methodName, Class[] sampSig ) {
33 fqName_ = WebClientProfile.WEBSAMP_CLIENT_PREFIX + methodName;
34 sampSig_ = sampSig;
35 }
36
37 /**
38 * Makes a call to a callable client of the method represented by
39 * this operation with a given list of parameters.
40 * No checking is performed on the parameter list.
41 *
42 * <p>This method should be private really, but abstract private is
43 * not permitted.
44 *
45 * @param client target callable client
46 * @param paramList parameters for call, assumed to be valid
47 */
48 abstract void dispatch( CallableClient client, List paramList )
49 throws Exception;
50
51 /**
52 * Dispatches a callback to a CallableClient.
53 *
54 * @param callback callback acquired from the hub
55 * @param client client which should execute <code>callback</code>
56 */
57 public static void invoke( Callback callback, CallableClient client )
58 throws Exception {
59 callback.check();
60 String methodName = callback.getMethodName();
61 List paramList = callback.getParams();
62 ClientCallbackOperation op =
63 (ClientCallbackOperation) OPERATION_MAP.get( methodName );
64 if ( op == null ) {
65 throw new UnsupportedOperationException(
66 "Unknown callback operation " + methodName );
67 }
68 else {
69 boolean sigOk = op.sampSig_.length == paramList.size();
70 for ( int i = 0; sigOk && i < op.sampSig_.length; i++ ) {
71 sigOk = sigOk
72 && op.sampSig_[ i ]
73 .isAssignableFrom( paramList.get( i ).getClass() );
74 }
75 if ( ! sigOk ) {
76 throw new IllegalArgumentException(
77 methodName + " callback signature mismatch" );
78 }
79 else {
80 op.dispatch( client, paramList );
81 }
82 }
83 }
84
85 /**
86 * Returns a map, keyed by unqualified operation name,
87 * of known callback operations.
88 *
89 * @param String->ClientCallbackOperation map
90 */
91 private static Map createOperationMap() {
92
93 // First assemble an array of known callback operations.
94 // It would be possible to assemble this array using reflection
95 // on the CallableClient interface, but more trouble than it's
96 // worth for three methods.
97 ClientCallbackOperation[] operations = new ClientCallbackOperation[] {
98 new ClientCallbackOperation( "receiveNotification",
99 new Class[] { String.class,
100 Map.class } ) {
101 public void dispatch( CallableClient client, List params )
102 throws Exception {
103 client.receiveNotification(
104 (String) params.get( 0 ),
105 new Message( (Map) params.get( 1 ) ) );
106 }
107 },
108 new ClientCallbackOperation( "receiveCall",
109 new Class[] { String.class,
110 String.class,
111 Map.class } ) {
112 public void dispatch( CallableClient client, List params )
113 throws Exception {
114 client.receiveCall(
115 (String) params.get( 0 ),
116 (String) params.get( 1 ),
117 new Message( (Map) params.get( 2 ) ) );
118 }
119 },
120 new ClientCallbackOperation( "receiveResponse",
121 new Class[] { String.class,
122 String.class,
123 Map.class } ) {
124 public void dispatch( CallableClient client, List params )
125 throws Exception {
126 client.receiveResponse(
127 (String) params.get( 0 ),
128 (String) params.get( 1 ),
129 new Response( (Map) params.get( 2 ) ) );
130 }
131 },
132 };
133
134 // Turn it into a map keyed by operation name, and return.
135 Map opMap = new HashMap();
136 for ( int i = 0; i < operations.length; i++ ) {
137 ClientCallbackOperation op = operations[ i ];
138 opMap.put( op.fqName_, op );
139 }
140 return Collections.unmodifiableMap( opMap );
141 }
142 }