Esempio n. 1
0
  public static OperationParams getAttributeParams(MBeanInfo info, String method, Object args[])
      throws PluginException {

    if (method.startsWith("set") || method.startsWith("get")) {
      method = method.substring(3);
    }

    MBeanAttributeInfo[] attrs = info.getAttributes();
    for (int i = 0; i < attrs.length; i++) {
      MBeanAttributeInfo attr = attrs[i];
      if (!attr.getName().equals(method)) {
        continue;
      }

      String sig = attr.getType();
      if (!hasConverter(sig)) {
        String msg = "Cannot convert String argument to " + sig;
        throw new PluginException(msg);
      }

      OperationParams params = new OperationParams();
      Object value = null;
      try {
        if (args.length > 0) {
          value = convert(sig, (String) args[0]);
        }
      } catch (Exception e) {
        String msg = "Exception converting param '" + args[0] + "' to type '" + sig + "'";
        throw new PluginException(msg + ": " + e);
      }
      if (value != null) {
        params.arguments = new Object[] {value};
      }
      params.isAttribute = true;
      return params;
    }

    return null;
  }
Esempio n. 2
0
  public static OperationParams getOperationParams(MBeanInfo info, String method, Object args[])
      throws PluginException {

    boolean isDebug = log.isDebugEnabled();
    MBeanOperationInfo[] ops = info.getOperations();
    MBeanParameterInfo[] pinfo = null;
    HashMap sigs = new HashMap();
    String methodSignature = null;

    if (args.length != 0) {
      String arg = (String) args[0];
      if (arg.startsWith("@(") && arg.endsWith(")")) {
        methodSignature = arg.substring(1);
        String[] dst = new String[args.length - 1];
        System.arraycopy(args, 1, dst, 0, dst.length);
        args = dst;
      }
    }

    if (isDebug) {
      String msg = "Converting params for: " + method + Arrays.asList(args);
      if (methodSignature != null) {
        msg += ", using provided signature: " + methodSignature;
      }
      log.debug(msg);
    }

    for (int i = 0; i < ops.length; i++) {
      if (ops[i].getName().equals(method)) {
        pinfo = ops[i].getSignature();
        StringBuffer sig = new StringBuffer();
        sig.append("(");
        for (int j = 0; j < pinfo.length; j++) {
          sig.append(pinfo[j].getType());
          if (j + 1 != pinfo.length) {
            sig.append(';');
          }
        }
        sig.append(')');
        log.debug("Found operation: " + method + sig);
        sigs.put(sig.toString(), pinfo);
        sigs.put(new Integer(pinfo.length), pinfo);
        // XXX might have more than 1 method w/ same
        // number of args but different signature
      }
    }

    if (sigs.size() == 0) {
      OperationParams op = getAttributeParams(info, method, args);
      if (op != null) {
        return op;
      }
      String msg = "No MBean Operation or Attribute Info found for: " + method;
      throw new PluginException(msg);
    } else if (sigs.size() > 1) {
      if (methodSignature == null) {
        // try exact match, else last one wins.
        Object o = sigs.get(new Integer(args.length));
        if (o != null) {
          pinfo = (MBeanParameterInfo[]) o;
          if (log.isDebugEnabled()) {
            log.debug("Using default sig: " + toString(pinfo));
          }
        }
      } else {
        pinfo = (MBeanParameterInfo[]) sigs.get(methodSignature);
        if (pinfo == null) {
          String msg = "No matching Operation signature found for: " + method + methodSignature;
          throw new PluginException(msg);
        } else if (log.isDebugEnabled()) {
          log.debug("Using matched sig: " + toString(pinfo));
        }
      }
    }

    int len = pinfo.length;
    int nargs = args.length;
    int consumed;
    String[] signature = new String[len];
    List arguments = new ArrayList();

    for (int i = 0, j = 0; i < len; i++, j += consumed) {
      consumed = 1;
      String sig = pinfo[i].getType();
      signature[i] = sig;

      if (!hasConverter(sig)) {
        String msg = "Cannot convert String argument to " + sig;
        throw new PluginException(msg);
      }

      if (j >= args.length) {
        throw invalidParams(method, sigs, args);
      }

      if (isListType(sig)) {
        String[] listArgs;
        if (len == 1) {
          listArgs = (String[]) args;
        } else {
          int remain = (len - 1) - j;
          consumed = args.length - j - remain;

          listArgs = new String[consumed];
          System.arraycopy(args, j, listArgs, 0, consumed);
        }

        nargs -= listArgs.length;
        try {
          arguments.add(convert(sig, listArgs));
        } catch (Exception e) {
          String msg =
              "Exception converting " + Arrays.asList(listArgs) + "' to type '" + sig + "'";
          throw new PluginException(msg + ": " + e);
        }
      } else {
        nargs--;
        try {
          arguments.add(convert(sig, (String) args[j]));
        } catch (Exception e) {
          String msg =
              "Exception converting param[" + j + "] '" + args[j] + "' to type '" + sig + "'";
          throw new PluginException(msg + ": " + e);
        }
      }

      if (isDebug) {
        Object arg = arguments.get(i);
        if (arg.getClass().isArray()) {
          if ((arg = asObjectArray(arg)) == null) {
            arg = arguments.get(i).toString();
          } else {
            arg = Arrays.asList((Object[]) arg).toString();
          }
        }
        log.debug(method + "() arg " + i + "=" + arg + ", type=" + sig);
      }
    }

    if (nargs != 0) {
      throw invalidParams(method, sigs, args);
    }

    OperationParams params = new OperationParams();
    params.signature = signature;
    params.arguments = arguments.toArray();
    return params;
  }