User: salborini
  Date: 00/10/23 12:35:44

  Added:       tomcat/src/main/org/jboss/tomcat
                        ContextClassLoaderInterceptor.java
                        EmbeddedTomcatService.java
                        EmbeddedTomcatServiceMBean.java
  Log:
  initial revision
  
  Revision  Changes    Path
  1.1                  
contrib/tomcat/src/main/org/jboss/tomcat/ContextClassLoaderInterceptor.java
  
  Index: ContextClassLoaderInterceptor.java
  ===================================================================
  /*
   * jBoss, the OpenSource EJB server
   *
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
   
  
  
  package org.jboss.tomcat;
  
  import org.apache.tomcat.core.BaseInterceptor;
  import org.apache.tomcat.core.TomcatException;
  import org.apache.tomcat.core.Context;
  
  /**
   *   <description>
   *   
   *   @see <related>
   *   @author <a href="mailto:[EMAIL PROTECTED]">Sebastien Alborini</a>
   *   @version $Revision: 1.1 $
   */
  public class ContextClassLoaderInterceptor extends BaseInterceptor {
  
      public ContextClassLoaderInterceptor() {
      }
  
      public void contextInit( Context context) throws TomcatException {
                
                
context.getServletLoader().setParentLoader(Thread.currentThread().getContextClassLoader());
      }
  
  }
  
  
  
  1.1                  
