KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > guice > DwrGuiceServletContextListener


1 /*
2  * Copyright 2007 Tim Peierls
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.guice;
17
18 import com.google.inject.AbstractModule;
19 import com.google.inject.Guice;
20 import com.google.inject.Injector;
21 import com.google.inject.Key;
22 import com.google.inject.Module;
23 import com.google.inject.Stage;
24 import com.google.inject.TypeLiteral;
25 import static com.google.inject.name.Names.named;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.prefs.Preferences JavaDoc;
31
32 import javax.servlet.ServletContext JavaDoc;
33 import javax.servlet.ServletContextEvent JavaDoc;
34 import javax.servlet.ServletContextListener JavaDoc;
35
36 import org.directwebremoting.impl.DefaultContainer;
37 import org.directwebremoting.util.Logger;
38
39 import static org.directwebremoting.guice.DwrGuiceUtil.popServletContext;
40 import static org.directwebremoting.guice.DwrGuiceUtil.pushServletContext;
41
42 /**
43  * Register a concrete subclass of this as a servlet context listener to
44  * configure an {@link Injector} and stash it in the {@link ServletContext}.
45  * @author Tim Peierls [tim at peierls dot net]
46  */

47 public abstract class DwrGuiceServletContextListener
48     extends AbstractDwrModule
49     implements ServletContextListener JavaDoc
50 {
51     public void contextInitialized(ServletContextEvent JavaDoc servletContextEvent)
52     {
53         ServletContext JavaDoc servletContext = servletContextEvent.getServletContext();
54         pushServletContext(servletContext);
55         try
56         {
57             Stage stage = getStage();
58             Injector injector = Guice.createInjector(stage, this);
59             publishInjector(servletContext, injector);
60         }
61         finally
62         {
63             popServletContext();
64         }
65     }
66    
67     public void contextDestroyed(ServletContextEvent JavaDoc servletContextEvent)
68     {
69         List JavaDoc<Exception JavaDoc> exceptions = new ArrayList JavaDoc<Exception JavaDoc>();
70         DwrScopes.GLOBAL.closeAll(new ExceptionLoggingCloseableHandler(exceptions));
71         for (Exception JavaDoc e : exceptions)
72         {
73             log.warn("During context destroy, closing globally-scoped Closeables: " + e, e);
74         }
75     }
76     
77     
78     /**
79      * Define this method to configure bindings at servlet context initialization.
80      * Call {@link AbstractModule#install AbstractModule.install(Module)} within
81      * this method to use binding code from other modules.
82      */

83     protected abstract void configure();
84
85     /**
86      * Override this method to specify which stage to run Guice in.
87      * Default behavior is to look first in user preferences and then
88      * in system preferences for node "org/directwebremoting/guice"
89      * with a value for key "stage". If not found, the default is
90      * Stage.PRODUCTION.
91      */

92     protected Stage getStage()
93     {
94         Stage stage = Stage.PRODUCTION;
95
96         try
97         {
98             Preferences JavaDoc userNode = Preferences.userNodeForPackage(PACKAGE);
99             String JavaDoc userStage = userNode.get(STAGE_KEY, null);
100             if (userStage != null)
101             {
102                 stage = Stage.valueOf(userStage);
103             }
104             else
105             {
106                 Preferences JavaDoc systemNode = Preferences.systemNodeForPackage(PACKAGE);
107                 String JavaDoc systemStage = systemNode.get(STAGE_KEY, null);
108                 if (systemStage != null)
109                 {
110                     stage = Stage.valueOf(systemStage);
111                 }
112             }
113         }
114         catch (Exception JavaDoc e)
115         {
116             // ignore errors reading Preferences
117
}
118
119         return stage;
120     }
121     
122     /**
123      * Subclasses can use this during stage determination and binding to
124      * read values from the current servlet context.
125      */

126     protected ServletContext JavaDoc getServletContext()
127     {
128         return DwrGuiceUtil.getServletContext();
129     }
130
131
132     /**
133      * Returns the Injector instance installed in the given ServletContext.
134      */

135     static Injector getPublishedInjector(ServletContext JavaDoc servletContext)
136     {
137         Injector injector = (Injector) servletContext.getAttribute(INJECTOR);
138
139         if (injector == null)
140         {
141             throw new IllegalStateException JavaDoc("Cannot find Injector in servlet context."
142                 + " You need to register a concrete extension of "
143                 + DwrGuiceServletContextListener.class.getName()
144                 + " as a servlet context listener in your web.xml.");
145         }
146
147         return injector;
148     }
149
150     static void publishInjector(ServletContext JavaDoc servletContext, Injector injector)
151     {
152         servletContext.setAttribute(INJECTOR, injector);
153     }
154
155     /**
156      * The key under which a provided Injector is stashed in a ServletContext.
157      * The name is prefixed by the package to avoid conflicting with other
158      * listeners using the same technique.
159      */

160     private static final String JavaDoc INJECTOR =
161         DwrGuiceServletContextListener.class.getPackage().getName() + ".Injector";
162
163     /** The name of the node to examine for a STAGE property. */
164     private static final Class JavaDoc<?> PACKAGE = DwrGuiceServletContextListener.class;
165
166     /** The node property to examine for a value for Stage. */
167     private static final String JavaDoc STAGE_KEY = "stage";
168
169     /**
170      * The log stream
171      */

172     private static final Logger log = Logger.getLogger(DwrGuiceServletContextListener.class);
173 }
174
Popular Tags