1 15 package org.apache.hivemind.impl.servicemodel; 16 17 import java.util.List ; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.hivemind.ApplicationRuntimeException; 22 import org.apache.hivemind.HiveMind; 23 import org.apache.hivemind.ShutdownCoordinator; 24 import org.apache.hivemind.definition.ImplementationConstructionContext; 25 import org.apache.hivemind.definition.ImplementationConstructor; 26 import org.apache.hivemind.definition.ImplementationDefinition; 27 import org.apache.hivemind.definition.InterceptorDefinition; 28 import org.apache.hivemind.events.RegistryShutdownListener; 29 import org.apache.hivemind.impl.ConstructableServicePoint; 30 import org.apache.hivemind.impl.InterceptorStackImpl; 31 import org.apache.hivemind.impl.ProxyBuilder; 32 import org.apache.hivemind.internal.ImplementationConstructionContextImpl; 33 import org.apache.hivemind.internal.Module; 34 import org.apache.hivemind.internal.RegistryInfrastructure; 35 import org.apache.hivemind.internal.ServiceModel; 36 import org.apache.hivemind.service.ClassFab; 37 import org.apache.hivemind.util.ConstructorUtils; 38 39 44 public abstract class AbstractServiceModelImpl implements ServiceModel 45 { 46 51 protected final Log _log; 52 53 private ConstructableServicePoint _servicePoint; 54 55 56 private Class _bridgeProxyClass; 57 58 public AbstractServiceModelImpl(ConstructableServicePoint servicePoint) 59 { 60 _log = LogFactory.getLog(servicePoint.getExtensionPointId()); 61 62 _servicePoint = servicePoint; 63 } 64 65 protected Object addInterceptors(Object core) 66 { 67 List interceptors = _servicePoint.getOrderedInterceptorContributions(); 68 69 int count = interceptors == null ? 0 : interceptors.size(); 70 71 if (count == 0) 72 return core; 73 74 InterceptorStackImpl stack = new InterceptorStackImpl(_log, _servicePoint, core); 75 76 85 for (int i = count - 1; i >= 0; i--) 86 { 87 InterceptorDefinition id = (InterceptorDefinition) interceptors 88 .get(i); 89 90 stack.process(id); 91 } 92 93 95 return stack.peek(); 96 } 97 98 103 protected Object constructCoreServiceImplementation() 104 { 105 if (_log.isDebugEnabled()) 106 _log.debug("Constructing core service implementation for service " 107 + _servicePoint.getExtensionPointId()); 108 109 Class serviceInterface = _servicePoint.getServiceInterface(); 110 Class declaredInterface = _servicePoint.getDeclaredInterface(); 111 112 ImplementationDefinition implementationDefinition = _servicePoint.getImplementationDefinition(); 113 ImplementationConstructor constructor = implementationDefinition.getServiceConstructor(); 114 String definingModuleId = implementationDefinition.getModuleId(); 116 117 Module definingModule = getRegistry().getModule(definingModuleId); 118 ImplementationConstructionContext context = new ImplementationConstructionContextImpl(definingModule, 119 _servicePoint); 120 Object result = constructor.constructCoreServiceImplementation(context); 121 122 if (result == null) 123 throw new ApplicationRuntimeException(ServiceModelMessages 124 .factoryReturnedNull(_servicePoint), constructor.getLocation(), null); 125 126 131 if (!(serviceInterface.isInstance(result) || declaredInterface.isInstance(result))) 132 throw new ApplicationRuntimeException(ServiceModelMessages.factoryWrongInterface( 133 _servicePoint, 134 result, 135 serviceInterface), constructor.getLocation(), null); 136 137 HiveMind.setLocation(result, constructor.getLocation()); 138 139 return result; 140 } 141 142 private RegistryInfrastructure getRegistry() 143 { 144 return _servicePoint.getModule().getRegistry(); 145 } 146 147 154 155 protected Object constructServiceImplementation() 156 { 157 Object result = constructNewServiceImplementation(); 158 159 162 _servicePoint.clearConstructorInformation(); 163 164 return result; 165 } 166 167 171 protected Object constructNewServiceImplementation() 172 { 173 try 174 { 175 Object core = constructCoreServiceImplementation(); 176 177 Object intercepted = addInterceptors(core); 178 179 return intercepted; 180 } 181 catch (Exception ex) 182 { 183 throw new ApplicationRuntimeException(ServiceModelMessages.unableToConstructService( 184 _servicePoint, 185 ex), ex); 186 } 187 188 } 189 190 public ConstructableServicePoint getServicePoint() 191 { 192 return _servicePoint; 193 } 194 195 200 protected Object constructBridgeProxy(Object service) 201 { 202 Class bridgeProxyClass = getBridgeProxyClass(service); 203 204 return ConstructorUtils.invokeConstructor(bridgeProxyClass, new Object [] 205 { getServicePoint().getExtensionPointId(), service }); 206 } 207 208 214 private synchronized Class getBridgeProxyClass(Object service) 215 { 216 if (_bridgeProxyClass == null) 217 _bridgeProxyClass = constructBridgeProxyClass(service); 218 219 return _bridgeProxyClass; 220 } 221 222 227 228 private Class constructBridgeProxyClass(Object service) 229 { 230 ProxyBuilder builder = new ProxyBuilder("BridgeProxy", getServicePoint().getModule(), 231 getServicePoint().getServiceInterface(), getServicePoint().getDeclaredInterface(), false); 232 233 ClassFab cf = builder.getClassFab(); 234 235 Class serviceType = service.getClass(); 236 237 cf.addField("_service", serviceType); 238 239 cf.addConstructor(new Class [] 240 { String .class, serviceType }, null, "{ this($1); _service = $2; }"); 241 242 builder.addServiceMethods("_service"); 243 244 return cf.createClass(); 245 } 246 247 257 protected void registerWithShutdownCoordinator(Object service) 258 { 259 if (service instanceof RegistryShutdownListener) 260 { 261 ShutdownCoordinator coordinator = ((ShutdownCoordinator) getServicePoint().getModule() 262 .getService(ShutdownCoordinator.class)); 263 264 RegistryShutdownListener asListener = (RegistryShutdownListener) service; 265 coordinator.addRegistryShutdownListener(asListener); 266 } 267 } 268 } | Popular Tags |