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

package org.apache.tomcat.j2ee;

import java.net.*;
import java.io.*;
import java.util.*;

import com.sun.web.server.*;
import org.apache.tomcat.core.*;
import org.apache.tomcat.request.*;
import org.apache.tomcat.context.*;
import java.security.*;
import javax.servlet.*;
import org.apache.tomcat.startup.EmbededTomcat;
/**
 * Simple adapter EmbededTomcat, workaround to avoid
 * using com.sun.web.servet.WebService inside tomcat.
 * 
 * @author costin@eng.sun.com
 */
public class TomcatJ2EEAdapter extends WebService {
    EmbededTomcat server;
    ContextManager cm;
    int debug=0;

    public TomcatJ2EEAdapter() {
	server=new EmbededTomcat();
	cm=server.getContextManager();
    }

    public void setDebug( int debug ) {
	super.setDebug(debug);
	server.setDebug(debug);
	this.debug=debug;
    }

    public void setApplication( Object app ) {
	super.setApplication( app );
	server.setApplication( app );
    }
    
    public void addApplicationAdapter( Object adapter ) {
	try {
	    server.addApplicationAdapter( (BaseInterceptor)adapter );
	} catch(TomcatException ex ) {
	    ex.printStackTrace();
	}
    }
    
    public void setWorkDir( String dir ) {
	cm.setWorkDir( dir );
    }
    
    public void addEndpoint( int port, InetAddress addr ,
			     String hostname)
    {
	try {
	    server.addEndpoint( port, addr, hostname);
	} catch(TomcatException ex ) {
	    ex.printStackTrace();
	}

    }
    public  void addSecureEndpoint( int port, InetAddress addr,
				    String hostname, String keyFile,
				    String keyPass ) {
	try {
	    server.addSecureEndpoint( port, addr, hostname, keyFile, keyPass);
	} catch(TomcatException ex ) {
	    ex.printStackTrace();
	}

    }

    Hashtable contexts=new Hashtable();
    
    public  ServletContext addContext( String ctxPath, URL docRoot ) {
	try {
	    Context ctx=(Context)server.addContext( ctxPath, docRoot, null);
	    contexts.put( ctx.getFacade(), ctx );
	    return (ServletContext)ctx.getFacade();
	} catch(TomcatException ex ) {
	    ex.printStackTrace();
	}
	return null;
    }

    public  void initContext( ServletContext sctx ) {
	Context ctx=(Context)contexts.get( sctx );

	Vector cp=(Vector)extraClassPaths.get( sctx );
	if( cp!=null ) {
	    for( int i=0; i0) cm.log( "remove context " + ctx );
	try {
	    server.getContextManager().removeContext( ctx );
	} catch( Exception ex ) {
	    cm.log("exception removing context " + sctx, ex);
	}
    }

    Hashtable extraClassPaths=new Hashtable();

    public  void addClassPath( ServletContext sctx, String cpath ) {
	Context ctx=(Context)contexts.get( sctx );
	if(debug>0) cm.log( "addClassPath " + ctx + " " +
			 cpath );
	try {
	    Vector cp=(Vector)extraClassPaths.get(ctx);
	    if( cp == null ) {
		cp=new Vector();
		extraClassPaths.put( ctx, cp );
	    }
	    cp.addElement( cpath );
	} catch( Exception ex ) {
	    cm.log("exception adding classpath " + cpath +
		" to context " + ctx, ex);
	}
    }

    public  void start() {
	try {
	    server.getContextManager().start();
	} catch( Exception ex ) {
	    cm.log("Error starting EmbededTomcat", ex);
	}
	if(debug>0) cm.log( "Started" );
    }

    public  void stop() {
	try {
	    cm.shutdown();
	} catch( Exception ex ) {
	    cm.log("Error starting EmbededTomcat", ex);
	}
	
    }
}






... 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.