/** * Return the temporary directory for the current web application, * as provided by the servlet container. * @return the File representing the temporary directory * @throws IllegalStateException if not running within a ServletContext * @see org.springframework.web.util.WebUtils#getTempDir(javax.servlet.ServletContext) */ protected final File getTempDir() throws IllegalStateException { ServletContext servletContext = getServletContext(); Assert.state(servletContext != null, "ServletContext is required"); return WebUtils.getTempDir(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; }
/** * Return the current application context as WebApplicationContext. * <p><b>NOTE:</b> Only use this if you actually need to access * WebApplicationContext-specific functionality. Preferably use * {@code getApplicationContext()} or {@code getServletContext()} * else, to be able to run in non-WebApplicationContext environments as well. * @throws IllegalStateException if not running in a WebApplicationContext * @see #getApplicationContext() */ @Nullable protected final WebApplicationContext getWebApplicationContext() throws IllegalStateException { ApplicationContext ctx = getApplicationContext(); if (ctx instanceof WebApplicationContext) { return (WebApplicationContext) getApplicationContext(); } else if (isContextRequired()) { throw new IllegalStateException("WebApplicationObjectSupport instance [" + this + "] does not run in a WebApplicationContext but in: " + ctx); } else { return null; } }
@Override public final void setServletContext(ServletContext servletContext) { if (servletContext != this.servletContext) { this.servletContext = servletContext; initServletContext(servletContext); } }
@Test @SuppressWarnings("resource") public void testWebApplicationObjectSupport() { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); File tempDir = new File(""); wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, tempDir); wac.registerBeanDefinition("test", new RootBeanDefinition(TestWebApplicationObject.class)); wac.refresh(); WebApplicationObjectSupport wao = (WebApplicationObjectSupport) wac.getBean("test"); assertEquals(wao.getServletContext(), wac.getServletContext()); assertEquals(wao.getTempDir(), tempDir); }
@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 } }
private List<ViewResolver> initViewResolvers(WebApplicationContext wac) { this.viewResolvers = (this.viewResolvers != null ? this.viewResolvers : Collections.singletonList(new InternalResourceViewResolver())); for (Object viewResolver : this.viewResolvers) { if (viewResolver instanceof WebApplicationObjectSupport) { ((WebApplicationObjectSupport) viewResolver).setApplicationContext(wac); } } return this.viewResolvers; }
/** * Return the current application context as WebApplicationContext. * @throws IllegalStateException if not running in a WebApplicationContext */ protected final WebApplicationContext getWebApplicationContext() throws IllegalStateException { ApplicationContext ctx = getApplicationContext(); if (!(ctx instanceof WebApplicationContext)) { throw new IllegalStateException( "WebApplicationObjectSupport instance [" + this + "] does not run in a WebApplicationContext but in: " + ctx); } return (WebApplicationContext) getApplicationContext(); }
@Override public final void setServletContext(ServletContext servletContext) { if (servletContext != this.servletContext) { this.servletContext = servletContext; initServletContext(servletContext); } }
/** * Return the current ServletContext. * @throws IllegalStateException if not running in a WebApplicationContext */ protected final ServletContext getServletContext() { return getWebApplicationContext().getServletContext(); }
private List<ViewResolver> initViewResolvers(WebApplicationContext wac) { this.viewResolvers = (this.viewResolvers != null ? this.viewResolvers : Collections.singletonList(new InternalResourceViewResolver())); for (Object viewResolver : this.viewResolvers) { if (viewResolver instanceof WebApplicationObjectSupport) { ((WebApplicationObjectSupport) viewResolver).setApplicationContext(wac); } } return this.viewResolvers; }
/** * 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 application context as WebApplicationContext. * <p><b>NOTE:</b> Only use this if you actually need to access * WebApplicationContext-specific functionality. Preferably use * {@code getApplicationContext()} or {@code getServletContext()} * else, to be able to run in non-WebApplicationContext environments as well. * @throws IllegalStateException if not running in a WebApplicationContext * @see #getApplicationContext() */ @Nullable protected final WebApplicationContext getWebApplicationContext() throws IllegalStateException { ApplicationContext ctx = getApplicationContext(); if (ctx instanceof WebApplicationContext) { return (WebApplicationContext) getApplicationContext(); } else if (isContextRequired()) { throw new IllegalStateException("WebApplicationObjectSupport instance [" + this + "] does not run in a WebApplicationContext but in: " + ctx); } else { return null; } }
/** * Return the temporary directory for the current web application, * as provided by the servlet container. * @return the File representing the temporary directory * @throws IllegalStateException if not running within a ServletContext * @see org.springframework.web.util.WebUtils#getTempDir(javax.servlet.ServletContext) */ protected final File getTempDir() throws IllegalStateException { ServletContext servletContext = getServletContext(); Assert.state(servletContext != null, "ServletContext is required"); return WebUtils.getTempDir(servletContext); }
/** * Calls {@link #initServletContext(javax.servlet.ServletContext)} if the * given ApplicationContext is a {@link WebApplicationContext}. */ @Override protected void initApplicationContext(ApplicationContext context) { super.initApplicationContext(context); if (this.servletContext == null && context instanceof WebApplicationContext) { this.servletContext = ((WebApplicationContext) context).getServletContext(); if (this.servletContext != null) { initServletContext(this.servletContext); } } }
private List<ViewResolver> initViewResolvers(WebApplicationContext wac) { this.viewResolvers = (this.viewResolvers == null) ? Arrays.<ViewResolver>asList(new InternalResourceViewResolver()) : viewResolvers; for (Object viewResolver : this.viewResolvers) { if (viewResolver instanceof WebApplicationObjectSupport) { ((WebApplicationObjectSupport) viewResolver).setApplicationContext(wac); } } return this.viewResolvers; }
/** * 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 application context as WebApplicationContext. * <p><b>NOTE:</b> Only use this if you actually need to access * WebApplicationContext-specific functionality. Preferably use * {@code getApplicationContext()} or {@code getServletContext()} * else, to be able to run in non-WebApplicationContext environments as well. * @throws IllegalStateException if not running in a WebApplicationContext * @see #getApplicationContext() */ @Nullable protected final WebApplicationContext getWebApplicationContext() throws IllegalStateException { ApplicationContext ctx = getApplicationContext(); if (ctx instanceof WebApplicationContext) { return (WebApplicationContext) getApplicationContext(); } else if (isContextRequired()) { throw new IllegalStateException("WebApplicationObjectSupport instance [" + this + "] does not run in a WebApplicationContext but in: " + ctx); } else { return null; } }
/** * Return the temporary directory for the current web application, * as provided by the servlet container. * @return the File representing the temporary directory * @throws IllegalStateException if not running within a ServletContext * @see org.springframework.web.util.WebUtils#getTempDir(javax.servlet.ServletContext) */ protected final File getTempDir() throws IllegalStateException { ServletContext servletContext = getServletContext(); Assert.state(servletContext != null, "ServletContext is required"); return WebUtils.getTempDir(servletContext); }
/** * Calls {@link #initServletContext(javax.servlet.ServletContext)} if the * given ApplicationContext is a {@link WebApplicationContext}. */ @Override protected void initApplicationContext(ApplicationContext context) { super.initApplicationContext(context); if (this.servletContext == null && context instanceof WebApplicationContext) { this.servletContext = ((WebApplicationContext) context).getServletContext(); if (this.servletContext != null) { initServletContext(this.servletContext); } } }