01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.openejb.tomcat.loader;
17:
18: import java.util.Properties;
19: import java.util.Enumeration;
20: import java.io.File;
21: import javax.servlet.ServletConfig;
22: import javax.servlet.ServletException;
23: import javax.servlet.ServletContext;
24: import javax.servlet.http.HttpServlet;
25:
26: /**
27: * @author <a href="mailto:david.blevins@visi.com">David Blevins </a>
28: */
29: public class LoaderServlet extends HttpServlet {
30: private static boolean embedded = false;
31:
32: public void init(ServletConfig config) throws ServletException {
33: // only install once
34: if (embedded)
35: return;
36: embedded = true;
37:
38: Properties properties = initParamsToProperties(config);
39: File webappDir = new File(getWebappPath(config));
40: properties.setProperty("openejb.war", webappDir
41: .getAbsolutePath());
42:
43: properties.setProperty("openejb.embedder.source", getClass()
44: .getSimpleName());
45:
46: TomcatEmbedder.embed(properties, config.getClass()
47: .getClassLoader());
48: }
49:
50: private Properties initParamsToProperties(ServletConfig config) {
51: Properties properties = new Properties();
52:
53: // Set some defaults
54: properties.setProperty("openejb.loader", "tomcat");
55:
56: // Load in each init-param as a property
57: Enumeration enumeration = config.getInitParameterNames();
58: System.out.println("OpenEJB init-params:");
59: while (enumeration.hasMoreElements()) {
60: String name = (String) enumeration.nextElement();
61: String value = config.getInitParameter(name);
62: properties.put(name, value);
63: System.out.println("\tparam-name: " + name
64: + ", param-value: " + value);
65: }
66:
67: return properties;
68: }
69:
70: private String getWebappPath(ServletConfig config) {
71: ServletContext ctx = config.getServletContext();
72: File webInf = new File(ctx.getRealPath("WEB-INF"));
73: File webapp = webInf.getParentFile();
74: String webappPath = webapp.getAbsolutePath();
75: return webappPath;
76: }
77: }
|