Commits

Anonymous committed 2340681

Moving object dispatcher from xwork to xwork-optional

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@283e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 63e30f4

Comments (0)

Files changed (8)

src/java/com/opensymphony/xwork/dispatcher/Mapping.java

-/*
- * Copyright (c) 2002-2003 by OpenSymphony
- * All rights reserved.
- */
-package com.opensymphony.xwork.dispatcher;
-
-
-/**
- * A mapping of data between a POJO and an XWork action
- * @author <a href="mailto:yellek@dev.java.net">Peter Kelley</a>
- */
-public class Mapping {
-    //~ Instance fields ////////////////////////////////////////////////////////
-
-    /** The OGNL used to set/get values on the action/value stack */
-    private String actionExpression;
-
-    /** The OGNL used to set/get values on the POJO */
-    private String pojoExpression;
-
-    //~ Constructors ///////////////////////////////////////////////////////////
-
-    /**
-     * Default constructor
-     * @param actionOGNL The OGNL used to set/get values on the action/value stack
-     * @param pojoOGNL The OGNL used to set/get values on the POJO
-     */
-    public Mapping(String actionOGNL, String pojoOGNL) {
-        setActionExpression(actionOGNL);
-        setPojoExpression(pojoOGNL);
-    }
-
-    //~ Methods ////////////////////////////////////////////////////////////////
-
-    /**
-     * Setter for property actionExpression.
-     * @param newActionExpression the new value for field actionExpression
-     */
-    public void setActionExpression(String newActionExpression) {
-        actionExpression = newActionExpression;
-    }
-
-    /**
-     * Getter for property actionExpression.
-     * @return the value of field actionExpression
-     */
-    public String getActionExpression() {
-        return actionExpression;
-    }
-
-    /**
-     * Setter for property pojoExpression.
-     * @param newPojoExpression the new value for field pojoExpression
-     */
-    public void setPojoExpression(String newPojoExpression) {
-        pojoExpression = newPojoExpression;
-    }
-
-    /**
-     * Getter for property pojoExpression.
-     * @return the value of field pojoExpression
-     */
-    public String getPojoExpression() {
-        return pojoExpression;
-    }
-}

src/java/com/opensymphony/xwork/dispatcher/MappingFactory.java

-/*
- * Copyright (c) 2002-2003 by OpenSymphony
- * All rights reserved.
- */
-package com.opensymphony.xwork.dispatcher;
-
-
-/**
- * This interface provides a way of accessing the POJO to action mapping configurations
- * @author <a href="mailto:yellek@dev.java.net">Peter Kelley</a>
- */
-public interface MappingFactory {
-    //~ Methods ////////////////////////////////////////////////////////////////
-
-    /**
-     * Get a mapping set with the given name.
-     * @param name The name of the mapping set to get
-     * @return Either the mapping set with the given name or <code>null</code> if no mapping set
-     * exists with that name
-     */
-    MappingSet getMappingSet(String name);
-}

src/java/com/opensymphony/xwork/dispatcher/MappingSet.java

