001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * glassfish/bootstrap/legal/CDDLv1.0.txt or
009: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
010: * See the License for the specific language governing
011: * permissions and limitations under the License.
012: *
013: * When distributing Covered Code, include this CDDL
014: * HEADER in each file and include the License file at
015: * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
016: * add the following below this CDDL HEADER, with the
017: * fields enclosed by brackets "[]" replaced with your
018: * own identifying information: Portions Copyright [yyyy]
019: * [name of copyright owner]
020: *
021: * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
022: */
023:
024: package javax.el;
025:
026: import java.lang.reflect.Constructor;
027: import java.io.InputStream;
028: import java.io.File;
029: import java.io.FileInputStream;
030: import java.util.Properties;
031: import java.io.BufferedReader;
032: import java.io.InputStreamReader;
033:
034: class FactoryFinder {
035:
036: /**
037: * Creates an instance of the specified class using the specified
038: * <code>ClassLoader</code> object.
039: *
040: * @exception ELException if the given class could not be found
041: * or could not be instantiated
042: */
043: private static Object newInstance(String className,
044: ClassLoader classLoader, Properties properties) {
045: try {
046: Class spiClass;
047: if (classLoader == null) {
048: spiClass = Class.forName(className);
049: } else {
050: spiClass = classLoader.loadClass(className);
051: }
052: if (properties != null) {
053: Constructor constr = null;
054: try {
055: constr = spiClass.getConstructor(Properties.class);
056: } catch (Exception ex) {
057: }
058: if (constr != null) {
059: return constr.newInstance(properties);
060: }
061: }
062: return spiClass.newInstance();
063: } catch (ClassNotFoundException x) {
064: throw new ELException("Provider " + className
065: + " not found", x);
066: } catch (Exception x) {
067: throw new ELException("Provider " + className
068: + " could not be instantiated: " + x, x);
069: }
070: }
071:
072: /**
073: * Finds the implementation <code>Class</code> object for the given
074: * factory name, or if that fails, finds the <code>Class</code> object
075: * for the given fallback class name. The arguments supplied must be
076: * used in order. If using the first argument is successful, the second
077: * one will not be used.
078: * <P>
079: * This method is package private so that this code can be shared.
080: *
081: * @return the <code>Class</code> object of the specified message factory;
082: * may not be <code>null</code>
083: *
084: * @param factoryId the name of the factory to find, which is
085: * a system property
086: * @param fallbackClassName the implementation class name, which is
087: * to be used only if nothing else
088: * is found; <code>null</code> to indicate that
089: * there is no fallback class name
090: * @exception ELException if there is an error
091: */
092: static Object find(String factoryId, String fallbackClassName,
093: Properties properties) {
094: ClassLoader classLoader;
095: try {
096: classLoader = Thread.currentThread()
097: .getContextClassLoader();
098: } catch (Exception x) {
099: throw new ELException(x.toString(), x);
100: }
101:
102: String serviceId = "META-INF/services/" + factoryId;
103: // try to find services in CLASSPATH
104: try {
105: InputStream is = null;
106: if (classLoader == null) {
107: is = ClassLoader.getSystemResourceAsStream(serviceId);
108: } else {
109: is = classLoader.getResourceAsStream(serviceId);
110: }
111:
112: if (is != null) {
113: BufferedReader rd = new BufferedReader(
114: new InputStreamReader(is, "UTF-8"));
115:
116: String factoryClassName = rd.readLine();
117: rd.close();
118:
119: if (factoryClassName != null
120: && !"".equals(factoryClassName)) {
121: return newInstance(factoryClassName, classLoader,
122: properties);
123: }
124: }
125: } catch (Exception ex) {
126: }
127:
128: // try to read from $java.home/lib/el.properties
129: try {
130: String javah = System.getProperty("java.home");
131: String configFile = javah + File.separator + "lib"
132: + File.separator + "el.properties";
133: File f = new File(configFile);
134: if (f.exists()) {
135: Properties props = new Properties();
136: props.load(new FileInputStream(f));
137: String factoryClassName = props.getProperty(factoryId);
138: return newInstance(factoryClassName, classLoader,
139: properties);
140: }
141: } catch (Exception ex) {
142: }
143:
144: // Use the system property
145: try {
146: String systemProp = System.getProperty(factoryId);
147: if (systemProp != null) {
148: return newInstance(systemProp, classLoader, properties);
149: }
150: } catch (SecurityException se) {
151: }
152:
153: if (fallbackClassName == null) {
154: throw new ELException("Provider for " + factoryId
155: + " cannot be found", null);
156: }
157:
158: return newInstance(fallbackClassName, classLoader, properties);
159: }
160: }
|