1 29 30 package javax.xml.bind; 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.net.URL ; 36 import java.util.ArrayList ; 37 import java.util.Enumeration ; 38 import java.util.HashMap ; 39 import java.util.Properties ; 40 import java.util.WeakHashMap ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 class FactoryLoader { 45 private static Logger log = 46 Logger.getLogger("javax.xml.bind.FactoryLoader"); 47 48 private static HashMap <String ,FactoryLoader> _factoryLoaders 49 = new HashMap <String ,FactoryLoader>(); 50 51 private String _factoryId; 52 53 private WeakHashMap <ClassLoader ,Object []> _providerMap 54 = new WeakHashMap <ClassLoader ,Object []>(); 55 56 public static FactoryLoader getFactoryLoader(String factoryId) { 57 58 FactoryLoader ret = _factoryLoaders.get(factoryId); 59 60 if (ret == null) { 61 ret = new FactoryLoader(factoryId); 62 _factoryLoaders.put(factoryId, ret); 63 } 64 65 return ret; 66 } 67 68 private FactoryLoader(String factoryId) 69 { 70 _factoryId = factoryId; 71 } 72 73 public Object newInstance(ClassLoader classLoader) 74 throws FactoryConfigurationError 75 { 76 String className = null; 77 78 className = System.getProperty(_factoryId); 79 80 if (className == null) { 81 String fileName = 82 System.getProperty("java.home") + 83 File.separatorChar + 84 "lib" + 85 File.separatorChar + 86 "jaxb.properties"; 87 88 FileInputStream is = null; 89 try { 90 is = new FileInputStream (new File (fileName)); 91 92 Properties props = new Properties (); 93 props.load(is); 94 95 className = props.getProperty(_factoryId); 96 97 } 98 catch (IOException e) { 99 log.log(Level.FINER, "ignoring exception", e); 100 } 101 finally { 102 if (is != null) 103 try { 104 is.close(); 105 } catch (IOException e) { 106 log.log(Level.FINER, "ignoring exception", e); 107 } 108 } 109 } 110 111 if (className == null) { 112 Object factory = createFactory("META-INF/services/"+_factoryId, 113 classLoader); 114 if (factory != null) 115 return factory; 116 } 117 118 if (className != null) { 119 120 try { 121 return classLoader.loadClass(className).newInstance(); 122 } 123 catch (Exception e) { 124 throw new FactoryConfigurationError(e); 125 } 126 } 127 128 return null; 129 } 130 131 public Object createFactory(String name, ClassLoader loader) 132 { 133 Object [] providers = getProviderList(name, loader); 134 135 for (int i = 0; i < providers.length; i++) { 136 Object factory; 137 138 factory = providers[i]; 139 140 if (factory != null) 141 return factory; 142 } 143 144 return null; 145 } 146 147 private Object []getProviderList(String service, ClassLoader loader) 148 { 149 150 Object []providers = _providerMap.get(loader); 151 152 if (providers != null) 153 return providers; 154 155 ArrayList <Object > list = new ArrayList <Object >(); 156 157 try { 158 Enumeration e = loader.getResources(service); 159 160 while (e.hasMoreElements()) { 161 URL url = (URL ) e.nextElement(); 162 163 Object provider = loadProvider(url, loader); 164 165 if (provider != null) 166 list.add(provider); 167 } 168 } catch (Throwable e) { 169 log.log(Level.WARNING, e.toString(), e); 170 } 171 172 providers = new Object [list.size()]; 173 list.toArray(providers); 174 175 _providerMap.put(loader, providers); 176 177 return providers; 178 } 179 180 private Object loadProvider(URL url, ClassLoader loader) 181 { 182 InputStream is = null; 183 try { 184 is = url.openStream(); 185 int ch; 186 187 while ((ch = is.read()) >= 0) { 188 if (Character.isWhitespace((char) ch)) { 189 } 190 else if (ch == '#') { 191 for (; ch >= 0 && ch != '\n' && ch != '\r'; ch = is.read()) { 192 } 193 } 194 else { 195 StringBuilder sb = new StringBuilder (); 196 197 for (; 198 ch >= 0 && ! Character.isWhitespace((char) ch); 199 ch = is.read()) { 200 sb.append((char) ch); 201 } 202 203 String className = sb.toString(); 204 205 Class cl = Class.forName(className, false, loader); 206 207 return (Object ) cl.newInstance(); 208 } 209 } 210 } catch (Throwable e) { 211 log.log(Level.WARNING, e.toString(), e); 212 } finally { 213 try { 214 if (is != null) 215 is.close(); 216 } catch (Throwable e) { 217 } 218 } 219 220 return null; 221 } 222 } 223 224 225 | Popular Tags |