Just to play around with this idea, I've attached my initial hack at a
1.x-compatible composable request processor using struts-chain.  Let me
know if this is anywhere near the mark for a 1.x request processor.

Here's a few notes/questions:
1)  It does not do the "servlet-complete" chain b/c in 1.x the module is
already selected by the time the request processor is executed as far as
I can tell.

2)  The XML config file is hard-coded into the initialize method.  I
assume we'd want that to be more configurable.

3)  The "servlet-standard" chain is executed in the process() method.  I
also assume we'd want that to be configurable.

4)  I wasn't sure if any synchronization needed to occur around the
creation/destroying of the catalog, so I left it out.

5)  If an exception occurs in the process() method, we may want to call
a processException chain or something more flexible than how I handled it.

To make it go I added this class and the struts-chain code to the source
tree and the commons-chain.jar to the build script.  I then added the
chain-config.xml file to my WEB-INF directory and it worked -- at least
for the simple case of executing an action.  Next, I'm going to see
about integrating Tiles and extending the chain.  I would appreciate
feedback on this.  We've come up with several places where this could be
very useful, so I'm probably going to be working on something like it
whether it ends up in Struts 1.x or not.

Greg



DISCLAIMER:
This email message is for the sole use of the intended recipient(s) and may contain 
confidential and privileged information.  Any unauthorized review, use, disclosure or 
distribution is prohibited.  If you are not the intended recipient, please contact the 
sender by reply email and destroy all copies of the original message and attachments.
/*
 * $Header: 
/home/cvspublic/jakarta-struts/src/share/org/apache/struts/action/RequestProcessor.java,v
 1.32 2003/07/02 04:06:53 dgraham Exp $
 * $Revision: 1.32 $
 * $Date: 2003/07/02 04:06:53 $
 *
 * ====================================================================
 *
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Struts", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

package org.apache.struts.chain;

import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.RequestProcessor;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.ModuleConfig;

import org.apache.commons.chain.web.servlet.ServletWebContext;
import org.apache.commons.chain.Catalog;
import org.apache.commons.chain.impl.CatalogBase;
import org.apache.commons.chain.Command;
import org.apache.commons.chain.config.ConfigParser;

/**
 * <p><strong>RequestProcessor</strong> contains the processing logic that
 * the Struts controller servlet performs as it receives each servlet request
 * from the container.  You can customize the request processing behavior by
 * subclassing this class and overriding the method(s) whose behavior you are
 * interested in changing.</p>
 *
 * @author Craig R. McClanahan
 * @author Cedric Dumoulin
 * @author Greg Reddin
 *
 * @version $Revision: 1.32 $ $Date: 2003/07/02 04:06:53 $
 * @since Struts 1.1
 */
public class ComposableRequestProcessor extends RequestProcessor {


    // ----------------------------------------------------- Instance Variables

    /**
     * The catalog containing all of the available command chains for this
     * module.
     */
    protected Catalog catalog;

    // --------------------------------------------------------- Public Methods


    /**
     * Clean up in preparation for a shutdown of this application.
     */
    public void destroy() {

        synchronized (this.actions) {
            Iterator actions = this.actions.values().iterator();
            while (actions.hasNext()) {
                Action action = (Action) actions.next();
                action.setServlet(null);
            }
            this.actions.clear();
        }
        this.servlet = null;

        // Does this need to be synchronized?
        this.catalog = null;

    }


    /**
     * Initialize this request processor instance.
     *
     * @param servlet The ActionServlet we are associated with
     * @param moduleConfig The ModuleConfig we are associated with.
     * @throws ServletException If an error occor during initialization
     */
    public void init(ActionServlet servlet,
                     ModuleConfig moduleConfig)
           throws ServletException {

        synchronized (actions) {
            actions.clear();
        }
        
        this.servlet = servlet;
        this.moduleConfig = moduleConfig;

        // Load chains for this module.
        // does this need to be synchronized ?
        catalog = new CatalogBase();
        ConfigParser configParser = new ConfigParser();
    
        try {
            String path = "/WEB-INF/chain-config.xml";
            URL configFile = getServletContext().getResource(path);
            configParser.parse(catalog, configFile);
        } catch (MalformedURLException e) {
            throw new ServletException(e);
        } catch (IOException e) {
            throw new ServletException(e);
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }


    /**
     * <p>Process an <code>HttpServletRequest</code> and create the
     * corresponding <code>HttpServletResponse</code>.</p>
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a processing exception occurs
     */
    public void process(HttpServletRequest request,
                        HttpServletResponse response)
        throws IOException, ServletException {

        // Create the context.
        ServletWebContext context = new ServletWebContext();
        context.initialize(getServletContext(), request, response);
        context.getAttributes().put("catalog", this.catalog);
        context.getAttributes().put(Constants.MODULE_CONFIG_KEY, this.moduleConfig);

        // Create and execute the command.
        Command command = this.catalog.getCommand("servlet-standard");
        try {
            command.execute(context);
        } catch (Exception e) {
            // Execute the exception processing chain??
            throw new ServletException(e);
        }

        // Release the context.
        context.release();
    }
}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to