1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.impl.servicemodel; |
16 |
| |
17 |
| import org.apache.hivemind.ApplicationRuntimeException; |
18 |
| import org.apache.hivemind.Discardable; |
19 |
| import org.apache.hivemind.HiveMind; |
20 |
| import org.apache.hivemind.ShutdownCoordinator; |
21 |
| import org.apache.hivemind.events.RegistryShutdownListener; |
22 |
| import org.apache.hivemind.impl.ConstructableServicePoint; |
23 |
| import org.apache.hivemind.impl.ProxyUtils; |
24 |
| import org.apache.hivemind.internal.Module; |
25 |
| import org.apache.hivemind.service.ThreadCleanupListener; |
26 |
| import org.apache.hivemind.service.ThreadEventNotifier; |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| public final class ThreadedServiceModel extends AbstractServiceModelImpl |
37 |
| { |
38 |
| |
39 |
| |
40 |
| |
41 |
| protected static final String SERVICE_ACCESSOR_METHOD_NAME = "_service"; |
42 |
| |
43 |
| private Object _serviceProxy; |
44 |
| |
45 |
| private ThreadEventNotifier _notifier; |
46 |
| |
47 |
| |
48 |
| |
49 |
| private Class _serviceInterface; |
50 |
| |
51 |
19
| public ThreadedServiceModel(ConstructableServicePoint servicePoint)
|
52 |
| { |
53 |
19
| super(servicePoint);
|
54 |
| |
55 |
19
| _serviceInterface = servicePoint.getServiceInterface();
|
56 |
| } |
57 |
| |
58 |
| class CleanupListener implements ThreadCleanupListener |
59 |
| { |
60 |
| |
61 |
| private Object _core; |
62 |
| |
63 |
18
| CleanupListener(Object core)
|
64 |
| { |
65 |
18
| _core = core;
|
66 |
| } |
67 |
| |
68 |
3
| public void threadDidCleanup()
|
69 |
| { |
70 |
| |
71 |
3
| _notifier.removeThreadCleanupListener(this);
|
72 |
| |
73 |
3
| unbindServiceFromCurrentThread();
|
74 |
| |
75 |
3
| if (_core instanceof Discardable)
|
76 |
| { |
77 |
1
| Discardable d = (Discardable) _core;
|
78 |
| |
79 |
1
| d.threadDidDiscardService();
|
80 |
| } |
81 |
| } |
82 |
| } |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| private ThreadLocal _activeService; |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
19
| public synchronized Object getService()
|
93 |
| { |
94 |
| |
95 |
| |
96 |
| |
97 |
19
| if (_activeService == null)
|
98 |
| { |
99 |
19
| _activeService = new ThreadLocal();
|
100 |
| |
101 |
19
| Module module = getServicePoint().getModule();
|
102 |
| |
103 |
19
| _notifier = (ThreadEventNotifier) module.getService(
|
104 |
| HiveMind.THREAD_EVENT_NOTIFIER_SERVICE, |
105 |
| ThreadEventNotifier.class); |
106 |
| |
107 |
19
| _serviceProxy = createServiceProxy();
|
108 |
| } |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
19
| return _serviceProxy;
|
117 |
| } |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
19
| private Object createServiceProxy()
|
124 |
| { |
125 |
19
| ConstructableServicePoint servicePoint = getServicePoint();
|
126 |
| |
127 |
19
| if (_log.isDebugEnabled())
|
128 |
1
| _log.debug("Creating ThreadedProxy for service " + servicePoint.getExtensionPointId());
|
129 |
| |
130 |
19
| Object proxy = ProxyUtils.createDelegatingProxy(
|
131 |
| "ThreadedProxy", |
132 |
| this, |
133 |
| "getServiceImplementationForCurrentThread", |
134 |
| servicePoint); |
135 |
| |
136 |
19
| Object intercepted = addInterceptors(proxy);
|
137 |
| |
138 |
19
| RegistryShutdownListener outerProxy = ProxyUtils
|
139 |
| .createOuterProxy(intercepted, servicePoint); |
140 |
| |
141 |
19
| ShutdownCoordinator coordinator = servicePoint.getShutdownCoordinator();
|
142 |
| |
143 |
19
| coordinator.addRegistryShutdownListener(outerProxy);
|
144 |
| |
145 |
19
| return outerProxy;
|
146 |
| } |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
31
| public Object getServiceImplementationForCurrentThread()
|
153 |
| { |
154 |
31
| Object result = _activeService.get();
|
155 |
| |
156 |
31
| if (result == null)
|
157 |
18
| result = constructServiceForCurrentThread();
|
158 |
| |
159 |
31
| return result;
|
160 |
| } |
161 |
| |
162 |
18
| private synchronized Object constructServiceForCurrentThread()
|
163 |
| { |
164 |
18
| try
|
165 |
| { |
166 |
18
| Object core = constructCoreServiceImplementation();
|
167 |
| |
168 |
18
| if (core instanceof RegistryShutdownListener)
|
169 |
2
| _log.error(ServiceModelMessages.registryCleanupIgnored(getServicePoint()));
|
170 |
| |
171 |
18
| _notifier.addThreadCleanupListener(new CleanupListener(core));
|
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
18
| if (!_serviceInterface.isInstance(core))
|
178 |
2
| core = constructBridgeProxy(core);
|
179 |
| |
180 |
18
| _activeService.set(core);
|
181 |
| |
182 |
18
| return core;
|
183 |
| } |
184 |
| catch (Exception ex) |
185 |
| { |
186 |
0
| throw new ApplicationRuntimeException(ServiceModelMessages.unableToConstructService(
|
187 |
| getServicePoint(), |
188 |
| ex), ex); |
189 |
| } |
190 |
| } |
191 |
| |
192 |
3
| private void unbindServiceFromCurrentThread()
|
193 |
| { |
194 |
3
| _activeService.set(null);
|
195 |
| } |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
1
| public void instantiateService()
|
203 |
| { |
204 |
1
| getServiceImplementationForCurrentThread();
|
205 |
| } |
206 |
| |
207 |
| } |