|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ThreadedServiceModel.java | 90% | 96.9% | 100% | 96.1% |
|
1 |
// Copyright 2004 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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.events.RegistryShutdownListener;
|
|
21 |
import org.apache.hivemind.impl.ConstructableServicePoint;
|
|
22 |
import org.apache.hivemind.impl.ProxyUtils;
|
|
23 |
import org.apache.hivemind.internal.Module;
|
|
24 |
import org.apache.hivemind.service.ThreadCleanupListener;
|
|
25 |
import org.apache.hivemind.service.ThreadEventNotifier;
|
|
26 |
|
|
27 |
/**
|
|
28 |
* Like
|
|
29 |
* {@link org.apache.hivemind.impl.SingletonServiceModel},
|
|
30 |
* this method returns a proxy (implementing the service interface); unlike
|
|
31 |
* SingletonServiceModel, it <em>always</em> returns the proxy.
|
|
32 |
* Invoking a service method on the proxy constructs a service implementation
|
|
33 |
* and binds it to the current thread.
|
|
34 |
*
|
|
35 |
* @author Howard Lewis Ship
|
|
36 |
*/
|
|
37 |
public final class ThreadedServiceModel extends AbstractServiceModelImpl |
|
38 |
{ |
|
39 |
/**
|
|
40 |
* Name of a method in the deferred proxy that is used to obtain
|
|
41 |
* the constructed service.
|
|
42 |
*/
|
|
43 |
protected static final String SERVICE_ACCESSOR_METHOD_NAME = "_service"; |
|
44 |
|
|
45 |
private Object _serviceProxy;
|
|
46 |
private ThreadEventNotifier _notifier;
|
|
47 |
|
|
48 | 6 |
public ThreadedServiceModel(ConstructableServicePoint servicePoint)
|
49 |
{ |
|
50 | 6 |
super(servicePoint);
|
51 |
} |
|
52 |
|
|
53 |
class CleanupListener implements ThreadCleanupListener |
|
54 |
{ |
|
55 |
// The core, wrapped by any interceptors
|
|
56 |
private Object _service;
|
|
57 |
// The core itself
|
|
58 |
private Object _core;
|
|
59 |
|
|
60 | 8 |
CleanupListener(Object service, Object core) |
61 |
{ |
|
62 | 8 |
_service = service; |
63 | 8 |
_core = core; |
64 |
} |
|
65 |
|
|
66 | 2 |
public void threadDidCleanup() |
67 |
{ |
|
68 |
// Orhpan this object
|
|
69 | 2 |
_notifier.removeThreadCleanupListener(this);
|
70 |
|
|
71 | 2 |
unbindServiceFromCurrentThread(); |
72 |
|
|
73 | 2 |
if (_core instanceof Discardable) |
74 |
{ |
|
75 | 1 |
Discardable d = (Discardable) _core; |
76 |
|
|
77 | 1 |
d.threadDidDiscardService(); |
78 |
} |
|
79 |
} |
|
80 |
} |
|
81 |
|
|
82 |
/**
|
|
83 |
* Used to store the active service for the current thread.
|
|
84 |
*/
|
|
85 |
private ThreadLocal _activeService;
|
|
86 |
|
|
87 |
/**
|
|
88 |
* Always returns the service proxy.
|
|
89 |
*/
|
|
90 | 6 |
public synchronized Object getService() |
91 |
{ |
|
92 |
// _activeService will be null on first invocation; a good
|
|
93 |
// time to create it, the proxy, and find the notifier.
|
|
94 |
|
|
95 | 6 |
if (_activeService == null) |
96 |
{ |
|
97 | 6 |
_activeService = new ThreadLocal();
|
98 |
|
|
99 | 6 |
Module module = getServicePoint().getModule(); |
100 |
|
|
101 | 6 |
_notifier = |
102 |
(ThreadEventNotifier) module.getService( |
|
103 |
HiveMind.THREAD_EVENT_NOTIFIER_SERVICE, |
|
104 |
ThreadEventNotifier.class);
|
|
105 |
|
|
106 | 6 |
_serviceProxy = createServiceProxy(); |
107 |
} |
|
108 |
|
|
109 | 6 |
return _serviceProxy;
|
110 |
} |
|
111 |
|
|
112 |
/**
|
|
113 |
* Creates a proxy class for the service and then constructs the class itself.
|
|
114 |
*/
|
|
115 | 6 |
private Object createServiceProxy()
|
116 |
{ |
|
117 | 6 |
if (_log.isDebugEnabled())
|
118 | 3 |
_log.debug( |
119 |
"Creating ThreadedProxy for service " + getServicePoint().getExtensionPointId());
|
|
120 |
|
|
121 | 6 |
return ProxyUtils.createDelegatingProxy(
|
122 |
"ThreadedProxy",
|
|
123 |
this,
|
|
124 |
"getServiceImplementationForCurrentThread",
|
|
125 |
getServicePoint(), |
|
126 |
getServicePoint().getShutdownCoordinator()); |
|
127 |
} |
|
128 |
|
|
129 |
/**
|
|
130 |
* Invoked by the proxy to return
|
|
131 |
* the active service impl for this thread,
|
|
132 |
* constructing it as necessary.
|
|
133 |
*/
|
|
134 | 17 |
public Object getServiceImplementationForCurrentThread()
|
135 |
{ |
|
136 | 17 |
Object result = _activeService.get(); |
137 |
|
|
138 | 17 |
if (result == null) |
139 | 8 |
result = constructServiceForCurrentThread(); |
140 |
|
|
141 | 17 |
return result;
|
142 |
} |
|
143 |
|
|
144 | 8 |
private Object constructServiceForCurrentThread()
|
145 |
{ |
|
146 | 8 |
try
|
147 |
{ |
|
148 | 8 |
Object core = constructCoreServiceImplementation(); |
149 | 8 |
Object result = addInterceptors(core); |
150 |
|
|
151 | 8 |
if (core instanceof RegistryShutdownListener) |
152 | 1 |
_log.error(ServiceModelMessages.registryCleanupIgnored(getServicePoint())); |
153 |
|
|
154 | 8 |
_notifier.addThreadCleanupListener(new CleanupListener(result, core));
|
155 |
|
|
156 | 8 |
_activeService.set(result); |
157 |
|
|
158 | 8 |
return result;
|
159 |
} |
|
160 |
catch (Exception ex)
|
|
161 |
{ |
|
162 | 0 |
throw new ApplicationRuntimeException( |
163 |
ServiceModelMessages.unableToConstructService(getServicePoint(), ex), |
|
164 |
ex); |
|
165 |
} |
|
166 |
} |
|
167 |
|
|
168 | 2 |
private void unbindServiceFromCurrentThread() |
169 |
{ |
|
170 | 2 |
_activeService.set(null);
|
171 |
} |
|
172 |
|
|
173 |
/**
|
|
174 |
* Invokes {@link #getServiceImplementationForCurrentThread()} to force the creation
|
|
175 |
* of the service implementation.
|
|
176 |
*/
|
|
177 |
|
|
178 | 1 |
public void instantiateService() |
179 |
{ |
|
180 | 1 |
getServiceImplementationForCurrentThread(); |
181 |
} |
|
182 |
|
|
183 |
} |
|
184 |
|
|