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 | 0 | 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 | 0 | this(name, new String[] { fileLoc }); |
48 | 0 | } |
49 | |
|
50 | |
public SpringResourceLoader(QName name, String[] fileLocs) { |
51 | 0 | super(name); |
52 | 0 | this.fileLocs = fileLocs; |
53 | 0 | } |
54 | |
|
55 | |
public Object getService(QName serviceName) { |
56 | 0 | if (!isStarted()) { |
57 | 0 | return null; |
58 | |
} |
59 | 0 | if (this.getContext().containsBean(serviceName.toString())) { |
60 | 0 | Object service = this.getContext().getBean(serviceName.toString()); |
61 | 0 | return postProcessService(serviceName, service); |
62 | |
} |
63 | 0 | return super.getService(serviceName); |
64 | |
} |
65 | |
|
66 | |
@Override |
67 | |
public void start() throws Exception { |
68 | 0 | if(!isStarted()){ |
69 | 0 | LOG.info("Creating Spring context " + StringUtils.join(this.fileLocs, ",")); |
70 | 0 | if (parentSpringResourceLoader != null && parentContext != null) { |
71 | 0 | throw new ConfigurationException("Both a parentSpringResourceLoader and parentContext were defined. Only one can be defined!"); |
72 | |
} |
73 | 0 | if (parentSpringResourceLoader != null) { |
74 | 0 | parentContext = parentSpringResourceLoader.getContext(); |
75 | |
} |
76 | 0 | this.context = new ClassPathXmlApplicationContext(this.fileLocs, parentContext); |
77 | 0 | super.start(); |
78 | |
} |
79 | 0 | } |
80 | |
|
81 | |
@Override |
82 | |
public void stop() throws Exception { |
83 | 0 | LOG.info("Stopping Spring context " + StringUtils.join(this.fileLocs, ",")); |
84 | 0 | this.context.close(); |
85 | 0 | super.stop(); |
86 | 0 | } |
87 | |
|
88 | |
public ConfigurableApplicationContext getContext() { |
89 | 0 | return this.context; |
90 | |
} |
91 | |
|
92 | |
public void setParentContext(ApplicationContext parentContext) { |
93 | 0 | this.parentContext = parentContext; |
94 | 0 | } |
95 | |
|
96 | |
public void setParentSpringResourceLoader( |
97 | |
SpringResourceLoader parentSpringResourceLoader) { |
98 | 0 | this.parentSpringResourceLoader = parentSpringResourceLoader; |
99 | 0 | } |
100 | |
|
101 | |
|
102 | |
|
103 | |
} |