1 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 36 public final class SingletonServiceModel extends AbstractServiceModelImpl 37 { 38 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 public SingletonServiceModel(ConstructableServicePoint servicePoint) 50 { 51 super(servicePoint); 52 } 53 54 public synchronized Object getService() 55 { 56 if (_serviceProxy == null) 57 _serviceProxy = createSingletonProxy(); 58 59 return _serviceProxy; 60 } 61 62 65 public synchronized Object getActualServiceImplementation() 66 { 67 if (_constructedService == null) 68 { 69 _constructedService = constructServiceImplementation(); 70 registerWithShutdownCoordinator(_constructedService); 71 } 72 73 79 Class serviceInterface = getServicePoint().getServiceInterface(); 80 81 if (!serviceInterface.isInstance(_constructedService)) 82 _constructedService = constructBridgeProxy(_constructedService); 83 84 return _constructedService; 85 } 86 87 90 private Object createSingletonProxy() 91 { 92 if (_log.isDebugEnabled()) 93 _log.debug("Creating SingletonProxy for service " 94 + getServicePoint().getExtensionPointId()); 95 96 try 97 { 98 99 102 Class proxyClass = createSingletonProxyClass(); 103 104 107 Class innerProxyClass = createInnerProxyClass(proxyClass); 108 109 111 Constructor co = proxyClass.getConstructor(new Class [] { String .class }); 112 113 Object result = co.newInstance(new Object [] { getServicePoint().getExtensionPointId() }); 114 115 118 Constructor c = innerProxyClass.getConstructor(new Class [] 119 { String .class, proxyClass, getClass() }); 120 121 _innerProxy = (SingletonInnerProxy) c.newInstance(new Object [] 122 { getServicePoint().getExtensionPointId(), result, this }); 123 124 RegistryShutdownListener asListener = (RegistryShutdownListener) result; 125 126 getServicePoint().addRegistryShutdownListener(asListener); 127 128 return result; 129 } 130 catch (Exception ex) 131 { 132 throw new ApplicationRuntimeException(ex); 133 } 134 135 } 136 137 143 private Class createSingletonProxyClass() 144 { 145 ConstructableServicePoint servicePoint = getServicePoint(); 146 147 ProxyBuilder proxyBuilder = new ProxyBuilder("SingletonProxy", servicePoint.getModule(), 148 servicePoint.getServiceInterface(), servicePoint.getDeclaredInterface(), true); 149 150 ClassFab classFab = proxyBuilder.getClassFab(); 151 152 Class serviceInterface = servicePoint.getServiceInterface(); 153 154 157 classFab.addField("_inner", serviceInterface); 158 classFab.addField("_shutdown", boolean.class); 159 if (!RegistryShutdownListener.class.isAssignableFrom(serviceInterface)) 160 { 161 classFab.addInterface(RegistryShutdownListener.class); 162 163 classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class, 164 "registryDidShutdown", null, null), "{ _shutdown = true; }"); 165 } 166 classFab.addMethod( 167 Modifier.PUBLIC | Modifier.SYNCHRONIZED | Modifier.FINAL, 168 new MethodSignature(void.class, "_setInner", new Class [] 169 { serviceInterface }, null), 170 "{ _inner = $1; }"); 171 172 BodyBuilder builder = new BodyBuilder(); 173 builder.begin(); 174 builder.addln("if (_shutdown)"); 175 builder.begin(); 176 builder.addln("_inner = null;"); 177 builder.addln("throw org.apache.hivemind.HiveMind#createRegistryShutdownException();"); 178 builder.end(); 179 180 builder.addln("return _inner;"); 181 builder.end(); 182 183 classFab.addMethod(Modifier.PRIVATE, new MethodSignature(serviceInterface, "_getInner", 184 null, null), builder.toString()); 185 186 proxyBuilder.addServiceMethods("_getInner()"); 187 188 return classFab.createClass(); 189 } 190 191 private Class createInnerProxyClass(Class deferredProxyClass) 192 { 193 ServicePoint servicePoint = getServicePoint(); 194 195 Class serviceInterface = servicePoint.getServiceInterface(); 196 197 ProxyBuilder builder = new ProxyBuilder("InnerProxy", servicePoint.getModule(), 198 serviceInterface, servicePoint.getDeclaredInterface(), false); 199 200 ClassFab classFab = builder.getClassFab(); 201 202 classFab.addField("_deferredProxy", deferredProxyClass); 203 classFab.addField("_service", serviceInterface); 204 classFab.addField("_serviceModel", getClass()); 205 206 BodyBuilder body = new BodyBuilder(); 207 208 211 body.begin(); 212 213 body.addln("this($1);"); 214 body.addln("_deferredProxy = $2;"); 215 body.addln("_serviceModel = $3;"); 216 body.addln("_deferredProxy._setInner(this);"); 217 218 body.end(); 219 220 classFab.addConstructor(new Class [] 221 { String .class, deferredProxyClass, getClass() }, null, body.toString()); 222 223 227 body.clear(); 228 body.begin(); 229 230 body.add("if (_service == null)"); 231 body.begin(); 232 233 body.add("_service = ("); 234 body.add(serviceInterface.getName()); 235 body.addln(") _serviceModel.getActualServiceImplementation();"); 236 237 body.add("_deferredProxy._setInner(_service);"); 238 239 body.end(); 240 241 body.add("return _service;"); 242 243 body.end(); 244 245 classFab.addMethod( 246 Modifier.PRIVATE | Modifier.FINAL | Modifier.SYNCHRONIZED, 247 new MethodSignature(serviceInterface, "_service", null, null), 248 body.toString()); 249 250 builder.addServiceMethods("_service()"); 251 252 254 body.clear(); 255 body.begin(); 256 257 body.add("_service();"); 258 259 body.end(); 260 261 classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class, 262 "_instantiateServiceImplementation", null, null), body.toString()); 263 264 classFab.addInterface(SingletonInnerProxy.class); 265 266 return classFab.createClass(); 267 } 268 269 public void instantiateService() 270 { 271 273 getService(); 274 275 278 _innerProxy._instantiateServiceImplementation(); 279 } 280 281 } | Popular Tags |