1 package org.jboss.cache.rpc; 2 3 import org.jboss.cache.CacheImpl; 4 import org.jboss.cache.config.Configuration; 5 import org.jboss.cache.marshall.MethodCall; 6 import org.jboss.cache.marshall.MethodCallFactory; 7 import org.jgroups.JChannel; 8 9 import java.lang.reflect.Method ; 10 import java.util.ArrayList ; 11 import java.util.HashMap ; 12 import java.util.List ; 13 import java.util.Map ; 14 import java.util.Vector ; 15 16 38 public class RpcTreeCache extends CacheImpl{ 40 41 44 public static final Method dispatchRpcCallMethod; 45 46 static 47 { 48 try 49 { 50 dispatchRpcCallMethod = RpcTreeCache.class.getDeclaredMethod("_dispatchRpcCall", String .class, MethodCall.class); 51 } 52 catch (NoSuchMethodException ex) 53 { 54 ex.printStackTrace(); 55 throw new ExceptionInInitializerError (ex.toString()); 56 } 57 } 58 59 62 protected Map rpcHandlers = new HashMap (); 63 64 68 public RpcTreeCache(String cluster_name, String props, long state_fetch_timeout) throws Exception 69 { 70 } 73 74 79 public RpcTreeCache() throws Exception 80 { 81 super(); 82 } 83 84 87 public RpcTreeCache(JChannel channel) throws Exception 88 { 89 super(channel); 90 } 91 92 119 public List callRemoteMethods(String serviceName, Vector members, Method method, Object [] args, boolean synchronous, boolean exclude_self, long timeout) throws Exception 120 { 121 122 return callRemoteMethods(serviceName, members, MethodCallFactory.create(method, args), synchronous, exclude_self, timeout); 123 } 124 125 151 public List callRemoteMethods(String serviceName, Vector mbrs, 152 MethodCall method_call, boolean synchronous, 153 boolean exclude_self, long timeout) 154 throws Exception 155 { 156 List responses = null; 157 158 if (getConfiguration().getCacheMode() == Configuration.CacheMode.LOCAL) 159 { 160 163 if (synchronous) 164 { 165 responses = new ArrayList (); 167 168 if (!exclude_self) 169 { 170 Object resp = _dispatchRpcCall(serviceName, method_call); 172 if ((resp instanceof NoHandlerForRPCException) == false) 173 { 174 responses.add(_dispatchRpcCall(serviceName, method_call)); 175 } 176 } 177 179 } 180 else if (!exclude_self) 181 { 182 _dispatchRpcCall(serviceName, method_call); 185 } 186 } 187 else 188 { 189 192 MethodCall wrapper = MethodCallFactory.create(dispatchRpcCallMethod, 195 serviceName, method_call); 196 197 responses = callRemoteMethods(mbrs, wrapper, synchronous, 198 exclude_self, timeout); 199 200 if (responses != null) 204 { 205 for (int i = 0; i < responses.size(); i++) 206 { 207 Object obj = responses.get(i); 208 if (obj instanceof NoHandlerForRPCException) 209 { 210 responses.remove(i); 211 i--; 212 } 213 } 214 } 215 } 216 217 return responses; 218 } 219 220 249 public List callRemoteMethods(String serviceName, Vector members, 250 String method_name, Class [] types, Object [] args, boolean synchronous, 251 boolean exclude_self, long timeout) throws Exception 252 { 253 Object handler = rpcHandlers.get(serviceName); 254 if (handler == null) 255 { 256 String msg = "No rpc handler registered under: " + serviceName; 257 258 log.trace(msg); 259 260 throw new NoHandlerForRPCException(msg); 261 } 262 263 Method method = handler.getClass().getDeclaredMethod(method_name, types); 264 return callRemoteMethods(serviceName, members, method, args, 265 synchronous, exclude_self, timeout); 266 } 267 268 269 278 public void registerRPCHandler(String serviceName, Object handler) 279 { 280 rpcHandlers.put(serviceName, handler); 281 } 282 283 293 public void unregisterRPCHandler(String serviceName, Object handler) 294 { 295 Object registered = rpcHandlers.remove(serviceName); 296 if (handler != registered) 297 { 298 rpcHandlers.put(serviceName, handler); 300 } 301 } 302 303 312 public Object _dispatchRpcCall(String serviceName, MethodCall call) 313 { 314 Object retval = null; 315 Object handler = rpcHandlers.get(serviceName); 316 if (handler == null) 317 { 318 String msg = "No rpc handler registered under: " + serviceName; 319 320 log.trace(msg); 321 322 return new NoHandlerForRPCException(msg, getLocalAddress()); 323 } 324 325 try 326 { 327 retval = call.invoke(handler); 328 } 329 catch (Throwable t) 330 { 331 log.trace("rpc call threw exception", t); 332 retval = t; 333 } 334 335 return retval; 336 } 337 338 339 } 340 | Popular Tags |