/** * Return the current ServletContext. * @throws IllegalStateException if not running within a required ServletContext * @see #isContextRequired() */ @Nullable protected final ServletContext getServletContext() throws IllegalStateException { if (this.servletContext != null) { return this.servletContext; } ServletContext servletContext = null; WebApplicationContext wac = getWebApplicationContext(); if (wac != null) { servletContext = wac.getServletContext(); } if (servletContext == null && isContextRequired()) { throw new IllegalStateException("WebApplicationObjectSupport instance [" + this + "] does not run within a ServletContext. Make sure the object is fully configured!"); } return servletContext; }
/** * Return the current ServletContext. * @throws IllegalStateException if not running within a required ServletContext * @see #isContextRequired() */ @Nullable protected final ServletContext getServletContext() throws IllegalStateException { if (this.servletContext != null) { return this.servletContext; } ServletContext servletContext = null; WebApplicationContext wac = getWebApplicationContext(); if (wac != null) { servletContext = wac.getServletContext(); } if (servletContext == null && isContextRequired()) { throw new IllegalStateException("WebApplicationObjectSupport instance [" + this + "] does not run within a ServletContext. Make sure the object is fully configured!"); } return servletContext; }
@Test @SuppressWarnings("resource") public void testWebApplicationObjectSupportWithWrongContext() { StaticApplicationContext ac = new StaticApplicationContext(); ac.registerBeanDefinition("test", new RootBeanDefinition(TestWebApplicationObject.class)); WebApplicationObjectSupport wao = (WebApplicationObjectSupport) ac.getBean("test"); try { wao.getWebApplicationContext(); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // expected } }
/** * Return the current ServletContext. * @throws IllegalStateException if not running in a WebApplicationContext */ protected final ServletContext getServletContext() { return getWebApplicationContext().getServletContext(); }
/** * Return the current ServletContext. * @throws IllegalStateException if not running within a required ServletContext * @see #isContextRequired() */ @Nullable protected final ServletContext getServletContext() throws IllegalStateException { if (this.servletContext != null) { return this.servletContext; } ServletContext servletContext = null; WebApplicationContext wac = getWebApplicationContext(); if (wac != null) { servletContext = wac.getServletContext(); } if (servletContext == null && isContextRequired()) { throw new IllegalStateException("WebApplicationObjectSupport instance [" + this + "] does not run within a ServletContext. Make sure the object is fully configured!"); } return servletContext; }
/** * Return the current ServletContext. * @throws IllegalStateException if not running within a required ServletContext * @see #isContextRequired() */ @Nullable protected final ServletContext getServletContext() throws IllegalStateException { if (this.servletContext != null) { return this.servletContext; } ServletContext servletContext = null; WebApplicationContext wac = getWebApplicationContext(); if (wac != null) { servletContext = wac.getServletContext(); } if (servletContext == null && isContextRequired()) { throw new IllegalStateException("WebApplicationObjectSupport instance [" + this + "] does not run within a ServletContext. Make sure the object is fully configured!"); } return servletContext; }