KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > invoker > InvokerServlet


1 /*
2  * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3  * Distributed under the terms of either:
4  * - the common development and distribution license (CDDL), v1.0; or
5  * - the GNU Lesser General Public License, v2.1 or later
6  */

7 package winstone.invoker;
8
9 import java.io.IOException JavaDoc;
10 import java.util.Hashtable JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import javax.servlet.ServletConfig JavaDoc;
15 import javax.servlet.ServletException JavaDoc;
16 import javax.servlet.http.HttpServlet JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19
20 import winstone.Logger;
21 import winstone.Mapping;
22 import winstone.RequestDispatcher;
23 import winstone.ServletConfiguration;
24 import winstone.WebAppConfiguration;
25 import winstone.WinstoneResourceBundle;
26
27 /**
28  * If a URI matches a servlet class name, mount an instance of that servlet, and
29  * try to process the request using that servlet.
30  *
31  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
32  * @version $Id: InvokerServlet.java,v 1.6 2006/03/24 17:24:24 rickknowles Exp $
33  */

34 public class InvokerServlet extends HttpServlet JavaDoc {
35 // private static final String FORWARD_PATH_INFO = "javax.servlet.forward.path_info";
36
private static final String JavaDoc INCLUDE_PATH_INFO = "javax.servlet.include.path_info";
37
38     private static final WinstoneResourceBundle INVOKER_RESOURCES =
39         new WinstoneResourceBundle("winstone.invoker.LocalStrings");
40     private Map JavaDoc mountedInstances;
41 // private String prefix;
42
// private String invokerPrefix;
43

44     /**
45      * Set up a blank map of servlet configuration instances
46      */

47     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
48         super.init(config);
49         this.mountedInstances = new Hashtable JavaDoc();
50 // this.prefix = config.getInitParameter("prefix");
51
// this.invokerPrefix = config.getInitParameter("invokerPrefix");
52
}
53
54     /**
55      * Destroy any mounted instances we might be holding, then destroy myself
56      */

57     public void destroy() {
58         if (this.mountedInstances != null) {
59             synchronized (this.mountedInstances) {
60                 for (Iterator JavaDoc i = this.mountedInstances.values().iterator(); i
61                         .hasNext();)
62                     ((ServletConfiguration) i.next()).destroy();
63                 this.mountedInstances.clear();
64             }
65         }
66         this.mountedInstances = null;
67 // this.prefix = null;
68
// this.invokerPrefix = null;
69
}
70
71     /**
72      * Get an instance of the servlet configuration object
73      */

74     protected ServletConfiguration getInvokableInstance(String JavaDoc servletName)
75             throws ServletException JavaDoc, IOException JavaDoc {
76         ServletConfiguration sc = null;
77         synchronized (this.mountedInstances) {
78             if (this.mountedInstances.containsKey(servletName)) {
79                 sc = (ServletConfiguration) this.mountedInstances.get(servletName);
80             }
81         }
82
83         if (sc == null) {
84             // If found, mount an instance
85
try {
86                 // Class servletClass = Class.forName(servletName, true,
87
// Thread.currentThread().getContextClassLoader());
88
sc = new ServletConfiguration((WebAppConfiguration) this.getServletContext(),
89                         getServletConfig().getServletName() + ":" + servletName, servletName,
90                         new Hashtable JavaDoc(), -1);
91                 this.mountedInstances.put(servletName, sc);
92                 Logger.log(Logger.DEBUG, INVOKER_RESOURCES,
93                         "InvokerServlet.MountingServlet", new String JavaDoc[] {
94                                 servletName,
95                                 getServletConfig().getServletName() });
96                 // just to trigger the servlet.init()
97
sc.ensureInitialization();
98             } catch (Throwable JavaDoc err) {
99                 sc = null;
100             }
101         }
102         return sc;
103     }
104
105     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc rsp)
106             throws ServletException JavaDoc, IOException JavaDoc {
107         boolean isInclude = (req.getAttribute(INCLUDE_PATH_INFO) != null);
108 // boolean isForward = (req.getAttribute(FORWARD_PATH_INFO) != null);
109
String JavaDoc servletName = null;
110
111         if (isInclude)
112             servletName = (String JavaDoc) req.getAttribute(INCLUDE_PATH_INFO);
113 // else if (isForward)
114
// servletName = (String) req.getAttribute(FORWARD_PATH_INFO);
115
else if (req.getPathInfo() != null)
116             servletName = req.getPathInfo();
117         else
118             servletName = "";
119         if (servletName.startsWith("/"))
120             servletName = servletName.substring(1);
121         ServletConfiguration invokedServlet = getInvokableInstance(servletName);
122
123         if (invokedServlet == null) {
124             Logger.log(Logger.WARNING, INVOKER_RESOURCES,
125                     "InvokerServlet.NoMatchingServletFound", servletName);
126             rsp.sendError(HttpServletResponse.SC_NOT_FOUND, INVOKER_RESOURCES
127                     .getString("InvokerServlet.NoMatchingServletFound",
128                             servletName));
129         } else {
130             RequestDispatcher rd = new RequestDispatcher(
131                     (WebAppConfiguration) getServletContext(),
132                     invokedServlet);
133             rd.setForNamedDispatcher(new Mapping[0], new Mapping[0]);
134             rd.forward(req, rsp);
135         }
136     }
137
138     protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc rsp)
139             throws ServletException JavaDoc, IOException JavaDoc {
140         doGet(req, rsp);
141     }
142 }
143
Popular Tags