KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package javax.xml.stream;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 /**
32  * Utility class that locates one of the many JSR-173 factories. This class is
33  * not public since it is not part of the JSR-173 specification.
34  *
35  * @author Jason T. Greene
36  * @version $Id: FactoryLocator.java 37459 2005-10-30 00:04:02Z starksm $
37  */

38 class FactoryLocator
39 {
40
41    private static final String JavaDoc JAVA_HOME = System.getProperty("java.home");
42
43    private static final String JavaDoc PROPERTY_FILE = JAVA_HOME + File.pathSeparator + "lib" + File.pathSeparator + "xml.stream.properties";
44
45    private static final String JavaDoc SERVICES_PATH = "META-INF/services/";
46
47    private static Object JavaDoc newInstance(String JavaDoc className, ClassLoader JavaDoc classLoader)
48    {
49       try
50       {
51          return classLoader.loadClass(className).newInstance();
52       }
53       catch (ClassNotFoundException JavaDoc e)
54       {
55          throw new FactoryConfigurationError("Could not locate provider [" + className + "]", e);
56       }
57       catch (Exception JavaDoc e)
58       {
59          throw new FactoryConfigurationError("Error creating provider [" + className + "]", e);
60       }
61    }
62
63    /**
64     * Gets property from JAVA_HOME/lib/xml.stream.properties file.
65     */

66    private static String JavaDoc getPropertyFromFile(String JavaDoc property)
67    {
68       File JavaDoc file = new File JavaDoc(PROPERTY_FILE);
69
70       if (!file.exists())
71          return null;
72
73       Properties JavaDoc properties = new Properties JavaDoc();
74       try
75       {
76          properties.load(new FileInputStream JavaDoc(file));
77          return properties.getProperty(property);
78       }
79       catch (Exception JavaDoc e)
80       {
81          // Eat
82
return null;
83       }
84    }
85
86    private static String JavaDoc getPropertyFromServices(String JavaDoc property, ClassLoader JavaDoc classLoader)
87    {
88       String JavaDoc resource = SERVICES_PATH + property;
89       InputStream JavaDoc stream = classLoader.getResourceAsStream(property);
90
91       if (stream == null)
92          return null;
93       try
94       {
95          BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(stream, "UTF-8"));
96          return reader.readLine();
97       }
98       catch (Exception JavaDoc e)
99       {
100          // Eat
101
return null;
102       }
103    }
104
105    /**
106     * Locate a factory following the rules in JSR 173 section 4.5.1 using the
107     * current classloader.
108     *
109     * @see FactoryLocator#find(String, String, ClassLoader)
110     */

111    static Object JavaDoc find(String JavaDoc factoryId, String JavaDoc defaultFactory) throws FactoryConfigurationError
112    {
113       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
114       return find(factoryId, defaultFactory, loader);
115    }
116
117    /**
118     * Locate a factory following the rules in JSR 173 section 4.5.1 using the
119     * specified classloader.
120     *
121     * @param factoryId the factory identifier
122     * @param defaultFactory the default factory class to load if the identifier
123     * returns nothing
124     * @param classLoader the classloader to load and search in
125     * @return the factory object
126     * @throws FactoryConfigurationError
127     */

128    static Object JavaDoc find(String JavaDoc factoryId, String JavaDoc defaultFactory, ClassLoader JavaDoc classLoader) throws FactoryConfigurationError
129    {
130       if (classLoader == null)
131          throw new FactoryConfigurationError("Could not determine classloader.");
132
133       String JavaDoc value = System.getProperty(factoryId);
134       if (value != null)
135          return newInstance(value, classLoader);
136
137       value = getPropertyFromFile(factoryId);
138       if (value != null)
139          return newInstance(value, classLoader);
140
141       value = getPropertyFromServices(factoryId, classLoader);
142       if (value != null)
143          return newInstance(value, classLoader);
144
145       return newInstance(defaultFactory, classLoader);
146    }
147 }
148
Popular Tags