1 15 package org.apache.hivemind.impl; 16 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Locale ; 23 import java.util.Map ; 24 25 import org.apache.hivemind.ErrorHandler; 26 import org.apache.hivemind.HiveMind; 27 import org.apache.hivemind.ShutdownCoordinator; 28 import org.apache.hivemind.definition.ConfigurationPointDefinition; 29 import org.apache.hivemind.definition.Contribution; 30 import org.apache.hivemind.definition.ContributionContext; 31 import org.apache.hivemind.definition.ImplementationConstructionContext; 32 import org.apache.hivemind.definition.ImplementationConstructor; 33 import org.apache.hivemind.definition.ModuleDefinition; 34 import org.apache.hivemind.definition.RegistryDefinition; 35 import org.apache.hivemind.definition.ServicePointDefinition; 36 import org.apache.hivemind.definition.impl.ModuleDefinitionHelper; 37 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl; 38 import org.apache.hivemind.impl.servicemodel.PooledServiceModelFactory; 39 import org.apache.hivemind.impl.servicemodel.PrimitiveServiceModelFactory; 40 import org.apache.hivemind.impl.servicemodel.SingletonServiceModelFactory; 41 import org.apache.hivemind.impl.servicemodel.ThreadedServiceModelFactory; 42 import org.apache.hivemind.internal.AbstractServiceImplementationConstructor; 43 import org.apache.hivemind.internal.ServiceModel; 44 import org.apache.hivemind.service.Autowiring; 45 import org.apache.hivemind.service.AutowiringStrategy; 46 import org.apache.hivemind.service.ClassFactory; 47 import org.apache.hivemind.service.InterfaceSynthesizer; 48 import org.apache.hivemind.service.ThreadEventNotifier; 49 import org.apache.hivemind.service.ThreadLocalStorage; 50 import org.apache.hivemind.service.ThreadLocale; 51 import org.apache.hivemind.service.impl.AutowiringByTypeStrategy; 52 import org.apache.hivemind.service.impl.AutowiringImpl; 53 import org.apache.hivemind.service.impl.AutowiringStrategyContribution; 54 import org.apache.hivemind.service.impl.ClassFactoryImpl; 55 import org.apache.hivemind.service.impl.EagerLoader; 56 import org.apache.hivemind.service.impl.InterfaceSynthesizerImpl; 57 import org.apache.hivemind.service.impl.ThreadEventNotifierImpl; 58 import org.apache.hivemind.service.impl.ThreadLocalStorageImpl; 59 import org.apache.hivemind.service.impl.ThreadLocaleImpl; 60 61 66 public class CoreServicesProvider implements RegistryProvider 67 { 68 private ModuleDefinitionHelper helper; 69 70 public void process(RegistryDefinition registryDefinition, ErrorHandler errorHandler) 71 72 { 73 DefaultClassResolver resolver = new DefaultClassResolver(); 74 75 80 ModuleDefinition md = registryDefinition.getModule("hivemind"); 81 if (md == null) 82 { 83 md = new ModuleDefinitionImpl("hivemind", HiveMind.getClassLocation(getClass(), resolver), 84 resolver, null); 85 registryDefinition.addModule(md); 86 } 87 88 helper = new ModuleDefinitionHelper((ModuleDefinitionImpl) md); 90 91 addClassFactory(md); 92 93 addThreadEventNotifier(md); 94 95 addThreadLocalStorage(md); 96 97 addThreadLocale(md); 98 99 addStartup(md); 100 101 addEagerLoad(md); 102 103 addShutdownCoordinator(md); 104 105 addInterfaceSynthesizer(md); 106 107 addServiceModelConfiguration(); 108 109 addAutowiring(md); 110 111 addAutowiringStrategiesConfiguration(); 112 } 113 114 117 private void addClassFactory(ModuleDefinition md) 118 { 119 ServicePointDefinition spd = helper.addServicePoint("ClassFactory", ClassFactory.class.getName()); 120 helper.addSimpleServiceImplementation(spd, ClassFactoryImpl.class.getName(), ServiceModel.PRIMITIVE); 121 } 122 123 127 private void addThreadEventNotifier(ModuleDefinition md) 128 { 129 ServicePointDefinition spd = helper.addServicePoint( 130 "ThreadEventNotifier", 131 ThreadEventNotifier.class.getName()); 132 helper.addSimpleServiceImplementation( 133 spd, 134 ThreadEventNotifierImpl.class.getName(), 135 ServiceModel.SINGLETON); 136 } 137 138 144 private void addThreadLocalStorage(ModuleDefinition md) 145 { 146 ServicePointDefinition spd = helper.addServicePoint( 147 "ThreadLocalStorage", 148 ThreadLocalStorage.class.getName()); 149 helper.addSimpleServiceImplementation(spd, ThreadLocalStorageImpl.class.getName(), ServiceModel.THREADED); 150 } 151 152 156 private void addThreadLocale(ModuleDefinition md) 157 { 158 ServicePointDefinition spd = helper.addServicePoint("ThreadLocale", ThreadLocale.class.getName()); 159 160 ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(md.getLocation()) 162 { 163 164 public Object constructCoreServiceImplementation(ImplementationConstructionContext context) 165 { 166 Locale defaultLocale = context.getDefiningModule().getLocale(); 168 return new ThreadLocaleImpl(defaultLocale); 169 } 170 171 }; 172 173 helper.addServiceImplementation(spd, constructor, ServiceModel.THREADED); 174 } 175 176 179 private void addShutdownCoordinator(ModuleDefinition md) 180 { 181 ServicePointDefinition spd = helper.addServicePoint("ShutdownCoordinator", ShutdownCoordinator.class.getName()); 182 helper.addSimpleServiceImplementation(spd, ShutdownCoordinatorImpl.class.getName(), ServiceModel.SINGLETON); 183 } 184 185 188 private void addEagerLoad(ModuleDefinition md) 189 { 190 ServicePointDefinition spd = helper.addServicePoint("EagerLoad", Runnable .class.getName()); 191 192 ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(md.getLocation()) 194 { 195 public Object constructCoreServiceImplementation(ImplementationConstructionContext context) 196 { 197 EagerLoader result = new EagerLoader(); 198 result.setServicePoints((List ) context.getConfiguration("EagerLoad")); 199 return result; 200 } 201 }; 202 helper.addServiceImplementation(spd, constructor, ServiceModel.PRIMITIVE); 203 204 207 helper.addConfigurationPoint("EagerLoad", List .class.getName()); 208 } 209 210 215 private void addStartup(ModuleDefinition md) 216 { 217 ServicePointDefinition spd = helper.addServicePoint("Startup", Runnable .class.getName()); 218 219 ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(md.getLocation()) 221 { 222 public Object constructCoreServiceImplementation(ImplementationConstructionContext context) 223 { 224 StartupImpl result = new StartupImpl(); 225 result.setRunnables((List ) context.getConfiguration("Startup")); 226 return result; 227 } 228 }; 229 helper.addServiceImplementation(spd, constructor, ServiceModel.PRIMITIVE); 230 231 234 ConfigurationPointDefinition cpd = helper.addConfigurationPoint("Startup", List .class.getName()); 235 236 final List services = getDefaultStartupServices(); 237 helper.addContributionDefinition(cpd, new Contribution() { 238 239 public void contribute(ContributionContext context) 240 { 241 List contribution = new ArrayList (); 242 for (Iterator iterServices = services.iterator(); iterServices.hasNext();) 243 { 244 String serviceName = (String ) iterServices.next(); 245 contribution.add(context.getService(serviceName, Runnable .class)); 246 } 247 context.mergeContribution(contribution); 248 }}); 249 } 250 251 254 private void addServiceModelConfiguration() 255 { 256 257 ConfigurationPointDefinition cpd = helper.addConfigurationPoint("ServiceModels", 258 Map .class.getName()); 259 260 final List serviceModels = getDefaultServiceModels(); 261 helper.addContributionDefinition(cpd, new Contribution() { 262 263 public void contribute(ContributionContext context) 264 { 265 Map contribution = new HashMap (); 266 for (Iterator iterServiceModels = serviceModels.iterator(); iterServiceModels.hasNext();) 267 { 268 ServiceModelContribution contrib = (ServiceModelContribution) iterServiceModels.next(); 269 contribution.put(contrib.getName(), contrib); 270 } 271 context.mergeContribution(contribution); 272 }}); 273 } 274 275 278 private List getDefaultStartupServices() 279 { 280 List result = new ArrayList (); 281 result.add("hivemind.EagerLoad"); 282 return result; 283 } 284 285 288 private List getDefaultServiceModels() 289 { 290 List result = new ArrayList (); 291 result.add(new ServiceModelContribution(ServiceModel.PRIMITIVE, new PrimitiveServiceModelFactory())); 292 result.add(new ServiceModelContribution(ServiceModel.SINGLETON, new SingletonServiceModelFactory())); 293 result.add(new ServiceModelContribution(ServiceModel.POOLED, new PooledServiceModelFactory())); 294 result.add(new ServiceModelContribution(ServiceModel.THREADED, new ThreadedServiceModelFactory())); 295 return result; 296 } 297 298 301 private void addInterfaceSynthesizer(ModuleDefinition md) 302 { 303 ServicePointDefinition spd = helper.addServicePoint("InterfaceSynthesizer", InterfaceSynthesizer.class.getName()); 304 305 ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(md.getLocation()) 307 { 308 public Object constructCoreServiceImplementation(ImplementationConstructionContext context) 309 { 310 InterfaceSynthesizerImpl result = new InterfaceSynthesizerImpl(); 311 result.setClassFactory((ClassFactory) context.getService(ClassFactory.class)); 313 return result; 314 } 315 }; 316 317 helper.addServiceImplementation(spd, constructor, ServiceModel.SINGLETON); 318 } 319 320 323 private void addAutowiring(ModuleDefinition md) 324 { 325 ServicePointDefinition spd = helper.addServicePoint("Autowiring", Autowiring.class.getName()); 326 327 ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(md.getLocation()) 329 { 330 public Object constructCoreServiceImplementation(ImplementationConstructionContext context) 331 { 332 List strategies = (List ) context.getConfiguration("AutowiringStrategies"); 333 Autowiring result = new AutowiringImpl(context.getRegistry(), strategies, context.getDefiningModule().getErrorHandler()); 334 return result; 335 } 336 }; 337 helper.addServiceImplementation(spd, constructor, ServiceModel.PRIMITIVE); 338 339 } 340 341 344 private void addAutowiringStrategiesConfiguration() 345 { 346 347 ConfigurationPointDefinition cpd = helper.addConfigurationPoint("AutowiringStrategies", List .class.getName()); 348 349 final List serviceModels = getDefaultAutowiringStrategies(); 350 helper.addContributionDefinition(cpd, new Contribution() { 351 352 public void contribute(ContributionContext context) 353 { 354 List contribution = new ArrayList (); 355 contribution.addAll(serviceModels); 356 context.mergeContribution(contribution); 357 }}); 358 } 359 360 363 private List getDefaultAutowiringStrategies() 364 { 365 List result = new ArrayList (); 366 result.add(new AutowiringStrategyContribution(new AutowiringByTypeStrategy(), AutowiringStrategy.BY_TYPE, null, null)); 367 return result; 368 } 369 370 } 371 | Popular Tags |