KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > impl > StartupUtil


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
19 import javax.servlet.ServletContext JavaDoc;
20 import javax.servlet.ServletException JavaDoc;
21 import javax.servlet.http.HttpServlet JavaDoc;
22
23 import org.directwebremoting.Container;
24 import org.directwebremoting.ServerContextFactory;
25 import org.directwebremoting.WebContextFactory;
26 import org.directwebremoting.ServerContextFactory.ServerContextBuilder;
27 import org.directwebremoting.WebContextFactory.WebContextBuilder;
28 import org.directwebremoting.util.FakeServletConfig;
29 import org.directwebremoting.util.FakeServletContext;
30 import org.directwebremoting.util.Logger;
31 import org.directwebremoting.util.ServletLoggingOutput;
32 import org.directwebremoting.util.VersionUtil;
33
34 /**
35  * Some utilities to help get DWR up and running
36  * @author Joe Walker [joe at getahead dot ltd dot uk]
37  */

38 public class StartupUtil
39 {
40     /**
41      * A way to setup DWR outside of any Containers.
42      * This method can also serve as a template for in container code wanting
43      * to get DWR setup. Callers of this method should clean up after themselves
44      * by calling {@link #outOfContainerDestroy(Container)}
45      * @return A new initialized container.
46      * @throws ServletException If the setup fails.
47      */

48     public Container outOfContainerInit() throws ServletException JavaDoc
49     {
50         try
51         {
52             ServletConfig JavaDoc servletConfig = new FakeServletConfig("test", new FakeServletContext());
53             ServletContext JavaDoc servletContext = servletConfig.getServletContext();
54
55             StartupUtil.setupLogging(servletConfig, null);
56             StartupUtil.logStartup(servletConfig);
57
58             DefaultContainer container = ContainerUtil.createDefaultContainer(servletConfig);
59             ContainerUtil.setupDefaultContainer(container, servletConfig);
60
61             WebContextBuilder webContextBuilder = StartupUtil.initWebContext(servletConfig, servletContext, container);
62             StartupUtil.initServerContext(servletConfig, servletContext, container);
63
64             ContainerUtil.prepareForWebContextFilter(servletContext, servletConfig, container, webContextBuilder, null);
65             ContainerUtil.configureContainerFully(container, servletConfig);
66             ContainerUtil.publishContainer(container, servletConfig);
67
68             return container;
69         }
70         catch (ServletException JavaDoc ex)
71         {
72             throw ex;
73         }
74         catch (Exception JavaDoc ex)
75         {
76             throw new ServletException JavaDoc(ex);
77         }
78     }
79
80     /**
81      * Clean up the current thread when {@link #outOfContainerInit()} has been
82      * called.
83      * @param container The container created by {@link #outOfContainerInit()}.
84      */

85     public void outOfContainerDestroy(Container container)
86     {
87         ServletLoggingOutput.unsetExecutionContext();
88
89         WebContextBuilder webContextBuilder = (WebContextBuilder) container.getBean(WebContextBuilder.class.getName());
90         if (webContextBuilder != null)
91         {
92             webContextBuilder.unset();
93         }
94     }
95
96     /**
97      * Some logging so we have a good clue what we are working with.
98      * @param config The servlet config
99      */

100     public static void logStartup(ServletConfig JavaDoc config)
101     {
102         log.info("DWR Version " + VersionUtil.getVersion() + " starting.");
103         log.info("- Servlet Engine: " + config.getServletContext().getServerInfo());
104         log.info("- Java Version: " + System.getProperty("java.version"));
105         log.info("- Java Vendor: " + System.getProperty("java.vendor"));
106     }
107
108     /**
109      * Get the {@link WebContextFactory.WebContextBuilder} out of the
110      * {@link Container}, configure it (call WebContextBuilder#set()) and use it
111      * to configure the {@link WebContextFactory}.
112      * @param servletConfig The servlet configuration
113      * @param servletContext The servlet context
114      * @param servlet The servlet that we are running under
115      * @param container The container to save in the ServletContext
116      * @return a new WebContextBuilder
117      */

118     public static WebContextBuilder initWebContext(ServletConfig JavaDoc servletConfig, ServletContext JavaDoc servletContext, Container container)
119     {
120         WebContextBuilder webContextBuilder = (WebContextBuilder) container.getBean(WebContextBuilder.class.getName());
121         WebContextFactory.setWebContextBuilder(webContextBuilder);
122         webContextBuilder.set(null, null, servletConfig, servletContext, container);
123
124         return webContextBuilder;
125     }
126
127     /**
128      * Get the {@link ServerContextFactory.ServerContextBuilder} out of the
129      * {@link Container}, configure it and use it to configure the
130      * {@link ServerContextFactory}
131      * @param servletConfig The servlet configuration
132      * @param servletContext The servlet context
133      * @param container The container to save in the ServletContext
134      * @return The newly created ServerContextBuilder
135      */

136     public static ServerContextBuilder initServerContext(ServletConfig JavaDoc servletConfig, ServletContext JavaDoc servletContext, Container container)
137     {
138         ServerContextBuilder serverContextBuilder = (ServerContextBuilder) container.getBean(ServerContextBuilder.class.getName());
139         ServerContextFactory.setServerContextBuilder(serverContextBuilder);
140         serverContextBuilder.set(servletConfig, servletContext, container);
141
142         return serverContextBuilder;
143     }
144
145     /**
146      * We have some special logging classes to maintain an optional dependence
147      * on commons-logging. This sets the servlet for when this is not available.
148      * @param servletConfig The servlet configuration
149      * @param servlet The servlet that we are running under
150      */

151     public static void setupLogging(ServletConfig JavaDoc servletConfig, HttpServlet JavaDoc servlet)
152     {
153         ServletLoggingOutput.setExecutionContext(servlet);
154         String JavaDoc logLevel = servletConfig.getInitParameter(ContainerUtil.INIT_LOGLEVEL);
155         if (logLevel != null)
156         {
157             ServletLoggingOutput.setLevel(logLevel);
158         }
159     }
160
161     /**
162      * The log stream
163      */

164     private static final Logger log = Logger.getLogger(StartupUtil.class);
165 }
166
Popular Tags