1 15 package org.apache.hivemind.impl; 16 17 import java.util.Collection ; 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.apache.hivemind.ErrorHandler; 26 import org.apache.hivemind.Location; 27 import org.apache.hivemind.conditional.EvaluationContextImpl; 28 import org.apache.hivemind.conditional.Node; 29 import org.apache.hivemind.conditional.Parser; 30 import org.apache.hivemind.definition.ConfigurationParserDefinition; 31 import org.apache.hivemind.definition.ImplementationDefinition; 32 import org.apache.hivemind.definition.RegistryDefinition; 33 import org.apache.hivemind.definition.impl.ConfigurationParserDefinitionImpl; 34 import org.apache.hivemind.definition.impl.ConfigurationPointDefinitionImpl; 35 import org.apache.hivemind.definition.impl.ContributionDefinitionImpl; 36 import org.apache.hivemind.definition.impl.ModuleDefinitionImpl; 37 import org.apache.hivemind.definition.impl.ImplementationDefinitionImpl; 38 import org.apache.hivemind.definition.impl.InterceptorDefinitionImpl; 39 import org.apache.hivemind.definition.impl.ServicePointDefinitionImpl; 40 import org.apache.hivemind.parse.ConfigurationPointDescriptor; 41 import org.apache.hivemind.parse.ContributionDescriptor; 42 import org.apache.hivemind.parse.DependencyDescriptor; 43 import org.apache.hivemind.parse.ImplementationDescriptor; 44 import org.apache.hivemind.parse.InstanceBuilder; 45 import org.apache.hivemind.parse.InterceptorDescriptor; 46 import org.apache.hivemind.parse.ModuleDescriptor; 47 import org.apache.hivemind.parse.SchemaAssignmentDescriptor; 48 import org.apache.hivemind.parse.ServicePointDescriptor; 49 import org.apache.hivemind.schema.impl.SchemaImpl; 50 import org.apache.hivemind.util.IdUtils; 51 import org.apache.hivemind.util.UniqueHashMap; 52 import org.apache.hivemind.xml.definition.impl.HiveMindSchemaParser; 53 import org.apache.hivemind.xml.definition.impl.HiveMindSchemaParserConstructor; 54 import org.apache.hivemind.xml.definition.impl.XmlContributionImpl; 55 import org.apache.hivemind.xml.definition.impl.XmlModuleDefinitionImpl; 56 import org.apache.hivemind.xml.definition.impl.XmlServicePointDefinitionImpl; 57 58 66 public class XmlModuleDescriptorProcessor 67 { 68 private static final Log _log = LogFactory.getLog(XmlModuleDescriptorProcessor.class); 69 70 private ErrorHandler _errorHandler; 71 72 private RegistryDefinition _registryDefinition; 73 74 private Parser _conditionalExpressionParser; 75 76 79 private Map _moduleDescriptors = new HashMap (); 80 81 public XmlModuleDescriptorProcessor(RegistryDefinition registryDefinition, ErrorHandler errorHandler) 82 { 83 _registryDefinition = registryDefinition; 84 _errorHandler = errorHandler; 85 } 86 87 90 public void processModuleDescriptor(ModuleDescriptor md) 91 { 92 String id = md.getModuleId(); 93 94 if (_log.isDebugEnabled()) 95 _log.debug("Processing module " + id); 96 97 _moduleDescriptors.put(id, md); 98 99 XmlModuleDefinitionImpl module = new XmlModuleDefinitionImpl(id, md.getLocation(), md.getClassResolver(), md.getPackageName()); 100 101 addSchemas(module, md); 102 103 addSchemaAssignments(module, md); 104 105 addServicePoints(module, md); 106 107 addConfigurationPoints(module, md); 108 109 addImplementations(module, md); 110 111 addContributions(module, md); 112 113 addDependencies(module, md); 114 115 _registryDefinition.addModule(module); 116 117 } 118 119 122 private void addDependencies(ModuleDefinitionImpl module, ModuleDescriptor md) 123 { 124 int count = size(md.getDependencies()); 125 for (int i = 0; i < count; i++) 126 { 127 DependencyDescriptor dependency = (DependencyDescriptor) md.getDependencies().get(i); 128 module.addDependency(dependency.getModuleId()); 130 } 131 } 132 133 136 private void addSchemas(XmlModuleDefinitionImpl module, ModuleDescriptor md) 137 { 138 for (Iterator schemas = md.getSchemas().iterator(); schemas.hasNext();) 139 { 140 SchemaImpl schema = (SchemaImpl) schemas.next(); 141 142 module.addSchema(schema.getId(), schema); 143 } 144 } 145 146 149 private void addSchemaAssignments(XmlModuleDefinitionImpl module, ModuleDescriptor md) 150 { 151 if (md.getSchemaAssignment() == null) 152 return; 153 154 for (Iterator assignments = md.getSchemaAssignment().iterator(); assignments.hasNext();) 155 { 156 SchemaAssignmentDescriptor sad = (SchemaAssignmentDescriptor) assignments.next(); 157 158 SchemaAssignment schemaAssignment = new SchemaAssignment(sad.getConfigurationId(), sad.getSchemaId(), sad.getLocation()); 159 module.addSchemaAssignment(schemaAssignment); 160 } 161 } 162 163 166 private void addServicePoints(XmlModuleDefinitionImpl module, ModuleDescriptor md) 167 { 168 List services = md.getServicePoints(); 169 int count = size(services); 170 171 for (int i = 0; i < count; i++) 172 { 173 ServicePointDescriptor sd = (ServicePointDescriptor) services.get(i); 174 XmlServicePointDefinitionImpl servicePoint = new XmlServicePointDefinitionImpl(module, sd.getId(), sd.getLocation(), 175 sd.getVisibility(), sd.getInterfaceClassName()); 176 177 if (sd.getParametersSchema() != null) { 180 servicePoint.setParametersSchema(sd.getParametersSchema()); 181 servicePoint.setParametersCount(sd.getParametersCount()); 182 } else if (sd.getParametersSchemaId() != null ) { 183 servicePoint.setParametersSchemaId(sd.getParametersSchemaId()); 185 } 186 187 188 module.addServicePoint(servicePoint); 189 190 addInternalImplementations(module, servicePoint, sd); 191 } 192 } 193 194 197 private void addImplementations(ModuleDefinitionImpl module, ModuleDescriptor md) 198 { 199 String moduleId = md.getModuleId(); 200 201 List implementations = md.getImplementations(); 202 int count = size(implementations); 203 204 for (int i = 0; i < count; i++) 205 { 206 ImplementationDescriptor impl = (ImplementationDescriptor) implementations.get(i); 207 208 if (!includeContribution(impl.getConditionalExpression(), module, impl 209 .getLocation())) 210 continue; 211 212 String pointId = impl.getServiceId(); 213 String qualifiedId = IdUtils.qualify(moduleId, pointId); 214 215 addImplementationAndInterceptors(module, qualifiedId, impl); 216 } 217 218 } 219 220 223 private void addImplementationAndInterceptors(ModuleDefinitionImpl sourceModule, String qualifiedPointId, ImplementationDescriptor id) 224 { 225 InstanceBuilder builder = id.getInstanceBuilder(); 226 List interceptors = id.getInterceptors(); 227 228 if (builder != null) { 229 ImplementationDefinitionImpl implementation = new ImplementationDefinitionImpl( 230 sourceModule, builder.getLocation(), builder.createConstructor(sourceModule.getId()), 231 builder.getServiceModel(), false); 232 sourceModule.addImplementation(qualifiedPointId, implementation); 233 } 234 235 int count = size(interceptors); 236 for (int i = 0; i < count; i++) 237 { 238 InterceptorDescriptor ind = (InterceptorDescriptor) interceptors.get(i); 239 240 addInterceptor(sourceModule, qualifiedPointId, ind); 241 } 242 } 243 244 248 private void addInternalImplementations(ModuleDefinitionImpl module, ServicePointDefinitionImpl point, 249 ServicePointDescriptor spd) 250 { 251 InstanceBuilder builder = spd.getInstanceBuilder(); 252 List interceptors = spd.getInterceptors(); 253 String pointId = point.getId(); 254 255 if (builder == null && interceptors == null) 256 return; 257 258 if (builder != null) { 259 ImplementationDefinition implementation = new ImplementationDefinitionImpl( 260 module, builder.getLocation(), builder.createConstructor(module.getId()), 261 builder.getServiceModel(), false); 262 point.addImplementation(implementation); 263 } 264 if (interceptors == null) 265 return; 266 267 int count = size(interceptors); 268 269 for (int i = 0; i < count; i++) 270 { 271 InterceptorDescriptor id = (InterceptorDescriptor) interceptors.get(i); 272 String qualifiedId = IdUtils.qualify(module.getId(), pointId); 273 addInterceptor(module, qualifiedId, id); 274 } 275 } 276 277 280 private void addConfigurationPoints(XmlModuleDefinitionImpl module, ModuleDescriptor md) 281 { 282 List points = md.getConfigurationPoints(); 283 int count = size(points); 284 285 for (int i = 0; i < count; i++) 286 { 287 ConfigurationPointDescriptor cpd = (ConfigurationPointDescriptor) points.get(i); 288 289 ConfigurationPointDefinitionImpl configurationPoint = new ConfigurationPointDefinitionImpl( 290 module, cpd.getId(), cpd.getLocation(), cpd.getVisibility(), 291 cpd.getType(), cpd.getCount()); 292 module.addConfigurationPoint(configurationPoint); 293 294 if (cpd.getContributionsSchema() != null) { 297 if (HashMap .class.getName().equals(cpd.getContributionsSchema().getRootElementClassName()) 299 || UniqueHashMap.class.getName().equals(cpd.getContributionsSchema().getRootElementClassName())) { 300 301 configurationPoint.setConfigurationTypeName(Map .class.getName()); 302 } 303 304 ConfigurationParserDefinition parserDef = new ConfigurationParserDefinitionImpl( 306 module, cpd.getContributionsSchema().getLocation(), HiveMindSchemaParser.INPUT_FORMAT_NAME, 307 new HiveMindSchemaParserConstructor(cpd.getContributionsSchema())); 308 309 configurationPoint.addParser(parserDef); 310 } else if (cpd.getContributionsSchemaId() != null) { 311 String qualifiedId = IdUtils.qualify(module.getId(), cpd.getId()); 313 SchemaAssignment schemaAssignment = new SchemaAssignment(qualifiedId, 314 cpd.getContributionsSchemaId(), cpd.getLocation()); 315 module.addSchemaAssignment(schemaAssignment); 316 } 317 } 318 } 319 320 323 private void addContributions(ModuleDefinitionImpl module, ModuleDescriptor md) 324 { 325 String moduleId = md.getModuleId(); 326 327 List contributions = md.getContributions(); 328 int count = size(contributions); 329 330 for (int i = 0; i < count; i++) 331 { 332 ContributionDescriptor cd = (ContributionDescriptor) contributions.get(i); 333 334 if (!includeContribution(cd.getConditionalExpression(), module, cd.getLocation())) 335 continue; 336 337 String pointId = cd.getConfigurationId(); 338 String qualifiedId = IdUtils.qualify(moduleId, pointId); 339 340 ContributionDefinitionImpl contribution = new ContributionDefinitionImpl(module, cd.getLocation(), 341 new XmlContributionImpl(moduleId, cd.getElements()), false); 342 module.addContribution(qualifiedId, contribution); 343 344 } 345 } 346 347 350 private void addInterceptor(ModuleDefinitionImpl module, String qualifiedPointId, InterceptorDescriptor id) 351 { 352 if (_log.isDebugEnabled()) 353 _log.debug("Adding " + id + " to service extension point " + qualifiedPointId); 354 355 InvokeFactoryInterceptorConstructor constructor = new InvokeFactoryInterceptorConstructor(id.getLocation()); 356 constructor.setFactoryServiceId(id.getFactoryServiceId()); 357 constructor.setParameters(id.getParameters()); 358 constructor.setPrecedingInterceptorIds(id.getAfter()); 359 constructor.setFollowingInterceptorIds(id.getBefore()); 360 InterceptorDefinitionImpl interceptor = new InterceptorDefinitionImpl( 361 module, id.getName(), id.getLocation(), constructor); 362 module.addInterceptor(qualifiedPointId, interceptor); 363 } 364 365 375 376 private boolean includeContribution(String expression, ModuleDefinitionImpl module, Location location) 377 { 378 if (expression == null) 379 return true; 380 381 if (_conditionalExpressionParser == null) 382 _conditionalExpressionParser = new Parser(); 383 384 try 385 { 386 Node node = _conditionalExpressionParser.parse(expression); 387 388 return node.evaluate(new EvaluationContextImpl(module.getClassResolver())); 389 } 390 catch (RuntimeException ex) 391 { 392 _errorHandler.error(_log, ex.getMessage(), location, ex); 393 394 return false; 395 } 396 } 397 398 private static int size(Collection c) 399 { 400 return c == null ? 0 : c.size(); 401 } 402 403 } 404 | Popular Tags |