1 28 29 package org.jibx.runtime; 30 31 import java.lang.reflect.Field ; 32 import java.lang.reflect.InvocationTargetException ; 33 import java.lang.reflect.Method ; 34 35 42 43 public abstract class BindingDirectory 44 { 45 46 public static final String BINDINGLIST_NAME = "JiBX_bindingList"; 47 48 49 public static final String BINDINGFACTORY_PREFIX = "JiBX_"; 50 51 52 public static final String BINDINGFACTORY_SUFFIX = "Factory"; 53 54 55 public static final String FACTORY_INSTMETHOD = "getInstance"; 56 57 58 public static final Class [] EMPTY_ARGS = new Class [0]; 59 60 68 private static String getBindingList(Class clas)throws JiBXException { 69 try { 70 Field field = clas.getDeclaredField(BINDINGLIST_NAME); 71 try { 72 field.setAccessible(true); 74 } catch (Exception e) { } 75 return (String )field.get(null); 76 } catch (NoSuchFieldException e) { 77 throw new JiBXException 78 ("Unable to access binding information for class " + 79 clas.getName() + 80 "\nMake sure the binding has been compiled", e); 81 } catch (IllegalAccessException e) { 82 throw new JiBXException 83 ("Error in added code for class " + clas.getName() + 84 "Please report this to the JiBX developers", e); 85 } 86 } 87 88 98 private static IBindingFactory getFactoryFromName(String name, Class clas) 99 throws JiBXException { 100 Throwable ex = null; 101 Object result = null; 102 IBindingFactory ifact = null; 103 try { 104 Class factory = clas.getClassLoader().loadClass(name); 105 Method method = factory.getMethod(FACTORY_INSTMETHOD, EMPTY_ARGS); 106 result = method.invoke(null, null); 107 } catch (SecurityException e) { 108 ex = e; 109 } catch (ClassNotFoundException e) { 110 ex = e; 111 } catch (NoSuchMethodException e) { 112 ex = e; 113 } catch (IllegalAccessException e) { 114 ex = e; 115 } catch (InvocationTargetException e) { 116 ex = e; 117 } finally { 118 if (ex == null) { 119 if (result instanceof IBindingFactory) { 120 ifact = (IBindingFactory)result; 121 int diff = ifact.getCompilerVersion() ^ 122 IBindingFactory.CURRENT_VERSION_NUMBER; 123 if ((diff & IBindingFactory.COMPATIBLE_VERSION_MASK) != 0) { 124 throw new JiBXException 125 ("Binding information for class " + clas.getName() + 126 " must be recompiled with current binding " + 127 "compiler (compiled with " + 128 ifact.getCompilerDistribution() + ", runtime is " + 129 IBindingFactory.CURRENT_VERSION_NAME + ")"); 130 } 131 } else { 132 throw new JiBXException 133 ("Binding information for class " + clas.getName() + 134 " must be regenerated with current binding " + 135 "compiler"); 136 } 137 } else { 138 throw new JiBXException 139 ("Unable to access binding information for class " + 140 clas.getName() + "\nMake sure classes generated by the " + 141 "binding compiler are available at runtime", ex); 142 } 143 } 144 return ifact; 145 } 146 147 157 public static IBindingFactory getFactory(String name, Class clas) 158 throws JiBXException { 159 String list = getBindingList(clas); 160 String match = BINDINGFACTORY_PREFIX + name + 161 BINDINGFACTORY_SUFFIX + '|'; 162 int index = list.indexOf(match); 163 if (index >= 0) { 164 int mark = list.lastIndexOf('|', index); 165 String fname = 166 list.substring(mark+1, index + match.length() - 1); 167 mark = fname.indexOf('='); 168 if (mark >= 0) { 169 fname = fname.substring(0, mark); 170 } 171 return getFactoryFromName(fname, clas); 172 } else { 173 throw new JiBXException("Binding " + name + 174 " not found for class " + clas.getName()); 175 } 176 } 177 178 188 public static IBindingFactory getFactory(Class clas) throws JiBXException { 189 String list = getBindingList(clas); 190 if (list != null && list.length() > 2) { 191 String fact = list.substring(1, list.length()-1); 192 if (fact.indexOf('|') < 0) { 193 return getFactoryFromName(fact, clas); 194 } 195 } 196 throw new JiBXException("Multiple bindings defined for class " + 197 clas.getName()); 198 } 199 } | Popular Tags |