1 16 17 package org.springframework.web.context.support; 18 19 import javax.servlet.ServletContext ; 20 21 import org.springframework.util.Assert; 22 import org.springframework.web.context.WebApplicationContext; 23 24 42 public abstract class WebApplicationContextUtils { 43 44 53 public static WebApplicationContext getWebApplicationContext(ServletContext sc) { 54 Assert.notNull(sc, "ServletContext must not be null"); 55 Object attr = sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 56 if (attr == null) { 57 return null; 58 } 59 if (attr instanceof RuntimeException ) { 60 throw (RuntimeException ) attr; 61 } 62 if (attr instanceof Error ) { 63 throw (Error ) attr; 64 } 65 if (!(attr instanceof WebApplicationContext)) { 66 throw new IllegalStateException ("Root context attribute is not of type WebApplicationContext: " + attr); 67 } 68 return (WebApplicationContext) attr; 69 } 70 71 81 public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc) 82 throws IllegalStateException { 83 84 WebApplicationContext wac = getWebApplicationContext(sc); 85 if (wac == null) { 86 throw new IllegalStateException ("No WebApplicationContext found: no ContextLoaderListener registered?"); 87 } 88 return wac; 89 } 90 91 } 92 | Popular Tags |