001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.workflow.util;
006:
007: import com.opensymphony.module.propertyset.PropertySet;
008:
009: import com.opensymphony.workflow.*;
010:
011: import webwork.action.Action;
012: import webwork.action.ActionContext;
013:
014: import webwork.dispatcher.ActionResult;
015: import webwork.dispatcher.GenericDispatcher;
016:
017: import java.lang.reflect.Method;
018:
019: import java.security.Principal;
020:
021: import java.util.*;
022:
023: /**
024: * DOCUMENT ME!
025: *
026: * @author $author$
027: * @version $Revision: 1.2 $
028: */
029: public class WebWorkValidator implements Validator {
030: //~ Methods ////////////////////////////////////////////////////////////////
031:
032: public void validate(Map transientVars, Map args, PropertySet ps)
033: throws WorkflowException {
034: final WorkflowContext wfContext = (WorkflowContext) transientVars
035: .get("context");
036:
037: String actionName = (String) args.get("action.name");
038: GenericDispatcher gd = new GenericDispatcher(actionName);
039: gd.prepareContext();
040: ActionContext.setPrincipal(new Principal() {
041: public String getName() {
042: return wfContext.getCaller();
043: }
044: });
045: ActionContext.setApplication(args);
046: ActionContext.setSession(ps.getProperties(""));
047: ActionContext.setLocale(Locale.getDefault());
048: ActionContext.setParameters(Collections
049: .unmodifiableMap(transientVars));
050:
051: boolean hasErrors = false;
052: InvalidInputException iie = new InvalidInputException();
053:
054: try {
055: gd.executeAction();
056:
057: ActionResult ar = gd.finish();
058: gd.finalizeContext();
059:
060: List actions = ar.getActions();
061:
062: for (Iterator iterator = actions.iterator(); iterator
063: .hasNext();) {
064: Action action = (Action) iterator.next();
065:
066: List errorMessages = getErrorMessages(action);
067:
068: for (Iterator iterator2 = errorMessages.iterator(); iterator2
069: .hasNext();) {
070: String error = (String) iterator2.next();
071: iie.addError(error);
072: hasErrors = true;
073: }
074:
075: Map errors = getErrors(action);
076:
077: for (Iterator iterator2 = errors.entrySet().iterator(); iterator2
078: .hasNext();) {
079: Map.Entry entry = (Map.Entry) iterator2.next();
080: String error = (String) entry.getKey();
081: String message = (String) entry.getValue();
082: iie.addError(error, message);
083: hasErrors = true;
084: }
085: }
086: } catch (Exception e) {
087: throw new WorkflowException("Could not execute action "
088: + actionName, e);
089: }
090:
091: if (hasErrors) {
092: throw iie;
093: }
094: }
095:
096: private List getErrorMessages(Action action) {
097: try {
098: Method m = action.getClass().getMethod("getErrorMessages",
099: new Class[] {
100:
101: });
102:
103: return (List) m.invoke(action, new Object[] {});
104: } catch (Throwable t) {
105: return Collections.EMPTY_LIST;
106: }
107: }
108:
109: private Map getErrors(Action action) {
110: try {
111: Method m = action.getClass().getMethod("getErrors",
112: new Class[] {});
113:
114: return (Map) m.invoke(action, new Object[] {});
115: } catch (Throwable t) {
116: return Collections.EMPTY_MAP;
117: }
118: }
119: }
|