-/*
- * Copyright (c) 2002-2003 by OpenSymphony
- * All rights reserved.
- */
-package com.opensymphony.xwork.dispatcher;
-
-import java.util.List;
-
-
-/**
- * A mapping set is a collection of {@link Mapping Mapping} objects categorized by type, each type
- * corresponding to a different type of data to set on the action.
- * @author <a href="mailto:yellek@dev.java.net">Peter Kelley</a>
- */
-public class MappingSet {
-    //~ Instance fields ////////////////////////////////////////////////////////
-
-    /** mappings for application data */
-    private List applicationMappings;
-
-    /** mappings for parameter data */
-    private List parameterMappings;
-
-    /** mappings for request data */
-    private List requestMappings;
-
-    /** mappings for results */
-    private List resultMappings;
-
-    /** mappings for session data */
-    private List sessionMappings;
-
-    /** name of this mapping set */
-    private String name;
-
-    //~ Constructors ///////////////////////////////////////////////////////////
-
-    /**
-     * default constructor
-     * @param setName The name of this mapping set
-     */
-    public MappingSet(String setName) {
-        name = setName;
-    }
-
-    //~ Methods ////////////////////////////////////////////////////////////////
-
-    /**
-     * Setter for property applicationMappings.
-     * @param newApplicationMappings the new value for field applicationMappings
-     */
-    public void setApplicationMappings(List newApplicationMappings) {
-        applicationMappings = newApplicationMappings;
-    }
-
-    /**
-     * Getter for property applicationMappings.
-     * @return the value of field applicationMappings
-     */
-    public List getApplicationMappings() {
-        return applicationMappings;
-    }
-
-    /**
-     * Getter for property name.
-     * @return the value of field name
-     */
-    public String getName() {
-        return name;
-    }
-
-    /**
-     * Setter for property parameterMappings.
-     * @param newParameterMappings the new value for field parameterMappings
-     */
-    public void setParameterMappings(List newParameterMappings) {
-        parameterMappings = newParameterMappings;
-    }
-
-    /**
-     * Getter for property parameterMappings.
-     * @return the value of field parameterMappings
-     */
-    public List getParameterMappings() {
-        return parameterMappings;
-    }
-
-    /**
-     * Setter for property requestMappings.
-     * @param newRequestMappings the new value for field requestMappings
-     */
-    public void setRequestMappings(List newRequestMappings) {
-        requestMappings = newRequestMappings;
-    }
-
-    /**
-     * Getter for property requestMappings.
-     * @return the value of field requestMappings
-     */
-    public List getRequestMappings() {
-        return requestMappings;
-    }
-
-    /**
-     * Setter for property resultMappings.
-     * @param newResultMappings the new value for field resultMappings
-     */
-    public void setResultMappings(List newResultMappings) {
-        resultMappings = newResultMappings;
-    }
-
-    /**
-     * Getter for property resultMappings.
-     * @return the value of field resultMappings
-     */
-    public List getResultMappings() {
-        return resultMappings;
-    }
-
-    /**
-     * Setter for property sessionMappings.
-     * @param newSessionMappings the new value for field sessionMappings
-     */
-    public void setSessionMappings(List newSessionMappings) {
-        sessionMappings = newSessionMappings;
-    }
-
-    /**
-     * Getter for property sessionMappings.
-     * @return the value of field sessionMappings
-     */
-    public List getSessionMappings() {
-        return sessionMappings;
-    }
-}

src/java/com/opensymphony/xwork/dispatcher/ObjectDispatcher.java

