1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.openejb.tomcat.loader; 18 19 import java.util.Properties; 20 import java.util.Enumeration; 21 import java.io.File; 22 import javax.servlet.ServletConfig; 23 import javax.servlet.ServletException; 24 import javax.servlet.ServletContext; 25 import javax.servlet.http.HttpServlet; 26 27 /** 28 * @author <a href="mailto:david.blevins@visi.com">David Blevins </a> 29 */ 30 public class LoaderServlet extends HttpServlet { 31 private static boolean embedded = false; 32 33 public void init(ServletConfig config) throws ServletException { 34 // only install once 35 if (embedded) return; 36 embedded = true; 37 38 Properties properties = initParamsToProperties(config); 39 File webappDir = new File(getWebappPath(config)); 40 properties.setProperty("openejb.war", webappDir.getAbsolutePath()); 41 42 properties.setProperty("openejb.embedder.source", getClass().getSimpleName()); 43 44 TomcatEmbedder.embed(properties, config.getClass().getClassLoader()); 45 } 46 /** 47 * Retrieves all intialization parameters for this servlet and stores them in a java.util.Properties object. 48 * @param config javax.servlet.ServletConfig 49 * @return java.util.Properties 50 */ 51 private Properties initParamsToProperties(ServletConfig config) { 52 Properties properties = new Properties(); 53 54 // Set some defaults 55 properties.setProperty("openejb.loader", "tomcat"); 56 57 // Load in each init-param as a property 58 Enumeration enumeration = config.getInitParameterNames(); 59 System.out.println("OpenEJB init-params:"); 60 while (enumeration.hasMoreElements()) { 61 String name = (String) enumeration.nextElement(); 62 String value = config.getInitParameter(name); 63 properties.put(name, value); 64 System.out.println("\tparam-name: " + name + ", param-value: " + value); 65 } 66 67 return properties; 68 } 69 /** 70 * Retrieves the absolute path of where this web application is located. 71 * 72 * @param config 73 * @return absolute path of this webapp directory 74 */ 75 private String getWebappPath(ServletConfig config) { 76 ServletContext ctx = config.getServletContext(); 77 File webInf = new File(ctx.getRealPath("WEB-INF")); 78 File webapp = webInf.getParentFile(); 79 String webappPath = webapp.getAbsolutePath(); 80 return webappPath; 81 } 82 }