Package org.huihoo.willow.deploy

Source Code of org.huihoo.willow.deploy.WillowServlet

//----------------------------BEGIN LICENSE----------------------------
/*
* Willow : the Open Source WorkFlow Project
* Distributable under GNU LGPL license by gun.org
*
* Copyright (C) 2004-2010 huihoo.org
* Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com>
*
* ====================================================================
* Project Homepage : http://www.huihoo.org/willow
* Source Forge     : http://sourceforge.net/projects/huihoo
* Mailing list     : willow@lists.sourceforge.net
*/
//----------------------------END  LICENSE----------------------------
package org.huihoo.willow.deploy;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.huihoo.willow.Context;
import org.huihoo.willow.Engine;
import org.huihoo.willow.Globals;
import org.huihoo.willow.LifecycleException;
import org.huihoo.willow.Logger;
import org.huihoo.willow.startup.Embedded;
import org.huihoo.willow.store.UserDatabaseRealm;

/**
* @author reic
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
/* 

  <context-param>   
    <param-name>WILLOW_STORE</param-name>
    <param-value>willow-store.properties</param-value>
  </context-param>

  <context-param>   
    <param-name>WILLOW_DATABASE</param-name>
    <param-value>willow-database.xml</param-value>
  </context-param>
            
  <servlet>
    <servlet-name>WillowServlet</servlet-name>
    <servlet-class>org.huihoo.willow.deploy.WillowServlet</servlet-class>
    <init-param>
      <param-name>WILLOW_STORE</param-name>
      <param-value>willow-store.properties</param-value>
    </init-param>
    <init-param>
      <param-name>WILLOW_DATABASE</param-name>
      <param-value>willow-database.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  The servlet init-parameter values will override those of the
  context-parameters.
*/

public class WillowServlet extends HttpServlet
{
  private Embedded embedded;
 
  public void init() throws ServletException
  {
    String storeFilename     = null;
    String databaseFilename  = null;
    String willowHome        = null;
   
    ClassLoader servletClassLoader = Thread.currentThread().getContextClassLoader();

    try
    {
      /*Make sure that we got a useful classloader; if we can not
      even load ourselves, then at least use the classloader that
      loaded us.*/
      servletClassLoader.loadClass(this.getClass().getName());
    }
    catch (ClassNotFoundException e)
    {
      servletClassLoader = getClass().getClassLoader();
    }

    if (servletClassLoader == null)
    {
      /*This is needed because some containers use hashtable to store
      servlet attributes and therefore they can't be null.
      If Class.forName is passed in null as the classloader, then it
      seems to default to the system class loader, so this should be ok.*/
      servletClassLoader = ClassLoader.getSystemClassLoader();
    }
    ServletConfig servletConfig = getServletConfig();
    ServletContext servletContext = servletConfig.getServletContext();

    String tempStr = null;

    //----------------------------------------------------------------
    //  WILLOW_HOME Property Setting (mandatory)
    //----------------------------------------------------------------
    willowHome=servletContext.getRealPath("/WEB-INF/");   
    System.setProperty(Globals.PROPS_WILLOW_HOME,willowHome);
   
    //----------------------------------------------------------------
    //  WILLOW_STORE Property Setting (mandatory)
    //----------------------------------------------------------------
    tempStr=servletConfig.getInitParameter(org.huihoo.willow.store.Constants.WILLOW_STORE);
    if (tempStr == null)
    {
      tempStr = servletContext.getInitParameter(org.huihoo.willow.store.Constants.WILLOW_STORE);
    }
   
    if(tempStr!=null)
    {
      storeFilename=tempStr;
      File tempFile=new File(tempStr);
      if(!tempFile.isAbsolute())
      {
        storeFilename=new File(servletContext.getRealPath("/WEB-INF/"),tempStr).getAbsolutePath();
      }
    }
    else
    {
      storeFilename=servletContext.getRealPath(org.huihoo.willow.store.Constants.DEFAULT_WILLOW_STORE);
    }
   
    File storeFile=new File(storeFilename);
    if(storeFile.exists()  && storeFile.isFile())
    {
      System.setProperty(org.huihoo.willow.store.Constants.PROPS_WILLOW_STORE,storeFile.getAbsolutePath());
    }

    //----------------------------------------------------------------
    //  WILLOW_DATABASE Property Setting (optional)
    //----------------------------------------------------------------
    tempStr=servletConfig.getInitParameter(org.huihoo.willow.store.Constants.WILLOW_DATABASE);
    if (tempStr == null)
    {
      tempStr = servletContext.getInitParameter(org.huihoo.willow.store.Constants.WILLOW_DATABASE);
    }
   
    if(tempStr!=null)
    {
      databaseFilename=tempStr;
      File tempFile=new File(tempStr);
      if(!tempFile.isAbsolute())
      {
        databaseFilename=new File(servletContext.getRealPath("/WEB-INF/"),tempStr).getAbsolutePath();
      }
    }
    else
    {
      databaseFilename=servletContext.getRealPath(org.huihoo.willow.store.Constants.DEFAULT_WILLOW_DATABASE);
    }
   
    File databaseFile=new File(databaseFilename);
    if(databaseFile.exists() && databaseFile.isFile())
    {
      System.setProperty(org.huihoo.willow.store.Constants.PROPS_WILLOW_DATABASE,databaseFile.getAbsolutePath());
    }

    //----------------------------------------------------------------
    //  Startup workflow engine
    //----------------------------------------------------------------
    Logger logger=new ServletContextLogger(servletContext);
    Embedded embedded=new Embedded(logger);
   
    String contextPath=servletContext.getRealPath("/");
    File  contextFile=new File(contextPath);
       
    Engine engine=embedded.createEngine();
    engine.setName(contextFile.getName());
    engine.setAutoDeploy(false);
    engine.setParentClassLoader(servletClassLoader);
   
    UserDatabaseRealm userRealm=new UserDatabaseRealm();
    engine.setRealmDatabase(userRealm);
   
    embedded.setEngine(engine);
   
    Context context=embedded.createContext(contextFile.getName(),contextFile.getAbsolutePath());
    context.setAutoDeploy(false);
    context.setReloadable(false);
    engine.addChild(context);
   
    try
    {
      embedded.start();
    }
    catch (LifecycleException ex)
    {
      ex.printStackTrace();
      throw new ServletException(ex);
    }
  }
 
  public void doGet (HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException
  {
    res.setContentType("text/html");
 
    PrintWriter out = res.getWriter ();
 
    out.println("<html><head><title>WILLOW WorkflowService Engine Initializer</title></head>");
    out.println ("<body><h1>WILLOW WorkflowService Engine Initializer</h1>");
    out.println ("<p>Sorry, I don't speak via HTTP GET/POST </p>");
    out.println ("</body></html>");
  }
 
  public void doPost (HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException
  {
    doGet(req,res);
  }
 
  public void destroy()
  {
    if(embedded!=null)
    {
      try
      {
        embedded.stop();
      }
      catch (LifecycleException e)
      {
        ;
      }
    }
  }
}
TOP

Related Classes of org.huihoo.willow.deploy.WillowServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.