Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Dispatcher eases workflow implementation

Improve your application's workflow with the Dispatcher design pattern and XSL

  • Print
  • Feedback

Page 4 of 7

Figure 4. Employment workflow activity diagram. Click on thumbnail to view full-size image.

The XMLDispatcher seems like a good choice to accomplish these goals. However, because it doesn't possess a Web-based component, one has to be built. In that task, it's helpful to employ the Front Controller pattern to build a servlet.

With that in mind, the WorkFlowServlet accepts requests from Webpages (as long as they follow a few naming conventions) and then uses the AbstractDispatcher to figure out the next screen. Here's the code:

package workflow.servlet;
/* Generated by Together */
import javax.servlet.http.*;
import javax.servlet.*;
import java.beans.Beans;
import java.io.IOException;
import workflow.*;
public class WorkflowServlet extends HttpServlet {
      public void doPost(HttpServletRequest req, HttpServletResponse resp) {
        // Determine the operation 'add' or 'next'
        String operation = req.getParameter("operation");
        try
        {
          System.out.println("applying workflow");
          // Get the session.
          HttpSession session = req.getSession(true);
          // What workflow are they using?
          String workflowName = (String)session.getAttribute("workflowName");
          // What dispatcher are they using?
          AbstractDispatcher dispatcher =
             (AbstractDispatcher)session.getAttribute("dispatcher");
          // If there isn't already a dispatcher, we'll create one.
          if ( dispatcher == null )
          {
            // We determine what factory to use. To keep it simple,
            // we'll let the client tell us.
            String factoryName = (String)session.getAttribute("factoryName");
            // Get the factory.
            AbstractDispatcherFactory dispatcherFactory =
              (AbstractDispatcherFactory)Beans.instantiate
              (Class.forName(factoryName).getClassLoader(),factoryName);
            // Get the dispatcher.
            dispatcher = dispatcherFactory.getDispatcher(workflowName);
            // Put the dispatcher on the session so it can be used again.
            session.setAttribute("dispatcher",dispatcher);
          }
          // Get the controller.
          Controller controller =
            (Controller)session.getAttribute("controller");
          // If there isn't a controller, create one.
          if (controller == null)
          {
            String controllerName =
              (String)session.getAttribute("controllerName");
            controller = (Controller) Beans.instantiate
              (Class.forName(controllerName).getClassLoader(),controllerName);
            session.setAttribute("controller",controller);
          }
          // Let the controller process the data.
          controller.process(req,resp);
          // Get the dispatcher.
          Dispatchable dispatchable =
            (Dispatchable)session.getAttribute("dispatchable");
          String nextScreen = new String();
          if ( operation.equalsIgnoreCase("next") )
          {
            // Get the next screen.
            nextScreen = dispatcher.next(null,dispatchable,
               req.getParameter("current"));
            session.setAttribute("nextScreen", nextScreen);
          }
          else if (operation.equalsIgnoreCase("add")
             || operation.equalsIgnoreCase("remove") )
          {
            // nextScreen stays the same.
          }
          System.out.println("The next screen is: " +nextScreen);
          // Get the view controller.
          String workflowScreen =
             (String)session.getAttribute("workflowScreen");
          // Go forward onto the view controller.
          this.getServletContext().getRequestDispatcher
             (workflowScreen).forward(req,resp);
        }
        catch(ClassNotFoundException ce)
        {
          ce.printStackTrace();
          sendError(resp, ce.getMessage());
        }
        catch(IOException ioe)
        {
          ioe.printStackTrace();
          sendError(resp, ioe.getMessage());
        }
        catch(DispatcherCreateException dce)
        {
          dce.printStackTrace();
          sendError(resp, dce.getMessage());
        }
        catch(ServletException se)
        {
          se.printStackTrace();
          sendError(resp, se.getMessage());
        }
        catch(Exception e)
        {
          e.printStackTrace();
          sendError(resp, e.getMessage());
        }
      }
      public void doGet(HttpServletRequest req, HttpServletResponse resp) {
          doPost(req,resp);
      }
   private void sendError(HttpServletResponse resp, String message)
   {
      try
      {
        resp.sendError(resp.SC_INTERNAL_SERVER_ERROR, message);
      }
      catch (IOException ioe)
      {
        ioe.printStackTrace();
      }
   }
}


The Application

The Application class is a Dispatchable object. The class represents a candidate's work application filled out on the company Website. The Application includes the candidate's name, experiences, felonies, and desired position:

  • Print
  • Feedback

Resources