001: package com.icesoft.faces.webapp.http.servlet;
002:
003: import com.icesoft.faces.webapp.http.common.Configuration;
004: import com.icesoft.faces.webapp.http.common.FileLocator;
005: import com.icesoft.faces.webapp.http.common.MimeTypeMatcher;
006: import com.icesoft.faces.webapp.http.core.ResourceServer;
007: import com.icesoft.faces.webapp.xmlhttp.PersistentFacesCommonlet;
008: import com.icesoft.util.IdGenerator;
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011:
012: import javax.servlet.ServletConfig;
013: import javax.servlet.ServletContext;
014: import javax.servlet.ServletException;
015: import javax.servlet.http.HttpServlet;
016: import javax.servlet.http.HttpServletRequest;
017: import javax.servlet.http.HttpServletResponse;
018: import javax.servlet.http.HttpSession;
019: import java.io.File;
020: import java.io.IOException;
021: import java.net.MalformedURLException;
022: import java.net.URI;
023: import java.net.URL;
024:
025: public class MainServlet extends HttpServlet {
026:
027: private static Log log = LogFactory.getLog(MainServlet.class);
028:
029: private PathDispatcher dispatcher = new PathDispatcher();
030: private static final String AWT_HEADLESS = "java.awt.headless";
031: private String contextPath;
032:
033: public void init(ServletConfig servletConfig)
034: throws ServletException {
035: super .init(servletConfig);
036: try {
037: final ServletContext servletContext = servletConfig
038: .getServletContext();
039: String awtHeadless = System.getProperty(AWT_HEADLESS);
040: if (null == awtHeadless) {
041: System.setProperty(AWT_HEADLESS, "true");
042: }
043: final Configuration configuration = new ServletContextConfiguration(
044: "com.icesoft.faces", servletContext);
045: final IdGenerator idGenerator = getIdGenerator(servletContext);
046: final MimeTypeMatcher mimeTypeMatcher = new MimeTypeMatcher() {
047: public String mimeTypeFor(String extension) {
048: return servletContext.getMimeType(extension);
049: }
050: };
051: final FileLocator localFileLocator = new FileLocator() {
052: public File locate(String path) {
053: URI contextURI = URI.create(contextPath);
054: URI pathURI = URI.create(path);
055: String result = contextURI.relativize(pathURI)
056: .getPath();
057: String fileLocation = servletContext
058: .getRealPath(result);
059: return new File(fileLocation);
060: }
061: };
062:
063: PseudoServlet resourceServer = new BasicAdaptingServlet(
064: new ResourceServer(configuration, mimeTypeMatcher,
065: localFileLocator));
066: PseudoServlet sessionServer = new SessionDispatcher() {
067: protected PseudoServlet newServlet(HttpSession session,
068: Listener.Monitor sessionMonitor) {
069: return new MainSessionBoundServlet(session,
070: sessionMonitor, idGenerator, configuration);
071: }
072: };
073:
074: dispatcher
075: .dispatchOn(
076: ".*(\\.iface$|\\.jsf|\\.faces$|\\.jsp$|\\.jspx$|\\.html$|\\.xhtml$|\\.seam$|uploadHtml$|block\\/)",
077: sessionServer);
078: dispatcher.dispatchOn(".*", resourceServer);
079: } catch (Exception e) {
080: throw new ServletException(e);
081: }
082: }
083:
084: private IdGenerator getIdGenerator(ServletContext servletContext)
085: throws MalformedURLException {
086: URL res = servletContext.getResource("/");
087: //ICE-985: Some app servers will return null when you ask for a
088: //directory as a resource. Those special circumstances where
089: //it doesn't work, we'll try to locate a known resource.
090: if (res == null) {
091: res = servletContext.getResource("/WEB-INF/web.xml");
092: if (res == null) {
093: if (log.isErrorEnabled()) {
094: log.error("invalid resource path");
095: }
096: throw new NullPointerException("invalid resource path");
097: }
098: }
099: return new IdGenerator(res.getPath());
100: }
101:
102: public void service(HttpServletRequest request,
103: HttpServletResponse response) throws ServletException,
104: IOException {
105: //set flag to indicate an ICEfaces request so that delegateNonIface
106: //will detect this and execute D2DViewHandler for it
107: request.setAttribute(PersistentFacesCommonlet.SERVLET_KEY,
108: PersistentFacesCommonlet.PERSISTENT);
109: contextPath = request.getContextPath();
110:
111: try {
112: dispatcher.service(request, response);
113: } catch (RuntimeException e) {
114: throw e;
115: } catch (Exception e) {
116: throw new ServletException(e);
117: }
118: }
119:
120: public void destroy() {
121: dispatcher.shutdown();
122: }
123: }
|