contrib/tomcat/src/main/org/jboss/tomcat/EmbeddedTomcatService.java
  
  Index: EmbeddedTomcatService.java
  ===================================================================
  /*
   * jBoss, the OpenSource EJB server
   *
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
   
  
  package org.jboss.tomcat;
  
  import java.net.URL;
  import java.net.InetAddress;
  import java.lang.reflect.Method;
  import java.lang.reflect.InvocationTargetException;
  import java.util.Hashtable;
  
  import javax.management.*;
  import javax.servlet.ServletContext;
  
  import org.jboss.logging.Log;
  import org.jboss.logging.Logger;
  import org.jboss.util.ServiceMBeanSupport;
  import org.jboss.ejb.DeploymentException;
  
  import org.apache.tomcat.startup.EmbededTomcat;
  import org.apache.tomcat.context.WebXmlReader;
  import org.apache.tomcat.context.PolicyInterceptor;
  import org.apache.tomcat.context.LoaderInterceptor;
  import org.apache.tomcat.context.DefaultCMSetter;
  import org.apache.tomcat.context.WorkDirInterceptor;
  import org.apache.tomcat.context.LoadOnStartupInterceptor;
  import org.apache.tomcat.request.SessionInterceptor;
  import org.apache.tomcat.request.SimpleMapper1;
  import org.apache.tomcat.request.InvokerInterceptor;
  import org.apache.tomcat.request.StaticInterceptor;
  import org.apache.tomcat.request.AccessInterceptor;
  import org.apache.tomcat.request.Jdk12Interceptor;
  import org.apache.tomcat.session.StandardSessionInterceptor;
  
  /**
   *   A service to launch tomcat from JMX.
   *      
   *   This uses the class org.apache.tomcat.startup.EmbededTomcat, which means 
   *   that we can add and remove tomcat "contexts" on the fly.
   *   
   *   If you use this service, Tomcat's server.xml file will NOT be processed, so 
   *   you have to add all contexts through JMX.
   *   
   *   @see <related>
   *   @author <a href="mailto:[EMAIL PROTECTED]">Sebastien Alborini</a>
   *   @version $Revision: 1.1 $
   */
  public class EmbeddedTomcatService extends ServiceMBeanSupport
        implements EmbeddedTomcatServiceMBean, MBeanRegistration {
        
        // Constants -----------------------------------------------------
        public static final String NAME = "EmbeddedTomcat";
        
        // Attributes ----------------------------------------------------
        
        // the tomcat launcher
        EmbededTomcat embededTomcat;
        
        // the path to tomcat. We need it to set the root context
        String tomcatHome;
        
        // the port tomcat must listen to
        int port;
        
        // repository for deployed URLs and the associated servletContexts
        Hashtable deployedURLs = new Hashtable();
  
        final Log log = new Log(NAME);
        
        // Static --------------------------------------------------------
        
        // Constructors --------------------------------------------------
        public EmbeddedTomcatService(int port) {
                this.port = port;
        }
        
        
        // Public --------------------------------------------------------
        public ObjectName getObjectName(MBeanServer server, ObjectName name)
                throws javax.management.MalformedObjectNameException {
                
                return new ObjectName(OBJECT_NAME);
        }
        
        public String getName() {
                return NAME;
        }
        
        
        public void startService() throws Exception {
          Log.setLog(log);
                
                Logger.log("Testing if Tomcat is present....");
                
                try {
                        // We need the tomcat home to set tomcat's working dir / ROOT 
context
                        // This is set by using "java -Dtomcat.home=$TOMCAT_HOME ..." 
in run.sh/bat
                        tomcatHome = System.getProperty("tomcat.home");
                        if (tomcatHome == null) {
                                Logger.log("failed");
                                Logger.log("System property tomcat.home not found. Be 
sure to set TOMCAT_HOME to the home of tomcat 3.2b4+");
                                throw new Exception("start failed");
                        }                          
                        
                        try {
                                
                                // Using EmbededTomcat instead of 
org.apache.tomcat.startup.Tomcat
                                // allows us to add/remove contexts on the fly
                                embededTomcat = new EmbededTomcat();
                                Logger.log("OK");
                        
                        } catch (NoClassDefFoundError e) {
                                Logger.log("failed");
                                Logger.log("org.apache.tomcat.startup.EmbededTomcat 
wasn't found. Be sure to have your CLASSPATH correctly set");
                                Logger.log("You need tomcat 3.2b4+ to use this 
service");
                                
                                throw e;
                        } 
                        
                        // Initialize the EmbededTomcat object.
                        // See javadoc in org.apache.tomcat.startup.EmbededTomcat
                        
                        // set debug  (Warning: setting debug to anything higher gave 
me lot of exceptions)
                        embededTomcat.setDebug(0);
                        
                        embededTomcat.setWorkDir(tomcatHome);
                        
                        // set the interceptors. 
                        addInterceptors();
                        
                        // add root context
                        deploy("/", "file:" + tomcatHome + "/webapps/ROOT");
                        
                        // add endpoint (web service)
                        embededTomcat.addEndpoint(port, null, null);
                        
                        // start
                        embededTomcat.start();
                
                } finally {
                        // unset log for the main thread.
                        // tomcat's child threads have a copy of it anyway.
                        Log.unsetLog();
                }
        }
        
        
        public void stopService() {
                // NYI in tomcat for now (3.2b6)
                embededTomcat.stop();
        }
        
        
        // warURL could be given as a java.net.URL, but the JMX RI's html adaptor can't
        // show inputs for URLs in HTML forms. 
        public void deploy(String ctxPath, String warUrl) throws DeploymentException {
                Log.setLog(log);
                
                try {
                        // add the context
                        ServletContext servletCtx = embededTomcat.addContext(ctxPath, 
new URL(warUrl));
                        
                        // init the context
                        embededTomcat.initContext(servletCtx);
                        
                        // keep track of deployed contexts for undeployment
                        deployedURLs.put(warUrl, servletCtx);
                
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new DeploymentException(e.getMessage());
                } finally {
                        Log.unsetLog();
                }
        }
        
        
        public void undeploy(String warUrl) throws DeploymentException {
                Log.setLog(log);
                
                try {
                        // find the javax.servlet.ServletContext in the repository
                        ServletContext servletCtx = 
(ServletContext)deployedURLs.get(warUrl);
                        
                        if (servletCtx == null) 
                                throw new DeploymentException("URL " + warUrl + " is 
not deployed");
                        
                        // remove the context
                        embededTomcat.removeContext(servletCtx);
                        
                } catch (Exception e) {
                        throw new DeploymentException(e.getMessage());
                } finally {
                        Log.unsetLog();
                }
        
        }
        
        
        public boolean isDeployed(String warUrl) {
                return deployedURLs.containsKey(warUrl);
        }
        
        
        // Protected -----------------------------------------------------
        
        // Private -------------------------------------------------------
        private void addInterceptors() {
                
                // Since we add one non-default interceptor, we have to specif them all
                // the list comes from org.apache.tomcat.startup.EmbededTomcat
                
                WebXmlReader webXmlI=new WebXmlReader();
                webXmlI.setValidate( false );
                embededTomcat.addContextInterceptor( webXmlI );
                
                PolicyInterceptor polI=new PolicyInterceptor();
                embededTomcat.addContextInterceptor( polI );
                polI.setDebug(0);
                
                LoaderInterceptor loadI=new LoaderInterceptor();
                embededTomcat.addContextInterceptor( loadI );
                
                // this one is custom
                ContextClassLoaderInterceptor ccli = new 
ContextClassLoaderInterceptor();
                embededTomcat.addContextInterceptor(ccli);
                
                DefaultCMSetter defaultCMI=new DefaultCMSetter();
                embededTomcat.addContextInterceptor( defaultCMI );
                
                WorkDirInterceptor wdI=new WorkDirInterceptor();
                embededTomcat.addContextInterceptor( wdI );
                
                
                LoadOnStartupInterceptor loadOnSI=new LoadOnStartupInterceptor();
                embededTomcat.addContextInterceptor( loadOnSI );
                
                // Debug
                //      LogEvents logEventsI=new LogEvents();
                //      addRequestInterceptor( logEventsI );
                
                SessionInterceptor sessI=new SessionInterceptor();
                embededTomcat.addRequestInterceptor( sessI );
                
                SimpleMapper1 mapI=new SimpleMapper1();
                embededTomcat.addRequestInterceptor( mapI );
                mapI.setDebug(0);
                
                InvokerInterceptor invI=new InvokerInterceptor();
                embededTomcat.addRequestInterceptor( invI );
                invI.setDebug(0);
                
                StaticInterceptor staticI=new StaticInterceptor();
                embededTomcat.addRequestInterceptor( staticI );
                mapI.setDebug(0);
                
                embededTomcat.addRequestInterceptor( new StandardSessionInterceptor());
                
                // access control ( find if a resource have constraints )
                AccessInterceptor accessI=new AccessInterceptor();
                embededTomcat.addRequestInterceptor( accessI );
                accessI.setDebug(0);
                
                // set context class loader
                Jdk12Interceptor jdk12I=new Jdk12Interceptor();
                embededTomcat.addRequestInterceptor( jdk12I );
        }
  }
  
  
  
  1.1                  
contrib/tomcat/src/main/org/jboss/tomcat/EmbeddedTomcatServiceMBean.java
  
  Index: EmbeddedTomcatServiceMBean.java
  ===================================================================
  /*
   * jBoss, the OpenSource EJB server
   *
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
   
   
  package org.jboss.tomcat;
  
  import org.jboss.ejb.DeploymentException;
  
  
  /**
   *   <description> 
   *      
   *   @see <related>
   *   @author <a href="mailto:[EMAIL PROTECTED]">Sebastien Alborini</a>
   *   @version $Revision: 1.1 $
   */
  public interface EmbeddedTomcatServiceMBean extends org.jboss.util.ServiceMBean {
        
        // Constants -----------------------------------------------------
        public static final String OBJECT_NAME = ":service=EmbeddedTomcat";
        
        // Public --------------------------------------------------------
        public void deploy(String ctxPath, String warUrl) throws DeploymentException;
        
        public void undeploy(String warUrl) throws DeploymentException;
        
        public boolean isDeployed(String warUrl);
  
  }
  
  
  

Reply via email to