View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.xbean.spring.context.impl;
18  
19  import org.springframework.beans.factory.BeanDefinitionStoreException;
20  import org.springframework.beans.factory.support.AbstractBeanDefinition;
21  import org.w3c.dom.Element;
22  
23  import java.beans.PropertyDescriptor;
24  import java.lang.reflect.Method;
25  
26  /**
27   * To avoid a runtime dependency on the QName class lets use reflection to
28   * process QName instances.
29   * 
30   * @version $Revision: 1.1 $
31   */
32  public class QNameReflectionHelper {
33  
34      protected static Method coerceMethod;
35      protected static Method createMethod;
36  
37      public static void coerceNamespaceAwarePropertyValues(AbstractBeanDefinition beanDefinition, Element element,
38              PropertyDescriptor[] descriptors, int index) {
39          QNameReflectionParams params = new QNameReflectionParams(beanDefinition, element, descriptors, index);
40          if (coerceMethod == null) {
41              coerceMethod = findMethod("coerceQNamePropertyValues");
42          }
43          if (coerceMethod != null) {
44              try {
45                  coerceMethod.invoke(null, new Object[] { params });
46              }
47              catch (Exception e) {
48                  throw new BeanDefinitionStoreException("Failed to invoke method: " + coerceMethod + " via reflection: " + e,
49                          e);
50              }
51          }
52      }
53      
54      public static Object createQName(Element element, String text) {
55          if (createMethod == null) {
56              createMethod = findMethod("createQName");
57          }
58          if (createMethod != null) {
59              try {
60                  return createMethod.invoke(null, new Object[] { element, text });
61              }
62              catch (Exception e) {
63                  throw new BeanDefinitionStoreException("Failed to invoke method: " + createMethod + " via reflection: " + e,
64                          e);
65              }
66          }
67          return null;
68      }
69  
70      protected static Method findMethod(String name) {
71          try {
72              Class type = PropertyEditorHelper.loadClass("org.apache.xbean.spring.context.impl.QNameHelper");
73              if (type != null) {
74                  Method[] methods = type.getMethods();
75                  for (int i = 0; i < methods.length; i++) {
76                      Method method = methods[i];
77                      if (method.getName().equals(name)) {
78                          return method;
79                      }
80                  }
81              }
82          } catch (Throwable t) {
83              // Ignore, this is usually because QName method is not in the classpath 
84          }
85          return null;
86      }
87  
88  }