1 16 17 package javax.xml.rpc; 18 19 import java.io.BufferedReader ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.InputStream ; 23 import java.io.InputStreamReader ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 import java.util.Properties ; 27 28 38 class FactoryFinder { 39 40 private static final boolean debug = false; 41 42 private static void debugPrintln(String msg) { 43 if (debug) { 44 System.err.println("JAXRPC: " + msg); 45 } 46 } 47 48 56 private static ClassLoader findClassLoader() 57 throws ConfigurationError 58 { 59 Method m = null; 60 61 try { 62 m = Thread .class.getMethod("getContextClassLoader", null); 63 } catch (NoSuchMethodException e) { 64 debugPrintln("assuming JDK 1.1"); 66 return FactoryFinder .class.getClassLoader(); 67 } 68 69 try { 70 return (ClassLoader ) m.invoke(Thread.currentThread(), null); 71 } catch (IllegalAccessException e) { 72 throw new ConfigurationError("Unexpected IllegalAccessException", 74 e); 75 } catch (InvocationTargetException e) { 76 throw new ConfigurationError("Unexpected InvocationTargetException", 78 e); 79 } 80 } 81 82 95 private static Object newInstance(String className, 96 ClassLoader classLoader) 97 throws ConfigurationError 98 { 99 try { 100 if (classLoader != null) { 101 try { 102 return classLoader.loadClass(className).newInstance (); 103 } catch (ClassNotFoundException x) { 104 } 106 } 107 return Class.forName(className).newInstance(); 108 } catch (ClassNotFoundException x) { 109 throw new ConfigurationError( 110 "Provider " + className + " not found", x); 111 } catch (Exception x) { 112 throw new ConfigurationError( 113 "Provider " + className + " could not be instantiated: " + x, 114 x); 115 } 116 } 117 118 132 static Object find(String factoryId, String fallbackClassName) 133 throws ConfigurationError 134 { 135 debugPrintln("debug is on"); 136 137 ClassLoader classLoader = findClassLoader(); 138 139 try { 141 String systemProp = 142 System.getProperty( factoryId ); 143 if( systemProp!=null) { 144 debugPrintln("found system property " + systemProp); 145 return newInstance(systemProp, classLoader); 146 } 147 } catch (SecurityException se) { 148 } 149 150 try { 152 String javah=System.getProperty( "java.home" ); 153 String configFile = javah + File.separator + 154 "lib" + File.separator + "jaxrpc.properties"; 155 File f=new File ( configFile ); 156 if( f.exists()) { 157 Properties props=new Properties (); 158 props.load( new FileInputStream (f)); 159 String factoryClassName = props.getProperty(factoryId); 160 debugPrintln("found java.home property " + factoryClassName); 161 return newInstance(factoryClassName, classLoader); 162 } 163 } catch(Exception ex ) { 164 if( debug ) ex.printStackTrace(); 165 } 166 167 String serviceId = "META-INF/services/" + factoryId; 168 try { 170 InputStream is=null; 171 if (classLoader == null) { 172 is=ClassLoader.getSystemResourceAsStream( serviceId ); 173 } else { 174 is=classLoader.getResourceAsStream( serviceId ); 175 } 176 177 if( is!=null ) { 178 debugPrintln("found " + serviceId); 179 180 BufferedReader rd; 197 try { 198 rd = new BufferedReader (new InputStreamReader (is, "UTF-8")); 199 } catch (java.io.UnsupportedEncodingException e) { 200 rd = new BufferedReader (new InputStreamReader (is)); 201 } 202 203 String factoryClassName = rd.readLine(); 204 rd.close(); 205 206 if (factoryClassName != null && 207 ! "".equals(factoryClassName)) { 208 debugPrintln("loaded from services: " + factoryClassName); 209 return newInstance(factoryClassName, classLoader); 210 } 211 } 212 } catch( Exception ex ) { 213 if( debug ) ex.printStackTrace(); 214 } 215 216 if (fallbackClassName == null) { 217 throw new ConfigurationError( 218 "Provider for " + factoryId + " cannot be found", null); 219 } 220 221 debugPrintln("loaded from fallback value: " + fallbackClassName); 222 return newInstance(fallbackClassName, classLoader); 223 } 224 225 static class ConfigurationError extends Error { 226 229 private Exception exception; 230 231 238 ConfigurationError(String msg, Exception x) { 239 super(msg); 240 this.exception = x; 241 } 242 243 Exception getException() { 244 return exception; 245 } 246 } 247 } 248 | Popular Tags |