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 {
61 this.configLocations = new String[0];
62 }
63
64
65
66
67 public void configure(Configuration configuration) throws ConfigurationException
68 {
69
70
71
72 Configuration[] configLocationConfigurationList = configuration.getChild("configurations").getChildren("configuration");
73 this.configLocations = new String[configLocationConfigurationList.length];
74
75 for(int i=0; i<configLocations.length; i++)
76 {
77 this.configLocations[i] = configLocationConfigurationList[i].getValue();
78 }
79
80 if(this.configLocations.length == 0)
81 {
82 String msg = "No configuration files for the Spring container are defined";
83 throw new ConfigurationException(msg);
84 }
85 }
86
87
88
89
90 public void initialize() throws Exception
91 {
92 this.ctx = new FileSystemXmlApplicationContext(this.configLocations);
93 this.beanFactoryServiceManager = new BeanFactoryServiceManager(ctx);
94 }
95
96
97
98
99 public void dispose()
100 {
101 if(this.ctx != null)
102 {
103 try
104 {
105 this.ctx.close();
106 }
107 catch(Exception e)
108 {
109 String msg = "Failed to dispose the Spring service";
110 this.getLogger().error(msg, e);
111 }
112 finally
113 {
114 this.ctx = null;
115 }
116 }
117
118 this.beanFactoryServiceManager = null;
119 }
120
121
122
123
124
125
126 public AbstractApplicationContext getAbstractApplicationContext()
127 {
128 return this.ctx;
129 }
130
131
132 public Object lookup(String key) throws ServiceException
133 {
134 if(this.beanFactoryServiceManager == null)
135 {
136 throw new RuntimeException("The SpringFrameworkService is not yet initialized");
137 }
138
139 return this.beanFactoryServiceManager.lookup(key);
140 }
141
142
143 public boolean hasService(String key)
144 {
145 if(this.beanFactoryServiceManager == null)
146 {
147 throw new RuntimeException("The SpringFrameworkService is not yet initialized");
148 }
149
150 return this.beanFactoryServiceManager.hasService(key);
151 }
152
153
154 public void release(Object o)
155 {
156 if(this.beanFactoryServiceManager == null)
157 {
158 throw new RuntimeException("The SpringFrameworkService is not yet initialized");
159 }
160
161 this.beanFactoryServiceManager.release(o);
162 }
163 }