1 17 package org.apache.servicemix.jbi.config.spring; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.springframework.beans.factory.BeanDefinitionStoreException; 22 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; 23 import org.w3c.dom.Document ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.Node ; 26 import org.w3c.dom.NodeList ; 27 import org.apache.xbean.spring.context.SpringApplicationContext; 28 import org.apache.xbean.spring.context.SpringXmlPreprocessor; 29 30 import java.io.BufferedReader ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.io.InputStreamReader ; 34 import java.util.Arrays ; 35 import java.util.HashSet ; 36 import java.util.Set ; 37 38 public class XBeanProcessor implements SpringXmlPreprocessor { 39 40 public static final String BEAN_NAME_DELIMITERS = ",; "; 41 42 46 public static final String TRUE_VALUE = "true"; 47 public static final String DEFAULT_VALUE = "default"; 48 public static final String DESCRIPTION_ELEMENT = "description"; 49 50 public static final String AUTOWIRE_BY_NAME_VALUE = "byName"; 51 public static final String AUTOWIRE_BY_TYPE_VALUE = "byType"; 52 public static final String AUTOWIRE_CONSTRUCTOR_VALUE = "constructor"; 53 public static final String AUTOWIRE_AUTODETECT_VALUE = "autodetect"; 54 55 public static final String DEPENDENCY_CHECK_ALL_ATTRIBUTE_VALUE = "all"; 56 public static final String DEPENDENCY_CHECK_SIMPLE_ATTRIBUTE_VALUE = "simple"; 57 public static final String DEPENDENCY_CHECK_OBJECTS_ATTRIBUTE_VALUE = "objects"; 58 59 public static final String DEFAULT_LAZY_INIT_ATTRIBUTE = "default-lazy-init"; 60 public static final String DEFAULT_DEPENDENCY_CHECK_ATTRIBUTE = "default-dependency-check"; 61 public static final String DEFAULT_AUTOWIRE_ATTRIBUTE = "default-autowire"; 62 63 public static final String IMPORT_ELEMENT = "import"; 64 public static final String RESOURCE_ATTRIBUTE = "resource"; 65 66 public static final String ALIAS_ELEMENT = "alias"; 67 public static final String NAME_ATTRIBUTE = "name"; 68 public static final String ALIAS_ATTRIBUTE = "alias"; 69 70 public static final String BEAN_ELEMENT = "bean"; 71 public static final String ID_ATTRIBUTE = "id"; 72 public static final String PARENT_ATTRIBUTE = "parent"; 73 74 public static final String CLASS_ATTRIBUTE = "class"; 75 public static final String ABSTRACT_ATTRIBUTE = "abstract"; 76 public static final String SINGLETON_ATTRIBUTE = "singleton"; 77 public static final String LAZY_INIT_ATTRIBUTE = "lazy-init"; 78 public static final String AUTOWIRE_ATTRIBUTE = "autowire"; 79 public static final String DEPENDENCY_CHECK_ATTRIBUTE = "dependency-check"; 80 public static final String DEPENDS_ON_ATTRIBUTE = "depends-on"; 81 public static final String INIT_METHOD_ATTRIBUTE = "init-method"; 82 public static final String DESTROY_METHOD_ATTRIBUTE = "destroy-method"; 83 public static final String FACTORY_METHOD_ATTRIBUTE = "factory-method"; 84 public static final String FACTORY_BEAN_ATTRIBUTE = "factory-bean"; 85 86 public static final String CONSTRUCTOR_ARG_ELEMENT = "constructor-arg"; 87 public static final String INDEX_ATTRIBUTE = "index"; 88 public static final String TYPE_ATTRIBUTE = "type"; 89 public static final String PROPERTY_ELEMENT = "property"; 90 public static final String REF_ATTRIBUTE = "ref"; 91 public static final String VALUE_ATTRIBUTE = "value"; 92 public static final String LOOKUP_METHOD_ELEMENT = "lookup-method"; 93 94 public static final String REPLACED_METHOD_ELEMENT = "replaced-method"; 95 public static final String REPLACER_ATTRIBUTE = "replacer"; 96 public static final String ARG_TYPE_ELEMENT = "arg-type"; 97 public static final String ARG_TYPE_MATCH_ATTRIBUTE = "match"; 98 99 public static final String REF_ELEMENT = "ref"; 100 public static final String IDREF_ELEMENT = "idref"; 101 public static final String BEAN_REF_ATTRIBUTE = "bean"; 102 public static final String LOCAL_REF_ATTRIBUTE = "local"; 103 public static final String PARENT_REF_ATTRIBUTE = "parent"; 104 105 public static final String VALUE_ELEMENT = "value"; 106 public static final String NULL_ELEMENT = "null"; 107 public static final String LIST_ELEMENT = "list"; 108 public static final String SET_ELEMENT = "set"; 109 public static final String MAP_ELEMENT = "map"; 110 public static final String ENTRY_ELEMENT = "entry"; 111 public static final String KEY_ELEMENT = "key"; 112 public static final String KEY_ATTRIBUTE = "key"; 113 public static final String KEY_REF_ATTRIBUTE = "key-ref"; 114 public static final String VALUE_REF_ATTRIBUTE = "value-ref"; 115 public static final String PROPS_ELEMENT = "props"; 116 public static final String PROP_ELEMENT = "prop"; 117 118 121 protected static final String [] RESERVED_ELEMENT_NAMES = { "beans", DESCRIPTION_ELEMENT, IMPORT_ELEMENT, ALIAS_ELEMENT, 122 BEAN_ELEMENT, 123 CONSTRUCTOR_ARG_ELEMENT, PROPERTY_ELEMENT, 124 LOOKUP_METHOD_ELEMENT, REPLACED_METHOD_ELEMENT, 125 ARG_TYPE_ELEMENT, REF_ELEMENT, IDREF_ELEMENT, 126 VALUE_ELEMENT, NULL_ELEMENT, LIST_ELEMENT, 127 SET_ELEMENT, MAP_ELEMENT, ENTRY_ELEMENT, KEY_ELEMENT, 128 PROPS_ELEMENT, PROP_ELEMENT }; 129 130 protected final Log logger = LogFactory.getLog(getClass()); 131 132 private Set reservedElementNames = new HashSet (Arrays.asList(RESERVED_ELEMENT_NAMES)); 133 134 public void preprocess(SpringApplicationContext applicationContext, XmlBeanDefinitionReader reader, Document document) { 135 preprocessXml(reader, document.getDocumentElement()); 136 } 137 138 public void preprocessXml(XmlBeanDefinitionReader reader, Element root) { 139 String localName = root.getNodeName(); 140 String uri = root.getNamespaceURI(); 141 boolean extensible = true; 142 if (uri == null || uri.length() == 0) { 143 if (reservedElementNames.contains(localName)) { 144 extensible = false; 145 } 146 } 147 if (extensible) { 148 ElementProcessor handler = findElementProcessor(uri, localName); 150 if (handler != null) { 151 handler.processElement(root, reader); 152 } 153 } 154 155 NodeList nl = root.getChildNodes(); 157 for (int i = 0; i < nl.getLength(); i++) { 158 Node node = nl.item(i); 159 if (node instanceof Element ) { 160 Element element = (Element ) node; 161 preprocessXml(reader, element); 162 } 163 } 164 } 165 166 174 protected ElementProcessor findElementProcessor(String namespaceURI, String localName) throws BeanDefinitionStoreException { 175 String uri = "META-INF/services/org/springframework/config/" + createDiscoveryPathName(namespaceURI, localName); 176 177 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(uri); 179 if (in == null) { 180 in = getClass().getClassLoader().getResourceAsStream(uri); 181 if (in == null) { 182 logger.warn("Could not find resource: " + uri); 183 return null; 184 } 185 } 186 187 BufferedReader reader = null; 189 try { 190 reader = new BufferedReader (new InputStreamReader (in)); 191 String line = reader.readLine(); 192 if (line == null) { 193 throw new BeanDefinitionStoreException("Empty file found for: " + uri); 194 } 195 line = line.trim(); 196 Class answer = null; 197 try { 198 answer = loadClass(line); 199 } 200 catch (ClassNotFoundException e) { 201 throw new BeanDefinitionStoreException("Could not find class: " + line, e); 202 } 203 try { 204 return (ElementProcessor) answer.newInstance(); 205 } 206 catch (Exception e) { 207 throw new BeanDefinitionStoreException("Failed to instantiate bean of type: " + answer.getName() + ". Reason: " + e, e); 208 } 209 } 210 catch (IOException e) { 211 throw new BeanDefinitionStoreException("Failed to load file for URI: " + uri + ". Reason: " + e, e); 212 } 213 finally { 214 try { 215 reader.close(); 216 } 217 catch (Exception e) { 218 } 220 } 221 } 222 223 226 protected String createDiscoveryPathName(String uri, String localName) { 227 if (uri == null || uri.length() == 0) { 228 return localName; 229 } 230 return uri.replaceAll("://", "/").replace(':', '/').replace(' ', '_') + "/" + localName; 233 } 234 235 238 protected Class loadClass(String name) throws ClassNotFoundException { 239 try { 240 return Thread.currentThread().getContextClassLoader().loadClass(name); 241 } 242 catch (ClassNotFoundException e) { 243 return getClass().getClassLoader().loadClass(name); 244 } 245 } 246 247 } 248 | Popular Tags |