Beispiel #1
0
    /**
     * Invokes an operation and puts the return value into map
     *
     * @param map
     * @param operation Protocol.OperationName[args], e.g. STABLE.foo[arg1 arg2 arg3]
     */
    protected void handleOperation(Map<String, String> map, String operation) throws Exception {
      int index = operation.indexOf(".");
      if (index == -1)
        throw new IllegalArgumentException(
            "operation " + operation + " is missing the protocol name");
      String prot_name = operation.substring(0, index);
      Protocol prot = prot_stack.findProtocol(prot_name);
      if (prot == null) return; // less drastic than throwing an exception...

      int args_index = operation.indexOf("[");
      String method_name;
      if (args_index != -1) method_name = operation.substring(index + 1, args_index).trim();
      else method_name = operation.substring(index + 1).trim();

      String[] args = null;
      if (args_index != -1) {
        int end_index = operation.indexOf("]");
        if (end_index == -1) throw new IllegalArgumentException("] not found");
        List<String> str_args =
            Util.parseCommaDelimitedStrings(operation.substring(args_index + 1, end_index));
        Object[] strings = str_args.toArray();
        args = new String[strings.length];
        for (int i = 0; i < strings.length; i++) args[i] = (String) strings[i];
      }

      Method method = MethodCall.findMethod(prot.getClass(), method_name, args);
      if (method == null) {
        log.warn(
            Util.getMessage("MethodNotFound"),
            local_addr,
            prot.getClass().getSimpleName(),
            method_name);
        return;
      }
      MethodCall call = new MethodCall(method);
      Object[] converted_args = null;
      if (args != null) {
        converted_args = new Object[args.length];
        Class<?>[] types = method.getParameterTypes();
        for (int i = 0; i < args.length; i++)
          converted_args[i] = MethodCall.convert(args[i], types[i]);
      }
      Object retval = call.invoke(prot, converted_args);
      if (retval != null) map.put(prot_name + "." + method_name, retval.toString());
    }
 /**
  * Calls a method remotely over the channel. Does nothing if {@link #isDroppedOut()} flag is set.
  *
  * @param id The ID of the method.
  * @param messageNumber The message number.
  * @param args The arguments.
  */
 protected void callRemoteMethod(final short id, final long messageNumber, final Object... args) {
   if (!cluster.channel.isConnected()) {
     return;
   }
   MethodCall call = createMethodCall(id, messageNumber, args);
   try {
     dispatcher.callRemoteMethods(null, call, ASYNC_REQUEST_OPTIONS);
     LOGGER.info(
         "Method called: "
             + methods.findMethod(id).getName()
             + " "
             + Arrays.toString(call.getArgs()));
   } catch (Exception e) {
     throw new RuntimeException(
         "Cannot call "
             + methods.findMethod(id)
             + " with parameters "
             + Arrays.toString(call.getArgs()),
         e);
   }
 }
Beispiel #3
0
 public Buffer objectToBuffer(Object obj) throws Exception {
   MethodCall call = (MethodCall) obj;
   ByteBuffer buf;
   switch (call.getId()) {
     case START:
     case GET_CONFIG:
       buf = ByteBuffer.allocate(Global.BYTE_SIZE);
       buf.put((byte) call.getId());
       return new Buffer(buf.array());
     case SET_OOB:
     case SET_SYNC:
       return new Buffer(booleanBuffer(call.getId(), (Boolean) call.getArgs()[0]));
     case SET_NUM_MSGS:
     case SET_NUM_THREADS:
     case SET_MSG_SIZE:
     case SET_ANYCAST_COUNT:
       return new Buffer(intBuffer(call.getId(), (Integer) call.getArgs()[0]));
     case GET:
       return new Buffer(longBuffer(call.getId(), (Long) call.getArgs()[0]));
     case PUT:
       Long long_arg = (Long) call.getArgs()[0];
       byte[] arg2 = (byte[]) call.getArgs()[1];
       buf =
           ByteBuffer.allocate(
               Global.BYTE_SIZE + Global.INT_SIZE + Global.LONG_SIZE + arg2.length);
       buf.put((byte) call.getId())
           .putLong(long_arg)
           .putInt(arg2.length)
           .put(arg2, 0, arg2.length);
       return new Buffer(buf.array());
     case SET_READ_PERCENTAGE:
       Double double_arg = (Double) call.getArgs()[0];
       buf = ByteBuffer.allocate(Global.BYTE_SIZE + Global.DOUBLE_SIZE);
       buf.put((byte) call.getId()).putDouble(double_arg);
       return new Buffer(buf.array());
     default:
       throw new IllegalStateException("method " + call.getMethod() + " not known");
   }
 }
Beispiel #4
0
    protected void handleJmx(Map<String, String> map, String input) {
      Map<String, Object> tmp_stats;
      int index = input.indexOf("=");
      if (index > -1) {
        List<String> list = null;
        String protocol_name = input.substring(index + 1);
        index = protocol_name.indexOf(".");
        if (index > -1) {
          String rest = protocol_name;
          protocol_name = protocol_name.substring(0, index);
          String attrs = rest.substring(index + 1); // e.g. "num_sent,msgs,num_received_msgs"
          list = Util.parseStringList(attrs, ",");

          // check if there are any attribute-sets in the list
          for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
            String tmp = it.next();
            index = tmp.indexOf("=");
            if (index != -1) {
              String attrname = tmp.substring(0, index);
              String attrvalue = tmp.substring(index + 1);
              Protocol prot = prot_stack.findProtocol(protocol_name);
              Field field = prot != null ? Util.getField(prot.getClass(), attrname) : null;
              if (field != null) {
                Object value = MethodCall.convert(attrvalue, field.getType());
                if (value != null) prot.setValue(attrname, value);
              } else {
                // try to find a setter for X, e.g. x(type-of-x) or setX(type-of-x)
                ResourceDMBean.Accessor setter =
                    ResourceDMBean.findSetter(
                        prot, attrname); // Util.getSetter(prot.getClass(), attrname);
                if (setter != null) {
                  try {
                    Class<?> type =
                        setter instanceof ResourceDMBean.FieldAccessor
                            ? ((ResourceDMBean.FieldAccessor) setter).getField().getType()
                            : setter instanceof ResourceDMBean.MethodAccessor
                                ? ((ResourceDMBean.MethodAccessor) setter)
                                    .getMethod()
                                    .getParameterTypes()[0]
                                    .getClass()
                                : null;
                    Object converted_value = MethodCall.convert(attrvalue, type);
                    setter.invoke(converted_value);
                  } catch (Exception e) {
                    log.error("unable to invoke %s() on %s: %s", setter, protocol_name, e);
                  }
                } else log.warn(Util.getMessage("FieldNotFound"), attrname, protocol_name);
              }

              it.remove();
            }
          }
        }
        tmp_stats = dumpStats(protocol_name, list);
        if (tmp_stats != null) {
          for (Map.Entry<String, Object> entry : tmp_stats.entrySet()) {
            Map<String, Object> tmp_map = (Map<String, Object>) entry.getValue();
            String key = entry.getKey();
            map.put(key, tmp_map != null ? tmp_map.toString() : null);
          }
        }
      } else {
        tmp_stats = dumpStats();
        if (tmp_stats != null) {
          for (Map.Entry<String, Object> entry : tmp_stats.entrySet()) {
            Map<String, Object> tmp_map = (Map<String, Object>) entry.getValue();
            String key = entry.getKey();
            map.put(key, tmp_map != null ? tmp_map.toString() : null);
          }
        }
      }
    }