KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > stream > FactoryLoader


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Adam Megacz
28  */

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