KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > server > jetty > JspPrecompileConfiguration


1 package com.sslexplorer.server.jetty;
2
3 import java.io.IOException JavaDoc;
4 import java.net.MalformedURLException JavaDoc;
5 import java.net.URL JavaDoc;
6 import java.util.Iterator JavaDoc;
7
8 import javax.servlet.UnavailableException JavaDoc;
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 /**
21  * Configuration implementation for precompiled JSP mappings.
22  *
23  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
24  */

25 public class JspPrecompileConfiguration implements Configuration {
26     private static Log log = LogFactory.getLog(JspPrecompileConfiguration.class);
27     WebApplicationContext _context;
28     protected XmlParser xmlParser;
29
30     /**
31      * Constructor.
32      *
33      */

34     public JspPrecompileConfiguration() {
35         super();
36         xmlParser = new XmlParser();
37         URL JavaDoc 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 JavaDoc {
51     }
52
53     public void configureDefaults() throws Exception JavaDoc {
54     }
55
56     public void configureWebApp() throws Exception JavaDoc {
57         Resource webInf = getWebApplicationContext().getWebInf();
58         // handle any WEB-INF descriptors
59
if (webInf != null && webInf.isDirectory()) {
60             // do web.xml file
61
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 JavaDoc, UnavailableException JavaDoc {
74         Iterator JavaDoc iter = config.iterator();
75         XmlParser.Node node = null;
76         while (iter.hasNext()) {
77             try {
78                 Object JavaDoc o = iter.next();
79                 if (!(o instanceof XmlParser.Node))
80                     continue;
81                 node = (XmlParser.Node) o;
82                 String JavaDoc name = node.getTag();
83                 initWebXmlElement(name, node);
84             } catch (ClassNotFoundException JavaDoc e) {
85                 throw e;
86             } catch (Exception JavaDoc e) {
87                 log.warn("Configuration problem at " + node, e);
88                 throw new UnavailableException JavaDoc("Configuration problem");
89             }
90         }
91     }
92
93     protected WebApplicationHandler getWebApplicationHandler()
94     {
95          return _context.getWebApplicationHandler();
96     }
97
98     protected void initWebXmlElement(String JavaDoc element, XmlParser.Node node) throws Exception JavaDoc {
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 JavaDoc,UnavailableException JavaDoc,IOException JavaDoc,
107             MalformedURLException JavaDoc
108     {
109         String JavaDoc name=node.getString("servlet-name",false,true);
110         String JavaDoc className=node.getString("servlet-class",false,true);
111         String JavaDoc 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 JavaDoc name=node.getString("servlet-name",false,true);
122         String JavaDoc pathSpec=node.getString("url-pattern",false,true);
123         getWebApplicationHandler().mapPathToServlet(pathSpec,name);
124     }
125
126 }
127
Popular Tags