private String filterNew(InvocationChain chain) throws Exception { Action action = chain.getAction(); super.setInput(action.getInput()); action.setInput(this); this.action.set(action); return chain.invoke(); }
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; }
/** * Filters the action, begining a transaction before the action's execution and commiting or * rollbacking the trasaction after the action's exection depending on the result. */ public String filter(InvocationChain chain) throws Exception { Action action = chain.getAction(); Input input = action.getInput(); if (onlyPost) { // only execute for POST... String method = input.getProperty("method"); boolean isPost = method != null && method.equalsIgnoreCase("post"); if (!isPost) return chain.invoke(); } Transaction transaction = (Transaction) input.getValue(transactionKey); // special case: two actions with transaction filter being called in a chain: /* * if (transaction != null && (transaction.wasCommited() || transaction.wasRolledBack())) { * * input.removeValue(transactionKey); * * transaction = (Transaction) input.getValue(transactionKey); } * * // by Sergio: You can now freely re-use transaction (since 1.16) */ if (transaction == null) { Debug.log(NAME, "Transaction was NULL inside TransactionFilter!!!"); throw new FilterException( "Cannot find transaction in action's " + "input with the given key: " + transactionKey); } try { Debug.log(NAME, "Beginning transaction..."); transaction.begin(); Debug.log(NAME, "Transaction was begun! Will invoke action..."); String result = chain.invoke(); boolean shouldRollback = resultsForRollback.contains(result); boolean shouldCommit = resultsForCommit.contains(result); if (shouldCommit || (!shouldCommit && !shouldRollback)) { Debug.log(NAME, "Result was ok! Will commit the transaction...", "Result=", result); transaction.commit(); Debug.log(NAME, "Transaction was committed!"); } else { Debug.log(NAME, "Result was not ok! Will rollback the transaction...", "Result=", result); transaction.rollback(); Debug.log(NAME, "Transaction was rolled back!"); } return result; } catch (Exception e) { e.printStackTrace(); // rollbacks the transcion if any error occours. Debug.log( NAME, "An exception was thrown while executing action! Will try to rollback...", "msg=", e.getMessage()); if (transaction.isActive()) { transaction.rollback(); Debug.log(NAME, "Transaction was rolled back!"); } throw e; } }