1 15 package org.apache.hivemind.impl; 16 17 import java.io.IOException ; 18 import java.net.URL ; 19 import java.util.Enumeration ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.apache.hivemind.ApplicationRuntimeException; 26 import org.apache.hivemind.ClassResolver; 27 import org.apache.hivemind.ErrorHandler; 28 import org.apache.hivemind.HiveMind; 29 import org.apache.hivemind.Resource; 30 import org.apache.hivemind.definition.RegistryDefinition; 31 import org.apache.hivemind.parse.ModuleDescriptor; 32 import org.apache.hivemind.parse.SubModuleDescriptor; 33 import org.apache.hivemind.parse.XmlResourceProcessor; 34 import org.apache.hivemind.util.ClasspathResource; 35 import org.apache.hivemind.util.URLResource; 36 37 48 public class XmlModuleReader 49 { 50 private static final Log LOG = LogFactory.getLog(XmlModuleReader.class); 51 52 private RegistryDefinition _registryDefinition; 53 54 57 private XmlResourceProcessor _parser; 58 59 private XmlModuleDescriptorProcessor _processor; 60 61 private ErrorHandler _errorHandler; 62 63 private ClassResolver _classResolver; 64 65 public XmlModuleReader(RegistryDefinition registryDefinition) 66 { 67 this(registryDefinition, new DefaultClassResolver(), new DefaultErrorHandler()); 68 } 69 70 public XmlModuleReader(RegistryDefinition registryDefinition, ClassResolver classResolver, 71 ErrorHandler errorHandler) 72 { 73 _registryDefinition = registryDefinition; 74 _classResolver = classResolver; 75 _errorHandler = errorHandler; 76 _processor = new XmlModuleDescriptorProcessor(_registryDefinition, _errorHandler); 77 _parser = new XmlResourceProcessor(_classResolver, _errorHandler); 78 } 79 80 86 public void readModule(Resource moduleResource) 87 { 88 if (moduleResource.getResourceURL() == null) 89 throw new ApplicationRuntimeException(XmlImplMessages.unableToFindModuleResource(moduleResource)); 90 91 processResource(moduleResource); 92 } 93 94 102 public void readClassPathModule(String moduleFileName) 103 { 104 readModule(new ClasspathResource(_classResolver, moduleFileName)); 105 } 106 107 115 public void readClassPathModules(String moduleFileName) 116 { 117 Enumeration foundResources = null; 118 ClassLoader loader = _classResolver.getClassLoader(); 119 120 try 121 { 122 foundResources = loader.getResources(moduleFileName); 123 } 124 catch (IOException ex) 125 { 126 throw new ApplicationRuntimeException(XmlImplMessages.unableToFindModulesError(_classResolver, ex), 127 ex); 128 } 129 130 if (!foundResources.hasMoreElements()) 131 throw new ApplicationRuntimeException(XmlImplMessages.unableToFindModuleResource(new ClasspathResource(_classResolver, moduleFileName))); 132 133 while (foundResources.hasMoreElements()) 134 { 135 URL descriptorURL = (URL ) foundResources.nextElement(); 136 137 readModule(new URLResource(descriptorURL)); 138 } 139 } 140 141 private void processResource(Resource resource) 142 { 143 try 144 { 145 ModuleDescriptor md = _parser.processResource(resource); 146 147 _processor.processModuleDescriptor(md); 148 149 processSubModules(md); 152 } 153 catch (RuntimeException ex) 154 { 155 _errorHandler.error(LOG, ex.getMessage(), HiveMind.getLocation(ex), ex); 156 } 157 } 158 159 private void processSubModules(ModuleDescriptor moduleDescriptor) 160 { 161 List subModules = moduleDescriptor.getSubModules(); 162 163 if (subModules == null) 164 return; 165 166 for (Iterator i = subModules.iterator(); i.hasNext();) 167 { 168 SubModuleDescriptor smd = (SubModuleDescriptor) i.next(); 169 170 Resource descriptorResource = smd.getDescriptor(); 171 172 if (descriptorResource.getResourceURL() == null) 173 { 174 _errorHandler.error( 175 LOG, 176 XmlImplMessages.subModuleDoesNotExist(descriptorResource), 177 smd.getLocation(), 178 null); 179 continue; 180 } 181 182 processResource(smd.getDescriptor()); 183 } 184 } 185 186 } 187 | Popular Tags |