/** * Removes all of the mappings from this map. */ public void clear() { try { MethodCall call=new MethodCall(CLEAR); disp.callRemoteMethods(null, call, call_options); } catch(Exception e) { throw new RuntimeException("clear() failed", e); } }
Method method=MethodCall.findMethod(target.getClass(), method_name, args); if(method == null) { if(prot instanceof AdditionalJmxObjects) { for(Object obj: ((AdditionalJmxObjects)prot).getJmxObjects()) { method=MethodCall.findMethod(obj.getClass(), method_name, args); if(method != null) { target=obj; MethodCall call=new MethodCall(method); Object[] converted_args=null; if(args != null) { converted_args[i]=Util.convert(args[i], types[i]); Object retval=call.invoke(target, converted_args); if(retval != null) map.put(prot_name + "." + method_name, retval.toString());
/** * Returns the first method that matches the specified name and parameter types. The overriding methods have priority. * The method is chosen from all the methods of the current class and all its superclasses and superinterfaces. * @return the matching method or null if no matching method has been found. */ protected static Method getMethod(Class target, String methodName, Class[] types) { if(types == null) types=new Class[0]; Method[] methods = getAllMethods(target); methods: for(int i = 0; i < methods.length; i++) { Method m= methods[i]; if(!methodName.equals(m.getName())) continue; Class[] parameters = m.getParameterTypes(); if (types.length != parameters.length) { continue; } for(int j = 0; j < types.length; j++) { if(!parameters[j].isAssignableFrom(types[j])) { continue methods; } } return m; } return null; }
protected static MethodCall methodCallFromBuffer(final byte[] buf, int offset, int length, Marshaller marshaller) throws Exception { ByteArrayDataInputStream in=new ByteArrayDataInputStream(buf, offset, length); MethodCall call=new MethodCall(); call.readFrom(in, marshaller); return call; }
/** Called by the ProbeHandler impl. All args are strings. Needs to find a method where all parameter * types are primitive types, so the strings can be converted */ public static Method findMethod(Class target_class, String method_name, Object[] args) throws Exception { int len=args != null? args.length : 0; Method retval=null; Method[] methods=getAllMethods(target_class); for(int i=0; i < methods.length; i++) { Method m=methods[i]; if(m.getName().equals(method_name)) { Class<?>[] parameter_types=m.getParameterTypes(); if(parameter_types.length == len) { retval=m; // now check if all parameter types are primitive types: boolean all_primitive=true; for(Class<?> parameter_type: parameter_types) { if(!isPrimitiveType(parameter_type)) { all_primitive=false; break; } } if(all_primitive) return m; } } } return retval; }
protected static Buffer methodCallToBuffer(final MethodCall call, Marshaller marshaller) throws Exception { Object[] args=call.args(); int estimated_size=64; if(args != null) for(Object arg: args) estimated_size+=marshaller != null? marshaller.estimatedSize(arg) : (arg == null? 2 : 50); ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(estimated_size, true); call.writeTo(out, marshaller); return out.getBuffer(); }
@SuppressWarnings("unchecked") <T> T invokeDirectly(String serviceName, String methodName, Object[] args, Class<?>[] types, List<T> remoteResponses, ResponseFilter filter) throws Exception { T retVal = null; Object handler = this.rpcHandlers.get(serviceName); if (handler != null) { MethodCall call = new MethodCall(methodName, args, types); try { Object result = call.invoke(handler); retVal = (T) result; if (remoteResponses != null && (filter == null || filter.isAcceptable(retVal, me))) { remoteResponses.add(retVal); } } catch (Exception e) { throw e; } catch (Error e) { throw e; } catch (Throwable e) { throw new RuntimeException(e); } } return retVal; }
/** * Message contains MethodCall. Execute it against *this* object and return result. * Use MethodCall.invoke() to do this. Return result. */ public Object handle(Message req) throws Exception { if(server_obj == null) { log.error(Util.getMessage("NoMethodHandlerIsRegisteredDiscardingRequest")); return null; } if(req == null || req.getLength() == 0) { log.error(Util.getMessage("MessageOrMessageBufferIsNull")); return null; } MethodCall method_call=methodCallFromBuffer(req.getRawBuffer(), req.getOffset(), req.getLength(), marshaller); if(log.isTraceEnabled()) log.trace("[sender=%s], method_call: %s", req.getSrc(), method_call); if(method_call.mode() == MethodCall.ID) { if(method_lookup == null) throw new Exception(String.format("MethodCall uses ID=%d, but method_lookup has not been set", method_call.methodId())); Method m=method_lookup.findMethod(method_call.methodId()); if(m == null) throw new Exception("no method found for " + method_call.methodId()); method_call.method(m); } return method_call.invoke(server_obj); }
local_addr=ch.getAddress().toString(); MethodCall call=new MethodCall(getClass().getMethod("handleMessage", String.class, String.class)); for(;;) { String line=Util.readStringFromStdin(": "); call.args(line, local_addr);
log.trace("[sender=" + req.getSrc() + "], method_call: " + method_call); if(method_call.getMode() == MethodCall.ID) { if(method_lookup == null) throw new Exception("MethodCall uses ID=" + method_call.getId() + ", but method_lookup has not been set"); Method m=method_lookup.findMethod(method_call.getId()); if(m == null) throw new Exception("no method foudn for " + method_call.getId()); method_call.setMethod(m); return method_call.invoke(server_obj);
switch(mode) { case OLD: meth=findMethod(cl); break; case METHOD: case TYPES: meth = getMethod(cl, method_name, types); break; case SIGNATURE: Class[] mytypes=null; if(signature != null) mytypes=getTypesFromString(cl, signature); meth = getMethod(cl, method_name, mytypes); break; case ID:
public Object invoke(Object target, Object[] args) throws Exception { if(args != null) this.args=args; return invoke(target); }
public MethodCall(Method method, Object... arguments) { init(method); if(arguments != null) args=arguments; }
@Override public byte[] objectToByteBuffer(Object obj) throws Exception { // wrap MethodCall in Object[service_name, byte[]] so that service name is available during demarshalling if (obj instanceof MethodCall) { String name = ((MethodCall)obj).getName(); int idx = name.lastIndexOf('.'); String serviceName = name.substring(0, idx); return CoreGroupCommunicationService.this.objectToByteBufferInternal(new Object[]{serviceName, CoreGroupCommunicationService.this.objectToByteBufferInternal(obj)}); } return CoreGroupCommunicationService.this.objectToByteBufferInternal(obj); } }
break; case TYPES: meth=getMethod(cl, method_name, types); break; case ID:
if (handler != null) MethodCall call = new MethodCall(methodName, args, types); try Object result = call.invoke(handler); if (returnType != null && void.class != returnType)
/** * Message contains MethodCall. Execute it against *this* object and return result. * Use MethodCall.invoke() to do this. Return result. */ public Object handle(Message req) throws Exception { if(server_obj == null) { log.error(Util.getMessage("NoMethodHandlerIsRegisteredDiscardingRequest")); return null; } if(req == null || req.getLength() == 0) { log.error(Util.getMessage("MessageOrMessageBufferIsNull")); return null; } MethodCall method_call=methodCallFromBuffer(req.getRawBuffer(), req.getOffset(), req.getLength(), marshaller); if(log.isTraceEnabled()) log.trace("[sender=%s], method_call: %s", req.getSrc(), method_call); if(method_call.mode() == MethodCall.ID) { if(method_lookup == null) throw new Exception(String.format("MethodCall uses ID=%d, but method_lookup has not been set", method_call.methodId())); Method m=method_lookup.findMethod(method_call.methodId()); if(m == null) throw new Exception("no method found for " + method_call.methodId()); method_call.method(m); } return method_call.invoke(server_obj); }
protected static MethodCall methodCallFromBuffer(final byte[] buf, int offset, int length, Marshaller marshaller) throws Exception { ByteArrayDataInputStream in=new ByteArrayDataInputStream(buf, offset, length); MethodCall call=new MethodCall(); call.readFrom(in, marshaller); return call; }