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