-/*
- * Copyright (c) 2002-2003 by OpenSymphony
- * All rights reserved.
- */
-package com.opensymphony.xwork.dispatcher;
-
-import com.opensymphony.xwork.ActionContext;
-import com.opensymphony.xwork.ActionProxy;
-import com.opensymphony.xwork.ActionProxyFactory;
-import com.opensymphony.xwork.config.ConfigurationException;
-import com.opensymphony.xwork.util.OgnlValueStack;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-
-/**
- * This class dispatches XWork actions based on data contained in POJO objects
- * @author <a href="mailto:yellek@dev.java.net">Peter Kelley</a>
- */
-public class ObjectDispatcher {
-    //~ Static fields/initializers /////////////////////////////////////////////
-
-    /** Logger for this class. */
-    protected static final Log LOG = LogFactory.getLog(ObjectDispatcher.class);
-
-    /** key for the mapping factory in the context */
-    public static final String MAPPING_FACTORY = "com.opensymphony.xwork.ActionContext.mappings";
-
-    /** key for the result factory in the context */
-    public static final String RESULT_FACTORY = "com.opensymphony.xwork.ActionContext.results";
-
-    //~ Instance fields ////////////////////////////////////////////////////////
-
-    /** mappings to use with this dispatcher */
-    private MappingFactory mappings;
-
-    //~ Constructors ///////////////////////////////////////////////////////////
-
-    /**
-     * Default constructor
-     * @param mappingFactory The factory to use to get the mappings between POJO's and actions
-     */
-    public ObjectDispatcher(MappingFactory mappingFactory) {
-        mappings = mappingFactory;
-    }
-
-    //~ Methods ////////////////////////////////////////////////////////////////
-
-    /**
-     * Merges all application attributes into a single <tt>HashMap</tt> to represent the
-     * entire <tt>Action</tt> context.
-     *
-     * @param requestMap a Map of all request attributes.
-     * @param parameterMap a Map of all request parameters.
-     * @param sessionMap a Map of all session attributes.
-     * @param applicationMap a Map of all servlet context attributes.
-     * @return a HashMap representing the <tt>Action</tt> context.
-     */
-    public static HashMap createContextMap(Map requestMap, Map parameterMap, Map sessionMap, Map applicationMap) {
-        HashMap extraContext = new HashMap();
-        extraContext.put(ActionContext.PARAMETERS, parameterMap);
-        extraContext.put(ActionContext.SESSION, sessionMap);
-        extraContext.put(ActionContext.APPLICATION, applicationMap);
-
-        // helpers to get access to request/session/application scope
-        extraContext.put("request", requestMap);
-        extraContext.put("session", sessionMap);
-        extraContext.put("application", applicationMap);
-        extraContext.put("parameters", parameterMap);
-
-        return extraContext;
-    }
-
-    /**
-     * Dispatch the action invocation
-     *
-     * @param source The object to copy the data from
-     * @param namespace The namespace of the action to execute (may be null)
-     * @param action The name of the action to execute
-     * @param mappingName The name of the mapping to use to copy the data
-     * @param resultObjectFactory The factory to use to set values on the result
-     * @throws Exception If there is a problem finding or executing the action
-     * @return The value stack that is the result of the execution. Note that if there is a result
-     * of type <code>object</code> then values will already have been set on objects obtained from
-     * <code>resultObjectFactory</code>.
-     */
-    public OgnlValueStack dispatch(Object source, String namespace, String action, String mappingName, ResultObjectFactory resultObjectFactory) throws Exception {
-        //Get the mappings
-        MappingSet mappingSet = mappings.getMappingSet(mappingName);
-
-        if (mappingSet == null) {
-            throw new ConfigurationException("Mapping with name " + mappingName + " does not exist");
-        }
-
-        Map sessionMap = getAttributeMap(mappingSet.getSessionMappings(), source);
-        Map requestMap = getAttributeMap(mappingSet.getRequestMappings(), source);
-        Map parameterMap = getAttributeMap(mappingSet.getParameterMappings(), source);
-        Map applicationMap = getAttributeMap(mappingSet.getApplicationMappings(), source);
-
-        Map extraContext = createContextMap(requestMap, parameterMap, sessionMap, applicationMap);
-
-        extraContext.put(MAPPING_FACTORY, mappings);
-        extraContext.put(RESULT_FACTORY, resultObjectFactory);
-
-        try {
-            ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, action, extraContext);
-            proxy.execute();
-
-            return proxy.getInvocation().getStack();
-        } catch (ConfigurationException e) {
-            LOG.error("Could not find action", e);
-            throw e;
-        } catch (Exception e) {
-            LOG.error("Could not execute action", e);
-            throw e;
-        }
-    }
-
-    /**
-     * Get a map containing data values from an object given a set of mappings
-     * @param mappingList <tt>List</tt> containing {@link Mapping Mapping} objects
-     * @param pojo The java object to use to get the values
-     * @return A <tt>map</tt> containing the data values specified by the pojoExpression properties
-     * of the mappings and keyed by the corresponding actionExpression properties
-     */
-    private Map getAttributeMap(List mappingList, Object pojo) {
-        Map result = new HashMap();
-
-        if (mappingList == null) {
-            // Nothing to do
-            return result;
-        } else {
-            OgnlValueStack sourceStack = new OgnlValueStack();
-            sourceStack.push(pojo);
-
-            Iterator iterator = mappingList.iterator();
-
-            while (iterator.hasNext()) {
-                Mapping mapping = (Mapping) iterator.next();
-                result.put(mapping.getActionExpression(), sourceStack.findValue(mapping.getPojoExpression()));
-            }
-
-            return result;
-        }
-    }
-}

