1 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 ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.prefs.Preferences ; 31 32 import javax.servlet.ServletContext ; 33 import javax.servlet.ServletContextEvent ; 34 import javax.servlet.ServletContextListener ; 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 47 public abstract class DwrGuiceServletContextListener 48 extends AbstractDwrModule 49 implements ServletContextListener 50 { 51 public void contextInitialized(ServletContextEvent servletContextEvent) 52 { 53 ServletContext 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 servletContextEvent) 68 { 69 List <Exception > exceptions = new ArrayList <Exception >(); 70 DwrScopes.GLOBAL.closeAll(new ExceptionLoggingCloseableHandler(exceptions)); 71 for (Exception e : exceptions) 72 { 73 log.warn("During context destroy, closing globally-scoped Closeables: " + e, e); 74 } 75 } 76 77 78 83 protected abstract void configure(); 84 85 92 protected Stage getStage() 93 { 94 Stage stage = Stage.PRODUCTION; 95 96 try 97 { 98 Preferences userNode = Preferences.userNodeForPackage(PACKAGE); 99 String userStage = userNode.get(STAGE_KEY, null); 100 if (userStage != null) 101 { 102 stage = Stage.valueOf(userStage); 103 } 104 else 105 { 106 Preferences systemNode = Preferences.systemNodeForPackage(PACKAGE); 107 String systemStage = systemNode.get(STAGE_KEY, null); 108 if (systemStage != null) 109 { 110 stage = Stage.valueOf(systemStage); 111 } 112 } 113 } 114 catch (Exception e) 115 { 116 } 118 119 return stage; 120 } 121 122 126 protected ServletContext getServletContext() 127 { 128 return DwrGuiceUtil.getServletContext(); 129 } 130 131 132 135 static Injector getPublishedInjector(ServletContext servletContext) 136 { 137 Injector injector = (Injector) servletContext.getAttribute(INJECTOR); 138 139 if (injector == null) 140 { 141 throw new IllegalStateException ("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 servletContext, Injector injector) 151 { 152 servletContext.setAttribute(INJECTOR, injector); 153 } 154 155 160 private static final String INJECTOR = 161 DwrGuiceServletContextListener.class.getPackage().getName() + ".Injector"; 162 163 164 private static final Class <?> PACKAGE = DwrGuiceServletContextListener.class; 165 166 167 private static final String STAGE_KEY = "stage"; 168 169 172 private static final Logger log = Logger.getLogger(DwrGuiceServletContextListener.class); 173 } 174 | Popular Tags |