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.Map ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.hivemind.ApplicationRuntimeException; 25 import org.apache.hivemind.ErrorHandler; 26 import org.apache.hivemind.Location; 27 import org.apache.hivemind.definition.ConfigurationParserDefinition; 28 import org.apache.hivemind.definition.ConfigurationPointDefinition; 29 import org.apache.hivemind.definition.ModuleDefinition; 30 import org.apache.hivemind.definition.RegistryDefinition; 31 import org.apache.hivemind.definition.ServicePointDefinition; 32 import org.apache.hivemind.definition.UnresolvedExtension; 33 import org.apache.hivemind.definition.impl.ConfigurationParserDefinitionImpl; 34 import org.apache.hivemind.definition.impl.ConfigurationPointDefinitionImpl; 35 import org.apache.hivemind.schema.Schema; 36 import org.apache.hivemind.util.IdUtils; 37 import org.apache.hivemind.util.UniqueHashMap; 38 import org.apache.hivemind.xml.definition.impl.HiveMindSchemaParser; 39 import org.apache.hivemind.xml.definition.impl.HiveMindSchemaParserConstructor; 40 import org.apache.hivemind.xml.definition.impl.XmlModuleDefinitionImpl; 41 import org.apache.hivemind.xml.definition.impl.XmlServicePointDefinitionImpl; 42 43 49 public class XmlExtensionResolver 50 { 51 private static final Log LOG = LogFactory.getLog(XmlExtensionResolver.class); 52 53 private ErrorHandler _errorHandler; 54 55 private RegistryDefinition _definition; 56 57 61 private Map _schemas = new HashMap (); 62 63 public XmlExtensionResolver(RegistryDefinition definition, ErrorHandler errorHandler) 64 { 65 _errorHandler = errorHandler; 66 _definition = definition; 67 buildGlobalSchemaMap(); 68 } 69 70 74 public void resolveSchemas() 75 { 76 Collection modules = _definition.getModules(); 77 78 for (Iterator iterModules = modules.iterator(); iterModules.hasNext();) 79 { 80 ModuleDefinition module = (ModuleDefinition) iterModules.next(); 81 if (module instanceof XmlModuleDefinitionImpl) { 82 XmlModuleDefinitionImpl xmlModule = (XmlModuleDefinitionImpl) module; 83 resolveServicePointSchemas(xmlModule); 84 resolveSchemaAssignments(xmlModule); 85 } 86 } 87 } 88 89 92 public void buildGlobalSchemaMap() 93 { 94 Collection modules = _definition.getModules(); 95 for (Iterator iterModules = modules.iterator(); iterModules.hasNext();) 96 { 97 ModuleDefinition module = (ModuleDefinition) iterModules.next(); 98 if (module instanceof XmlModuleDefinitionImpl) { 99 XmlModuleDefinitionImpl xmlModule = (XmlModuleDefinitionImpl) module; 100 Collection schemas = xmlModule.getSchemas(); 101 for (Iterator iterSchemas = schemas.iterator(); iterSchemas.hasNext();) 102 { 103 Schema schema = (Schema) iterSchemas.next(); 104 String schemaId = IdUtils.qualify(module.getId(), schema.getId()); 105 _schemas.put(schemaId, schema); 106 } 107 } 108 } 109 } 110 111 private void resolveServicePointSchemas(ModuleDefinition sourceModule) 112 { 113 Collection points = sourceModule.getServicePoints(); 114 115 for (Iterator iterSp = points.iterator(); iterSp.hasNext();) 116 { 117 ServicePointDefinition spd = (ServicePointDefinition) iterSp.next(); 118 119 if (spd instanceof XmlServicePointDefinitionImpl) { 121 XmlServicePointDefinitionImpl xspd = (XmlServicePointDefinitionImpl) spd; 122 123 if (xspd.getParametersSchemaId() != null) { 124 ServicePointDefinition point = sourceModule.getServicePoint(xspd.getId()); 125 126 XmlServicePointDefinitionImpl xmlServicePoint = (XmlServicePointDefinitionImpl) point; 127 Schema schema = findSchema(sourceModule, xspd.getParametersSchemaId(), xspd.getLocation()); 128 if (schema == null) { 129 } else { 131 xmlServicePoint.setParametersSchema(schema); 132 xmlServicePoint.setParametersCount(xspd.getParametersCount()); 133 } 134 } 135 } 136 } 137 } 138 139 144 private void resolveSchemaAssignments(XmlModuleDefinitionImpl sourceModule) 145 { 146 Collection schemaAssignments = sourceModule.getSchemaAssignments(); 147 148 for (Iterator iterSa = schemaAssignments.iterator(); iterSa.hasNext();) 149 { 150 SchemaAssignment schemaAssignment = (SchemaAssignment) iterSa.next(); 151 152 String configurationPointId = IdUtils.qualify(sourceModule.getId(), schemaAssignment.getConfigurationId()); 153 ConfigurationPointDefinition cpd = _definition.getConfigurationPoint(configurationPointId); 154 if (cpd == null) { 155 throw new ApplicationRuntimeException(XmlImplMessages.unknownConfigurationPointOfSchemaAssignment(configurationPointId, schemaAssignment)); 156 } 157 158 String schemaId = IdUtils.qualify(sourceModule.getId(), schemaAssignment.getSchemaId()); 159 Schema schema = getSchema(schemaId, sourceModule.getId(), schemaAssignment.getLocation()); 160 161 if (schema != null) { 162 165 ConfigurationParserDefinition parserDef = new ConfigurationParserDefinitionImpl( 167 sourceModule, schemaAssignment.getLocation(), HiveMindSchemaParser.INPUT_FORMAT_NAME, 168 new HiveMindSchemaParserConstructor(schema)); 169 170 cpd.addParser(parserDef); 171 172 if (HashMap .class.getName().equals(schema.getRootElementClassName()) 174 || UniqueHashMap.class.getName().equals(schema.getRootElementClassName())) { 175 if (cpd instanceof ConfigurationPointDefinitionImpl) { 179 ((ConfigurationPointDefinitionImpl) cpd).setConfigurationTypeName(Map .class.getName()); 180 } 181 } 182 183 } 184 } 185 } 186 187 private Schema findSchema(ModuleDefinition module, String schemaId, Location location) 188 { 189 if (schemaId == null) 190 return null; 191 192 String moduleId = module.getId(); 193 String qualifiedId = IdUtils.qualify(moduleId, schemaId); 194 195 return getSchema(qualifiedId, moduleId, location); 196 } 197 198 204 private Schema getSchema(String qualifiedSchemaId, String referencingModule, Location location) 205 { 206 Schema schema = (Schema) _schemas.get(qualifiedSchemaId); 207 208 if (schema == null) 209 _errorHandler 210 .error(LOG, XmlImplMessages.unableToResolveSchema(qualifiedSchemaId), location, null); 211 else if (!schema.visibleToModule(referencingModule)) 212 { 213 _errorHandler.error( 214 LOG, 215 XmlImplMessages.schemaNotVisible(qualifiedSchemaId, referencingModule), 216 location, 217 null); 218 schema = null; 219 } 220 221 return schema; 222 } 223 224 } 225 | Popular Tags |