01: /*
02: * Created on Sep 18, 2005
03: */
04: package uk.org.ponder.rsac;
05:
06: import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
07: import org.springframework.context.ApplicationContext;
08: import org.springframework.context.ConfigurableApplicationContext;
09: import org.springframework.context.support.GenericApplicationContext;
10: import org.springframework.core.io.Resource;
11:
12: import uk.org.ponder.arrayutil.ArrayUtil;
13: import uk.org.ponder.util.UniversalRuntimeException;
14:
15: /**
16: * This code, grievously stolen from Andrew Thornton, hides away all the
17: * unpleasant munging required to obtain a "Generic" Spring application context
18: * using standard Spring resource location semantics.
19: *
20: * @author Antranig Basman (antranig@caret.cam.ac.uk)
21: *
22: */
23:
24: public class RSACBeanLocatorFactory {
25: private RSACBeanLocatorImpl rsacbeanlocator = null;
26: private RSACResourceLocator resourcelocator;
27:
28: /**
29: * Creates a RequestScopeAppContextPool from the given config locations and
30: * parent Application Context
31: *
32: * @param configLocation
33: * @param parent
34: * @return a new pool
35: */
36: public static ConfigurableApplicationContext readContext(
37: String[] configLocations, ApplicationContext parent) {
38: GenericApplicationContext initialContext = new GenericApplicationContext();
39: XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(
40: initialContext);
41:
42: Resource[] resources = new Resource[0];
43:
44: if (configLocations != null) {
45: for (int i = 0; i < configLocations.length; ++i) {
46: String location = configLocations[i];
47: try {
48: Resource[] newresources = parent
49: .getResources(location);
50: resources = (Resource[]) ArrayUtil.concat(
51: resources, newresources);
52: } catch (Exception e) {
53: throw UniversalRuntimeException.accumulate(e,
54: "Bad configuration location " + location
55: + " for RSACBeanLocator");
56: }
57: }
58: }
59: beanDefinitionReader.loadBeanDefinitions(resources);
60: initialContext.setParent(parent);
61: return initialContext;
62: }
63:
64: public void setRSACResourceLocator(
65: RSACResourceLocator resourcelocator) {
66: this .resourcelocator = resourcelocator;
67: ConfigurableApplicationContext cac = readContext(
68: resourcelocator.getConfigLocations(), resourcelocator
69: .getApplicationContext());
70: rsacbeanlocator = new RSACBeanLocatorImpl();
71: rsacbeanlocator.setBlankContext(cac);
72: }
73:
74: public RSACBeanLocator getRSACBeanLocator() {
75: return rsacbeanlocator;
76: }
77:
78: }
|