KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > ws > spi > FactoryFinder


1 /*
2  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4  */

5
6 package javax.xml.ws.spi;
7
8 import java.io.InputStream JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11
12 import java.util.Properties JavaDoc;
13 import java.io.BufferedReader JavaDoc;
14 import java.io.InputStreamReader JavaDoc;
15 import javax.xml.ws.WebServiceException;
16
17 class FactoryFinder {
18
19     /**
20      * Creates an instance of the specified class using the specified
21      * <code>ClassLoader</code> object.
22      *
23      * @exception WebServiceException if the given class could not be found
24      * or could not be instantiated
25      */

26     private static Object JavaDoc newInstance(String JavaDoc className,
27                                       ClassLoader JavaDoc classLoader)
28     {
29         try {
30             Class JavaDoc 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 JavaDoc x) {
38             throw new WebServiceException(
39                 "Provider " + className + " not found", x);
40         } catch (Exception JavaDoc x) {
41             throw new WebServiceException(
42                 "Provider " + className + " could not be instantiated: " + x,
43                 x);
44         }
45     }
46
47     /**
48      * Finds the implementation <code>Class</code> object for the given
49      * factory name, or if that fails, finds the <code>Class</code> object
50      * for the given fallback class name. The arguments supplied MUST be
51      * used in order. If using the first argument is successful, the second
52      * one will not be used.
53      * <P>
54      * This method is package private so that this code can be shared.
55      *
56      * @return the <code>Class</code> object of the specified message factory;
57      * may not be <code>null</code>
58      *
59      * @param factoryId the name of the factory to find, which is
60      * a system property
61      * @param fallbackClassName the implementation class name, which is
62      * to be used only if nothing else
63      * is found; <code>null</code> to indicate that
64      * there is no fallback class name
65      * @exception WebServiceException if there is an error
66      */

67     static Object JavaDoc find(String JavaDoc factoryId, String JavaDoc fallbackClassName)
68     {
69         ClassLoader JavaDoc classLoader;
70         try {
71             classLoader = Thread.currentThread().getContextClassLoader();
72         } catch (Exception JavaDoc x) {
73             throw new WebServiceException(x.toString(), x);
74         }
75
76         String JavaDoc serviceId = "META-INF/services/" + factoryId;
77         // try to find services in CLASSPATH
78
try {
79             InputStream JavaDoc 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 JavaDoc rd =
88                     new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is, "UTF-8"));
89         
90                 String JavaDoc factoryClassName = rd.readLine();
91                 rd.close();
92
93                 if (factoryClassName != null &&
94                     ! "".equals(factoryClassName)) {
95                     return newInstance(factoryClassName, classLoader);
96                 }
97             }
98         } catch( Exception JavaDoc ex ) {
99         }
100         
101
102         // try to read from $java.home/lib/jaxws.properties
103
try {
104             String JavaDoc javah=System.getProperty( "java.home" );
105             String JavaDoc configFile = javah + File.separator +
106                 "lib" + File.separator + "jaxws.properties";
107             File JavaDoc f=new File JavaDoc( configFile );
108             if( f.exists()) {
109                 Properties JavaDoc props=new Properties JavaDoc();
110                 props.load( new FileInputStream JavaDoc(f));
111                 String JavaDoc factoryClassName = props.getProperty(factoryId);
112                 return newInstance(factoryClassName, classLoader);
113             }
114         } catch(Exception JavaDoc ex ) {
115         }
116
117
118         // Use the system property
119
try {
120             String JavaDoc systemProp =
121                 System.getProperty( factoryId );
122             if( systemProp!=null) {
123                 return newInstance(systemProp, classLoader);
124             }
125         } catch (SecurityException JavaDoc 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