1 7 package winstone; 8 9 import java.io.IOException ; 10 import java.util.Collections ; 11 import java.util.Enumeration ; 12 import java.util.Hashtable ; 13 import java.util.Map ; 14 15 import javax.servlet.Servlet ; 16 import javax.servlet.ServletContext ; 17 import javax.servlet.ServletException ; 18 import javax.servlet.ServletRequest ; 19 import javax.servlet.ServletResponse ; 20 import javax.servlet.UnavailableException ; 21 import javax.servlet.http.HttpServletResponse ; 22 23 import org.w3c.dom.Node ; 24 25 32 public class ServletConfiguration implements javax.servlet.ServletConfig , 33 Comparable { 34 35 static final String ELEM_NAME = "servlet-name"; 36 static final String ELEM_DISPLAY_NAME = "display-name"; 37 static final String ELEM_CLASS = "servlet-class"; 38 static final String ELEM_JSP_FILE = "jsp-file"; 39 static final String ELEM_DESCRIPTION = "description"; 40 static final String ELEM_INIT_PARAM = "init-param"; 41 static final String ELEM_INIT_PARAM_NAME = "param-name"; 42 static final String ELEM_INIT_PARAM_VALUE = "param-value"; 43 static final String ELEM_LOAD_ON_STARTUP = "load-on-startup"; 44 static final String ELEM_RUN_AS = "run-as"; 45 static final String ELEM_SECURITY_ROLE_REF = "security-role-ref"; 46 static final String ELEM_ROLE_NAME = "role-name"; 47 static final String ELEM_ROLE_LINK = "role-link"; 48 49 final String JSP_FILE = "org.apache.catalina.jsp_file"; 50 51 private String servletName; 52 private String classFile; 53 private Servlet instance; 54 private Map initParameters; 55 private WebAppConfiguration webAppConfig; 56 private int loadOnStartup; 57 private String jspFile; 58 private Map securityRoleRefs; 60 private Object servletSemaphore = new Boolean (true); 61 private boolean isSingleThreadModel = false; 62 private boolean unavailable = false; 63 private Throwable unavailableException = null; 64 65 protected ServletConfiguration(WebAppConfiguration webAppConfig) { 66 this.webAppConfig = webAppConfig; 67 this.initParameters = new Hashtable (); 68 this.loadOnStartup = -1; 69 this.securityRoleRefs = new Hashtable (); 70 } 71 72 public ServletConfiguration(WebAppConfiguration webAppConfig, String servletName, 73 String className, Map initParams, int loadOnStartup) { 74 this(webAppConfig); 75 if (initParams != null) 76 this.initParameters.putAll(initParams); 77 this.servletName = servletName; 78 this.classFile = className; 79 this.jspFile = null; 80 this.loadOnStartup = loadOnStartup; 81 } 82 83 public ServletConfiguration(WebAppConfiguration webAppConfig, Node elm) { 84 this(webAppConfig); 85 86 for (int n = 0; n < elm.getChildNodes().getLength(); n++) { 88 Node child = elm.getChildNodes().item(n); 89 if (child.getNodeType() != Node.ELEMENT_NODE) 90 continue; 91 String nodeName = child.getNodeName(); 92 93 if (nodeName.equals(ELEM_NAME)) 95 this.servletName = WebAppConfiguration.getTextFromNode(child); 96 else if (nodeName.equals(ELEM_CLASS)) 97 this.classFile = WebAppConfiguration.getTextFromNode(child); 98 else if (nodeName.equals(ELEM_JSP_FILE)) 99 this.jspFile = WebAppConfiguration.getTextFromNode(child); 100 else if (nodeName.equals(ELEM_LOAD_ON_STARTUP)) { 101 String index = child.getFirstChild() == null ? "-1" : 102 WebAppConfiguration.getTextFromNode(child); 103 this.loadOnStartup = Integer.parseInt(index); 104 } else if (nodeName.equals(ELEM_INIT_PARAM)) { 105 String paramName = ""; 106 String paramValue = ""; 107 for (int k = 0; k < child.getChildNodes().getLength(); k++) { 108 Node paramNode = child.getChildNodes().item(k); 109 if (paramNode.getNodeType() != Node.ELEMENT_NODE) 110 continue; 111 else if (paramNode.getNodeName().equals(ELEM_INIT_PARAM_NAME)) 112 paramName = WebAppConfiguration.getTextFromNode(paramNode); 113 else if (paramNode.getNodeName().equals(ELEM_INIT_PARAM_VALUE)) 114 paramValue = WebAppConfiguration.getTextFromNode(paramNode); 115 } 116 if (!paramName.equals("")) { 117 this.initParameters.put(paramName, paramValue); 118 } 119 } else if (nodeName.equals(ELEM_RUN_AS)) { 120 for (int m = 0; m < child.getChildNodes().getLength(); m++) { 121 Node roleElm = child.getChildNodes().item(m); 122 if ((roleElm.getNodeType() == Node.ELEMENT_NODE) 123 && (roleElm.getNodeName().equals(ELEM_ROLE_NAME))) { 124 } 126 } 127 } else if (nodeName.equals(ELEM_SECURITY_ROLE_REF)) { 128 String name = ""; 129 String link = ""; 130 for (int k = 0; k < child.getChildNodes().getLength(); k++) { 131 Node roleRefNode = child.getChildNodes().item(k); 132 if (roleRefNode.getNodeType() != Node.ELEMENT_NODE) 133 continue; 134 else if (roleRefNode.getNodeName().equals(ELEM_ROLE_NAME)) 135 name = WebAppConfiguration.getTextFromNode(roleRefNode); 136 else if (roleRefNode.getNodeName().equals(ELEM_ROLE_LINK)) 137 link = WebAppConfiguration.getTextFromNode(roleRefNode); 138 } 139 if (!name.equals("") && !link.equals("")) 140 this.initParameters.put(name, link); 141 } 142 } 143 144 if ((this.jspFile != null) && (this.classFile == null)) { 145 this.classFile = WebAppConfiguration.JSP_SERVLET_CLASS; 146 WebAppConfiguration.addJspServletParams(this.initParameters); 147 } 148 Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES, 149 "ServletConfiguration.DeployedInstance", new String [] { 150 this.servletName, this.classFile }); 151 } 152 153 public void ensureInitialization() { 154 155 if (this.instance != null) { 156 return; } 158 159 synchronized (this.servletSemaphore) { 160 161 if (this.instance != null) { 162 return; } 164 165 if (this.unavailableException != null) { 167 return; 168 } 169 170 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 172 Thread.currentThread().setContextClassLoader(this.webAppConfig.getLoader()); 173 174 Servlet newInstance = null; 175 Throwable otherError = null; 176 try { 177 Class servletClass = Class.forName(classFile, true, this.webAppConfig.getLoader()); 178 this.isSingleThreadModel = Class.forName("javax.servlet.SingleThreadModel").isInstance(this.instance); 179 newInstance = (Servlet ) servletClass.newInstance(); 180 181 Logger.log(Logger.DEBUG, Launcher.RESOURCES, "ServletConfiguration.init", this.servletName); 183 newInstance.init(this); 184 this.instance = newInstance; 185 } catch (ClassNotFoundException err) { 186 Logger.log(Logger.WARNING, Launcher.RESOURCES, 187 "ServletConfiguration.ClassLoadError", this.classFile, err); 188 setUnavailable(newInstance); 189 this.unavailableException = err; 190 } catch (IllegalAccessException err) { 191 Logger.log(Logger.WARNING, Launcher.RESOURCES, 192 "ServletConfiguration.ClassLoadError", this.classFile, err); 193 setUnavailable(newInstance); 194 this.unavailableException = err; 195 } catch (InstantiationException err) { 196 Logger.log(Logger.WARNING, Launcher.RESOURCES, 197 "ServletConfiguration.ClassLoadError", this.classFile, err); 198 setUnavailable(newInstance); 199 this.unavailableException = err; 200 } catch (ServletException err) { 201 Logger.log(Logger.WARNING, Launcher.RESOURCES, 202 "ServletConfiguration.InitError", this.servletName, err); 203 this.instance = null; setUnavailable(newInstance); 205 this.unavailableException = err; 206 } catch (RuntimeException err) { 207 otherError = err; 208 throw err; 209 } catch (Error err) { 210 otherError = err; 211 throw err; 212 } finally { 213 Thread.currentThread().setContextClassLoader(cl); 214 if ((otherError == null) && (this.unavailableException == null)) { 215 this.instance = newInstance; 216 } 217 } 218 } 219 return; 220 } 221 222 public void execute(ServletRequest request, ServletResponse response, String requestURI) 223 throws ServletException , IOException { 224 225 ensureInitialization(); 226 227 if (this.unavailable) { 229 RequestDispatcher rd = this.webAppConfig.getErrorDispatcherByClass( 232 this.unavailableException); 233 rd.forward(request, response); 234 return; 235 } 236 237 if (this.jspFile != null) 238 request.setAttribute(JSP_FILE, this.jspFile); 239 240 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 241 Thread.currentThread().setContextClassLoader(this.webAppConfig.getLoader()); 242 243 try { 244 if (this.isSingleThreadModel) { 245 synchronized (this) { 246 this.instance.service(request, response); 247 } 248 } else 249 this.instance.service(request, response); 250 } catch (UnavailableException err) { 251 setUnavailable(this.instance); 254 ((HttpServletResponse ) response).sendError(HttpServletResponse.SC_NOT_FOUND, 255 Launcher.RESOURCES.getString("StaticResourceServlet.PathNotFound", requestURI)); 256 } finally { 259 Thread.currentThread().setContextClassLoader(cl); 260 } 261 } 262 263 public int getLoadOnStartup() { 264 return this.loadOnStartup; 265 } 266 267 public String getInitParameter(String name) { 268 return (String ) this.initParameters.get(name); 269 } 270 271 public Enumeration getInitParameterNames() { 272 return Collections.enumeration(this.initParameters.keySet()); 273 } 274 275 public ServletContext getServletContext() { 276 return this.webAppConfig; 277 } 278 279 public String getServletName() { 280 return this.servletName; 281 } 282 283 public Map getSecurityRoleRefs() { 284 return this.securityRoleRefs; 285 } 286 287 291 public int compareTo(Object objTwo) { 292 Integer one = new Integer (this.loadOnStartup); 293 Integer two = new Integer (((ServletConfiguration) objTwo).loadOnStartup); 294 return one.compareTo(two); 295 } 296 297 300 public void destroy() { 301 synchronized (this.servletSemaphore) { 302 setUnavailable(this.instance); 303 } 304 } 305 306 protected void setUnavailable(Servlet unavailableServlet) { 307 308 this.unavailable = true; 309 if (unavailableServlet != null) { 310 Logger.log(Logger.DEBUG, Launcher.RESOURCES, 311 "ServletConfiguration.destroy", this.servletName); 312 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 313 Thread.currentThread().setContextClassLoader(this.webAppConfig.getLoader()); 314 try { 315 unavailableServlet.destroy(); 316 } finally { 317 Thread.currentThread().setContextClassLoader(cl); 318 this.instance = null; 319 } 320 } 321 322 this.webAppConfig.removeServletConfigurationAndMappings(this); 324 } 325 } 326 | Popular Tags |