1 7 package winstone.invoker; 8 9 import java.io.IOException ; 10 import java.util.Hashtable ; 11 import java.util.Iterator ; 12 import java.util.Map ; 13 14 import javax.servlet.ServletConfig ; 15 import javax.servlet.ServletException ; 16 import javax.servlet.http.HttpServlet ; 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 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 34 public class InvokerServlet extends HttpServlet { 35 private static final String 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 mountedInstances; 41 44 47 public void init(ServletConfig config) throws ServletException { 48 super.init(config); 49 this.mountedInstances = new Hashtable (); 50 } 53 54 57 public void destroy() { 58 if (this.mountedInstances != null) { 59 synchronized (this.mountedInstances) { 60 for (Iterator i = this.mountedInstances.values().iterator(); i 61 .hasNext();) 62 ((ServletConfiguration) i.next()).destroy(); 63 this.mountedInstances.clear(); 64 } 65 } 66 this.mountedInstances = null; 67 } 70 71 74 protected ServletConfiguration getInvokableInstance(String servletName) 75 throws ServletException , IOException { 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 try { 86 sc = new ServletConfiguration((WebAppConfiguration) this.getServletContext(), 89 getServletConfig().getServletName() + ":" + servletName, servletName, 90 new Hashtable (), -1); 91 this.mountedInstances.put(servletName, sc); 92 Logger.log(Logger.DEBUG, INVOKER_RESOURCES, 93 "InvokerServlet.MountingServlet", new String [] { 94 servletName, 95 getServletConfig().getServletName() }); 96 sc.ensureInitialization(); 98 } catch (Throwable err) { 99 sc = null; 100 } 101 } 102 return sc; 103 } 104 105 protected void doGet(HttpServletRequest req, HttpServletResponse rsp) 106 throws ServletException , IOException { 107 boolean isInclude = (req.getAttribute(INCLUDE_PATH_INFO) != null); 108 String servletName = null; 110 111 if (isInclude) 112 servletName = (String ) req.getAttribute(INCLUDE_PATH_INFO); 113 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 req, HttpServletResponse rsp) 139 throws ServletException , IOException { 140 doGet(req, rsp); 141 } 142 } 143 | Popular Tags |