1 5 6 package javax.xml.ws.spi; 7 8 import java.io.InputStream ; 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 12 import java.util.Properties ; 13 import java.io.BufferedReader ; 14 import java.io.InputStreamReader ; 15 import javax.xml.ws.WebServiceException; 16 17 class FactoryFinder { 18 19 26 private static Object newInstance(String className, 27 ClassLoader classLoader) 28 { 29 try { 30 Class spiClass; 31 if (classLoader == null) { 32 spiClass = Class.forName(className); 33 } else { 34 spiClass = classLoader.loadClass(className); 35 } 36 return spiClass.newInstance(); 37 } catch (ClassNotFoundException x) { 38 throw new WebServiceException( 39 "Provider " + className + " not found", x); 40 } catch (Exception x) { 41 throw new WebServiceException( 42 "Provider " + className + " could not be instantiated: " + x, 43 x); 44 } 45 } 46 47 67 static Object find(String factoryId, String fallbackClassName) 68 { 69 ClassLoader classLoader; 70 try { 71 classLoader = Thread.currentThread().getContextClassLoader(); 72 } catch (Exception x) { 73 throw new WebServiceException(x.toString(), x); 74 } 75 76 String serviceId = "META-INF/services/" + factoryId; 77 try { 79 InputStream is=null; 80 if (classLoader == null) { 81 is=ClassLoader.getSystemResourceAsStream(serviceId); 82 } else { 83 is=classLoader.getResourceAsStream(serviceId); 84 } 85 86 if( is!=null ) { 87 BufferedReader rd = 88 new BufferedReader (new InputStreamReader (is, "UTF-8")); 89 90 String factoryClassName = rd.readLine(); 91 rd.close(); 92 93 if (factoryClassName != null && 94 ! "".equals(factoryClassName)) { 95 return newInstance(factoryClassName, classLoader); 96 } 97 } 98 } catch( Exception ex ) { 99 } 100 101 102 try { 104 String javah=System.getProperty( "java.home" ); 105 String configFile = javah + File.separator + 106 "lib" + File.separator + "jaxws.properties"; 107 File f=new File ( configFile ); 108 if( f.exists()) { 109 Properties props=new Properties (); 110 props.load( new FileInputStream (f)); 111 String factoryClassName = props.getProperty(factoryId); 112 return newInstance(factoryClassName, classLoader); 113 } 114 } catch(Exception ex ) { 115 } 116 117 118 try { 120 String systemProp = 121 System.getProperty( factoryId ); 122 if( systemProp!=null) { 123 return newInstance(systemProp, classLoader); 124 } 125 } catch (SecurityException se) { 126 } 127 128 if (fallbackClassName == null) { 129 throw new WebServiceException( 130 "Provider for " + factoryId + " cannot be found", null); 131 } 132 133 return newInstance(fallbackClassName, classLoader); 134 } 135 } 136 | Popular Tags |