001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.impl;
017:
018: import javax.servlet.ServletContext;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.directwebremoting.Hub;
023: import org.directwebremoting.ServerContext;
024: import org.directwebremoting.WebContextFactory;
025: import org.directwebremoting.HubFactory.HubBuilder;
026:
027: /**
028: * A HubBuilder that creates DefaultHubs.
029: * @author Joe Walker [joe at getahead dot ltd dot uk]
030: */
031: public class DefaultHubBuilder implements HubBuilder {
032: /* (non-Javadoc)
033: * @see HubBuilder#set(javax.servlet.ServletContext)
034: */
035: public void set(ServletContext context) {
036: try {
037: Hub hub = new DefaultHub();
038: context.setAttribute(ATTRIBUTE_SERVER_CONTEXT, hub);
039: } catch (Exception ex) {
040: log.fatal("Failed to create an ExecutionContext", ex);
041: }
042: }
043:
044: /* (non-Javadoc)
045: * @see org.directwebremoting.HubBuilder#get()
046: */
047: public Hub get() {
048: ServerContext serverContext;
049:
050: // Try to get the context from the thread
051: serverContext = WebContextFactory.get();
052: if (serverContext == null) {
053: // If not see if there is a singleton
054: serverContext = ContainerUtil.getSingletonServerContext();
055: if (serverContext == null) {
056: log
057: .fatal("Error initializing Hub because singleton ServerContext == null.");
058: log
059: .fatal("This probably means that either DWR has not been properly initialized (in which case you should delay the current action until it has)");
060: log
061: .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.");
062: throw new IllegalStateException(
063: "No singleton ServerContext see logs for possible causes and solutions.");
064: }
065: }
066:
067: ServletContext servletContext = serverContext
068: .getServletContext();
069: Hub reply = (Hub) servletContext
070: .getAttribute(ATTRIBUTE_SERVER_CONTEXT);
071: if (reply == null) {
072: log
073: .warn("HubFactory.get() returns null when DWR has not been initialized in the given ServletContext");
074: }
075:
076: return reply;
077: }
078:
079: /* (non-Javadoc)
080: * @see org.directwebremoting.HubBuilder#get()
081: */
082: public Hub get(ServletContext servletContext) {
083: if (servletContext == null) {
084: throw new NullPointerException("servletContext");
085: }
086:
087: Hub reply = (Hub) servletContext
088: .getAttribute(ATTRIBUTE_SERVER_CONTEXT);
089: if (reply == null) {
090: log
091: .warn("HubFactory.get(ServletContext) returns null when DWR has not been initialized in the given ServletContext");
092: }
093:
094: return reply;
095: }
096:
097: /**
098: * The attribute under which we publish the Hub
099: */
100: private static final String ATTRIBUTE_SERVER_CONTEXT = "org.directwebremoting.impl.Hub";
101:
102: /**
103: * The log stream
104: */
105: private static final Log log = LogFactory
106: .getLog(DefaultHubBuilder.class);
107: }
|