alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.httpserver;

import org.apache.catalina.*;
import org.apache.catalina.logger.FileLogger;
import org.apache.catalina.realm.MemoryRealm;
import org.apache.catalina.startup.HostConfig;
import org.apache.catalina.core.StandardHost;
import java.io.File;
import org.openide.TopManager;
import org.openide.execution.NbClassPath;

/**
 * An embedded Tomcat 4 instance.
 *
 * @author Rich Unger
 */
public class TomcatServer extends org.apache.catalina.startup.Embedded
{
    public static synchronized TomcatServer getInstance()
    {
        if( s_instance == null )
        {
            s_instance = new TomcatServer();
        }
        return s_instance;
    }

    protected TomcatServer()
    {
        super(new FileLogger(), new MemoryRealm());
        
        // Establish where the logs and webapps will go
        m_workingDir = NbClassPath.toFile(
                TopManager.getDefault().getRepository().getDefaultFileSystem().getRoot());

        // sets the tomcat home to netbeans/system/httpwork
        m_workingDir = new File(m_workingDir, "httpwork"); //NOI18N
        System.setProperty("catalina.home", m_workingDir.getAbsolutePath());
        System.setProperty("catalina.base", m_workingDir.getAbsolutePath());

        m_loggingDir = new File(m_workingDir, "logs"); //NOI18N
        m_docrootDir = new File(m_workingDir, "webapps"); //NOI18N

        // log to a file, in netbeans/system/httpserver/logs
        ((FileLogger)this.logger).setDirectory(m_loggingDir.getAbsolutePath());

        // since we're using embedded tomcat, we can't set this in server.xml
        // TODO: make this a netbeans system property
        setDebug(1);
    }

    public void start() throws LifecycleException
    {
        HttpServerSettings settings = HttpServerModule.httpserverSettings();

        m_engine = makeEngine();
        Host host = makeHost();
        m_engine.addChild(host);

        // for fielding requests that aren't handled within a WAR file
        m_rootContext = createContext(
                "", m_docrootDir.getAbsolutePath() + "/ROOT"); //NOI18N

        // add servlet mappings for the NbServlets to implement the 
        // openide HttpServer.Impl interface
        addServlet( "RepositoryServlet",
                    "org.netbeans.modules.httpserver.RepositoryServlet",
                    settings.getRepositoryBaseURL() + '*'); //NOI18N

        addServlet( "ClasspathServlet",
                    "org.netbeans.modules.httpserver.ClasspathServlet",
                    settings.getClasspathBaseURL() + '*'); //NOI18N

        addServlet( "JavadocServlet",
                    "org.netbeans.modules.httpserver.JavadocServlet",
                    settings.getJavadocBaseURL() + '*'); //NOI18N

        addServlet( "WrapperServlet",
                    "org.netbeans.modules.httpserver.WrapperServlet",
                    settings.getWrapperBaseURL() + '*'); //NOI18N

        // add standard tomcat servlets
        // leave the DefaultServlet to be loaded by web.xml (doesn't seem to
        // work right when it's declared here...)
        addServlet( "invoker",
                    "org.apache.catalina.servlets.InvokerServlet",
                    "/servlet/*"); //NOI18N

        addServlet( "jsp",
                    "org.apache.jasper.servlet.JspServlet",
                    "*.jsp"); //NOI18N

        // Set the tomcat context's class loader to the module classloader
        // (so it will pick up the HttpServer.Impl servlets and the JspServlet
        Loader loader = createLoader(getClass().getClassLoader());
        m_rootContext.setLoader(loader);

        // set the hosts's parent class loader, so it will propogate to WAR files
        host.setParentClassLoader(getClass().getClassLoader());

        host.addChild(m_rootContext);

        addEngine(m_engine);

        Connector connector = makeConnector();
        addConnector(connector);

        super.start();
    }

    public void stop() throws LifecycleException
    {
        removeContext(m_rootContext);
        removeEngine(m_engine);

        super.stop(); // throws lifecycleexception
    }

    /** Determine if the server is currently running */
    public boolean isRunning()
    {
        return started;
    }

    /**
     * Adds a servlet to the root context.
     * @param sServletName An identifier for this servlet
     * @param sClass The fully qualified class name of the servlet
     * @param sBaseURL The servlet's base URL
     */
    protected void addServlet( String sServletName, 
                               String sClass, 
                               String sBaseURL )
    {
        Wrapper wrapper = m_rootContext.createWrapper();
        wrapper.setName(sServletName);
        wrapper.setLoadOnStartup(1);
        wrapper.setServletClass( sClass );
        m_rootContext.addChild(wrapper);
        m_rootContext.addServletMapping( sBaseURL, sServletName );
    }

    protected Engine makeEngine()
    {
        Engine engine = createEngine();
        engine.setDefaultHost("localhost"); //NOI18N
        return engine;
    }

    protected Host makeHost()
    {
        StandardHost host = (StandardHost)createHost(
                "localhost", m_docrootDir.getAbsolutePath());

        // don't expand every WAR.  Serve pages from within the .war file
        host.setUnpackWARs(false);

        // for installing WARs
        host.addLifecycleListener(new HostConfig());

        return host;
    }

    protected Connector makeConnector()
    {
        Connector connector = createConnector(
                null, // listen to localhost
                HttpServerModule.httpserverSettings().getPort(), 
                false); // no SSL
        return connector;
    }

    protected Engine m_engine;

    protected Context m_rootContext;

    protected File m_workingDir;
    protected File m_loggingDir;
    protected File m_docrootDir;

    protected static TomcatServer s_instance;
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.