1 15 package org.apache.hivemind.impl.servicemodel; 16 17 import org.apache.hivemind.ApplicationRuntimeException; 18 import org.apache.hivemind.Discardable; 19 import org.apache.hivemind.HiveMind; 20 import org.apache.hivemind.events.RegistryShutdownListener; 21 import org.apache.hivemind.impl.ConstructableServicePoint; 22 import org.apache.hivemind.impl.ProxyUtils; 23 import org.apache.hivemind.internal.Module; 24 import org.apache.hivemind.service.ThreadCleanupListener; 25 import org.apache.hivemind.service.ThreadEventNotifier; 26 27 35 public final class ThreadedServiceModel extends AbstractServiceModelImpl 36 { 37 40 protected static final String SERVICE_ACCESSOR_METHOD_NAME = "_service"; 41 42 private final Object _serviceProxy; 43 44 45 private final ThreadEventNotifier _notifier; 46 47 50 private final ThreadLocal _activeService = new ThreadLocal (); 51 52 53 54 private Class _serviceInterface; 55 56 public ThreadedServiceModel(ConstructableServicePoint servicePoint) 57 { 58 super(servicePoint); 59 60 _serviceInterface = servicePoint.getServiceInterface(); 61 62 Module module = getServicePoint().getModule(); 63 64 _notifier = (ThreadEventNotifier) module.getService( 65 HiveMind.THREAD_EVENT_NOTIFIER_SERVICE, 66 ThreadEventNotifier.class); 67 68 _serviceProxy = createServiceProxy(); 69 } 70 71 class CleanupListener implements ThreadCleanupListener 72 { 73 private final Object _core; 75 76 CleanupListener(Object core) 77 { 78 _core = core; 79 } 80 81 public void threadDidCleanup() 82 { 83 unbindServiceFromCurrentThread(); 84 85 if (_core instanceof Discardable) 86 { 87 Discardable d = (Discardable) _core; 88 89 d.threadDidDiscardService(); 90 } 91 } 92 } 93 94 97 public Object getService() 98 { 99 102 108 return _serviceProxy; 109 } 110 111 115 private Object createServiceProxy() 116 { 117 ConstructableServicePoint servicePoint = getServicePoint(); 118 119 if (_log.isDebugEnabled()) 120 _log.debug("Creating ThreadedProxy for service " + servicePoint.getExtensionPointId()); 121 122 Object proxy = ProxyUtils.createDelegatingProxy( 123 "ThreadedProxy", 124 this, 125 "getServiceImplementationForCurrentThread", 126 servicePoint); 127 128 Object intercepted = addInterceptors(proxy); 129 130 RegistryShutdownListener outerProxy = ProxyUtils 131 .createOuterProxy(intercepted, servicePoint); 132 133 servicePoint.addRegistryShutdownListener(outerProxy); 134 135 return outerProxy; 136 } 137 138 142 public Object getServiceImplementationForCurrentThread() 143 { 144 Object result = _activeService.get(); 145 146 if (result == null) 147 result = constructInstanceForCurrentThread(); 148 149 return result; 150 } 151 152 private Object constructInstanceForCurrentThread() 153 { 154 try 155 { 156 Object core = constructCoreServiceImplementation(); 157 158 if (core instanceof RegistryShutdownListener) 159 _log.error(ServiceModelMessages.registryCleanupIgnored(getServicePoint())); 160 161 _notifier.addThreadCleanupListener(new CleanupListener(core)); 162 163 167 if (!_serviceInterface.isInstance(core)) 168 core = constructBridgeProxy(core); 169 170 _activeService.set(core); 171 172 return core; 173 } 174 catch (Exception ex) 175 { 176 throw new ApplicationRuntimeException(ServiceModelMessages.unableToConstructService( 177 getServicePoint(), 178 ex), ex); 179 } 180 } 181 182 private void unbindServiceFromCurrentThread() 183 { 184 _activeService.set(null); 185 } 186 187 191 192 public void instantiateService() 193 { 194 getServiceImplementationForCurrentThread(); 195 } 196 197 } | Popular Tags |