01: /*
02: * Created on 31-Dec-2005
03: */
04: package uk.org.ponder.springutil;
05:
06: import java.io.InputStream;
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.Resource;
12: import org.springframework.core.io.ResourceLoader;
13:
14: import uk.org.ponder.streamutil.StreamResolver;
15: import uk.org.ponder.util.UniversalRuntimeException;
16:
17: /**
18: * @author Antranig Basman (amb26@ponder.org.uk)
19: *
20: */
21: public class SpringStreamResolver implements ApplicationContextAware,
22: StreamResolver {
23: private ResourceLoader resourceLoader;
24:
25: public SpringStreamResolver() {
26: }
27:
28: public SpringStreamResolver(ResourceLoader resourceLoader) {
29: this .resourceLoader = resourceLoader;
30: }
31:
32: public void setApplicationContext(
33: ApplicationContext applicationContext)
34: throws BeansException {
35: this .resourceLoader = applicationContext;
36: }
37:
38: public InputStream openStream(String path) {
39: try {
40: Resource res = resourceLoader.getResource(path);
41: return res.getInputStream();
42: } catch (Exception e) {
43: throw UniversalRuntimeException.accumulate(e,
44: "Unable to open resource with path " + path
45: + " from application context"
46: + resourceLoader);
47: }
48: }
49:
50: }
|