src/java/com/opensymphony/xwork/dispatcher/ObjectResult.java

-/*
- * Copyright (c) 2002-2003 by OpenSymphony
- * All rights reserved.
- */
-package com.opensymphony.xwork.dispatcher;
-
-import com.opensymphony.xwork.ActionContext;
-import com.opensymphony.xwork.ActionInvocation;
-import com.opensymphony.xwork.Result;
-import com.opensymphony.xwork.util.OgnlValueStack;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import java.util.Iterator;
-import java.util.List;
-
-
-/**
- * XWork result that writes data to a Plain Old Java Object (POJO)
- * @author <a href="mailto:yellek@dev.java.net">Peter Kelley</a>
- */
-public class ObjectResult implements Result {
-    //~ Static fields/initializers /////////////////////////////////////////////
-
-    /** logger for this class */
-    private static final Log log = LogFactory.getLog(ObjectResult.class);
-
-    //~ Instance fields ////////////////////////////////////////////////////////
-
-    /** name of the mapping to use to map the results */
-    private String mappingName;
-
-    /** view name to retrieve from the result factory */
-    private String viewName;
-
-    //~ Constructors ///////////////////////////////////////////////////////////
-
-    /**
-     * Default constructor
-     */
-    public ObjectResult() {
-    }
-
-    //~ Methods ////////////////////////////////////////////////////////////////
-
-    /**
-     * Setter for property mappingName.
-     * @param newMappingName the new value for field mappingName
-     */
-    public void setMappingName(String newMappingName) {
-        mappingName = newMappingName;
-    }
-
-    /**
-     * Getter for property mappingName.
-     * @return the value of field mappingName
-     */
-    public String getMappingName() {
-        return mappingName;
-    }
-
-    /**
-     * Setter for property viewName.
-     * @param newViewName the new value for field viewName
-     */
-    public void setViewName(String newViewName) {
-        viewName = newViewName;
-    }
-
-    /**
-     * Getter for property viewName.
-     * @return the value of field viewName
-     */
-    public String getViewName() {
-        return viewName;
-    }
-
-    /**
-     * Write the data to the result object (or objects) based on a given mapping. Result objects
-     * and mappings are obtained from factories set in the action context.
-     *
-     * @param invocation The invocation object to process the result for
-     * @throws Exception if something goes wrong
-     */
-    public void execute(ActionInvocation invocation) throws Exception {
-        if (log.isDebugEnabled()) {
-            log.debug("executing result");
-        }
-
-        List mappings = getMappings(invocation);
-
-        if (mappings == null) {
-            log.warn("No result mappings for mapping set with name " + getMappingName());
-        } else {
-            Object resultObject = getResultObject(invocation);
-
-            if (resultObject == null) {
-                log.warn("No result object found with name " + getViewName());
-            } else {
-                copyResultData(invocation, mappings, resultObject);
-            }
-        }
-    }
-
-    /**
-     * Get the mappings
-     * @param invocation The invocation we are processing the result for
-     * @throws IllegalArgumentException
-     * @return List
-     */
-    private List getMappings(ActionInvocation invocation) throws IllegalArgumentException {
-        List mappings = null;
-
-        ActionContext context = ActionContext.getContext();
-        MappingFactory mappingFactory = (MappingFactory) context.get(ObjectDispatcher.MAPPING_FACTORY);
-
-        if (mappingFactory == null) {
-            String message = "Mapping factory not set in action context";
-            log.error(message);
-        } else {
-            MappingSet mappingSet = mappingFactory.getMappingSet(getMappingName());
-
-            if (mappingSet == null) {
-                String message = "Mapping with name " + getMappingName() + " not found.";
-                log.error(message);
-                throw new IllegalArgumentException(message);
-            }
-
-            mappings = mappingSet.getResultMappings();
-        }
-
-        return mappings;
-    }
-
-    /**
-     * Get the result object
-     * @param invocation The invocation we are processing the result for
-     * @return The object to set the data on
-     */
-    private Object getResultObject(ActionInvocation invocation) throws IllegalArgumentException {
-        Object resultObject = null;
-        ActionContext context = ActionContext.getContext();
-        ResultObjectFactory resultFactory = (ResultObjectFactory) context.get(ObjectDispatcher.RESULT_FACTORY);
-
-        if (resultFactory == null) {
-            String message = "Result factory not set in action context";
-            log.error(message);
-        } else {
-            resultObject = resultFactory.getResultObject(getViewName());
-
-            if (resultObject == null) {
-                String message = "Result view with name " + getViewName() + " not found.";
-                log.error(message);
-                throw new IllegalArgumentException(message);
-            }
-        }
-
-        return resultObject;
-    }
-
-    /**
-     * Copy the data to the result object
-     * @param invocation The invocation we are processing the result for
-     * @param mappings The collection of mappings to use to map the data
-     * @param resultObject The object to set the data on
-     */
-    private void copyResultData(ActionInvocation invocation, List mappings, Object resultObject) {
-        //Copy the data to the result object
-        OgnlValueStack sourceStack = invocation.getStack();
-        OgnlValueStack resultStack = new OgnlValueStack();
-        resultStack.push(resultObject);
-
-        Iterator iterator = mappings.iterator();
-
-        while (iterator.hasNext()) {
-            Mapping mapping = (Mapping) iterator.next();
-            Object dataValue = sourceStack.findValue(mapping.getActionExpression());
-            resultStack.setValue(mapping.getPojoExpression(), dataValue);
-        }
-    }
-}

