1 15 package org.apache.hivemind.impl; 16 17 import java.util.ArrayList ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.ErrorHandler; 25 import org.apache.hivemind.SymbolExpander; 26 import org.apache.hivemind.SymbolSource; 27 import org.apache.hivemind.TranslatorManager; 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.RegistryDefinitionPostProcessor; 36 import org.apache.hivemind.definition.ServicePointDefinition; 37 import org.apache.hivemind.definition.impl.ModuleDefinitionHelper; 38 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl; 39 import org.apache.hivemind.internal.AbstractServiceImplementationConstructor; 40 import org.apache.hivemind.internal.ServiceModel; 41 42 47 public class XmlCoreServicesProvider implements RegistryProvider 48 { 49 private static final Log LOG = LogFactory.getLog(XmlCoreServicesProvider.class); 50 private ModuleDefinitionHelper helper; 51 52 public void process(RegistryDefinition registryDefinition, ErrorHandler errorHandler) 53 { 54 if (LOG.isDebugEnabled()) { 55 LOG.debug("Adding xml core services to registry"); 56 } 57 58 ModuleDefinitionImpl moduleDefinition = (ModuleDefinitionImpl) registryDefinition.getModule("hivemind"); 62 if (moduleDefinition == null) { 63 throw new ApplicationRuntimeException("Module 'hivemind' not found."); 64 } 65 66 helper = new ModuleDefinitionHelper(moduleDefinition); 67 68 addTranslatorManager(moduleDefinition, errorHandler); 69 70 addSymbolSourcesConfiguration(moduleDefinition); 71 72 addSymbolSource(moduleDefinition, "FactoryDefaultsSymbolSource", "FactoryDefaults"); 74 75 addSymbolSource(moduleDefinition, "ApplicationDefaultsSymbolSource", "ApplicationDefaults"); 77 78 addSymbolExpander(moduleDefinition, errorHandler); 79 80 registryDefinition.addPostProcessor(new XmlPostProcessor()); 81 } 82 83 86 private void addTranslatorManager(ModuleDefinition md, final ErrorHandler errorHandler) 87 { 88 ServicePointDefinition spd = helper.addServicePoint("TranslationManager", TranslatorManager.class.getName()); 89 90 ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(md.getLocation()) 92 { 93 public Object constructCoreServiceImplementation(ImplementationConstructionContext context) 94 { 95 List translators = (List ) context.getConfiguration("Translators"); 96 TranslatorManager result = new TranslatorManagerImpl(translators, errorHandler); 97 return result; 98 } 99 }; 100 helper.addServiceImplementation(spd, constructor, ServiceModel.PRIMITIVE); 101 } 102 103 106 private void addSymbolSourcesConfiguration(ModuleDefinition md) 107 { 108 ConfigurationPointDefinition cpd = helper.addConfigurationPoint("SymbolSources", List .class.getName()); 109 110 helper.addContributionDefinition(cpd, new Contribution() 111 { 112 113 public void contribute(ContributionContext context) 114 { 115 List contribution = new ArrayList (); 116 117 SymbolSource factoryDefaults = (SymbolSource) context.getService( 120 "FactoryDefaultsSymbolSource", SymbolSource.class); 121 SymbolSourceContribution factoryDefaultsContrib = new SymbolSourceContribution(factoryDefaults, 122 "hivemind.FactoryDefaults", null, null); 123 contribution.add(factoryDefaultsContrib); 124 125 SymbolSource applicationDefaults = (SymbolSource) context.getService( 126 "ApplicationDefaultsSymbolSource", SymbolSource.class); 127 SymbolSourceContribution applicationDefaultsContrib = new SymbolSourceContribution(applicationDefaults, 128 "hivemind.ApplicationDefaults", null, "hivemind.FactoryDefaults"); 129 contribution.add(applicationDefaultsContrib); 130 131 context.mergeContribution(contribution); 132 } 133 }); 134 } 135 136 140 private void addSymbolSource(ModuleDefinition md, final String servicePointId, final String configurationId) 141 { 142 ServicePointDefinition spd = helper.addServicePoint(servicePointId, SymbolSource.class.getName()); 143 144 ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(md.getLocation()) 146 { 147 public Object constructCoreServiceImplementation(ImplementationConstructionContext context) 148 { 149 DefaultsSymbolSource result = new DefaultsSymbolSource(); 150 result.setDefaults(((Map ) context.getConfiguration(configurationId)).values()); 151 result.initializeService(); 152 return result; 153 } 154 }; 155 helper.addServiceImplementation(spd, constructor, ServiceModel.SINGLETON); 156 157 159 helper.addConfigurationPoint(configurationId, Map .class.getName()); 160 } 161 162 165 private void addSymbolExpander(ModuleDefinition md, final ErrorHandler errorHandler) 166 { 167 ServicePointDefinition spd = helper.addServicePoint("SymbolExpander", SymbolExpander.class.getName()); 168 169 ImplementationConstructor constructor = new AbstractServiceImplementationConstructor(md.getLocation()) 171 { 172 public Object constructCoreServiceImplementation(ImplementationConstructionContext context) 173 { 174 List symbolSources = (List ) context.getConfiguration("SymbolSources"); 175 176 SymbolExpander result = new SymbolExpanderImpl(errorHandler, symbolSources); 177 return result; 178 } 179 }; 180 helper.addServiceImplementation(spd, constructor, ServiceModel.PRIMITIVE); 181 182 } 183 184 } 185 186 class XmlPostProcessor implements RegistryDefinitionPostProcessor 187 { 188 public void postprocess(RegistryDefinition registryDefinition, ErrorHandler errorHandler) 189 { 190 XmlExtensionResolver extensionResolver = new XmlExtensionResolver(registryDefinition, errorHandler); 191 extensionResolver.resolveSchemas(); 192 } 193 } 194 | Popular Tags |