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;
017:
018: import javax.servlet.ServletConfig;
019: import javax.servlet.ServletContext;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: /**
025: * Accessor for the current ServerContext.
026: * @author Joe Walker [joe at getahead dot ltd dot uk]
027: */
028: public class ServerContextFactory {
029: /**
030: * Accessor for the current ServerContext in the normal case where there is
031: * only one DWR in the current classloader
032: * @return The current ServerContext.
033: */
034: public static ServerContext get() {
035: if (builder == null) {
036: log
037: .warn("ServerContextBuilder is null. This probably means that DWR has not initialized properly");
038: return null;
039: }
040:
041: return builder.get();
042: }
043:
044: /**
045: * Accessor for the current ServerContext.
046: * @param ctx The servlet context to allow us to bootstrap
047: * @return The current ServerContext.
048: */
049: public static ServerContext get(ServletContext ctx) {
050: if (builder == null) {
051: log
052: .warn("ServerContextBuilder is null. This probably means that DWR has not initialized properly");
053: return null;
054: }
055:
056: return builder.get(ctx);
057: }
058:
059: /**
060: * Internal method to allow us to get the ServerContextBuilder from which we
061: * will get ServerContext objects.
062: * Do not call this method from outside of DWR.
063: * @param builder The factory object (from DwrServlet)
064: */
065: public static void setServerContextBuilder(
066: ServerContextBuilder builder) {
067: ServerContextFactory.builder = builder;
068: }
069:
070: /**
071: * The ServerContextBuilder from which we will get ServerContext objects
072: */
073: private static ServerContextBuilder builder = null;
074:
075: /**
076: * Class to enable us to access servlet parameters.
077: */
078: public interface ServerContextBuilder {
079: /**
080: * Make the current webapp know what the current config/context is.
081: * This method is only for use internally to DWR.
082: * @param config The servlet configuration
083: * @param context The servlet context
084: * @param container The IoC container
085: */
086: void set(ServletConfig config, ServletContext context,
087: Container container);
088:
089: /**
090: * Accessor for the current ServerContext in the normal case where there
091: * is only one DWR in the current classloader
092: * @return The ServerContext that is associated with this web application
093: */
094: ServerContext get();
095:
096: /**
097: * Accessor for the current ServerContext
098: * @param context The web application environment
099: * @return The ServerContext that is associated with this web application
100: */
101: ServerContext get(ServletContext context);
102: }
103:
104: /**
105: * The log stream
106: */
107: private static final Log log = LogFactory
108: .getLog(WebContextFactory.class);
109: }
|