KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > xml > bind > 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.bind;
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.bind.FactoryLoader");
47
48   private static HashMap JavaDoc<String JavaDoc,FactoryLoader> _factoryLoaders
49     = new HashMap JavaDoc<String JavaDoc,FactoryLoader>();
50
51   private String JavaDoc _factoryId;
52
53   private WeakHashMap JavaDoc<ClassLoader JavaDoc,Object JavaDoc[]> _providerMap
54     = new WeakHashMap JavaDoc<ClassLoader JavaDoc,Object 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     _factoryId = factoryId;
71   }
72
73   public Object JavaDoc newInstance(ClassLoader JavaDoc classLoader)
74     throws FactoryConfigurationError
75   {
76     String JavaDoc className = null;
77
78     className = System.getProperty(_factoryId);
79
80     if (className == null) {
81       String JavaDoc fileName =
82         System.getProperty("java.home") +
83         File.separatorChar +
84         "lib" +
85         File.separatorChar +
86         "jaxb.properties";
87
88       FileInputStream JavaDoc is = null;
89       try {
90         is = new FileInputStream JavaDoc(new File JavaDoc(fileName));
91
92         Properties JavaDoc props = new Properties JavaDoc();
93         props.load(is);
94
95         className = props.getProperty(_factoryId);
96
97       }
98       catch (IOException JavaDoc e) {
99         log.log(Level.FINER, "ignoring exception", e);
100       }
101       finally {
102         if (is != null)
103           try {
104             is.close();
105           } catch (IOException JavaDoc e) {
106             log.log(Level.FINER, "ignoring exception", e);
107           }
108       }
109     }
110
111     if (className == null) {
112       Object JavaDoc factory = createFactory("META-INF/services/"+_factoryId,
113                                      classLoader);
114       if (factory != null)
115         return factory;
116     }
117
118     if (className != null) {
119         
120       try {
121         return classLoader.loadClass(className).newInstance();
122       }
123       catch (Exception JavaDoc e) {
124         throw new FactoryConfigurationError(e);
125       }
126     }
127
128     return null;
129   }
130
131   public Object JavaDoc createFactory(String JavaDoc name, ClassLoader JavaDoc loader)
132   {
133     Object JavaDoc[] providers = getProviderList(name, loader);
134
135     for (int i = 0; i < providers.length; i++) {
136       Object JavaDoc factory;
137
138       factory = providers[i];
139
140       if (factory != null)
141         return factory;
142     }
143     
144     return null;
145   }
146   
147   private Object JavaDoc []getProviderList(String JavaDoc service, ClassLoader JavaDoc loader)
148   {
149
150     Object JavaDoc []providers = _providerMap.get(loader);
151
152     if (providers != null)
153       return providers;
154     
155     ArrayList JavaDoc<Object JavaDoc> list = new ArrayList JavaDoc<Object JavaDoc>();
156
157     try {
158       Enumeration JavaDoc e = loader.getResources(service);
159
160       while (e.hasMoreElements()) {
161         URL JavaDoc url = (URL JavaDoc) e.nextElement();
162
163         Object JavaDoc provider = loadProvider(url, loader);
164
165         if (provider != null)
166           list.add(provider);
167       }
168     } catch (Throwable JavaDoc e) {
169       log.log(Level.WARNING, e.toString(), e);
170     }
171
172     providers = new Object JavaDoc[list.size()];
173     list.toArray(providers);
174
175     _providerMap.put(loader, providers);
176     
177     return providers;
178   }
179
180   private Object JavaDoc loadProvider(URL JavaDoc url, ClassLoader JavaDoc loader)
181   {
182     InputStream JavaDoc is = null;
183     try {
184       is = url.openStream();
185       int ch;
186
187       while ((ch = is.read()) >= 0) {
188         if (Character.isWhitespace((char) ch)) {
189         }
190         else if (ch == '#') {
191           for (; ch >= 0 && ch != '\n' && ch != '\r'; ch = is.read()) {
192           }
193         }
194         else {
195           StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
196
197           for (;
198                ch >= 0 && ! Character.isWhitespace((char) ch);
199                ch = is.read()) {
200             sb.append((char) ch);
201           }
202
203           String JavaDoc className = sb.toString();
204
205           Class JavaDoc cl = Class.forName(className, false, loader);
206
207           return (Object JavaDoc) cl.newInstance();
208         }
209       }
210     } catch (Throwable JavaDoc e) {
211       log.log(Level.WARNING, e.toString(), e);
212     } finally {
213       try {
214         if (is != null)
215           is.close();
216       } catch (Throwable JavaDoc e) {
217       }
218     }
219
220     return null;
221   }
222 }
223
224
225
Popular Tags