Debian Codegraph @

src/java/org/astrogrid/samp/xmlrpc/ActorHandler.java

raw
1 package org.astrogrid.samp.xmlrpc;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.lang.reflect.Method;
5 import java.lang.reflect.Modifier;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.HashMap;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.logging.Logger;
13 import org.astrogrid.samp.DataException;
14
15 /**
16 * Utility class to facilitate constructing a SampXmlRpcHandler which handles
17 * particular named methods.
18 * You supply at construction time an interface which defines the methods
19 * to be handled and an object which implements that interface.
20 * This object then uses reflection to invoke the correct methods on the
21 * implementation object as they are required from incoming XML-RPC
22 * <code>execute</code> requests. This insulates the implementation object
23 * from having to worry about any XML-RPC specifics.
24 *
25 * @author Mark Taylor
26 * @since 15 Jul 2008
27 */
28 public abstract class ActorHandler implements SampXmlRpcHandler {
29
30 private final String prefix_;
31 private final Object actor_;
32 private final Map methodMap_;
33 private final Logger logger_ =
34 Logger.getLogger( ActorHandler.class.getName() );
35
36 /**
37 * Constructor.
38 *
39 * @param prefix string prepended to every method name in the
40 * <code>actorType</code> interface to form the XML-RPC
41 * <code>methodName</code> element
42 * @param actorType interface defining the XML-RPC methods
43 * @param actor object implementing <code>actorType</code>
44 */
45 public ActorHandler( String prefix, Class actorType, Object actor ) {
46 prefix_ = prefix;
47 actor_ = actor;
48 methodMap_ = new HashMap();
49
50 // Construct a map keyed by method signature of the known methods.
51 Method[] methods = actorType.getDeclaredMethods();
52 for ( int im = 0; im < methods.length; im++ ) {
53 Method method = methods[ im ];
54 if ( Modifier.isPublic( method.getModifiers() ) ) {
55 String name = method.getName();
56 Class[] clazzes = method.getParameterTypes();
57 SampType[] types = new SampType[ clazzes.length ];
58 for ( int ic = 0; ic < clazzes.length; ic++ ) {
59 types[ ic ] = SampType.getClassType( clazzes[ ic ] );
60 }
61 Signature sig = new Signature( prefix_ + name, types );
62 methodMap_.put( sig, method );
63 }
64 }
65 }
66
67 public boolean canHandleCall( String fqName ) {
68 return fqName.startsWith( prefix_ );
69 }
70
71 public Object handleCall( String fqName, List params, Object reqInfo )
72 throws Exception {
73 if ( ! canHandleCall( fqName ) ) {
74 throw new IllegalArgumentException( "No I can't" );
75 }
76
77 // Work out the signature for this method and see if it is recognised.
78 String name = fqName.substring( prefix_.length() );
79 List typeList = new ArrayList();
80 for ( Iterator it = params.iterator(); it.hasNext(); ) {
81 typeList.add( SampType.getParamType( it.next() ) );
82 }
83 SampType[] types = (SampType[]) typeList.toArray( new SampType[ 0 ] );
84 Signature sig = new Signature( fqName, types );
85 Method method = (Method) methodMap_.get( sig );
86
87 // If the signature is recognised, invoke the relevant method
88 // on the implementation object.
89 if ( method != null ) {
90 Object result;
91 Throwable error;
92 try {
93 result = invokeMethod( method, actor_, params.toArray() );
94 }
95 catch ( InvocationTargetException e ) {
96 Throwable e2 = e.getCause();
97 if ( e2 instanceof Error ) {
98 throw (Error) e2;
99 }
100 else {
101 throw (Exception) e2;
102 }
103 }
104 catch ( Exception e ) {
105 throw e;
106 }
107 catch ( Error e ) {
108 throw e;
109 }
110 return result == null ? "" : result;
111 }
112
113 // If the signature is not recognised, but the method name is,
114 // try to make a helpful comment.
115 else {
116 for ( Iterator it = methodMap_.keySet().iterator();
117 it.hasNext(); ) {
118 Signature foundSig = (Signature) it.next();
119 if ( foundSig.name_.equals( fqName ) ) {
120 throw new IllegalArgumentException(
121 "Bad arguments: " + foundSig + " got "
122 + sig.typeList_ );
123 }
124 }
125 throw new UnsupportedOperationException( "Unknown method "
126 + fqName );
127 }
128 }
129
130 /**
131 * Returns the implementation object for this handler.
132 *
133 * @return implementation object
134 */
135 public Object getActor() {
136 return actor_;
137 }
138
139 /**
140 * Invokes a method reflectively on an object.
141 * This method should be implemented in the obvious way, that is
142 * <code>return method.invoke(obj,params)</code>.
143 *
144 * <p>If the implementation is effectively prescribed, why is this
145 * abstract method here? It's tricky.
146 * The reason is so that reflective method invocation from this class
147 * is done by code within the actor implementation class itself
148 * rather than by code in the superclass, <code>ActorHandler</code>.
149 * That in turn means that the <code>actorType</code> class specified
150 * in the constructor does not need to be visible from
151 * <code>ActorHandler</code>'s package, only from the package where
152 * the implementation class lives.
153 *
154 * @param method method to invoke
155 * @param obj object to invoke the method on
156 * @param args arguments for the method call
157 * @see java.lang.reflect.Method#invoke
158 */
159 protected abstract Object invokeMethod( Method method, Object obj,
160 Object[] args )
161 throws IllegalAccessException, InvocationTargetException;
162
163 /**
164 * Enumeration of permitted types within a SAMP data structure.
165 */
166 private static class SampType {
167
168 /** String type. */
169 public static final SampType STRING =
170 new SampType( String.class, "string" );
171
172 /** List type. */
173 public static final SampType LIST =
174 new SampType( List.class, "list" );
175
176 /** Map type. */
177 public static final SampType MAP =
178 new SampType( Map.class, "map" );
179
180 private final Class clazz_;
181 private final String name_;
182
183 /**
184 * Constructor.
185 *
186 * @param clazz java class
187 * @param name name of SAMP type
188 */
189 private SampType( Class clazz, String name ) {
190 clazz_ = clazz;
191 name_ = name;
192 }
193
194 /**
195 * Returns the java class corresponding to this type.
196 *
197 * @return class
198 */
199 public Class getTypeClass() {
200 return clazz_;
201 }
202
203 /**
204 * Returns the SAMP name for this type.
205 *
206 * @return name
207 */
208 public String toString() {
209 return name_;
210 }
211
212 /**
213 * Returns the SampType corresponding to a given java class.
214 *
215 * @param clazz class
216 * @return SAMP type
217 */
218 public static SampType getClassType( Class clazz ) {
219 if ( String.class.equals( clazz ) ) {
220 return STRING;
221 }
222 else if ( List.class.equals( clazz ) ) {
223 return LIST;
224 }
225 else if ( Map.class.equals( clazz ) ) {
226 return MAP;
227 }
228 else {
229 throw new IllegalArgumentException( "Illegal type "
230 + clazz.getName() );
231 }
232 }
233
234 /**
235 * Returns the SampType corresponding to a given object.
236 *
237 * @param param object
238 * return SAMP type
239 */
240 public static SampType getParamType( Object param ) {
241 if ( param instanceof String ) {
242 return STRING;
243 }
244 else if ( param instanceof List ) {
245 return LIST;
246 }
247 else if ( param instanceof Map ) {
248 return MAP;
249 }
250 else {
251 throw new DataException( "Param is not a SAMP type" );
252 }
253 }
254 }
255
256 /**
257 * Characterises a method signature.
258 * The <code>equals</code> and <code>hashCode</code> methods are
259 * implemented sensibly.
260 */
261 private static class Signature {
262 private final String name_;
263 private final List typeList_;
264
265 /**
266 * Constructor.
267 *
268 * @param name method name
269 * @param types types of method arguments
270 */
271 Signature( String name, SampType[] types ) {
272 name_ = name;
273 typeList_ = new ArrayList( Arrays.asList( types ) );
274 }
275
276 public boolean equals( Object o ) {
277 if ( o instanceof Signature ) {
278 Signature other = (Signature) o;
279 return this.name_.equals( other.name_ )
280 && this.typeList_.equals( other.typeList_ );
281 }
282 else {
283 return false;
284 }
285 }
286
287 public int hashCode() {
288 int code = 999;
289 code = 23 * code + name_.hashCode();
290 code = 23 * code + typeList_.hashCode();
291 return code;
292 }
293
294 public String toString() {
295 return name_ + typeList_;
296 }
297 }
298 }