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.ServletConfig;
019: import javax.servlet.ServletContext;
020: import javax.servlet.http.HttpServlet;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.directwebremoting.Container;
025: import org.directwebremoting.HubFactory;
026: import org.directwebremoting.ServerContextFactory;
027: import org.directwebremoting.WebContextFactory;
028: import org.directwebremoting.HubFactory.HubBuilder;
029: import org.directwebremoting.ServerContextFactory.ServerContextBuilder;
030: import org.directwebremoting.WebContextFactory.WebContextBuilder;
031: import org.directwebremoting.extend.ContainerConfigurationException;
032: import org.directwebremoting.util.FakeServletConfig;
033: import org.directwebremoting.util.FakeServletContext;
034: import org.directwebremoting.util.VersionUtil;
035:
036: /**
037: * Some utilities to help get DWR up and running
038: * @author Joe Walker [joe at getahead dot ltd dot uk]
039: */
040: public class StartupUtil {
041: /**
042: * A way to setup DWR outside of any Containers.
043: * This method can also serve as a template for in container code wanting
044: * to get DWR setup. Callers of this method should clean up after themselves
045: * by calling {@link #outOfContainerDestroy(Container)}
046: * @return A new initialized container.
047: * @throws ContainerConfigurationException If we can't use a bean
048: */
049: public static Container outOfContainerInit()
050: throws ContainerConfigurationException {
051: try {
052: ServletConfig servletConfig = new FakeServletConfig("test",
053: new FakeServletContext());
054: ServletContext servletContext = servletConfig
055: .getServletContext();
056:
057: StartupUtil.logStartup(servletConfig);
058:
059: Container container = ContainerUtil
060: .createAndSetupDefaultContainer(servletConfig);
061:
062: StartupUtil.initContainerBeans(servletConfig,
063: servletContext, container);
064: WebContextBuilder webContextBuilder = container
065: .getBean(WebContextBuilder.class);
066:
067: ContainerUtil.prepareForWebContextFilter(servletContext,
068: servletConfig, container, webContextBuilder, null);
069: ContainerUtil.publishContainer(container, servletConfig);
070: ContainerUtil.configureContainerFully(container,
071: servletConfig);
072:
073: return container;
074: } catch (ContainerConfigurationException ex) {
075: throw ex;
076: } catch (Exception ex) {
077: throw new ContainerConfigurationException(ex);
078: }
079: }
080:
081: /**
082: * Clean up the current thread when {@link #outOfContainerInit()} has been
083: * called.
084: * @param container The container created by {@link #outOfContainerInit()}.
085: */
086: public static void outOfContainerDestroy(Container container) {
087: WebContextBuilder webContextBuilder = container
088: .getBean(WebContextBuilder.class);
089: if (webContextBuilder != null) {
090: webContextBuilder.unset();
091: }
092: }
093:
094: /**
095: * Some logging so we have a good clue what we are working with.
096: * @param config The servlet config
097: */
098: public static void logStartup(ServletConfig config) {
099: log
100: .info("DWR Version " + VersionUtil.getLabel()
101: + " starting.");
102: log.info("- Servlet Engine: "
103: + config.getServletContext().getServerInfo());
104: log.info("- Java Version: "
105: + System.getProperty("java.version"));
106: log.info("- Java Vendor: "
107: + System.getProperty("java.vendor"));
108: }
109:
110: /**
111: * Get the various objects out of the {@link Container}, and configure them.
112: * @param servletConfig The servlet configuration
113: * @param servletContext The servlet context
114: * @param container The container to save in the ServletContext
115: */
116: public static void initContainerBeans(ServletConfig servletConfig,
117: ServletContext servletContext, Container container) {
118: initWebContext(servletConfig, servletContext, container);
119: initServerContext(servletConfig, servletContext, container);
120: initHub(servletContext, container);
121: }
122:
123: /**
124: * Get the {@link WebContextFactory.WebContextBuilder} out of the
125: * {@link Container}, configure it (call WebContextBuilder#set()) and use it
126: * to configure the {@link WebContextFactory}.
127: * @param servletConfig The servlet configuration
128: * @param servletContext The servlet context
129: * @param container The container to save in the ServletContext
130: * @return a new WebContextBuilder
131: * @deprecated Use {@link #initContainerBeans(ServletConfig, ServletContext, Container)}
132: */
133: @Deprecated
134: public static WebContextBuilder initWebContext(
135: ServletConfig servletConfig, ServletContext servletContext,
136: Container container) {
137: WebContextBuilder webContextBuilder = container
138: .getBean(WebContextBuilder.class);
139: WebContextFactory.setWebContextBuilder(webContextBuilder);
140: webContextBuilder.set(null, null, servletConfig,
141: servletContext, container);
142:
143: return webContextBuilder;
144: }
145:
146: /**
147: * Get the {@link ServerContextFactory.ServerContextBuilder} out of the
148: * {@link Container}, configure it and use it to configure the
149: * {@link ServerContextFactory}
150: * @param servletConfig The servlet configuration
151: * @param servletContext The servlet context
152: * @param container The container to save in the ServletContext
153: * @return The newly created ServerContextBuilder
154: * @deprecated Use {@link #initContainerBeans(ServletConfig, ServletContext, Container)}
155: */
156: @Deprecated
157: public static ServerContextBuilder initServerContext(
158: ServletConfig servletConfig, ServletContext servletContext,
159: Container container) {
160: ServerContextBuilder serverContextBuilder = container
161: .getBean(ServerContextBuilder.class);
162: ServerContextFactory
163: .setServerContextBuilder(serverContextBuilder);
164: serverContextBuilder.set(servletConfig, servletContext,
165: container);
166:
167: return serverContextBuilder;
168: }
169:
170: /**
171: * Get the {@link HubFactory.HubBuilder} out of the {@link Container},
172: * configure it and use it to configure the {@link HubFactory}
173: * @param servletContext The servlet context
174: * @param container The container to save in the ServletContext
175: * @return The newly created HubBuilder
176: * @deprecated Use {@link #initContainerBeans(ServletConfig, ServletContext, Container)}
177: */
178: @Deprecated
179: public static HubBuilder initHub(ServletContext servletContext,
180: Container container) {
181: HubBuilder hubBuilder = container.getBean(HubBuilder.class);
182: HubFactory.setHubBuilder(hubBuilder);
183: hubBuilder.set(servletContext);
184:
185: return hubBuilder;
186: }
187:
188: /**
189: * We have some special logging classes to maintain an optional dependence
190: * on commons-logging. This sets the servlet for when this is not available.
191: * @param servletConfig The servlet configuration
192: * @param servlet The servlet that we are running under
193: * @deprecated Since version 2.1 DWR does not use Servlet Logging
194: */
195: @Deprecated
196: @SuppressWarnings("unused")
197: public static void setupLogging(ServletConfig servletConfig,
198: HttpServlet servlet) {
199: }
200:
201: /**
202: * The log stream
203: */
204: private static final Log log = LogFactory.getLog(StartupUtil.class);
205: }
|