|
|||||||||||||||||||
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 | |||||||||||||||
SingletonServiceModel.java | 87.5% | 98.7% | 100% | 97.9% |
|
1 |
// Copyright 2004, 2005 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 java.lang.reflect.Constructor;
|
|
18 |
import java.lang.reflect.Modifier;
|
|
19 |
|
|
20 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
21 |
import org.apache.hivemind.events.RegistryShutdownListener;
|
|
22 |
import org.apache.hivemind.impl.ConstructableServicePoint;
|
|
23 |
import org.apache.hivemind.impl.ProxyBuilder;
|
|
24 |
import org.apache.hivemind.internal.ServicePoint;
|
|
25 |
import org.apache.hivemind.service.BodyBuilder;
|
|
26 |
import org.apache.hivemind.service.ClassFab;
|
|
27 |
import org.apache.hivemind.service.MethodSignature;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* Subclass of {@link org.apache.hivemind.impl.servicemodel.AbstractServiceModelImpl} which
|
|
31 |
* supports creation of a singleton service proxy (deferring the actual construction of the service
|
|
32 |
* until absolutely necessary). This is used with the singleton service type, which is the default.
|
|
33 |
*
|
|
34 |
* @author Howard Lewis Ship
|
|
35 |
*/
|
|
36 |
public final class SingletonServiceModel extends AbstractServiceModelImpl |
|
37 |
{ |
|
38 |
/**
|
|
39 |
* Name of a method in the deferred proxy that is used to obtain the constructed service.
|
|
40 |
*/
|
|
41 |
protected static final String SERVICE_ACCESSOR_METHOD_NAME = "_service"; |
|
42 |
|
|
43 |
private Object _serviceProxy;
|
|
44 |
|
|
45 |
private SingletonInnerProxy _innerProxy;
|
|
46 |
|
|
47 |
private Object _constructedService;
|
|
48 |
|
|
49 | 874 |
public SingletonServiceModel(ConstructableServicePoint servicePoint)
|
50 |
{ |
|
51 | 874 |
super(servicePoint);
|
52 |
} |
|
53 |
|
|
54 | 875 |
public synchronized Object getService() |
55 |
{ |
|
56 | 875 |
if (_serviceProxy == null) |
57 | 874 |
_serviceProxy = createSingletonProxy(); |
58 |
|
|
59 | 875 |
return _serviceProxy;
|
60 |
} |
|
61 |
|
|
62 |
/**
|
|
63 |
* This is invoked by the proxy to create the actual implementation.
|
|
64 |
*/
|
|
65 | 430 |
public synchronized Object getActualServiceImplementation() |
66 |
{ |
|
67 | 430 |
if (_constructedService == null) |
68 | 430 |
_constructedService = constructServiceImplementation(); |
69 |
|
|
70 |
// The inner proxy needs the service to implement the service interface.
|
|
71 |
// For bean services (not interface services) with no interceptors,
|
|
72 |
// the implementation may be the bean provided by the factory ... which
|
|
73 |
// does not implement the service interface (which was created at runtime).
|
|
74 |
// So we introduce a "bridge" between the two.
|
|
75 |
|
|
76 | 430 |
Class serviceInterface = getServicePoint().getServiceInterface(); |
77 |
|
|
78 | 430 |
if (!serviceInterface.isInstance(_constructedService))
|
79 | 2 |
_constructedService = constructBridgeProxy(_constructedService); |
80 |
|
|
81 | 430 |
return _constructedService;
|
82 |
} |
|
83 |
|
|
84 |
/**
|
|
85 |
* Creates a proxy class for the service and then construct the class itself.
|
|
86 |
*/
|
|
87 | 874 |
private Object createSingletonProxy()
|
88 |
{ |
|
89 | 874 |
if (_log.isDebugEnabled())
|
90 | 4 |
_log.debug("Creating SingletonProxy for service "
|
91 |
+ getServicePoint().getExtensionPointId()); |
|
92 |
|
|
93 | 874 |
try
|
94 |
{ |
|
95 |
|
|
96 |
// Create the outer proxy, the one visible to client code (including
|
|
97 |
// other services). It is dependent on an inner proxy.
|
|
98 |
|
|
99 | 874 |
Class proxyClass = createSingletonProxyClass(); |
100 |
|
|
101 |
// Create the inner proxy, whose job is to replace itself
|
|
102 |
// when the first service method is invoked.
|
|
103 |
|
|
104 | 874 |
Class innerProxyClass = createInnerProxyClass(proxyClass); |
105 |
|
|
106 |
// Create the outer proxy.
|
|
107 |
|
|
108 | 874 |
Object result = proxyClass.newInstance(); |
109 |
|
|
110 |
// The inner proxy's construct invokes a method on the
|
|
111 |
// outer proxy to connect the two.
|
|
112 |
|
|
113 | 874 |
Constructor c = innerProxyClass.getConstructor(new Class[]
|
114 |
{ proxyClass, getClass() }); |
|
115 |
|
|
116 | 874 |
_innerProxy = (SingletonInnerProxy) c.newInstance(new Object[]
|
117 |
{ result, this });
|
|
118 |
|
|
119 | 874 |
getServicePoint().getShutdownCoordinator().addRegistryShutdownListener( |
120 |
(RegistryShutdownListener) result); |
|
121 |
|
|
122 | 874 |
return result;
|
123 |
} |
|
124 |
catch (Exception ex)
|
|
125 |
{ |
|
126 | 0 |
throw new ApplicationRuntimeException(ex); |
127 |
} |
|
128 |
|
|
129 |
} |
|
130 |
|
|
131 |
/**
|
|
132 |
* Creates a class that implements the service interface. Implements a private synchronized
|
|
133 |
* method, _service(), that constructs the service as needed, and has each service interface
|
|
134 |
* method re-invoke on _service(). Adds a toString() method if the service interface does not
|
|
135 |
* define toString().
|
|
136 |
*/
|
|
137 | 874 |
private Class createSingletonProxyClass()
|
138 |
{ |
|
139 | 874 |
ConstructableServicePoint servicePoint = getServicePoint(); |
140 |
|
|
141 | 874 |
ProxyBuilder proxyBuilder = new ProxyBuilder("SingletonProxy", servicePoint, true); |
142 |
|
|
143 | 874 |
ClassFab classFab = proxyBuilder.getClassFab(); |
144 |
|
|
145 | 874 |
Class serviceInterface = servicePoint.getServiceInterface(); |
146 |
|
|
147 |
// This will initally be the inner proxy, then switch over to the
|
|
148 |
// service implementation.
|
|
149 |
|
|
150 | 874 |
classFab.addField("_inner", serviceInterface);
|
151 | 874 |
classFab.addField("_shutdown", boolean.class); |
152 |
|
|
153 | 874 |
classFab.addInterface(RegistryShutdownListener.class);
|
154 |
|
|
155 | 874 |
classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class, |
156 |
"registryDidShutdown", null, null), "{ _shutdown = true; }"); |
|
157 |
|
|
158 | 874 |
classFab.addMethod( |
159 |
Modifier.PUBLIC | Modifier.SYNCHRONIZED | Modifier.FINAL, |
|
160 |
new MethodSignature(void.class, "_setInner", new Class[] |
|
161 |
{ serviceInterface }, null),
|
|
162 |
"{ _inner = $1; }");
|
|
163 |
|
|
164 | 874 |
BodyBuilder builder = new BodyBuilder();
|
165 | 874 |
builder.begin(); |
166 | 874 |
builder.addln("if (_shutdown)");
|
167 | 874 |
builder.begin(); |
168 | 874 |
builder.addln("_inner = null;");
|
169 | 874 |
builder.addln("throw org.apache.hivemind.HiveMind#createRegistryShutdownException();");
|
170 | 874 |
builder.end(); |
171 |
|
|
172 | 874 |
builder.addln("return _inner;");
|
173 | 874 |
builder.end(); |
174 |
|
|
175 | 874 |
classFab.addMethod(Modifier.PRIVATE, new MethodSignature(serviceInterface, "_getInner", |
176 |
null, null), builder.toString()); |
|
177 |
|
|
178 | 874 |
proxyBuilder.addServiceMethods("_getInner()");
|
179 |
|
|
180 | 874 |
return classFab.createClass();
|
181 |
} |
|
182 |
|
|
183 | 874 |
private Class createInnerProxyClass(Class deferredProxyClass)
|
184 |
{ |
|
185 | 874 |
ServicePoint servicePoint = getServicePoint(); |
186 |
|
|
187 | 874 |
Class serviceInterface = servicePoint.getServiceInterface(); |
188 | 874 |
ProxyBuilder builder = new ProxyBuilder("InnerProxy", servicePoint); |
189 |
|
|
190 | 874 |
ClassFab classFab = builder.getClassFab(); |
191 |
|
|
192 | 874 |
classFab.addField("_deferredProxy", deferredProxyClass);
|
193 | 874 |
classFab.addField("_service", serviceInterface);
|
194 | 874 |
classFab.addField("_serviceModel", getClass());
|
195 |
|
|
196 | 874 |
BodyBuilder body = new BodyBuilder();
|
197 |
|
|
198 |
// The constructor remembers the outer proxy and registers itself
|
|
199 |
// with the outer proxy.
|
|
200 |
|
|
201 | 874 |
body.begin(); |
202 |
|
|
203 | 874 |
body.addln("super();");
|
204 | 874 |
body.addln("_deferredProxy = $1;");
|
205 | 874 |
body.addln("_serviceModel = $2;");
|
206 | 874 |
body.addln("_deferredProxy._setInner(this);");
|
207 |
|
|
208 | 874 |
body.end(); |
209 |
|
|
210 | 874 |
classFab.addConstructor(new Class[]
|
211 |
{ deferredProxyClass, getClass() }, null, body.toString());
|
|
212 |
|
|
213 |
// Method _service() will look up the service implementation,
|
|
214 |
// then update the deferred proxy to go directly to the
|
|
215 |
// service implementation, bypassing itself!
|
|
216 |
|
|
217 | 874 |
body.clear(); |
218 | 874 |
body.begin(); |
219 |
|
|
220 | 874 |
body.add("if (_service == null)");
|
221 | 874 |
body.begin(); |
222 |
|
|
223 | 874 |
body.add("_service = (");
|
224 | 874 |
body.add(serviceInterface.getName()); |
225 | 874 |
body.addln(") _serviceModel.getActualServiceImplementation();");
|
226 |
|
|
227 | 874 |
body.add("_deferredProxy._setInner(_service);");
|
228 |
|
|
229 | 874 |
body.end(); |
230 |
|
|
231 | 874 |
body.add("return _service;");
|
232 |
|
|
233 | 874 |
body.end(); |
234 |
|
|
235 | 874 |
classFab.addMethod( |
236 |
Modifier.PRIVATE | Modifier.FINAL | Modifier.SYNCHRONIZED, |
|
237 |
new MethodSignature(serviceInterface, "_service", null, null), |
|
238 |
body.toString()); |
|
239 |
|
|
240 | 874 |
builder.addServiceMethods("_service()");
|
241 |
|
|
242 |
// Build the implementation of interface SingletonInnerProxy
|
|
243 |
|
|
244 | 874 |
body.clear(); |
245 | 874 |
body.begin(); |
246 |
|
|
247 | 874 |
body.add("_service();");
|
248 |
|
|
249 | 874 |
body.end(); |
250 |
|
|
251 | 874 |
classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class, |
252 |
"_instantiateServiceImplementation", null, null), body.toString()); |
|
253 |
|
|
254 | 874 |
classFab.addInterface(SingletonInnerProxy.class);
|
255 |
|
|
256 | 874 |
return classFab.createClass();
|
257 |
} |
|
258 |
|
|
259 | 1 |
public void instantiateService() |
260 |
{ |
|
261 |
// Ensure that the outer and inner proxies have been created
|
|
262 |
|
|
263 | 1 |
getService(); |
264 |
|
|
265 |
// Force the inner proxy to resolve the service and install the result into
|
|
266 |
// the outer proxy.
|
|
267 |
|
|
268 | 1 |
_innerProxy._instantiateServiceImplementation(); |
269 |
} |
|
270 |
|
|
271 |
} |
|