1 15 package org.apache.hivemind.annotations; 16 17 import java.util.Locale ; 18 19 import org.apache.hivemind.ClassResolver; 20 import org.apache.hivemind.ErrorHandler; 21 import org.apache.hivemind.annotations.internal.TypedRegistryImpl; 22 import org.apache.hivemind.definition.RegistryDefinition; 23 import org.apache.hivemind.definition.impl.RegistryDefinitionImpl; 24 import org.apache.hivemind.events.RegistryInitializationListener; 25 import org.apache.hivemind.impl.DefaultClassResolver; 26 import org.apache.hivemind.impl.DefaultErrorHandler; 27 import org.apache.hivemind.impl.RegistryBuilder; 28 import org.apache.hivemind.internal.RegistryInfrastructure; 29 30 36 public class AnnotatedRegistryBuilder 37 { 38 private ClassResolver _classResolver; 39 private ErrorHandler _errorHandler; 40 private Locale _locale; 41 42 45 public AnnotatedRegistryBuilder() 46 { 47 this(new DefaultClassResolver(), new DefaultErrorHandler(), Locale.getDefault()); 48 } 49 50 57 public AnnotatedRegistryBuilder(ClassResolver classResolver, ErrorHandler errorHandler, 58 Locale locale) 59 { 60 _classResolver = classResolver; 61 _errorHandler = errorHandler; 62 _locale = locale; 63 } 64 65 70 public TypedRegistry constructRegistry(String ... moduleClassNames) 71 { 72 RegistryDefinition definition = constructRegistryDefinition(moduleClassNames); 73 return constructRegistry(definition); 74 } 75 76 81 public TypedRegistry constructRegistry(Class ... moduleClasses) 82 { 83 RegistryDefinition definition = constructRegistryDefinition(moduleClasses); 84 return constructRegistry(definition); 85 } 86 87 private RegistryDefinition constructRegistryDefinition(String ... moduleClassNames) 88 { 89 RegistryDefinition definition = new RegistryDefinitionImpl(); 90 91 for (int i = 0; i < moduleClassNames.length; i++) 92 { 93 AnnotatedModuleReader reader = new AnnotatedModuleReader(definition, 94 _classResolver, _errorHandler); 95 reader.readModule(moduleClassNames[i]); 96 } 97 98 return definition; 99 } 100 101 private RegistryDefinition constructRegistryDefinition(Class ... moduleClasses) 102 { 103 RegistryDefinition definition = new RegistryDefinitionImpl(); 104 105 for (int i = 0; i < moduleClasses.length; i++) 106 { 107 AnnotatedModuleReader reader = new AnnotatedModuleReader(definition, 108 _classResolver, _errorHandler); 109 reader.readModule(moduleClasses[i]); 110 } 111 112 return definition; 113 } 114 115 private TypedRegistry constructRegistry(RegistryDefinition definition) 116 { 117 RegistryInfrastructureHolder infrastructureHolder = new RegistryInfrastructureHolder(); 120 121 definition.addRegistryInitializationListener(infrastructureHolder); 122 123 RegistryBuilder.constructRegistry(definition, _errorHandler, _locale); 124 return new TypedRegistryImpl(null, infrastructureHolder.getInfrastructure()); 126 } 127 128 final class RegistryInfrastructureHolder implements RegistryInitializationListener 129 { 130 private RegistryInfrastructure _infrastructure; 131 132 public void registryInitialized(RegistryInfrastructure registry) 133 { 134 _infrastructure = registry; 135 } 136 137 public RegistryInfrastructure getInfrastructure() 138 { 139 return _infrastructure; 140 } 141 } 142 } 143 | Popular Tags |