craigmcc 01/09/03 19:56:51 Added: workflow/src/java/org/apache/commons/workflow/web ActivityServlet.java Log: Add an example servlet demonstrating one way to integrate workflow processing into a web application. The theory of operation is that you will create a servlet definition (using this servlet class), and one or more servlet mappings, for each workflow Activity that is supported by your web application. Revision Changes Path 1.1 jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/web/ActivityServlet.java Index: ActivityServlet.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/web/ActivityServlet.java,v 1.1 2001/09/04 02:56:51 craigmcc Exp $ * $Revision: 1.1 $ * $Date: 2001/09/04 02:56:51 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2001 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", "Commons", 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.commons.workflow.web; import java.io.InputStream; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.UnavailableException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.commons.digester.Digester; import org.apache.commons.workflow.Activity; import org.apache.commons.workflow.StepException; import org.apache.commons.workflow.base.BaseRuleSet; import org.apache.commons.workflow.core.CoreRuleSet; import org.apache.commons.workflow.io.IoRuleSet; import org.apache.commons.workflow.web.WebContext; import org.apache.commons.workflow.web.WebRuleSet; /** * <p>Demonstration servlet that illustrates how workflow support can be * integrated into web applications (or web services) without any * dependencies on application frameworks. A servlet <em>definition</em> * (plus one or more servlet <em>mappings</em>) will be associated with * each <code>Activity</code> supported by a particular web application.</p> * * <p>The following servlet initialization parameters are supported, with * default values in square brackets):</p> * <ul> * <li><strong>activity</strong> - Context-relative resource path to the * definition file for the <code>Activity</code> that is supported by * this servlet definition.</li> * <li><strong>attribute</strong> - Session attribute name under which our * current <code>Context</code> instance is stored. * [org.apache.commons.workflow.web.CONTEXT]</li> * </ul> * * @author Craig R. McClanahan * @version $Revision: 1.1 $ $Date: 2001/09/04 02:56:51 $ */ public class ActivityServlet extends HttpServlet { // ----------------------------------------------------- Instance Variables /** * The <code>Activity</code> that is supported by this servlet definition. */ protected Activity activity = null; /** * Name of the session attribute for our current <code>Context</code>. */ protected String attribute = "org.apache.commons.workflow.web.CONTEXT"; // --------------------------------------------------------- Public Methods /** * Perform graceful shutdown of this servlet definition. */ public void destroy() { ; // No processing required } /** * Process a GET transaction. * * @param request The servlet request we are processing * @param response The servlet response we are processing * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet exception occurs */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doPost(request, response); } /** * Process a POST transaction. * * @param request The servlet request we are processing * @param response The servlet response we are processing * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet exception occurs */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Acquire or create the current Context for this user HttpSession session = request.getSession(true); WebContext context = (WebContext) session.getAttribute(attribute); if (context == null) { context = new WebContext(); context.setActivity(activity); context.setHttpSession(session); context.setServletContext(getServletContext()); session.setAttribute(attribute, context); } // If we are not already executing our associated Activity, call it // (to facilitate nesting by simply linking to the URI associated // with a different activity) if (!activity.equals(context.getActivity())) context.call(activity); // Execute our activity until suspended or completed (synchronized // to avoid race conditions in framed environments, or other situations // where multiple simultaneous requests might be active) synchronized (context) { context.setServletRequest(request); context.setServletResponse(response); try { context.execute(); } catch (StepException e) { if (e.getCause() == null) throw new ServletException(e.getMessage(), e); else throw new ServletException(e.getMessage(), e.getCause()); } } } /** * Perform graceful startup of this servlet definition. * * @exception ServletException if we cannot process the activity * definition file for this activity */ public void init() throws ServletException { // Record the session attribute name for our current Context String value = getServletConfig().getInitParameter("attribute"); if (value != null) attribute = value; // Parse the activity definition resource for our Activity String path = getServletConfig().getInitParameter("activity"); if (path == null) throw new UnavailableException ("Must specify an 'activity' initialization parameter"); parse(path); if (activity == null) throw new UnavailableException("No activity defined in resource " + path); } /** * Set the <code>Activity</code> associated with this instance. * * @param activity The new associated Activity */ public void setActivity(Activity activity) { this.activity = activity; } // ------------------------------------------------------ Protected Methods /** * Parse the activity definition resource for this servlet definition. * * @param path Context-relative resource path of the activity definition * resource * * @exception ServletException on any processing error during parsing */ protected void parse(String path) throws ServletException { // Get an input source for the specified resource path InputStream is = getServletContext().getResourceAsStream(path); if (is == null) throw new UnavailableException("Cannot access resource " + path); // Configure a Digester instance to parse our activity definition Digester digester = new Digester(); digester.setDebug(999); digester.setNamespaceAware(true); digester.setValidating(false); digester.push(this); // Add rules to recognize the built-in Steps that we know about BaseRuleSet brs = new BaseRuleSet(); digester.addRuleSet(brs); digester.addRuleSet(new CoreRuleSet()); digester.addRuleSet(new IoRuleSet()); digester.addRuleSet(new WebRuleSet()); // Add a rule to register the Activity being created digester.setRuleNamespaceURI(brs.getNamespaceURI()); digester.addSetNext("activity", "setActivity", "org.apache.commons.workflow.Activity"); // Parse the activity definition try { digester.parse(is); } catch (Throwable t) { log("Cannot parse resource " + path, t); throw new UnavailableException("Cannot parse resource " + path); } finally { try { is.close(); } catch (Throwable u) { ; } } } }