src/java/com/opensymphony/xwork/dispatcher/ResultObjectFactory.java

-/*
- * Copyright (c) 2002-2003 by OpenSymphony
- * All rights reserved.
- */
-package com.opensymphony.xwork.dispatcher;
-
-
-/**
- * This interface provides access to result objects for the
- * {@link com.opensymphony.xwork.result.ObjectResult ObjectResult} result to use.
- * @author <a href="mailto:yellek@dev.java.net">Peter Kelley</a>
- */
-public interface ResultObjectFactory {
-    //~ Methods ////////////////////////////////////////////////////////////////
-
-    /**
-     * Get the result object corresponding to the given key
-     * @param key The key of the object to get
-     * @return Either the result object corresponding to the given key or <code>null</code> if no
-     * result object exists for that key
-     */
-    Object getResultObject(String key);
-}

src/test/com/opensymphony/xwork/dispatcher/ObjectDispatcherTest.java

-/*
- * Copyright (c) 2002-2003 by OpenSymphony
- * All rights reserved.
- */
-package com.opensymphony.xwork.dispatcher;
-
-import com.opensymphony.xwork.util.OgnlValueStack;
-
-import junit.framework.*;
-
-import java.util.ArrayList;
-import java.util.List;
-
-
-/**
- * test the object dispatcher
- * @author Peter Kelley
- */
-public class ObjectDispatcherTest extends TestCase implements MappingFactory, ResultObjectFactory {
-    //~ Instance fields ////////////////////////////////////////////////////////
-
-    private ObjectDispatcher objectDispatcher = null;
-    private String bar = "bar";
-    private String foo = "foo";
-    private String foobar = "something else";
-
-    //~ Constructors ///////////////////////////////////////////////////////////
-
-    /**
-     * default constructor
-     * @param name The name of the test fixture
-     */
-    public ObjectDispatcherTest(String name) {
-        super(name);
-    }
-
-    //~ Methods ////////////////////////////////////////////////////////////////
-
-    /**
-     * Setter for property bar.
-     * @param newBar the new value for field bar
-     */
-    public void setBar(String newBar) {
-        bar = newBar;
-    }
-
-    /**
-     * Getter for property bar.
-     * @return the value of field bar
-     */
-    public String getBar() {
-        return bar;
-    }
-
-    /**
-     * Setter for property foo.
-     * @param newFoo the new value for field foo
-     */
-    public void setFoo(String newFoo) {
-        foo = newFoo;
-    }
-
-    /**
-     * Getter for property foo.
-     * @return the value of field foo
-     */
-    public String getFoo() {
-        return foo;
-    }
-
-    /**
-     * Setter for property foobar.
-     * @param newFoobar the new value for field foobar
-     */
-    public void setFoobar(String newFoobar) {
-        foobar = newFoobar;
-    }
-
-    /**
-     * Getter for property foobar.
-     * @return the value of field foobar
-     */
-    public String getFoobar() {
-        return foobar;
-    }
-
-    /**
-     * Get a mapping set with the given name.
-     *
-     * @param name The name of the mapping set to get
-     * @return Either the mapping set with the given name or <code>null</code> if no mapping set
-     *   exists with that name
-     */
-    public MappingSet getMappingSet(String name) {
-        MappingSet mappingSet = new MappingSet(name);
-
-        Mapping newMapping = new Mapping("blah", "foo + bar");
-        List parameterList = new ArrayList();
-        parameterList.add(newMapping);
-        mappingSet.setParameterMappings(parameterList);
-
-        Mapping resultMapping = new Mapping("blah", "foobar");
-        List resultList = new ArrayList();
-        resultList.add(resultMapping);
-        mappingSet.setResultMappings(resultList);
-
-        return mappingSet;
-    }
-
-    /**
-     * Get the result object
-     * @param key The key of the object to get (ignored)
-     * @return this
-     */
-    public Object getResultObject(String key) {
-        return this;
-    }
-
-    /**
-     * Test dispatching from an object
-     * @throws Exception
-     */
-    public void testDispatch() throws Exception {
-        String namespace = "";
-        String action = "TestObjectDispatcher";
-        String mappingName = "dispatch1";
-        ResultObjectFactory resultObjectFactory = this;
-        OgnlValueStack result = objectDispatcher.dispatch(this, namespace, action, mappingName, resultObjectFactory);
-        assertEquals("Value returned was incorrect", getFoo() + getBar(), result.findValue("blah"));
-        assertEquals("Value on result not set correctly", getFoo() + getBar(), getFoobar());
-    }
-
-    /**
-     * set up for a test
-     * @throws Exception if something bad happens
-     */
-    protected void setUp() throws Exception {
-        super.setUp();
-        objectDispatcher = new ObjectDispatcher(this);
-    }
-
-    /**
-     * clean up after a test
-     * @throws Exception if something bad happens
-     */
-    protected void tearDown() throws Exception {
-        objectDispatcher = null;
-        super.tearDown();
-    }
-}

