KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > soap > FactoryFinder


1 /*
2  * $Id: FactoryFinder.java,v 1.6 2005/04/05 22:28:12 mk125090 Exp $
3  * $Revision: 1.6 $
4  * $Date: 2005/04/05 22:28:12 $
5  */

6
7 /*
8  * The contents of this file are subject to the terms
9  * of the Common Development and Distribution License
10  * (the License). You may not use this file except in
11  * compliance with the License.
12  *
13  * You can obtain a copy of the license at
14  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
15  * See the License for the specific language governing
16  * permissions and limitations under the License.
17  *
18  * When distributing Covered Code, include this CDDL
19  * Header Notice in each file and include the License file
20  * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
21  * If applicable, add the following below the CDDL Header,
22  * with the fields enclosed by brackets [] replaced by
23  * you own identifying information:
24  * "Portions Copyrighted [year] [name of copyright owner]"
25  *
26  * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
27  */

28
29 package javax.xml.soap;
30
31 import java.io.*;
32 import java.util.Properties JavaDoc;
33
34
35 class FactoryFinder {
36
37     /**
38      * Creates an instance of the specified class using the specified
39      * <code>ClassLoader</code> object.
40      *
41      * @exception SOAPException if the given class could not be found
42      * or could not be instantiated
43      */

44     private static Object JavaDoc newInstance(String JavaDoc className,
45                                       ClassLoader JavaDoc classLoader)
46         throws SOAPException JavaDoc
47     {
48         try {
49             Class JavaDoc spiClass;
50             if (classLoader == null) {
51                 spiClass = Class.forName(className);
52             } else {
53                 spiClass = classLoader.loadClass(className);
54             }
55             return spiClass.newInstance();
56         } catch (ClassNotFoundException JavaDoc x) {
57             throw new SOAPException JavaDoc(
58                 "Provider " + className + " not found", x);
59         } catch (Exception JavaDoc x) {
60             throw new SOAPException JavaDoc(
61                 "Provider " + className + " could not be instantiated: " + x,
62                 x);
63         }
64     }
65
66     /**
67      * Finds the implementation <code>Class</code> object for the given
68      * factory name, or null if that fails.
69      * <P>
70      * This method is package private so that this code can be shared.
71      *
72      * @return the <code>Class</code> object of the specified message factory;
73      * or <code>null</code>
74      *
75      * @param factoryId the name of the factory to find, which is
76      * a system property
77      * @exception SOAPException if there is a SOAP error
78      */

79     static Object JavaDoc find(String JavaDoc factoryId)
80         throws SOAPException JavaDoc
81     {
82         ClassLoader JavaDoc classLoader;
83         try {
84             classLoader = Thread.currentThread().getContextClassLoader();
85         } catch (Exception JavaDoc x) {
86             throw new SOAPException JavaDoc(x.toString(), x);
87         }
88
89         // Use the system property first
90
try {
91             String JavaDoc systemProp =
92                 System.getProperty( factoryId );
93             if( systemProp!=null) {
94                 return newInstance(systemProp, classLoader);
95             }
96         } catch (SecurityException JavaDoc se) {
97         }
98
99         // try to read from $java.home/lib/jaxm.properties
100
try {
101             String JavaDoc javah=System.getProperty( "java.home" );
102             String JavaDoc configFile = javah + File.separator +
103                 "lib" + File.separator + "jaxm.properties";
104             File f=new File( configFile );
105             if( f.exists()) {
106                 Properties JavaDoc props=new Properties JavaDoc();
107                 props.load( new FileInputStream(f));
108                 String JavaDoc factoryClassName = props.getProperty(factoryId);
109                 return newInstance(factoryClassName, classLoader);
110             }
111         } catch(Exception JavaDoc ex ) {
112         }
113
114         String JavaDoc serviceId = "META-INF/services/" + factoryId;
115         // try to find services in CLASSPATH
116
try {
117             InputStream is=null;
118             if (classLoader == null) {
119                 is=ClassLoader.getSystemResourceAsStream(serviceId);
120             } else {
121                 is=classLoader.getResourceAsStream(serviceId);
122             }
123         
124             if( is!=null ) {
125                 BufferedReader rd =
126                     new BufferedReader(new InputStreamReader(is, "UTF-8"));
127         
128                 String JavaDoc factoryClassName = rd.readLine();
129                 rd.close();
130
131                 if (factoryClassName != null &&
132                     ! "".equals(factoryClassName)) {
133                     return newInstance(factoryClassName, classLoader);
134                 }
135             }
136         } catch( Exception JavaDoc ex ) {
137         }
138
139         return null;
140     }
141
142     /**
143      * Finds the implementation <code>Class</code> object for the given
144      * factory name, or if that fails, finds the <code>Class</code> object
145      * for the given fallback class name. The arguments supplied must be
146      * used in order. If using the first argument is successful, the second
147      * one will not be used.
148      * <P>
149      * This method is package private so that this code can be shared.
150      *
151      * @return the <code>Class</code> object of the specified message factory;
152      * may not be <code>null</code>
153      *
154      * @param factoryId the name of the factory to find, which is
155      * a system property
156      * @param fallbackClassName the implementation class name, which is
157      * to be used only if nothing else
158      * is found; <code>null</code> to indicate that
159      * there is no fallback class name
160      * @exception SOAPException if there is a SOAP error
161      */

162     static Object JavaDoc find(String JavaDoc factoryId, String JavaDoc fallbackClassName)
163         throws SOAPException JavaDoc
164     {
165
166         Object JavaDoc obj = find(factoryId);
167         if (obj != null)
168             return obj;
169
170         ClassLoader JavaDoc classLoader;
171         try {
172             classLoader = Thread.currentThread().getContextClassLoader();
173         } catch (Exception JavaDoc x) {
174             throw new SOAPException JavaDoc(x.toString(), x);
175         }
176
177         if (fallbackClassName == null) {
178             throw new SOAPException JavaDoc(
179                 "Provider for " + factoryId + " cannot be found", null);
180         }
181
182         return newInstance(fallbackClassName, classLoader);
183     }
184 }
185
Popular Tags