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.impl;
17:
18: import javax.servlet.ServletConfig;
19: import javax.servlet.ServletContext;
20:
21: import org.apache.commons.logging.LogFactory;
22: import org.apache.commons.logging.Log;
23: import org.directwebremoting.Container;
24: import org.directwebremoting.ServerContext;
25: import org.directwebremoting.ServerContextFactory.ServerContextBuilder;
26:
27: /**
28: * A ServerContextBuilder that creates DefaultServerContexts.
29: * @author Joe Walker [joe at getahead dot ltd dot uk]
30: */
31: public class DefaultServerContextBuilder implements
32: ServerContextBuilder {
33: /* (non-Javadoc)
34: * @see ServerContextBuilder#set(javax.servlet.ServletConfig, javax.servlet.ServletContext, org.directwebremoting.Container)
35: */
36: public void set(ServletConfig config, ServletContext context,
37: Container container) {
38: try {
39: ServerContext ec = new DefaultServerContext(config,
40: context, container);
41: context.setAttribute(ATTRIBUTE_SERVER_CONTEXT, ec);
42: } catch (Exception ex) {
43: log.fatal("Failed to create an ExecutionContext", ex);
44: }
45: }
46:
47: /* (non-Javadoc)
48: * @see org.directwebremoting.ServerContextBuilder#get()
49: */
50: public ServerContext get(ServletContext context) {
51: if (context == null) {
52: throw new NullPointerException("context");
53: }
54:
55: ServerContext reply = (ServerContext) context
56: .getAttribute(ATTRIBUTE_SERVER_CONTEXT);
57: if (reply == null) {
58: log
59: .warn("ServerContextFactory.get(ServletContext) returns null when DWR has not been initialized in the given ServletContext");
60: }
61:
62: return reply;
63: }
64:
65: /* (non-Javadoc)
66: * @see org.directwebremoting.ServerContextBuilder#get()
67: */
68: public ServerContext get() {
69: ServerContext serverContext = ContainerUtil
70: .getSingletonServerContext();
71: if (serverContext == null) {
72: log
73: .fatal("Error initializing Hub because singleton ServerContext == null.");
74: log
75: .fatal("This probably means that either DWR has not been properly initialized (in which case you should delay the current action until it has)");
76: log
77: .fatal("or that there is more than 1 DWR servlet is configured in this classloader, in which case you should provide a ServletContext to the Hub yourself.");
78: throw new IllegalStateException(
79: "No singleton ServerContext see logs for possible causes and solutions.");
80: }
81:
82: return serverContext;
83: }
84:
85: /**
86: * The attribute under which we publish the ServerContext
87: */
88: private static final String ATTRIBUTE_SERVER_CONTEXT = "org.directwebremoting.impl.ServerContext";
89:
90: /**
91: * The log stream
92: */
93: private static final Log log = LogFactory
94: .getLog(DefaultServerContextBuilder.class);
95: }
|