On Monday, February 10, 2003, at 03:35 PM, Chris Rousseau wrote:


Anyone have sample code handy for running multiple instances in a single VM,
using VelocityEngine instead of VelocityServlet?
Code? It's not difficult. Here's something I use all the time - some stuff has been chopped out to protect the guilty, but it shows basically how to do it.

This is a trivial action model where you put the action in the URL, and that invokes through introspection the method to handle the request, It's just one of many ways of doing it - but shows how a servlet can use the VE w/o the trappings/pattern of the VelocityServlet

geir
public abstract class FooServlet extends HttpServlet
{
    public static final String DEFAULT_PROPS = 
"foo/bar/woogie/defaults/velocity.properties";

    public static final String HTTP_ACTION_VERB = "action";

    private VelocityEngine ve = new VelocityEngine();

    protected Class[] args = { HttpServletRequest.class,  HttpServletResponse.class };

    public void init()
        throws ServletException
    {
        /*
         *  get the default properties from the classloader
         */

        Properties p = null;

        try
        {
            InputStream is = 
getClass().getClassLoader().getResourceAsStream(DEFAULT_PROPS);

            p = new Properties();

            p.load(is);
        }
        catch(Exception e)
        {
            fatal("default " + DEFAULT_PROPS + " not found.", e);
            throw new ServletException(e);
        }

        /*
         *  run through them and use them
         */

        for (Enumeration en=p.propertyNames(); en.hasMoreElements(); )
        {
            String key = (String) en.nextElement();

            String value = p.getProperty(key);

            /*
             *  if it's the file.resource.loader.path then 'stick it'
             */

            if (key.equals(Velocity.FILE_RESOURCE_LOADER_PATH))
            {
                String root = getServletContext().getRealPath(value);
                ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, root);
            }
            else
            {
                ve.setProperty(key, value);
            }
        }

        /*
         *  for now, log to the servlet log
         */

        ServletLogger sl = new ServletLogger( getServletContext() );
        ve.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, sl );

        /*
         * set an app context for the webapploader if we are using it
         */

        ServletAppContext vssac = new ServletAppContext(getServletContext());
        ve.setApplicationAttribute( WebappLoader.ATTRIBUTE_KEY, vssac);

        try
        {
            ve.init();
        }
        catch(Exception e)
        {
            fatal("FooServlet",e);
            throw new ServletException(e);
        }
    }

    protected String getActionVerb()
    {
        return HTTP_ACTION_VERB;
    }

    /**
      *  basic action stuff.  Should be cached maybe
      */
    public void service(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, java.io.IOException
    {
        String actionVerb = getActionVerb();

        String what = req.getParameter(actionVerb);

        if (what == null)
          what = "defaultAction";

        try
        {
          Method method = this.getClass().getMethod(what, args);

          Object[] objs = { req, res };
          method.invoke( this, objs);
        }
        catch(NoSuchMethodException nsme)
        {
          defaultAction(req, res);
        }
        catch(Exception e)
        {
          log("service() : exception", e);
        }
    }

    public abstract void defaultAction(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, java.io.IOException;


    protected VelocityEngine getVelocityEngine()
    {
        return ve;
    }

    protected String getInitParameter(String key, String defaultValue)
    {
        String val = getServletConfig().getInitParameter(key);

        if (val == null)
            val = defaultValue;

        return val;
    }

    public void info(String msg)
    {
        super.log("INFO : " + msg);
    }

    public void info(String msg, Throwable e)
    {
        super.log("INFO : " + msg, e);
    }

    /**
     *  warning message
     */
    public void warn(String msg)
    {
        super.log("WARN : " + msg);
    }

    public void warn(String msg, Throwable e)
    {
        super.log("WARN : " + msg, e);
    }


    /**
     *  error message
     */
    public void error(String msg)
    {
        super.log("ERROR : " + msg);
    }

    public void error(String msg, Throwable e)
    {
        super.log("ERROR : " + msg, e);
    }

    /**
     *  fatal message
     */
    public void fatal(String msg)
    {
        super.log("FATAL : " + msg);
    }

    public void fatal(String msg, Throwable e)
    {
        super.log("FATAL : " + msg, e);
    }

    /**
     *  debug trace messages
     */
    public void debug(String msg)
    {
        super.log("DEBUG : " + msg);
    }

    public void debug(String msg, Throwable e)
    {
        super.log("DEBUG : " + msg, e);
    }


    /**
     *  little wrapper class to safely pass the ServletContext to the loader
     */
    public class ServletAppContext implements WebappLoaderAppContext
    {
        ServletContext servletContext = null;

        ServletAppContext(ServletContext sc)
        {
            servletContext = sc;
        }

        public ServletContext getServletContext()
        {
           return servletContext;
        }
   }

}



-- Geir Magnusson Jr 203-956-2604(w)
Adeptra, Inc. 203-247-1713(m)
[EMAIL PROTECTED]

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

Reply via email to