1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.core.resourceloader;
18
19 import javax.xml.namespace.QName;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.log4j.Logger;
23 import org.kuali.rice.core.config.ConfigurationException;
24 import org.springframework.context.ApplicationContext;
25 import org.springframework.context.ConfigurableApplicationContext;
26 import org.springframework.context.support.ClassPathXmlApplicationContext;
27
28
29
30
31
32
33
34
35 public class SpringResourceLoader extends BaseResourceLoader {
36
37 private static final Logger LOG = Logger.getLogger(SpringResourceLoader.class);
38
39 private SpringResourceLoader parentSpringResourceLoader;
40
41 private ApplicationContext parentContext;
42 private ConfigurableApplicationContext context;
43
44 private final String[] fileLocs;
45
46 public SpringResourceLoader(QName name, String fileLoc) {
47 this(name, new String[] { fileLoc });
48 }
49
50 public SpringResourceLoader(QName name, String[] fileLocs) {
51 super(name);
52 this.fileLocs = fileLocs;
53 }
54
55 public Object getService(QName serviceName) {
56 if (!isStarted()) {
57 return null;
58 }
59 if (this.getContext().containsBean(serviceName.toString())) {
60 Object service = this.getContext().getBean(serviceName.toString());
61 return postProcessService(serviceName, service);
62 }
63 return super.getService(serviceName);
64 }
65
66 @Override
67 public void start() throws Exception {
68 if(!isStarted()){
69 LOG.info("Creating Spring context " + StringUtils.join(this.fileLocs, ","));
70 if (parentSpringResourceLoader != null && parentContext != null) {
71 throw new ConfigurationException("Both a parentSpringResourceLoader and parentContext were defined. Only one can be defined!");
72 }
73 if (parentSpringResourceLoader != null) {
74 parentContext = parentSpringResourceLoader.getContext();
75 }
76 this.context = new ClassPathXmlApplicationContext(this.fileLocs, parentContext);
77 super.start();
78 }
79 }
80
81 @Override
82 public void stop() throws Exception {
83 LOG.info("Stopping Spring context " + StringUtils.join(this.fileLocs, ","));
84 this.context.close();
85 super.stop();
86 }
87
88 public ConfigurableApplicationContext getContext() {
89 return this.context;
90 }
91
92 public void setParentContext(ApplicationContext parentContext) {
93 this.parentContext = parentContext;
94 }
95
96 public void setParentSpringResourceLoader(
97 SpringResourceLoader parentSpringResourceLoader) {
98 this.parentSpringResourceLoader = parentSpringResourceLoader;
99 }
100
101
102
103 }