1 15 package org.apache.hivemind.impl; 16 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Locale ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.apache.hivemind.ErrorHandler; 24 import org.apache.hivemind.Registry; 25 import org.apache.hivemind.definition.ConfigurationPointDefinition; 26 import org.apache.hivemind.definition.DefinitionMessages; 27 import org.apache.hivemind.definition.ModuleDefinition; 28 import org.apache.hivemind.definition.Occurances; 29 import org.apache.hivemind.definition.RegistryDefinition; 30 import org.apache.hivemind.definition.RegistryDefinitionPostProcessor; 31 import org.apache.hivemind.definition.impl.RegistryDefinitionImpl; 32 import org.apache.hivemind.events.RegistryInitializationListener; 33 import org.apache.hivemind.internal.RegistryInfrastructure; 34 35 50 public final class RegistryBuilder 51 { 52 private static final Log LOG = LogFactory.getLog(RegistryBuilder.class); 53 54 static 55 { 56 if (!LOG.isErrorEnabled()) 57 { 58 System.err 59 .println("********************************************************************************"); 60 System.err 61 .println("* L O G G I N G C O N F I G U R A T I O N E R R O R *"); 62 System.err 63 .println("* ---------------------------------------------------------------------------- *"); 64 System.err 65 .println("* Logging is not enabled for org.apache.hivemind.impl.RegistryBuilder. *"); 66 System.err 67 .println("* Errors during HiveMind module descriptor parsing and validation may not be *"); 68 System.err 69 .println("* logged. This may result in difficult-to-trace runtime exceptions, if there *"); 70 System.err 71 .println("* are errors in any of your module descriptors. You should enable error *"); 72 System.err 73 .println("* logging for the org.apache.hivemind and hivemind loggers. *"); 74 System.err 75 .println("********************************************************************************"); 76 } 77 } 78 79 82 83 private ErrorHandler _errorHandler; 84 85 private RegistryDefinition _registryDefinition; 86 87 91 public RegistryBuilder() 92 { 93 this(new RegistryDefinitionImpl(), new DefaultErrorHandler()); 94 } 95 96 100 public RegistryBuilder(RegistryDefinition registryDefinition) 101 { 102 this(registryDefinition, new DefaultErrorHandler()); 103 } 104 105 public RegistryBuilder(ErrorHandler errorHandler) 106 { 107 this(new RegistryDefinitionImpl(), errorHandler); 108 } 109 110 public RegistryBuilder(RegistryDefinition registryDefinition, ErrorHandler errorHandler) 111 { 112 _registryDefinition = registryDefinition; 113 _errorHandler = errorHandler; 114 } 115 116 119 public RegistryDefinition getRegistryDefinition() 120 { 121 return _registryDefinition; 122 } 123 124 128 public Registry constructRegistry() 129 { 130 return constructRegistry(); 131 } 132 133 137 public Registry constructRegistry(Locale locale) 138 { 139 CoreServicesProvider coreServicesProvider = new CoreServicesProvider(); 141 coreServicesProvider.process(_registryDefinition, _errorHandler); 142 143 ExtensionResolver extensionResolver = new ExtensionResolver(_registryDefinition, _errorHandler); 145 extensionResolver.resolveExtensions(); 146 147 checkDependencies(_registryDefinition); 148 checkContributionCounts(_registryDefinition); 149 150 for (Iterator i = _registryDefinition.getPostProcessors().iterator(); i.hasNext();) 152 { 153 RegistryDefinitionPostProcessor processor = (RegistryDefinitionPostProcessor) i.next(); 154 155 processor.postprocess(_registryDefinition, _errorHandler); 156 } 157 158 RegistryInfrastructureConstructor constructor = new RegistryInfrastructureConstructor(_errorHandler, LOG, locale); 159 RegistryInfrastructure infrastructure = constructor 160 .constructRegistryInfrastructure(_registryDefinition); 161 162 for (Iterator i = _registryDefinition.getRegistryInitializationListeners().iterator(); i.hasNext();) 164 { 165 RegistryInitializationListener listener = (RegistryInitializationListener) i.next(); 166 167 listener.registryInitialized(infrastructure); 168 } 169 170 infrastructure.startup(); 171 172 return new RegistryImpl(infrastructure); 173 } 174 175 182 public static Registry constructRegistry(RegistryDefinition definition, ErrorHandler errorHandler, 183 Locale locale) 184 { 185 RegistryBuilder builder = new RegistryBuilder(definition, errorHandler); 186 return builder.constructRegistry(locale); 187 } 188 189 192 private void checkDependencies(RegistryDefinition definition) 193 { 194 for (Iterator iterModules = definition.getModules().iterator(); iterModules.hasNext();) 195 { 196 ModuleDefinition module = (ModuleDefinition) iterModules.next(); 197 198 for (Iterator iterDependencies = module.getDependencies().iterator(); iterDependencies.hasNext();) 199 { 200 String requiredModuleId = (String ) iterDependencies.next(); 201 checkModuleDependency(definition, module, requiredModuleId); 202 } 203 } 204 205 } 206 207 private void checkModuleDependency(RegistryDefinition definition, ModuleDefinition sourceModule, String requiredModuleId) 208 { 209 ModuleDefinition requiredModule = (ModuleDefinition) definition.getModule(requiredModuleId); 210 if (requiredModule == null) 211 { 212 _errorHandler.error( 214 LOG, 215 DefinitionMessages.dependencyOnUnknownModule(requiredModuleId), 216 null, 217 null); 218 return; 219 } 220 } 221 222 225 public void checkContributionCounts(RegistryDefinition definition) 226 { 227 for (Iterator iterModules = definition.getModules().iterator(); iterModules.hasNext();) 228 { 229 ModuleDefinition module = (ModuleDefinition) iterModules.next(); 230 231 for (Iterator iterConfigurations = module.getConfigurationPoints().iterator(); iterConfigurations.hasNext();) 232 { 233 ConfigurationPointDefinition cpd = (ConfigurationPointDefinition) iterConfigurations.next(); 234 checkContributionCounts(module, cpd); 235 } 236 } 237 } 238 239 private void checkContributionCounts(ModuleDefinition definingModule, ConfigurationPointDefinition configurationPoint) 240 { 241 Occurances expected = configurationPoint.getExpectedContributions(); 242 243 int actual = configurationPoint.getContributions().size(); 244 245 if (expected.inRange(actual)) 246 return; 247 248 _errorHandler.error(LOG, DefinitionMessages.wrongNumberOfContributions( 249 definingModule, configurationPoint, 250 actual, 251 expected), configurationPoint.getLocation(), null); 252 } 253 254 258 public void autoDetectModules() 259 { 260 RegistryProviderAutoDetector detector = new RegistryProviderAutoDetector(new DefaultClassResolver()); 261 List providers = detector.getProviders(); 262 for (Iterator iterProviders = providers.iterator(); iterProviders.hasNext();) 263 { 264 RegistryProvider provider = (RegistryProvider) iterProviders.next(); 265 provider.process(getRegistryDefinition(), _errorHandler); 266 } 267 } 268 269 275 public static Registry constructDefaultRegistry() 276 { 277 RegistryBuilder builder = new RegistryBuilder(); 278 builder.autoDetectModules(); 279 return builder.constructRegistry(Locale.getDefault()); 280 } 281 282 public ErrorHandler getErrorHandler() 283 { 284 return _errorHandler; 285 } 286 287 } | Popular Tags |