1 15 package org.apache.hivemind.impl; 16 17 import java.util.Iterator ; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.hivemind.ErrorHandler; 22 import org.apache.hivemind.Location; 23 import org.apache.hivemind.definition.ConfigurationParserDefinition; 24 import org.apache.hivemind.definition.ConfigurationPointDefinition; 25 import org.apache.hivemind.definition.ContributionDefinition; 26 import org.apache.hivemind.definition.DefinitionMessages; 27 import org.apache.hivemind.definition.ModuleDefinition; 28 import org.apache.hivemind.definition.RegistryDefinition; 29 import org.apache.hivemind.definition.ImplementationDefinition; 30 import org.apache.hivemind.definition.InterceptorDefinition; 31 import org.apache.hivemind.definition.ServicePointDefinition; 32 import org.apache.hivemind.definition.UnresolvedExtension; 33 import org.apache.hivemind.definition.Visibility; 34 import org.apache.hivemind.util.IdUtils; 35 36 48 public class ExtensionResolver 49 { 50 private static final Log LOG = LogFactory.getLog(ExtensionResolver.class); 51 52 private ErrorHandler _errorHandler; 53 54 private RegistryDefinition _definition; 55 56 public ExtensionResolver(RegistryDefinition definition, ErrorHandler errorHandler) 57 { 58 _errorHandler = errorHandler; 59 _definition = definition; 60 } 61 62 70 public void resolveExtensions() 71 { 72 for (Iterator iterModules = _definition.getModules().iterator(); iterModules.hasNext();) 73 { 74 ModuleDefinition module = (ModuleDefinition) iterModules.next(); 75 76 resolveImplementations(module); 77 resolveInterceptors(module); 78 resolveContributions(module); 79 resolveConfigurationParsers(module); 80 } 81 } 82 83 private void resolveImplementations(ModuleDefinition module) 84 { 85 for (Iterator iter = module.getImplementations().iterator(); iter.hasNext();) 86 { 87 UnresolvedExtension unresolved = (UnresolvedExtension) iter.next(); 88 String servicePointId = unresolved.getExtensionPointId(); 89 if (LOG.isDebugEnabled()) { 90 LOG.debug("Trying to resolve service point " + servicePointId + " referenced by" + 91 " implementation" + logLocation(unresolved.getExtension().getLocation())); 92 } 93 ServicePointDefinition servicePoint = _definition.getServicePoint(servicePointId); 94 if (servicePoint == null) 95 { 96 _errorHandler.error( 97 LOG, 98 DefinitionMessages.unknownServicePoint( 99 IdUtils.extractModule(servicePointId), 100 IdUtils.stripModule(servicePointId)), 101 unresolved.getExtension().getLocation(), 102 null); 103 } else { 104 servicePoint.addImplementation((ImplementationDefinition) unresolved.getExtension()); 105 } 106 iter.remove(); 107 } 108 } 109 110 private String logLocation(Location location) 111 { 112 if (location == null) { 113 return ""; 114 } else { 115 return " at " + location.toString(); 116 } 117 } 118 119 private void resolveInterceptors(ModuleDefinition module) 120 { 121 for (Iterator iter = module.getInterceptors().iterator(); iter.hasNext();) 122 { 123 UnresolvedExtension unresolved = (UnresolvedExtension) iter.next(); 124 String servicePointId = unresolved.getExtensionPointId(); 125 if (LOG.isDebugEnabled()) { 126 LOG.debug("Trying to resolve service point " + servicePointId + " referenced by" + 127 " interceptor" + logLocation(unresolved.getExtension().getLocation())); 128 } 129 ServicePointDefinition servicePoint = _definition.getServicePoint(servicePointId); 130 if (servicePoint == null) 131 { 132 _errorHandler.error( 133 LOG, 134 DefinitionMessages.unknownServicePoint( 135 IdUtils.extractModule(servicePointId), 136 IdUtils.stripModule(servicePointId)), 137 unresolved.getExtension().getLocation(), 138 null); 139 } else { 140 servicePoint.addInterceptor((InterceptorDefinition) unresolved.getExtension()); 141 } 142 iter.remove(); 143 } 144 } 145 146 private void resolveContributions(ModuleDefinition module) 147 { 148 for (Iterator iter = module.getContributions().iterator(); iter.hasNext();) 149 { 150 UnresolvedExtension unresolved = (UnresolvedExtension) iter.next(); 151 String configurationPointId = unresolved.getExtensionPointId(); 152 if (LOG.isDebugEnabled()) { 153 LOG.debug("Trying to resolve configuration point " + configurationPointId + " referenced by" + 154 " contribution " + logLocation(unresolved.getExtension().getLocation())); 155 } 156 ConfigurationPointDefinition configurationPoint = _definition.getConfigurationPoint(configurationPointId); 157 if (configurationPoint == null) 158 { 159 _errorHandler.error( 160 LOG, 161 DefinitionMessages.unknownConfigurationPoint( 162 IdUtils.extractModule(configurationPointId), 163 IdUtils.stripModule(configurationPointId)), 164 unresolved.getExtension().getLocation(), 165 null); 166 } else { 167 configurationPoint.addContribution((ContributionDefinition) unresolved.getExtension()); 168 } 169 iter.remove(); 170 } 171 } 172 173 private void resolveConfigurationParsers(ModuleDefinition module) 174 { 175 for (Iterator iter = module.getConfigurationParsers().iterator(); iter.hasNext();) 176 { 177 UnresolvedExtension unresolved = (UnresolvedExtension) iter.next(); 178 String configurationPointId = unresolved.getExtensionPointId(); 179 if (LOG.isDebugEnabled()) { 180 LOG.debug("Trying to resolve configuration point " + configurationPointId + " referenced by" + 181 " ConfigurationParser " + logLocation(unresolved.getExtension().getLocation())); 182 } 183 ConfigurationPointDefinition configurationPoint = _definition.getConfigurationPoint(configurationPointId); 184 if (configurationPoint == null) 185 { 186 _errorHandler.error( 187 LOG, 188 DefinitionMessages.unknownConfigurationPoint( 189 IdUtils.extractModule(configurationPointId), 190 IdUtils.stripModule(configurationPointId)), 191 unresolved.getExtension().getLocation(), 192 null); 193 } else { 194 if (Visibility.PRIVATE.equals(configurationPoint.getVisibility()) 195 && !module.getId().equals(IdUtils.extractModule(configurationPointId))) { 196 _errorHandler.error( 197 LOG, 198 DefinitionMessages.configurationPointNotVisible( 199 configurationPoint, 200 module), 201 unresolved.getExtension().getLocation(), 202 null); 203 204 } 205 206 configurationPoint.addParser((ConfigurationParserDefinition) unresolved.getExtension()); 207 } 208 iter.remove(); 209 } 210 } 211 212 } 213 | Popular Tags |