2010/12/15 - Apache Excalibur has been retired.

For more information, please explore the Attic.

Servlet Based Applications

Servlet based applications are viewed on the web. Examples of these types of Java projects are Jakarta Turbine, and Apache Cocoon. Servlets have their own lifecycle, so it is rather easy to tie the life of a container to the life of a servlet.

Servlet based applications can either tie the Avalon container to a Servlet or bind it to JNDI. The advantage of binding it to JNDI is better resource management because the container is not being torn down and rebuilt periodically. The disadvantage is that there is no standard way of doing it in your Servlet container. Most of the time we bind the container to a Servlet like the following code:

                
public class MyAvalonServlet implements HttpServlet
{
    private static final String ROOT =
      "resource://org/apache/excalibur/fortress/examples/servlet/";
    private static final String XCONF =
      ROOT + ServletContainer.xconf";
    private static final String XLOG =
      ROOT + ServletContainer.xlog";
    private static final String ROLES =
      ROOT + ServletContainer.roles";

    private MyServletContainer m_container;
    private ContainerManager m_containerManager;

    /**
     * Initializes Servlet and creates a <code>ServletContainer</code> instance
     *
     * NOTE: this is the servlet init code.
     *
     * @exception ServletException if an error occurs
     */
    public void init()
        throws ServletException
    {
        super.init();

        try
        {
            final FortressConfig config = new FortressConfig();
            config.setContainerClass(
              "org.apache.avalon.fortress.examples.servlet.ServletContainer" );
            config.setContextDirectory( "./" );
            config.setWorkDirectory( "./" );
            config.setContainerConfiguration( XCONF );
            config.setLoggerManagerConfiguration( XLOG );
            config.setRoleManagerConfiguration( ROLES );

            m_containerManager =
              new DefaultContainerManager( config.getContext() );
            ContainerUtil.initialize(m_containerManager);

            m_container =
             (ServletContainer)m_containerManager.getContainer();
        }
        catch( Exception e )
        {
            throw new ServletException( "Error during initialization", e );
        }
    }

    /**
     * Pass all servlet requests through to container to be handled. In a more
     * complex system, there could be multiple containers that handle different
     * requests, or a main controlling container with subcontainers for different
     * requests.
     *
     * @param request a <code>ServletRequest</code> instance
     * @param response a <code>ServletResponse</code> instance
     * @exception IOException if an IO error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void service( ServletRequest request, ServletResponse response )
        throws IOException, ServletException
    {
        m_container.handleRequest( request, response );
    }

    /**
     * Disposes of container manager and container instance.
     *
     * NOTE: This is the Servlet destruction callback
     */
    public void destroy()
    {
        ContainerUtil.dispose(m_containerManager);
    }
}