KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > loader > LoaderServlet


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: LoaderServlet.java 2487 2006-02-22 22:05:03Z dblevins $
44  */

45 package org.openejb.loader;
46
47 import java.io.File JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.util.Enumeration JavaDoc;
50 import java.util.Properties JavaDoc;
51 import java.lang.reflect.Method JavaDoc;
52 import java.lang.reflect.InvocationTargetException JavaDoc;
53
54 import javax.servlet.ServletConfig JavaDoc;
55 import javax.servlet.ServletContext JavaDoc;
56 import javax.servlet.ServletException JavaDoc;
57 import javax.servlet.http.HttpServlet JavaDoc;
58 import javax.servlet.http.HttpServletRequest JavaDoc;
59 import javax.servlet.http.HttpServletResponse JavaDoc;
60
61 /**
62  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins </a>
63  */

64 public class LoaderServlet extends HttpServlet JavaDoc {
65
66     private Loader loader;
67
68     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
69         if (loader != null) {
70             return;
71         }
72
73         // Do just enough to get the Tomcat Loader into the classpath
74
// let it do the rest.
75
Properties JavaDoc p = initParamsToProperties(config);
76
77         String JavaDoc embeddingStyle = p.getProperty("openejb.loader");
78
79         // Set the mandatory values for a webapp-only setup
80
if (embeddingStyle.endsWith("tomcat-webapp")) {
81             setPropertyIfNUll(p, "openejb.base", getWebappPath(config));
82 // setPropertyIfNUll(p, "openejb.configuration", "META-INF/openejb.xml");
83
// setPropertyIfNUll(p, "openejb.container.decorators", "org.openejb.tomcat.TomcatJndiSupport");
84
// setPropertyIfNUll(p, "log4j.configuration", "META-INF/log4j.properties");
85
}
86
87         try {
88             SystemInstance.init(p);
89             Embedder embedder = new Embedder("org.openejb.tomcat.TomcatLoader");
90             Class JavaDoc loaderClass = embedder.load();
91             Object JavaDoc instance = loaderClass.newInstance();
92             try {
93                 loader = (Loader) instance;
94             } catch (ClassCastException JavaDoc e) {
95                 loader = new LoaderWrapper(instance);
96             }
97
98             loader.init(config);
99         } catch (Exception JavaDoc e) {
100             e.printStackTrace();
101         }
102     }
103
104
105     protected void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
106         loader.service(request, response);
107     }
108
109     private String JavaDoc getWebappPath(ServletConfig JavaDoc config) {
110         ServletContext JavaDoc ctx = config.getServletContext();
111         File JavaDoc webInf = new File JavaDoc(ctx.getRealPath("WEB-INF"));
112         File JavaDoc webapp = webInf.getParentFile();
113         String JavaDoc webappPath = webapp.getAbsolutePath();
114         return webappPath;
115     }
116
117     private Properties JavaDoc initParamsToProperties(ServletConfig JavaDoc config) {
118         Properties JavaDoc p = new Properties JavaDoc();
119
120         // Set some defaults
121
p.setProperty("openejb.loader","tomcat");
122
123         // Load in each init-param as a property
124
Enumeration JavaDoc enumeration = config.getInitParameterNames();
125         System.out.println("OpenEJB init-params:");
126         while (enumeration.hasMoreElements()) {
127             String JavaDoc name = (String JavaDoc) enumeration.nextElement();
128             String JavaDoc value = config.getInitParameter(name);
129             p.put(name, value);
130             System.out.println("\tparam-name: " + name + ", param-value: " + value);
131         }
132
133         return p;
134     }
135
136     private Object JavaDoc setPropertyIfNUll(Properties JavaDoc properties, String JavaDoc key, String JavaDoc value){
137         String JavaDoc currentValue = properties.getProperty(key);
138         if (currentValue == null){
139             properties.setProperty(key, value);
140         }
141         return currentValue;
142     }
143
144     /**
145      * Ain't classloaders fun?
146      * This class exists to reconcile that loader implementations
147      * may exist in the parent classloader while the loader interface
148      * is also in this classloader. Use this class in the event that
149      * this is the case.
150      * Think of this as an adapter for adapting the parent's idea of a
151      * Loader to our idea of a Loader.
152      */

153     public static class LoaderWrapper implements Loader {
154         private final Object JavaDoc loader;
155         private final Method JavaDoc init;
156         private final Method JavaDoc service;
157
158         public LoaderWrapper(Object JavaDoc loader) {
159             this.loader = loader;
160             try {
161                 Class JavaDoc loaderClass = loader.getClass();
162                 this.init = loaderClass.getMethod("init", new Class JavaDoc[]{ServletConfig JavaDoc.class});
163                 this.service = loaderClass.getMethod("service", new Class JavaDoc[]{HttpServletRequest JavaDoc.class, HttpServletResponse JavaDoc.class});
164             } catch (NoSuchMethodException JavaDoc e) {
165                 throw (IllegalStateException JavaDoc) new IllegalStateException JavaDoc("Signatures for Loader are no longer correct.").initCause(e);
166             }
167         }
168
169         public void init(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc {
170             try {
171                 init.invoke(loader, new Object JavaDoc[]{servletConfig});
172             } catch (InvocationTargetException JavaDoc e) {
173                 Throwable JavaDoc cause = e.getCause();
174                 if (cause instanceof RuntimeException JavaDoc) {
175                     throw (RuntimeException JavaDoc) cause;
176                 } else if (cause instanceof Error JavaDoc) {
177                     throw (Error JavaDoc) cause;
178                 } else {
179                     throw (ServletException JavaDoc) cause;
180                 }
181             } catch (Exception JavaDoc e) {
182                 throw new RuntimeException JavaDoc("Loader.init: "+e.getMessage()+e.getClass().getName()+": "+e.getMessage(), e);
183             }
184         }
185
186         public void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
187             try {
188                 service.invoke(loader, new Object JavaDoc[]{request, response});
189             } catch (InvocationTargetException JavaDoc e) {
190                 Throwable JavaDoc cause = e.getCause();
191                 if (cause instanceof RuntimeException JavaDoc) {
192                     throw (RuntimeException JavaDoc) cause;
193                 } else if (cause instanceof Error JavaDoc) {
194                     throw (Error JavaDoc) cause;
195                 } else if (cause instanceof IOException JavaDoc) {
196                     throw (IOException JavaDoc) cause;
197                 } else {
198                     throw (ServletException JavaDoc) cause;
199                 }
200             } catch (Exception JavaDoc e) {
201                 throw new RuntimeException JavaDoc("Loader.service: "+e.getMessage()+e.getClass().getName()+": "+e.getMessage(), e);
202             }
203         }
204     }
205 }
206
Popular Tags