1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.xbean.spring.context.v2c;
18
19 import java.beans.BeanInfo;
20 import java.beans.IntrospectionException;
21 import java.beans.Introspector;
22 import java.beans.PropertyDescriptor;
23
24 import org.apache.xbean.spring.context.impl.PropertyEditorHelper;
25 import org.apache.xbean.spring.context.impl.QNameReflectionHelper;
26 import org.springframework.beans.factory.BeanDefinitionStoreException;
27 import org.springframework.beans.factory.config.BeanDefinition;
28 import org.springframework.beans.factory.support.AbstractBeanDefinition;
29 import org.springframework.beans.factory.xml.XmlReaderContext;
30 import org.w3c.dom.Element;
31
32 public class XBeanQNameHelper {
33
34 private XmlReaderContext readerContext;
35
36 private boolean qnameIsOnClassPath;
37
38 private boolean initQNameOnClassPath;
39
40 public XBeanQNameHelper(XmlReaderContext readerContext) {
41 this.readerContext = readerContext;
42 }
43
44
45
46
47
48
49
50 public void coerceNamespaceAwarePropertyValues(BeanDefinition definition, Element element) {
51 if (definition instanceof AbstractBeanDefinition && isQnameIsOnClassPath()) {
52 AbstractBeanDefinition bd = (AbstractBeanDefinition) definition;
53
54 BeanInfo beanInfo = getBeanInfo(bd.getBeanClassName());
55 if (beanInfo != null) {
56 PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
57 for (int i = 0; i < descriptors.length; i++) {
58 QNameReflectionHelper.coerceNamespaceAwarePropertyValues(bd, element, descriptors, i);
59 }
60 }
61 }
62 }
63
64 public BeanInfo getBeanInfo(String className) throws BeanDefinitionStoreException {
65 if (className == null) {
66 return null;
67 }
68
69 BeanInfo info = null;
70 Class type = null;
71 try {
72 type = loadClass(className);
73 }
74 catch (ClassNotFoundException e) {
75 throw new BeanDefinitionStoreException("Failed to load type: " + className + ". Reason: " + e, e);
76 }
77 try {
78 info = Introspector.getBeanInfo(type);
79 }
80 catch (IntrospectionException e) {
81 throw new BeanDefinitionStoreException("Failed to introspect type: " + className + ". Reason: " + e, e);
82 }
83 return info;
84 }
85
86
87
88
89
90 protected Class loadClass(String name) throws ClassNotFoundException {
91 ClassLoader beanClassLoader = readerContext.getReader().getBeanClassLoader();
92 if (beanClassLoader != null) {
93 try {
94 return beanClassLoader.loadClass(name);
95 }
96 catch (ClassNotFoundException e) {
97 }
98 }
99 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
100 if (contextClassLoader != null) {
101 try {
102 return contextClassLoader.loadClass(name);
103 }
104 catch (ClassNotFoundException e) {
105 }
106 }
107 return getClass().getClassLoader().loadClass(name);
108 }
109
110 protected boolean isQnameIsOnClassPath() {
111 if (initQNameOnClassPath == false) {
112 qnameIsOnClassPath = PropertyEditorHelper.loadClass("javax.xml.namespace.QName") != null;
113 initQNameOnClassPath = true;
114 }
115 return qnameIsOnClassPath;
116 }
117
118 }