1 | |
package org.apache.fulcrum.spring; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.avalon.framework.activity.Disposable; |
23 | |
import org.apache.avalon.framework.activity.Initializable; |
24 | |
import org.apache.avalon.framework.logger.AbstractLogEnabled; |
25 | |
import org.apache.avalon.framework.service.ServiceException; |
26 | |
import org.apache.avalon.framework.service.ServiceManager; |
27 | |
import org.apache.avalon.framework.configuration.Configurable; |
28 | |
import org.apache.avalon.framework.configuration.ConfigurationException; |
29 | |
import org.apache.avalon.framework.configuration.Configuration; |
30 | |
import org.springframework.context.support.AbstractApplicationContext; |
31 | |
import org.springframework.context.support.FileSystemXmlApplicationContext; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class SpringFrameworkServiceImpl |
40 | |
extends AbstractLogEnabled |
41 | |
implements SpringFrameworkService, Configurable, Initializable, Disposable, ServiceManager |
42 | |
{ |
43 | |
|
44 | |
private String[] configLocations; |
45 | |
|
46 | |
|
47 | |
private AbstractApplicationContext ctx; |
48 | |
|
49 | |
|
50 | |
private BeanFactoryServiceManager beanFactoryServiceManager; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public SpringFrameworkServiceImpl() |
60 | 0 | { |
61 | 0 | this.configLocations = new String[0]; |
62 | 0 | } |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public void configure(Configuration configuration) throws ConfigurationException |
68 | |
{ |
69 | |
|
70 | |
|
71 | |
|
72 | 0 | Configuration[] configLocationConfigurationList = configuration.getChild("configurations").getChildren("configuration"); |
73 | 0 | this.configLocations = new String[configLocationConfigurationList.length]; |
74 | |
|
75 | 0 | for(int i=0; i<configLocations.length; i++) |
76 | |
{ |
77 | 0 | this.configLocations[i] = configLocationConfigurationList[i].getValue(); |
78 | |
} |
79 | |
|
80 | 0 | if(this.configLocations.length == 0) |
81 | |
{ |
82 | 0 | String msg = "No configuration files for the Spring container are defined"; |
83 | 0 | throw new ConfigurationException(msg); |
84 | |
} |
85 | 0 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public void initialize() throws Exception |
91 | |
{ |
92 | 0 | this.ctx = new FileSystemXmlApplicationContext(this.configLocations); |
93 | 0 | this.beanFactoryServiceManager = new BeanFactoryServiceManager(ctx); |
94 | 0 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public void dispose() |
100 | |
{ |
101 | 0 | if(this.ctx != null) |
102 | |
{ |
103 | |
try |
104 | |
{ |
105 | 0 | this.ctx.close(); |
106 | |
} |
107 | 0 | catch(Exception e) |
108 | |
{ |
109 | 0 | String msg = "Failed to dispose the Spring service"; |
110 | 0 | this.getLogger().error(msg, e); |
111 | |
} |
112 | |
finally |
113 | |
{ |
114 | 0 | this.ctx = null; |
115 | 0 | } |
116 | |
} |
117 | |
|
118 | 0 | this.beanFactoryServiceManager = null; |
119 | 0 | } |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public AbstractApplicationContext getAbstractApplicationContext() |
127 | |
{ |
128 | 0 | return this.ctx; |
129 | |
} |
130 | |
|
131 | |
|
132 | |
public Object lookup(String key) throws ServiceException |
133 | |
{ |
134 | 0 | if(this.beanFactoryServiceManager == null) |
135 | |
{ |
136 | 0 | throw new RuntimeException("The SpringFrameworkService is not yet initialized"); |
137 | |
} |
138 | |
|
139 | 0 | return this.beanFactoryServiceManager.lookup(key); |
140 | |
} |
141 | |
|
142 | |
|
143 | |
public boolean hasService(String key) |
144 | |
{ |
145 | 0 | if(this.beanFactoryServiceManager == null) |
146 | |
{ |
147 | 0 | throw new RuntimeException("The SpringFrameworkService is not yet initialized"); |
148 | |
} |
149 | |
|
150 | 0 | return this.beanFactoryServiceManager.hasService(key); |
151 | |
} |
152 | |
|
153 | |
|
154 | |
public void release(Object o) |
155 | |
{ |
156 | 0 | if(this.beanFactoryServiceManager == null) |
157 | |
{ |
158 | 0 | throw new RuntimeException("The SpringFrameworkService is not yet initialized"); |
159 | |
} |
160 | |
|
161 | 0 | this.beanFactoryServiceManager.release(o); |
162 | 0 | } |
163 | |
} |