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