mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-22 03:54:33 +02:00
6657673: Issues with JAXP
Reviewed-by: alanb, lancea, ahgross, mullan
This commit is contained in:
parent
7c84115ffb
commit
8b12f5abcc
111 changed files with 1134 additions and 3199 deletions
|
@ -54,9 +54,10 @@ import java.lang.reflect.InvocationTargetException;
|
|||
* including versions of Java 2.</p>
|
||||
*
|
||||
* @author Edwin Goei, David Brownell
|
||||
* @version 2.0.1 (sax2r2)
|
||||
*/
|
||||
class NewInstance {
|
||||
|
||||
private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal";
|
||||
/**
|
||||
* Creates a new instance of the specified class name
|
||||
*
|
||||
|
@ -66,8 +67,16 @@ class NewInstance {
|
|||
throws ClassNotFoundException, IllegalAccessException,
|
||||
InstantiationException
|
||||
{
|
||||
// make sure we have access to restricted packages
|
||||
boolean internal = false;
|
||||
if (System.getSecurityManager() != null) {
|
||||
if (className != null && className.startsWith(DEFAULT_PACKAGE)) {
|
||||
internal = true;
|
||||
}
|
||||
}
|
||||
|
||||
Class driverClass;
|
||||
if (classLoader == null) {
|
||||
if (classLoader == null || internal) {
|
||||
driverClass = Class.forName(className);
|
||||
} else {
|
||||
driverClass = classLoader.loadClass(className);
|
||||
|
@ -75,29 +84,4 @@ class NewInstance {
|
|||
return driverClass.newInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Figure out which ClassLoader to use. For JDK 1.2 and later use
|
||||
* the context ClassLoader.
|
||||
*/
|
||||
static ClassLoader getClassLoader ()
|
||||
{
|
||||
Method m = null;
|
||||
|
||||
try {
|
||||
m = Thread.class.getMethod("getContextClassLoader", (Class[]) null);
|
||||
} catch (NoSuchMethodException e) {
|
||||
// Assume that we are running JDK 1.1, use the current ClassLoader
|
||||
return NewInstance.class.getClassLoader();
|
||||
}
|
||||
|
||||
try {
|
||||
return (ClassLoader) m.invoke(Thread.currentThread(), (Object[]) null);
|
||||
} catch (IllegalAccessException e) {
|
||||
// assert(false)
|
||||
throw new UnknownError(e.getMessage());
|
||||
} catch (InvocationTargetException e) {
|
||||
// assert(e.getTargetException() instanceof SecurityException)
|
||||
throw new UnknownError(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -74,13 +74,14 @@ import org.xml.sax.SAXNotSupportedException;
|
|||
*
|
||||
* @since SAX 2.0
|
||||
* @author David Megginson
|
||||
* @version 2.0.1 (sax2r2)
|
||||
* @see org.xml.sax.helpers.XMLReaderAdapter
|
||||
* @see org.xml.sax.XMLReader
|
||||
* @see org.xml.sax.Parser
|
||||
*/
|
||||
public class ParserAdapter implements XMLReader, DocumentHandler
|
||||
{
|
||||
|
||||
private static SecuritySupport ss = new SecuritySupport();
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Constructors.
|
||||
|
@ -102,7 +103,7 @@ public class ParserAdapter implements XMLReader, DocumentHandler
|
|||
{
|
||||
super();
|
||||
|
||||
String driver = System.getProperty("org.xml.sax.parser");
|
||||
String driver = ss.getSystemProperty("org.xml.sax.parser");
|
||||
|
||||
try {
|
||||
setup(ParserFactory.makeParser());
|
||||
|
|
|
@ -30,12 +30,6 @@
|
|||
|
||||
package org.xml.sax.helpers;
|
||||
|
||||
import java.lang.ClassNotFoundException;
|
||||
import java.lang.IllegalAccessException;
|
||||
import java.lang.InstantiationException;
|
||||
import java.lang.SecurityException;
|
||||
import java.lang.ClassCastException;
|
||||
|
||||
import org.xml.sax.Parser;
|
||||
|
||||
|
||||
|
@ -69,9 +63,10 @@ import org.xml.sax.Parser;
|
|||
* interface.
|
||||
* @since SAX 1.0
|
||||
* @author David Megginson
|
||||
* @version 2.0.1 (sax2r2)
|
||||
*/
|
||||
public class ParserFactory {
|
||||
|
||||
private static SecuritySupport ss = new SecuritySupport();
|
||||
|
||||
/**
|
||||
* Private null constructor.
|
||||
|
@ -109,7 +104,7 @@ public class ParserFactory {
|
|||
NullPointerException,
|
||||
ClassCastException
|
||||
{
|
||||
String className = System.getProperty("org.xml.sax.parser");
|
||||
String className = ss.getSystemProperty("org.xml.sax.parser");
|
||||
if (className == null) {
|
||||
throw new NullPointerException("No value for sax.parser property");
|
||||
} else {
|
||||
|
@ -146,7 +141,7 @@ public class ParserFactory {
|
|||
ClassCastException
|
||||
{
|
||||
return (Parser) NewInstance.newInstance (
|
||||
NewInstance.getClassLoader (), className);
|
||||
ss.getContextClassLoader(), className);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
108
jaxp/src/org/xml/sax/helpers/SecuritySupport.java
Normal file
108
jaxp/src/org/xml/sax/helpers/SecuritySupport.java
Normal file
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* Copyright (c) 2004, 2006, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package org.xml.sax.helpers;
|
||||
|
||||
import java.io.*;
|
||||
import java.security.*;
|
||||
|
||||
/**
|
||||
* This class is duplicated for each JAXP subpackage so keep it in sync.
|
||||
* It is package private and therefore is not exposed as part of the JAXP
|
||||
* API.
|
||||
*
|
||||
* Security related methods that only work on J2SE 1.2 and newer.
|
||||
*/
|
||||
class SecuritySupport {
|
||||
|
||||
|
||||
ClassLoader getContextClassLoader() throws SecurityException{
|
||||
return (ClassLoader)
|
||||
AccessController.doPrivileged(new PrivilegedAction() {
|
||||
public Object run() {
|
||||
ClassLoader cl = null;
|
||||
//try {
|
||||
cl = Thread.currentThread().getContextClassLoader();
|
||||
//} catch (SecurityException ex) { }
|
||||
|
||||
if (cl == null)
|
||||
cl = ClassLoader.getSystemClassLoader();
|
||||
|
||||
return cl;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
String getSystemProperty(final String propName) {
|
||||
return (String)
|
||||
AccessController.doPrivileged(new PrivilegedAction() {
|
||||
public Object run() {
|
||||
return System.getProperty(propName);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
FileInputStream getFileInputStream(final File file)
|
||||
throws FileNotFoundException
|
||||
{
|
||||
try {
|
||||
return (FileInputStream)
|
||||
AccessController.doPrivileged(new PrivilegedExceptionAction() {
|
||||
public Object run() throws FileNotFoundException {
|
||||
return new FileInputStream(file);
|
||||
}
|
||||
});
|
||||
} catch (PrivilegedActionException e) {
|
||||
throw (FileNotFoundException)e.getException();
|
||||
}
|
||||
}
|
||||
|
||||
InputStream getResourceAsStream(final ClassLoader cl,
|
||||
final String name)
|
||||
{
|
||||
return (InputStream)
|
||||
AccessController.doPrivileged(new PrivilegedAction() {
|
||||
public Object run() {
|
||||
InputStream ris;
|
||||
if (cl == null) {
|
||||
ris = Object.class.getResourceAsStream(name);
|
||||
} else {
|
||||
ris = cl.getResourceAsStream(name);
|
||||
}
|
||||
return ris;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
boolean doesFileExist(final File f) {
|
||||
return ((Boolean)
|
||||
AccessController.doPrivileged(new PrivilegedAction() {
|
||||
public Object run() {
|
||||
return new Boolean(f.exists());
|
||||
}
|
||||
})).booleanValue();
|
||||
}
|
||||
|
||||
}
|
|
@ -34,8 +34,6 @@ package org.xml.sax.helpers;
|
|||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
|
@ -85,8 +83,8 @@ final public class XMLReaderFactory
|
|||
}
|
||||
|
||||
private static final String property = "org.xml.sax.driver";
|
||||
private static SecuritySupport ss = new SecuritySupport();
|
||||
|
||||
private static String _clsFromJar = null;
|
||||
private static boolean _jarread = false;
|
||||
/**
|
||||
* Attempt to create an XMLReader from system defaults.
|
||||
|
@ -134,43 +132,45 @@ final public class XMLReaderFactory
|
|||
throws SAXException
|
||||
{
|
||||
String className = null;
|
||||
ClassLoader loader = NewInstance.getClassLoader ();
|
||||
ClassLoader cl = ss.getContextClassLoader();
|
||||
|
||||
// 1. try the JVM-instance-wide system property
|
||||
try { className = System.getProperty (property); }
|
||||
catch (RuntimeException e) { /* normally fails for applets */ }
|
||||
try {
|
||||
className = ss.getSystemProperty(property);
|
||||
}
|
||||
catch (RuntimeException e) { /* continue searching */ }
|
||||
|
||||
// 2. if that fails, try META-INF/services/
|
||||
if (className == null) {
|
||||
if (!_jarread) {
|
||||
final ClassLoader loader1 = loader;
|
||||
_jarread = true;
|
||||
_clsFromJar = (String)
|
||||
AccessController.doPrivileged(new PrivilegedAction() {
|
||||
public Object run() {
|
||||
String clsName = null;
|
||||
try {
|
||||
String service = "META-INF/services/" + property;
|
||||
InputStream in;
|
||||
BufferedReader reader;
|
||||
if (loader1 == null)
|
||||
in = ClassLoader.getSystemResourceAsStream (service);
|
||||
else
|
||||
in = loader1.getResourceAsStream (service);
|
||||
String service = "META-INF/services/" + property;
|
||||
InputStream in;
|
||||
BufferedReader reader;
|
||||
|
||||
if (in != null) {
|
||||
reader = new BufferedReader (
|
||||
new InputStreamReader (in, "UTF8"));
|
||||
clsName = reader.readLine ();
|
||||
in.close ();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
if (cl != null) {
|
||||
in = ss.getResourceAsStream(cl, service);
|
||||
|
||||
// If no provider found then try the current ClassLoader
|
||||
if (in == null) {
|
||||
cl = null;
|
||||
in = ss.getResourceAsStream(cl, service);
|
||||
}
|
||||
return clsName;
|
||||
} else {
|
||||
// No Context ClassLoader, try the current ClassLoader
|
||||
in = ss.getResourceAsStream(cl, service);
|
||||
}
|
||||
});
|
||||
|
||||
if (in != null) {
|
||||
reader = new BufferedReader (
|
||||
new InputStreamReader (in, "UTF8"));
|
||||
className = reader.readLine ();
|
||||
in.close ();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
className = _clsFromJar;
|
||||
}
|
||||
|
||||
// 3. Distro-specific fallback
|
||||
|
@ -187,7 +187,7 @@ final public class XMLReaderFactory
|
|||
|
||||
// do we know the XMLReader implementation class yet?
|
||||
if (className != null)
|
||||
return loadClass (loader, className);
|
||||
return loadClass (cl, className);
|
||||
|
||||
// 4. panic -- adapt any SAX1 parser
|
||||
try {
|
||||
|
@ -217,7 +217,7 @@ final public class XMLReaderFactory
|
|||
public static XMLReader createXMLReader (String className)
|
||||
throws SAXException
|
||||
{
|
||||
return loadClass (NewInstance.getClassLoader (), className);
|
||||
return loadClass (ss.getContextClassLoader(), className);
|
||||
}
|
||||
|
||||
private static XMLReader loadClass (ClassLoader loader, String className)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue