01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.directwebremoting;
17:
18: import javax.servlet.ServletConfig;
19: import javax.servlet.ServletContext;
20: import javax.servlet.http.HttpServletRequest;
21: import javax.servlet.http.HttpServletResponse;
22:
23: import org.apache.commons.logging.Log;
24: import org.apache.commons.logging.LogFactory;
25:
26: /**
27: * Accessor for the current WebContext.
28: * @author Joe Walker [joe at getahead dot ltd dot uk]
29: */
30: public class WebContextFactory {
31: /**
32: * Accessor for the current WebContext.
33: * @return The current WebContext or null if the current thread was not
34: * started by DWR.
35: */
36: public static WebContext get() {
37: if (builder == null) {
38: log
39: .warn("WebContextFactory.get() returns null when accessed from outside a DWR thread. Try ServerContext.get(), or see http://getahead.org/dwr/server/javaapi");
40: return null;
41: }
42:
43: return builder.get();
44: }
45:
46: /**
47: * The WebContextBuilder from which we will get WebContext objects
48: */
49: private static WebContextBuilder builder = null;
50:
51: /**
52: * Internal method to allow us to get the WebContextBuilder from which we
53: * will get WebContext objects.
54: * Do not call this method from outside of DWR.
55: * @param builder The factory object (from DwrServlet)
56: */
57: public static void setWebContextBuilder(WebContextBuilder builder) {
58: WebContextFactory.builder = builder;
59: }
60:
61: /**
62: * Class to enable us to access servlet parameters.
63: */
64: public interface WebContextBuilder {
65: /**
66: * Make the current thread know what the current request is.
67: * This method is only for use internally to DWR.
68: * @param request The incoming http request
69: * @param response The outgoing http reply
70: * @param config The servlet configuration
71: * @param context The servlet context
72: * @param container The IoC container
73: * @see #unset()
74: */
75: void set(HttpServletRequest request,
76: HttpServletResponse response, ServletConfig config,
77: ServletContext context, Container container);
78:
79: /**
80: * @return The WebContext that is associated with this thread
81: */
82: WebContext get();
83:
84: /**
85: * Unset the current ExecutionContext
86: * This method is only for use internally to DWR.
87: * @see #set(HttpServletRequest, HttpServletResponse, ServletConfig, ServletContext, Container)
88: */
89: void unset();
90: }
91:
92: /**
93: * The log stream
94: */
95: private static final Log log = LogFactory
96: .getLog(WebContextFactory.class);
97: }
|