KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > webapp > http > servlet > EnvironmentAdaptingServlet


1 package com.icesoft.faces.webapp.http.servlet;
2
3 import com.icesoft.faces.webapp.http.common.Configuration;
4 import com.icesoft.faces.webapp.http.common.Server;
5 import com.icesoft.faces.webapp.http.core.ViewQueue;
6
7 import java.util.Collection JavaDoc;
8
9 import javax.servlet.http.HttpServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11 import javax.servlet.ServletContext JavaDoc;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 public class EnvironmentAdaptingServlet implements PseudoServlet {
17     private static final Log LOG =
18         LogFactory.getLog(EnvironmentAdaptingServlet.class);
19
20     private PseudoServlet servlet;
21
22     public EnvironmentAdaptingServlet(
23         final Server server, final Configuration configuration,
24         final String JavaDoc icefacesID, final Collection JavaDoc synchronouslyUpdatedViews,
25         final ViewQueue allUpdatedViews, final ServletContext JavaDoc servletContext) {
26
27         boolean useAsyncHttpServerByDefault;
28         try {
29             getClass().getClassLoader().
30                 loadClass(
31                     "com.icesoft.faces.async.server." +
32                         "AsyncHttpServerAdaptingServlet");
33             useAsyncHttpServerByDefault = true;
34         } catch (ClassNotFoundException JavaDoc exception) {
35             useAsyncHttpServerByDefault = false;
36         }
37         boolean useJettyContinuationsByDefault;
38         try {
39             getClass().getClassLoader().
40                 loadClass("org.mortbay.util.ajax.Continuation");
41             useJettyContinuationsByDefault = true;
42         } catch (ClassNotFoundException JavaDoc e) {
43             useJettyContinuationsByDefault = false;
44         }
45         if (configuration.getAttributeAsBoolean(
46                 "useAsyncHttpServer", useAsyncHttpServerByDefault)) {
47
48             if (LOG.isDebugEnabled()) {
49                 LOG.debug("Adapting to Asynchronous HTTP Server environment.");
50             }
51             try {
52                 servlet =
53                     (PseudoServlet)
54                         this.getClass().getClassLoader().loadClass(
55                             "com.icesoft.faces.async.server." +
56                                 "AsyncHttpServerAdaptingServlet").
57                                     getDeclaredConstructor(
58                                         new Class JavaDoc[] {
59                                             Server.class,
60                                             String JavaDoc.class,
61                                             Collection JavaDoc.class,
62                                             ViewQueue.class,
63                                             ServletContext JavaDoc.class
64                                         }).
65                                     newInstance(
66                                         new Object JavaDoc[] {
67                                             server,
68                                             icefacesID,
69                                             synchronouslyUpdatedViews,
70                                             allUpdatedViews,
71                                             servletContext
72                                         });
73             } catch (Exception JavaDoc exception) {
74                 // Possible exceptions: ClassNotFoundException,
75
// NoSuchMethodException,
76
// IntantiationException,
77
// InvocationTargetException,
78
// IllegalAccessException
79
if (LOG.isFatalEnabled()) {
80                     LOG.fatal(
81                         "Failed to instantiate AsyncHttpServerAdaptingServlet!",
82                         exception);
83                 }
84             }
85         } else if (configuration.getAttributeAsBoolean(
86                 "useJettyContinuations", useJettyContinuationsByDefault)) {
87
88             if (LOG.isDebugEnabled()) {
89                 LOG.debug("Adapting to Jetty's Continuation environment.");
90             }
91             servlet = new ContinuationAdaptingServlet(server);
92         } else {
93             if (LOG.isDebugEnabled()) {
94                 LOG.debug("Adapting to thread blocking environment.");
95             }
96             servlet = new ThreadBlockingAdaptingServlet(server);
97         }
98     }
99
100     public void service(
101         final HttpServletRequest JavaDoc request, final HttpServletResponse JavaDoc response)
102     throws Exception JavaDoc {
103         servlet.service(request, response);
104     }
105
106     public void shutdown() {
107         servlet.shutdown();
108     }
109 }
110
Popular Tags