1 package com.sslexplorer.server.jetty; 2 3 import java.io.IOException ; 4 import java.net.MalformedURLException ; 5 import java.net.URL ; 6 import java.util.Iterator ; 7 8 import javax.servlet.UnavailableException ; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.mortbay.jetty.servlet.ServletHolder; 13 import org.mortbay.jetty.servlet.WebApplicationContext; 14 import org.mortbay.jetty.servlet.WebApplicationHandler; 15 import org.mortbay.jetty.servlet.WebApplicationContext.Configuration; 16 import org.mortbay.util.Resource; 17 import org.mortbay.xml.XmlParser; 18 19 20 25 public class JspPrecompileConfiguration implements Configuration { 26 private static Log log = LogFactory.getLog(JspPrecompileConfiguration.class); 27 WebApplicationContext _context; 28 protected XmlParser xmlParser; 29 30 34 public JspPrecompileConfiguration() { 35 super(); 36 xmlParser = new XmlParser(); 37 URL dtd=WebApplicationContext.class.getResource("/com/sslexplorer/server/jetty/jsp-mappings.dtd"); 38 xmlParser.redirectEntity("jsp-mappings.dtd",dtd); 39 xmlParser.redirectEntity("-//3SP//JSP Mappings//EN",dtd); 40 } 41 42 public void setWebApplicationContext(WebApplicationContext context) { 43 this._context = context; 44 } 45 46 public WebApplicationContext getWebApplicationContext() { 47 return _context; 48 } 49 50 public void configureClassPath() throws Exception { 51 } 52 53 public void configureDefaults() throws Exception { 54 } 55 56 public void configureWebApp() throws Exception { 57 Resource webInf = getWebApplicationContext().getWebInf(); 58 if (webInf != null && webInf.isDirectory()) { 60 Resource web = webInf.addPath("jsp-mappings.xml"); 62 if (!web.exists()) { 63 log.info("No WEB-INF/jsp-mappings.xml in " + getWebApplicationContext().getWAR() 64 + ". Jsps will be compile on use making initial page loading slow."); 65 } else { 66 XmlParser.Node config = null; 67 config = xmlParser.parse(web.getURL()); 68 initialize(config); 69 } 70 } 71 } 72 73 protected void initialize(XmlParser.Node config) throws ClassNotFoundException , UnavailableException { 74 Iterator iter = config.iterator(); 75 XmlParser.Node node = null; 76 while (iter.hasNext()) { 77 try { 78 Object o = iter.next(); 79 if (!(o instanceof XmlParser.Node)) 80 continue; 81 node = (XmlParser.Node) o; 82 String name = node.getTag(); 83 initWebXmlElement(name, node); 84 } catch (ClassNotFoundException e) { 85 throw e; 86 } catch (Exception e) { 87 log.warn("Configuration problem at " + node, e); 88 throw new UnavailableException ("Configuration problem"); 89 } 90 } 91 } 92 93 protected WebApplicationHandler getWebApplicationHandler() 94 { 95 return _context.getWebApplicationHandler(); 96 } 97 98 protected void initWebXmlElement(String element, XmlParser.Node node) throws Exception { 99 if ("servlet".equals(element)) 100 initServlet(node); 101 else if ("servlet-mapping".equals(element)) 102 initServletMapping(node); 103 } 104 105 106 protected void initServlet(XmlParser.Node node) throws ClassNotFoundException ,UnavailableException ,IOException , 107 MalformedURLException 108 { 109 String name=node.getString("servlet-name",false,true); 110 String className=node.getString("servlet-class",false,true); 111 String jspFile=null; 112 if(name==null) 113 name=className; 114 ServletHolder holder=getWebApplicationHandler().newServletHolder(name,className,jspFile); 115 if(jspFile!=null) 116 holder.setInitParameter("classpath",getWebApplicationContext().getFileClassPath()); 117 } 118 119 protected void initServletMapping(XmlParser.Node node) 120 { 121 String name=node.getString("servlet-name",false,true); 122 String pathSpec=node.getString("url-pattern",false,true); 123 getWebApplicationHandler().mapPathToServlet(pathSpec,name); 124 } 125 126 } 127 | Popular Tags |