src/test/com/opensymphony/xwork/dispatcher/ObjectTestAction.java

-/*
- * Copyright (c) 2002-2003 by OpenSymphony
- * All rights reserved.
- */
-package com.opensymphony.xwork.dispatcher;
-
-import com.opensymphony.xwork.ActionSupport;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-
-/**
- * Test action for object dispatching
- * @author <a href="mailto:yellek@dev.java.net">Peter Kelley</a>
- */
-public class ObjectTestAction extends ActionSupport {
-    //~ Instance fields ////////////////////////////////////////////////////////
-
-    /** logger for this class */
-    protected Log log = LogFactory.getLog(ObjectTestAction.class);
-
-    /** blah text property */
-    private String blah;
-
-    //~ Constructors ///////////////////////////////////////////////////////////
-
-    public ObjectTestAction() {
-    }
-
-    //~ Methods ////////////////////////////////////////////////////////////////
-
-    /**
-     * Setter for property blah.
-     * @param newBlah the new value for field blah
-     */
-    public void setBlah(String newBlah) {
-        blah = newBlah;
-    }
-
-    /**
-     * Getter for property blah.
-     * @return the value of field blah
-     */
-    public String getBlah() {
-        return blah;
-    }
-
-    /**
-     * This methodName is where the logic of the action is executed.
-     *
-     * @return a string representing the logical result of the execution. See constants in this
-     *   interface for a list of standard result values.
-     * @throws Exception thrown if a system level exception occurs. Application level exceptions
-     *   should be handled by returning an error value, such as Action.ERROR.
-     * @todo Implement this com.opensymphony.xwork.Action method
-     */
-    public String execute() throws Exception {
-        if (log.isDebugEnabled()) {
-            log.debug("Blah was " + getBlah());
-        }
-
-        return SUCCESS;
-    }
-}