1 15 package org.apache.hivemind.annotations.internal; 16 17 import org.apache.hivemind.ApplicationRuntimeException; 18 import org.apache.hivemind.annotations.TypedRegistry; 19 import org.apache.hivemind.internal.Module; 20 import org.apache.hivemind.internal.RegistryInfrastructure; 21 import org.apache.hivemind.util.Defense; 22 import org.apache.hivemind.util.PropertyUtils; 23 24 29 public class ModuleInstanceProviderImpl implements ModuleInstanceProvider 30 { 31 private static final String REGISTRY_PROPERTY_NAME = "registry"; 32 33 private Class _moduleClass; 34 35 private Object _instance; 36 37 private String _moduleId; 38 39 public ModuleInstanceProviderImpl(Class moduleClass, String moduleId) 40 { 41 _moduleClass = moduleClass; 42 _moduleId = moduleId; 43 } 44 45 public Object getModuleInstance() 46 { 47 Defense.fieldNotNull(_instance, "instance"); 48 return _instance; 49 } 50 51 private void createModuleInstance(RegistryInfrastructure _registry) 52 { 53 try 54 { 55 _instance = _moduleClass.newInstance(); 56 injectRegistry(_instance, _registry); 57 } 58 catch (Exception ex) 59 { 60 throw new ApplicationRuntimeException(ex.getMessage(), ex); 62 } 63 } 64 65 71 private void injectRegistry(Object moduleInstance, RegistryInfrastructure _registry) 72 { 73 if (PropertyUtils.isWritable(moduleInstance, REGISTRY_PROPERTY_NAME) 74 && PropertyUtils.getPropertyType(moduleInstance, REGISTRY_PROPERTY_NAME).equals(TypedRegistry.class)) { 75 76 Module callingModule = _registry.getModule(_moduleId); 77 TypedRegistry annotatedRegistry = new TypedRegistryImpl(callingModule, _registry); 78 PropertyUtils.write(moduleInstance, REGISTRY_PROPERTY_NAME, annotatedRegistry); 79 } 80 } 81 82 90 public void registryInitialized(RegistryInfrastructure registry) 91 { 92 createModuleInstance(registry); 93 } 94 95 } 96 | Popular Tags |