01: /*
02: * Created on 24 Nov 2006
03: */
04: package uk.org.ponder.springutil;
05:
06: import javax.servlet.ServletContext;
07:
08: import org.springframework.beans.BeansException;
09: import org.springframework.context.ApplicationContext;
10: import org.springframework.context.ApplicationContextAware;
11: import org.springframework.core.io.DefaultResourceLoader;
12: import org.springframework.core.io.Resource;
13: import org.springframework.web.context.WebApplicationContext;
14: import org.springframework.web.context.support.ServletContextResource;
15:
16: import uk.org.ponder.servletutil.ServletContextLocator;
17:
18: public class ExtraContextResourceLoader extends DefaultResourceLoader
19: implements ApplicationContextAware {
20:
21: private ApplicationContext applicationContext;
22: private WebApplicationContext wac;
23:
24: private ServletContextLocator servletContextLocator;
25:
26: public void setServletContextLocator(
27: ServletContextLocator servletContextLocator) {
28: this .servletContextLocator = servletContextLocator;
29: }
30:
31: public Resource getResource(String location) {
32: if (wac != null && location.startsWith("/..")) {
33: int slashpos = location.indexOf('/', 4);
34: String uripath = location.substring(3, slashpos); // include leading /
35: String relpath = location.substring(slashpos + 1);
36: ServletContext extracontext = null;
37: if (servletContextLocator != null) {
38: extracontext = servletContextLocator
39: .locateContext(uripath);
40: }
41: if (extracontext == null) {
42: extracontext = wac.getServletContext().getContext(
43: uripath);
44: }
45: if (extracontext == null) {
46: return null;
47: } else {
48: return new ServletContextResource(extracontext, relpath);
49: }
50: } else
51: return applicationContext.getResource(location);
52: }
53:
54: public void setApplicationContext(
55: ApplicationContext applicationContext)
56: throws BeansException {
57: this .applicationContext = applicationContext;
58: if (applicationContext instanceof WebApplicationContext) {
59: this .wac = (WebApplicationContext) applicationContext;
60: }
61: }
62:
63: }
|