1 16 17 package org.springframework.beans.factory.xml; 18 19 import java.io.IOException ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 import java.util.Properties ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import org.springframework.beans.BeanUtils; 29 import org.springframework.core.io.support.PropertiesLoaderUtils; 30 import org.springframework.util.Assert; 31 import org.springframework.util.ClassUtils; 32 33 47 public class DefaultNamespaceHandlerResolver implements NamespaceHandlerResolver { 48 49 52 private static final String SPRING_HANDLER_MAPPINGS_LOCATION = "META-INF/spring.handlers"; 53 54 55 56 protected final Log logger = LogFactory.getLog(getClass()); 57 58 59 private Map handlerMappings; 60 61 62 69 public DefaultNamespaceHandlerResolver() { 70 this(null, SPRING_HANDLER_MAPPINGS_LOCATION); 71 } 72 73 74 81 public DefaultNamespaceHandlerResolver(ClassLoader classLoader) { 82 this(classLoader, SPRING_HANDLER_MAPPINGS_LOCATION); 83 } 84 85 93 public DefaultNamespaceHandlerResolver(ClassLoader classLoader, String handlerMappingsLocation) { 94 Assert.notNull(handlerMappingsLocation, "Handler mappings location must not be null"); 95 ClassLoader classLoaderToUse = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader()); 96 initHandlerMappings(classLoaderToUse, handlerMappingsLocation); 97 } 98 99 100 107 private void initHandlerMappings(ClassLoader classLoader, String handlerMappingsLocation) { 108 Properties mappings = loadMappings(classLoader, handlerMappingsLocation); 109 if (logger.isDebugEnabled()) { 110 logger.debug("Loaded mappings [" + mappings + "]"); 111 } 112 this.handlerMappings = new HashMap (mappings.size()); 113 for (Enumeration en = mappings.propertyNames(); en.hasMoreElements();) { 114 String namespaceUri = (String ) en.nextElement(); 115 String className = mappings.getProperty(namespaceUri); 116 try { 117 Class handlerClass = ClassUtils.forName(className, classLoader); 118 if (!NamespaceHandler.class.isAssignableFrom(handlerClass)) { 119 throw new IllegalArgumentException ("Class [" + className + 120 "] does not implement the NamespaceHandler interface"); 121 } 122 NamespaceHandler namespaceHandler = (NamespaceHandler) BeanUtils.instantiateClass(handlerClass); 123 namespaceHandler.init(); 124 this.handlerMappings.put(namespaceUri, namespaceHandler); 125 } 126 catch (ClassNotFoundException ex) { 127 if (logger.isDebugEnabled()) { 128 logger.debug("Ignoring namespace handler [" + className + "]: handler class not found", ex); 129 } 130 } 131 catch (LinkageError err) { 132 if (logger.isWarnEnabled()) { 133 logger.warn("Ignoring namespace handler [" + className + 134 "]: problem with handler class file or dependent class", err); 135 } 136 } 137 } 138 } 139 140 private Properties loadMappings(ClassLoader classLoader, String handlerMappingsLocation) { 141 try { 142 return PropertiesLoaderUtils.loadAllProperties(handlerMappingsLocation, classLoader); 143 } 144 catch (IOException ex) { 145 throw new IllegalStateException ( 146 "Unable to load NamespaceHandler mappings from location [" + 147 handlerMappingsLocation + "]. Root cause: " + ex); 148 } 149 } 150 151 152 158 public NamespaceHandler resolve(String namespaceUri) { 159 return (NamespaceHandler) this.handlerMappings.get(namespaceUri); 160 } 161 162 } 163 | Popular Tags |