001: /*
002: * $Id: ClassStuff.java,v 1.4 2001/11/29 22:38:26 db Exp $
003: * Copyright (C) 2001 David Brownell
004: *
005: * This file is part of GNU JAXP, a library.
006: *
007: * GNU JAXP is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * GNU JAXP is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * As a special exception, if you link this library with other files to
022: * produce an executable, this library does not by itself cause the
023: * resulting executable to be covered by the GNU General Public License.
024: * This exception does not however invalidate any other reasons why the
025: * executable file might be covered by the GNU General Public License.
026: */
027:
028: package javax.xml.parsers;
029:
030: import java.io.BufferedReader;
031: import java.io.File;
032: import java.io.FileInputStream;
033: import java.io.InputStream;
034: import java.io.InputStreamReader;
035: import java.lang.reflect.Method;
036: import java.util.Properties;
037:
038: // $Id: ClassStuff.java,v 1.4 2001/11/29 22:38:26 db Exp $
039:
040: /**
041: * Package-private utility methods for sharing
042: * magic related to class loading.
043: * NOTE: This is cloned in javax.xml.transform,
044: * where a different exception is thrown (bleech).
045: * Keep changes to the two copies in sync.
046: *
047: * @author David Brownell
048: * @version $Id: ClassStuff.java,v 1.4 2001/11/29 22:38:26 db Exp $
049: */
050: final class ClassStuff {
051: private ClassStuff() {
052: }
053:
054: /**
055: * Get the default factory using the four-stage defaulting
056: * mechanism defined by JAXP.
057: */
058: static Object createFactory(String label, String defaultClass)
059: throws FactoryConfigurationError {
060: String name = null;
061: ClassLoader loader = null;
062:
063: // figure out which class loader to use.
064: // source compiles on jdk 1.1, but works with jdk 1.2+ security
065: try {
066: Method m = null;
067:
068: // Can we use JDK 1.2 APIs? Preferred: security policies apply.
069: m = Thread.class.getMethod("getContextClassLoader", null);
070: loader = (ClassLoader) m.invoke(Thread.currentThread(),
071: null);
072: } catch (NoSuchMethodException e) {
073: // Assume that we are running JDK 1.1; use current ClassLoader
074: loader = ClassStuff.class.getClassLoader();
075: } catch (Exception e) {
076: // "should not happen"
077: throw new UnknownError(e.getMessage());
078: }
079:
080: // 1. Check System Property
081: // ... normally fails in applet environments
082: try {
083: name = System.getProperty(label);
084: } catch (SecurityException e) { /* IGNORE */
085: }
086:
087: // 2. Check in $JAVA_HOME/lib/jaxp.properties
088: try {
089: if (name == null) {
090: String javaHome;
091: File file;
092:
093: javaHome = System.getProperty("java.home");
094: file = new File(new File(javaHome, "lib"),
095: "jaxp.properties");
096: if (file.exists() == true) {
097: FileInputStream in = new FileInputStream(file);
098: Properties props = new Properties();
099:
100: props.load(in);
101: name = props.getProperty(label);
102: in.close();
103: }
104: }
105: } catch (Exception e) { /* IGNORE */
106: }
107:
108: // 3. Check Services API
109: if (name == null) {
110: try {
111: String service = "META-INF/services/" + label;
112: InputStream in;
113: BufferedReader reader;
114:
115: if (loader == null)
116: in = ClassLoader.getSystemResourceAsStream(service);
117: else
118: in = loader.getResourceAsStream(service);
119: if (in != null) {
120: reader = new BufferedReader(new InputStreamReader(
121: in, "UTF8"));
122: name = reader.readLine();
123: in.close();
124: }
125: } catch (Exception e2) { /* IGNORE */
126: }
127: }
128:
129: // 4. Distro-specific fallback
130: if (name == null)
131: name = defaultClass;
132:
133: // Instantiate!
134: try {
135: Class klass;
136:
137: if (loader == null)
138: klass = Class.forName(name);
139: else
140: klass = loader.loadClass(name);
141: return klass.newInstance();
142:
143: } catch (ClassNotFoundException e) {
144: throw new FactoryConfigurationError(e, "Factory class "
145: + name + " not found");
146: } catch (IllegalAccessException e) {
147: throw new FactoryConfigurationError(e, "Factory class "
148: + name + " found but cannot be loaded");
149: } catch (InstantiationException e) {
150: throw new FactoryConfigurationError(e, "Factory class "
151: + name + " loaded but cannot be instantiated"
152: + " ((no default constructor?)");
153: }
154: }
155: }
|