Пример #1
0
  public Object execute(IEval aEval, IContext aCtx, Object[] aArgs) throws CommandException {
    try {
      Object[] lArgs = aArgs;

      if (argList != null) {
        lArgs = argList.guard(aArgs, aCtx);
      }

      if (argMapping != null) {
        lArgs = argMapping.map(aEval, aCtx, lArgs);
      }

      Object lResult = method.invoke(instance, lArgs);

      if (resultMapping != null) {
        resultMapping.map(lResult, aCtx);
      }

      return lResult;
    } catch (IllegalAccessException e) {
      throw new CommandException(
          String.format(
              "Command '%s' illegal access exception.\n%s",
              aArgs[0], CmdUtil.concatExceptionMessages(e)));
    } catch (InvocationTargetException e) {
      // The internal invocation threw an exception, we have to fetch the original exception.
      // We don't add our own message at this level, this handler should be transparent because it
      // does
      // not add extra information.
      Throwable lOrigExc = e.getTargetException();
      throw new CommandException(CmdUtil.concatExceptionMessages(lOrigExc));
    } catch (ArgMappingException e) {
      throw new CommandException(
          String.format(
              "Command '%s' parameter mapping exception.\n%s",
              aArgs[0], CmdUtil.concatExceptionMessages(e)));
    } catch (ArgSpecException e) {
      final String lMsg =
          String.format(
              "Command '%s' was invoked with incorrect arguments.\n%s",
              aArgs[0], CmdUtil.concatExceptionMessages(e));
      throw new CommandException(lMsg);
    } catch (IllegalArgumentException e) {
      final Class lClass = method.getDeclaringClass();
      final String lMsg =
          String.format(
              "Command '%s' internal error. One of the parameters in method '%s' in class '%s' has a wrong type.",
              aArgs[0], method.getName(), lClass.getName());
      throw new CommandException(lMsg);
    } catch (ResultMappingException e) {
      throw new CommandException(
          String.format(
              "Command '%s' the result could not be bound to the context.\n%s",
              aArgs[0], CmdUtil.concatExceptionMessages(e)));
    }
  }