tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sush0000 <sushil.mi...@ars.usda.gov>
Subject Embedded tomcat 5.5.17 and WAR file
Date Mon, 07 Jan 2008 21:58:56 GMT

Hi All,

I am trying to embedded tomcat5.5.17 in my java application.
I want Tomcat to unpack *.WAR file 
Tomcat starts fine but http://localhost:8080/  keeps saying HTTP status 404. 

I am not sure if it could unpack given WAR file.
Could anyone guide me here. Also how to see the log file.

5.5.17 version seems to be different than 4.XX

Here is the code I am using
>>>>>>>>>>.>>>>>>>>>>>>>>>>>>

package test.embed;

import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URL;

import org.apache.catalina.*;
import org.apache.catalina.core.*;
import org.apache.catalina.connector.*;
import org.apache.catalina.Context.*;
import org.apache.catalina.Engine.*;
import org.apache.catalina.Host.*;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.*;

public class WebAppServer {
    private String baseDir;
    private String workDir;
    private String tempDir;
    private String warFile;

    private Embedded tomcat;
    private 	StandardHost host;
    
    public WebAppServer(String baseDir) {
	this.baseDir = baseDir;
	System.setProperty("catalina.home", baseDir);
	tempDir = System.getProperty("java.io.tmpdir");
	if (tempDir != null) workDir = tempDir + "/tomcat";
	else workDir = System.getProperty("user.dir") + "/tomcat";
    }
    
    public void setWarFile(String warFile) {
	this.warFile = warFile;

    }
    
    public void start() throws Exception {
	tomcat = new Embedded();
	Engine engine = tomcat.createEngine();
	engine.setDefaultHost("localhost");
	host = (StandardHost)tomcat.createHost("localhost", baseDir + "/apps");
            host.setDeployOnStartup(true);
	host.setWorkDir(workDir);
             host.setUnpackWARs(true);
             host.setAutoDeploy(true);
             StandardContext context
=(StandardContext)tomcat.createContext("",baseDir + "/apps" );
	context.setReloadable(false);
	((StandardContext)context).setWorkDir(workDir + "/localhost");
	((StandardContext)context).setDefaultWebXml("web.xml");
            System.out.println("Inside START2, warFile= "+ warFile);
	((StandardContext)context).setUnpackWAR(true);
	host.addChild(context);

        System.out.println("Inside START11, warFile= "+ warFile);
	engine.addChild(host);
	tomcat.addEngine(engine);
	Connector connector = tomcat.createConnector((String)null, 8080, false);
	tomcat.addConnector(connector);
	tomcat.start();


        System.out.println("host logger "+host.getLogger());
        System.out.println("Inside autodeploy="+host.getAutoDeploy());
        System.out.println("Inside contextclass="+host.getContextClass());
        System.out.println("Inside START, workDir= "+ workDir);
        System.out.println("Inside START, appl base="+host.getAppBase());

    }
    
    public void stop() throws Exception {
	tomcat.stop();
    }
    

    /**
     * Registers a WAR with the container.
     * 
     * @param contextPath -
     *            the context path under which the application will be
     *            registered
     * @param warFile -
     *            the URL of the WAR to be registered.
     */
    public void registerWAR(String contextPath, String absolutePath)
            throws Exception {
        StandardContext context =
(StandardContext)this.tomcat.createContext(contextPath, absolutePath);
        context.setReloadable(false);
        System.out.println("Inside START, warFile= "+ warFile);

        this.host.addChild(context);
    }

    /**
     * Unregisters a WAR from the web server.
     * 
     * @param contextPath -
     *            the context path to be removed
     */
    public void unregisterWAR(String contextPath) throws Exception {

        Context context = this.host.map(contextPath);
        if (context != null) {
            this.tomcat.removeContext(context);
        } else {
            throw new Exception("Context does not exist for named path : "
                    + contextPath);
        }
    }


    public static void main(String[] args) {
	String baseDir = System.getProperty("base");
	if (baseDir == null) {
	    System.out.println("You must specify the -Dbase=[dir] option");
	    System.exit(-1);
	}

	if (args.length == 0) {
	    System.out.println("Usage: WebAppServer [warfilename]");
	    System.exit(-1);
	}
	String warFile = "file:" + baseDir + "/" + args[0];	
         System.out.println("baseDir" +baseDir );
         System.out.println("warfile" +warFile );
    	WebAppServer was = new WebAppServer(baseDir);

	try {
    	    was.setWarFile(warFile);
         System.out.println("warfile in" +warFile );
	    was.start();
         System.out.println("sush:warfile done" );
	    
  	    BufferedReader is = new BufferedReader(
		    new InputStreamReader(System.in));
	    
	    System.out.println("Tomcat is running.");
	    System.out.println();

	    System.out.println("register.");
//          was.registerWAR("/",warFile);
	    System.out.println("registered.");
	    while (true) {
		System.out.print("Type 'q' to exit: ");
		String input = is.readLine();
		if (input.compareToIgnoreCase("q") == 0) break;
	    }
		
	    was.stop();
	} catch (Exception e) {
	    System.err.println(e);
	}
    }
}

-- 
View this message in context: http://www.nabble.com/Embedded-tomcat-5.5.17-and-WAR-file-tp14676916p14676916.html
Sent from the Tomcat - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message