1 15 package org.apache.hivemind.impl; 16 17 import java.util.Collection ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.HiveMind; 25 import org.apache.hivemind.Orderable; 26 import org.apache.hivemind.ShutdownCoordinator; 27 import org.apache.hivemind.definition.ImplementationConstructor; 28 import org.apache.hivemind.definition.ImplementationDefinition; 29 import org.apache.hivemind.definition.InterceptorDefinition; 30 import org.apache.hivemind.definition.ServicePointDefinition; 31 import org.apache.hivemind.events.RegistryShutdownListener; 32 import org.apache.hivemind.internal.Module; 33 import org.apache.hivemind.internal.ServiceModel; 34 import org.apache.hivemind.internal.ServiceModelFactory; 35 import org.apache.hivemind.order.Orderer; 36 import org.apache.hivemind.service.InterfaceSynthesizer; 37 import org.apache.hivemind.util.ToStringBuilder; 38 39 46 public final class ServicePointImpl extends AbstractExtensionPoint implements 47 ConstructableServicePoint 48 { 49 private Object _service; 50 51 private boolean _building; 52 53 private Class _serviceInterface; 54 55 private Class _declaredInterface; 56 57 private List _orderedInterceptorDefinitions; 58 59 private boolean _interceptorsOrdered; 60 61 private ShutdownCoordinator _shutdownCoordinator; 62 63 private ServiceModel _serviceModelObject; 64 65 public ServicePointImpl(Module module, ServicePointDefinition definition) 66 { 67 super(module, definition); 68 } 69 70 protected void extendDescription(ToStringBuilder builder) 71 { 72 if (_service != null) 73 builder.append("service", _service); 74 75 builder.append("serviceInterfaceName", getServiceInterfaceClassName()); 76 builder.append("serviceModel", getServiceModel()); 77 builder.append("interceptorDefinitions", getInterceptorDefinitions()); 78 79 if (_building) 80 builder.append("building", _building); 81 } 82 83 public synchronized Class getServiceInterface() 84 { 85 if (_serviceInterface == null) 86 _serviceInterface = lookupServiceInterface(); 87 88 return _serviceInterface; 89 } 90 91 public synchronized Class getDeclaredInterface() 92 { 93 if (_declaredInterface == null) 94 _declaredInterface = lookupDeclaredInterface(); 95 96 return _declaredInterface; 97 } 98 99 public String getServiceInterfaceClassName() 100 { 101 return getServicePointDefinition().getInterfaceClassName(); 102 } 103 104 private Object getInterceptorDefinitions() 105 { 106 return getServicePointDefinition().getInterceptors(); 107 } 108 109 private Class lookupDeclaredInterface() 110 { 111 Class result = null; 112 113 try 114 { 115 result = getModule().resolveType(getServiceInterfaceClassName()); 116 } 117 catch (Exception ex) 118 { 119 throw new ApplicationRuntimeException(ImplMessages.badInterface( 120 getServiceInterfaceClassName(), 121 getExtensionPointId()), getLocation(), ex); 122 } 123 124 return result; 125 } 126 127 private Class lookupServiceInterface() 128 { 129 Class declaredInterface = getDeclaredInterface(); 130 131 if (declaredInterface.isInterface()) 132 return declaredInterface; 133 134 136 InterfaceSynthesizer is = (InterfaceSynthesizer) getModule().getService( 137 HiveMind.INTERFACE_SYNTHESIZER_SERVICE, 138 InterfaceSynthesizer.class); 139 140 return is.synthesizeInterface(declaredInterface); 141 } 142 143 150 private synchronized Object getService() 151 { 152 if (_service == null) 153 { 154 155 if (_building) 156 throw new ApplicationRuntimeException(ImplMessages.recursiveServiceBuild(this)); 157 158 _building = true; 159 160 try 161 { 162 163 ServiceModelFactory factory = getModule().getServiceModelFactory(getServiceModel()); 164 165 _serviceModelObject = factory.createServiceModelForService(this); 166 167 _service = _serviceModelObject.getService(); 168 } 169 finally 170 { 171 _building = false; 172 } 173 } 174 175 return _service; 176 } 177 178 public Object getService(Class serviceInterface) 179 { 180 Object result = getService(); 181 182 if (!serviceInterface.isAssignableFrom(result.getClass())) 183 { 184 throw new ApplicationRuntimeException(ImplMessages.serviceWrongInterface( 185 this, 186 serviceInterface), getLocation(), null); 187 } 188 189 return result; 190 } 191 192 public String getServiceModel() 193 { 194 if (getServicePointDefinition() == null) 195 return null; 196 197 return getImplementationDefinition().getServiceModel(); 198 } 199 200 public void clearConstructorInformation() 201 { 202 _orderedInterceptorDefinitions = null; 203 } 204 205 207 210 public List getOrderedInterceptorContributions() 211 { 212 if (!_interceptorsOrdered) 213 { 214 _orderedInterceptorDefinitions = orderInterceptors(); 215 _interceptorsOrdered = true; 216 } 217 218 return _orderedInterceptorDefinitions; 219 } 220 221 224 private List orderInterceptors() 225 { 226 Collection interceptorDefinitions = getServicePointDefinition().getInterceptors(); 227 if (HiveMind.isEmpty(interceptorDefinitions)) 228 return null; 229 230 233 Log log = LogFactory.getLog(getExtensionPointId()); 234 235 Orderer orderer = new Orderer(log, getModule().getErrorHandler(), ImplMessages 236 .interceptorContribution()); 237 238 Iterator i = interceptorDefinitions.iterator(); 239 while (i.hasNext()) 240 { 241 InterceptorDefinition sid = (InterceptorDefinition) i.next(); 242 243 247 String precedingNames = null; 248 String followingNames = null; 249 if (sid instanceof Orderable) { 251 Orderable orderable = (Orderable) sid; 252 precedingNames = orderable.getPrecedingNames(); 253 followingNames = orderable.getFollowingNames(); 254 } 255 256 orderer.add(sid, sid.getName(), precedingNames, followingNames); 257 } 258 259 return orderer.getOrderedObjects(); 260 } 261 262 public void setShutdownCoordinator(ShutdownCoordinator coordinator) 263 { 264 _shutdownCoordinator = coordinator; 265 } 266 267 public void addRegistryShutdownListener(RegistryShutdownListener listener) 268 { 269 _shutdownCoordinator.addRegistryShutdownListener(listener); 270 } 271 272 275 public void forceServiceInstantiation() 276 { 277 getService(); 278 279 _serviceModelObject.instantiateService(); 280 } 281 282 285 286 public ImplementationConstructor getServiceConstructor() 287 { 288 return getImplementationDefinition().getServiceConstructor(); 289 } 290 291 public ImplementationDefinition getImplementationDefinition() 292 { 293 if (getServicePointDefinition().getDefaultImplementation() == null) 294 throw new ApplicationRuntimeException(ImplMessages.servicePointDefinitionWithoutImplementation(getServicePointDefinition())); 295 return getServicePointDefinition().getDefaultImplementation(); 296 } 297 298 301 public ServicePointDefinition getServicePointDefinition() 302 { 303 return (ServicePointDefinition) super.getDefinition(); 304 } 305 306 307 } | Popular Tags |