Save This Page
Home » openejb-3.1.2-src » org.apache.openejb.tomcat » loader » [javadoc | source]
    1   /**
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * the License.  You may obtain a copy of the License at
    9    *
   10    *     http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing, software
   13    *  distributed under the License is distributed on an "AS IS" BASIS,
   14    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    *  See the License for the specific language governing permissions and
   16    *  limitations under the License.
   17    */
   18   package org.apache.openejb.tomcat.loader;
   19   
   20   import java.io.File;
   21   import java.util.Properties;
   22   
   23   import javax.management.ObjectName;
   24   
   25   import org.apache.catalina.Container;
   26   import org.apache.catalina.LifecycleEvent;
   27   import org.apache.catalina.LifecycleListener;
   28   import org.apache.catalina.ServerFactory;
   29   import org.apache.catalina.Service;
   30   import org.apache.catalina.core.StandardContext;
   31   import org.apache.catalina.core.StandardEngine;
   32   import org.apache.catalina.core.StandardHost;
   33   import org.apache.catalina.core.StandardServer;
   34   
   35   public class OpenEJBListener implements LifecycleListener {
   36       static private boolean listenerInstalled;
   37   
   38       public static boolean isListenerInstalled() {
   39           return listenerInstalled;
   40       }
   41   
   42       public void lifecycleEvent(LifecycleEvent event) {
   43           // only install once
   44           if (listenerInstalled) return;
   45           listenerInstalled = true;
   46   
   47           Properties properties = new Properties();
   48           File webappDir = findOpenEjbWar();
   49           properties.setProperty("openejb.war", webappDir.getAbsolutePath());
   50   
   51           properties.setProperty("openejb.embedder.source", getClass().getSimpleName());
   52   
   53           TomcatEmbedder.embed(properties, StandardServer.class.getClassLoader());
   54       }
   55   
   56       private static File findOpenEjbWar() {
   57           // in Tomcat 5.5 the OpenEjb war is in the server/webapps director
   58           String catalinaBase = System.getProperty("catalina.base");
   59           File serverWebapps = new File(catalinaBase, "server/webapps");
   60           File openEjbWar = findOpenEjbWar(serverWebapps);
   61           if (openEjbWar != null) {
   62               return openEjbWar;
   63           }
   64   
   65           // in Tomcat 6 the OpenEjb war is normally in webapps, but we just scan all hosts directories
   66           for (Service service : ServerFactory.getServer().findServices()) {
   67               Container container = service.getContainer();
   68               if (container instanceof StandardEngine) {
   69                   StandardEngine engine = (StandardEngine) container;
   70                   for (Container child : engine.findChildren()) {
   71                       if (child instanceof StandardHost) {
   72                           StandardHost host = (StandardHost) child;
   73                           String appBase = host.getAppBase();
   74   
   75                           // determine the host dir (normally webapps)
   76                           File hostDir = new File(appBase);
   77                           if (!hostDir.isAbsolute()) {
   78                               hostDir = new File(catalinaBase, appBase);
   79                           }
   80   
   81                           openEjbWar = findOpenEjbWar(hostDir);
   82                           if (openEjbWar != null) {
   83                               return openEjbWar;
   84                           } else {
   85                           	return findOpenEjbWar(host);
   86                           }
   87                       }
   88                   }
   89               }
   90           }
   91   
   92   
   93           return null;
   94       }
   95       
   96       private static File findOpenEjbWar(StandardHost standardHost) {
   97       	//look for openejb war in a Tomcat context
   98       	for(Container container : standardHost.findChildren()) {
   99       		if(container instanceof StandardContext) {
  100       			StandardContext standardContext = (StandardContext)container;
  101       			File contextDocBase = new File(standardContext.getDocBase());
  102       			if(contextDocBase.isDirectory()) {
  103   	    			File openEjbWar = findOpenEjbWarInContext(contextDocBase);
  104   	    	        if (openEjbWar != null) {
  105   	    	            return openEjbWar;
  106   	    	        }
  107       			}
  108       		}
  109       	}
  110       	return null;
  111       }
  112   
  113       private static File findOpenEjbWar(File hostDir) {
  114           if (!hostDir.isDirectory()) {
  115               return null;
  116           }
  117   
  118           // iterate over the contexts
  119           for (File contextDir : hostDir.listFiles()) {
  120           	File foundContextDir = findOpenEjbWarInContext(contextDir);
  121           	if(foundContextDir != null) {
  122           		return foundContextDir;
  123           	}
  124           }
  125           return null;
  126       }
  127        
  128       private static File findOpenEjbWarInContext(File contextDir) {
  129           // does this war have a web-inf lib dir
  130           File webInfLib = new File(new File(contextDir, "WEB-INF"), "lib");
  131           if (!webInfLib.isDirectory()) {
  132                return null;
  133           }
  134           // iterate over the libs looking for the openejb-loader-*.jar
  135           for (File file : webInfLib.listFiles()) {
  136               if (file.getName().startsWith("openejb-tomcat-loader-") && file.getName().endsWith(".jar")) {
  137                   // this should be the openejb war...
  138                   // make sure it has a lib directory
  139                   if (new File(contextDir, "lib").isDirectory()) {
  140                       return contextDir;
  141                   }
  142               }
  143           }
  144           return null;
  145       }
  146   }

Save This Page
Home » openejb-3.1.2-src » org.apache.openejb.tomcat » loader » [javadoc | source]