Exemplo n.º 1
0
  @Override
  public Object getValue(String name) {

    if (name.equals(this.name)) {

      Object value = super.getValue(name);

      if (value != null) return value;

      Action action = this.action.get();

      if (action == null) throw new IllegalStateException("Action cannot be null here!");

      Output output = action.getOutput();

      setValue(name, output);

      return output;

    } else {

      return super.getValue(name);
    }
  }
Exemplo n.º 2
0
  public String filter(InvocationChain chain) throws Exception {

    if (name != null) {

      // new filter! (the old one is deprecated, should use
      // OutjectionFilter instead!)

      return filterNew(chain);
    }

    String result = chain.invoke();

    Action action = chain.getAction();
    Output output = action.getOutput();

    boolean isModelDriven = false;

    boolean isPojoAction = false;

    if (action instanceof ModelDriven) {

      isModelDriven = true;

    } else if (action instanceof PojoAction) {

      isPojoAction = true;
    }

    Method[] methods = null;
    if (isModelDriven) {
      ModelDriven md = (ModelDriven) action;
      methods = md.getModel().getClass().getMethods();
    } else if (isPojoAction) {

      PojoAction pa = (PojoAction) action;
      methods = pa.getPojo().getClass().getMethods();

    } else {
      methods = action.getClass().getDeclaredMethods();
    }

    for (int i = 0; i < methods.length; i++) {
      String name = methods[i].getName();
      if (name.length() > 3
          && name.startsWith("get")
          && methods[i].getParameterTypes().length == 0) {

        if (name.equals("getClass")) continue;

        try {
          methods[i].setAccessible(true);

          Object value = null;

          if (isModelDriven) {

            ModelDriven md = (ModelDriven) action;

            value = methods[i].invoke(md.getModel(), (Object[]) null);

          } else if (isPojoAction) {

            PojoAction pa = (PojoAction) action;

            value = methods[i].invoke(pa.getPojo(), (Object[]) null);

          } else {

            value = methods[i].invoke(action, new Object[0]);
          }

          output.setValue(adjustName(name), value);

        } catch (Exception e) {
          System.err.println("Error calling method in OutputFilter: " + name);
          e.printStackTrace();
        }
      }
    }
    return result;
  }