8129572: Cleanup usage of getResourceAsStream in jaxp

Reviewed-by: alanb, joehw, mchung, redestad
This commit is contained in:
Daniel Fuchs 2015-06-23 19:50:10 +02:00
parent 9ced0d90d5
commit 4ebbfc918f
14 changed files with 17 additions and 239 deletions

View file

@ -24,7 +24,6 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.InputStream;
import java.lang.ClassLoader; import java.lang.ClassLoader;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
@ -115,33 +114,6 @@ public final class SecuritySupport {
} }
} }
/**
* Return resource using the same classloader for the ObjectFactory by
* default or bootclassloader when Security Manager is in place
*/
public static InputStream getResourceAsStream(final String name) {
if (System.getSecurityManager() != null) {
return getResourceAsStream(null, name);
} else {
return getResourceAsStream(findClassLoader(), name);
}
}
public static 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;
}
});
}
/** /**
* Gets a resource bundle using the specified base name, the default locale, * Gets a resource bundle using the specified base name, the default locale,
* and the caller's class loader. * and the caller's class loader.

View file

@ -128,29 +128,10 @@ public final class SecuritySupport {
} }
} }
/**
* Return resource using the same classloader for the ObjectFactory by
* default or bootclassloader when Security Manager is in place
*/
public static InputStream getResourceAsStream(final String name) { public static InputStream getResourceAsStream(final String name) {
if (System.getSecurityManager()!=null) {
return getResourceAsStream(null, name);
} else {
return getResourceAsStream(ObjectFactory.findClassLoader(), name);
}
}
public static InputStream getResourceAsStream(final ClassLoader cl,
final String name) {
return (InputStream) AccessController.doPrivileged(new PrivilegedAction() { return (InputStream) AccessController.doPrivileged(new PrivilegedAction() {
public Object run() { public Object run() {
InputStream ris; return SecuritySupport.class.getResourceAsStream("/"+name);
if (cl == null) {
ris = Object.class.getResourceAsStream("/"+name);
} else {
ris = cl.getResourceAsStream(name);
}
return ris;
} }
}); });
} }

View file

@ -118,34 +118,6 @@ public final class SecuritySupport {
throw (FileNotFoundException)e.getException(); throw (FileNotFoundException)e.getException();
} }
} }
/**
* Return resource using the same classloader for the ObjectFactory by default
* or bootclassloader when Security Manager is in place
*/
public static InputStream getResourceAsStream(final String name) {
if (System.getSecurityManager()!=null) {
return getResourceAsStream(null, name);
} else {
return getResourceAsStream(ObjectFactory.findClassLoader(), name);
}
}
public static 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;
}
});
}
/** /**
* Gets a resource bundle using the specified base name, the default locale, and the caller's class loader. * Gets a resource bundle using the specified base name, the default locale, and the caller's class loader.

View file

@ -23,7 +23,6 @@ package com.sun.org.apache.xerces.internal.xinclude;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.InputStream;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
@ -113,23 +112,6 @@ final class SecuritySupport {
} }
} }
InputStream getResourceAsStream(final ClassLoader cl,
final String name)
{
return (InputStream)
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
InputStream ris;
if (cl == null) {
ris = ClassLoader.getSystemResourceAsStream(name);
} else {
ris = cl.getResourceAsStream(name);
}
return ris;
}
});
}
boolean getFileExists(final File f) { boolean getFileExists(final File f) {
return ((Boolean) return ((Boolean)
AccessController.doPrivileged(new PrivilegedAction() { AccessController.doPrivileged(new PrivilegedAction() {

View file

@ -23,7 +23,6 @@ package com.sun.org.apache.xml.internal.serialize;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.InputStream;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
@ -113,23 +112,6 @@ final class SecuritySupport {
} }
} }
InputStream getResourceAsStream(final ClassLoader cl,
final String name)
{
return (InputStream)
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
InputStream ris;
if (cl == null) {
ris = ClassLoader.getSystemResourceAsStream(name);
} else {
ris = cl.getResourceAsStream(name);
}
return ris;
}
});
}
boolean getFileExists(final File f) { boolean getFileExists(final File f) {
return ((Boolean) return ((Boolean)
AccessController.doPrivileged(new PrivilegedAction() { AccessController.doPrivileged(new PrivilegedAction() {

View file

@ -27,11 +27,9 @@ import java.io.InputStream;
import java.util.Properties; import java.util.Properties;
import com.sun.org.apache.xpath.internal.XPathContext; import com.sun.org.apache.xpath.internal.XPathContext;
import com.sun.org.apache.xpath.internal.objects.XNumber;
import com.sun.org.apache.xpath.internal.objects.XObject; import com.sun.org.apache.xpath.internal.objects.XObject;
import com.sun.org.apache.xpath.internal.objects.XString; import com.sun.org.apache.xpath.internal.objects.XString;
import com.sun.org.apache.xpath.internal.res.XPATHErrorResources; import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
import com.sun.org.apache.xalan.internal.utils.SecuritySupport; import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
/** /**
@ -69,7 +67,7 @@ public class FuncSystemProperty extends FunctionOneArg
// property argument is to be looked for. // property argument is to be looked for.
Properties xsltInfo = new Properties(); Properties xsltInfo = new Properties();
loadPropertyFile(XSLT_PROPERTIES, xsltInfo); loadPropertyFile(xsltInfo);
if (indexOfNSSep > 0) if (indexOfNSSep > 0)
{ {
@ -159,25 +157,21 @@ public class FuncSystemProperty extends FunctionOneArg
} }
/** /**
* Retrieve a propery bundle from a specified file * Retrieve a property bundle from a XSLT_PROPERTIES
* *
* @param file The string name of the property file. The name
* should already be fully qualified as path/filename
* @param target The target property bag the file will be placed into. * @param target The target property bag the file will be placed into.
*/ */
public void loadPropertyFile(String file, Properties target) private void loadPropertyFile(Properties target)
{ {
try try
{ {
// Use SecuritySupport class to provide priveleged access to property file // Use SecuritySupport class to provide privileged access to property file
InputStream is = SecuritySupport.getResourceAsStream(ObjectFactory.findClassLoader(), InputStream is = SecuritySupport.getResourceAsStream(XSLT_PROPERTIES);
file);
// get a buffered version // get a buffered version
BufferedInputStream bis = new BufferedInputStream(is); try (BufferedInputStream bis = new BufferedInputStream(is)) {
target.load(bis); // and load up the property bag from this
target.load(bis); // and load up the property bag from this }
bis.close(); // close out after reading
} }
catch (Exception ex) catch (Exception ex)
{ {

View file

@ -26,9 +26,7 @@
package javax.xml.datatype; package javax.xml.datatype;
import java.security.*; import java.security.*;
import java.net.*;
import java.io.*; import java.io.*;
import java.util.*;
/** /**
* This class is duplicated for each JAXP subpackage so keep it in sync. * This class is duplicated for each JAXP subpackage so keep it in sync.
@ -77,23 +75,6 @@ class SecuritySupport {
} }
} }
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) { boolean doesFileExist(final File f) {
return ((Boolean) return ((Boolean)
AccessController.doPrivileged(new PrivilegedAction() { AccessController.doPrivileged(new PrivilegedAction() {

View file

@ -26,9 +26,7 @@
package javax.xml.parsers; package javax.xml.parsers;
import java.security.*; import java.security.*;
import java.net.*;
import java.io.*; import java.io.*;
import java.util.*;
/** /**
* This class is duplicated for each JAXP subpackage so keep it in sync. * This class is duplicated for each JAXP subpackage so keep it in sync.
@ -81,23 +79,6 @@ class SecuritySupport {
} }
} }
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) { boolean doesFileExist(final File f) {
return ((Boolean) return ((Boolean)
AccessController.doPrivileged(new PrivilegedAction() { AccessController.doPrivileged(new PrivilegedAction() {

View file

@ -26,9 +26,7 @@
package javax.xml.stream; package javax.xml.stream;
import java.security.*; import java.security.*;
import java.net.*;
import java.io.*; import java.io.*;
import java.util.*;
/** /**
* This class is duplicated for each JAXP subpackage so keep it in sync. * This class is duplicated for each JAXP subpackage so keep it in sync.
@ -81,23 +79,6 @@ class SecuritySupport {
} }
} }
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) { boolean doesFileExist(final File f) {
return ((Boolean) return ((Boolean)
AccessController.doPrivileged(new PrivilegedAction() { AccessController.doPrivileged(new PrivilegedAction() {

View file

@ -26,9 +26,7 @@
package javax.xml.transform; package javax.xml.transform;
import java.security.*; import java.security.*;
import java.net.*;
import java.io.*; import java.io.*;
import java.util.*;
/** /**
* This class is duplicated for each JAXP subpackage so keep it in sync. * This class is duplicated for each JAXP subpackage so keep it in sync.
@ -79,23 +77,6 @@ class SecuritySupport {
} }
} }
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) { boolean doesFileExist(final File f) {
return ((Boolean) return ((Boolean)
AccessController.doPrivileged(new PrivilegedAction() { AccessController.doPrivileged(new PrivilegedAction() {

View file

@ -28,7 +28,6 @@ package javax.xml.validation;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.security.*; import java.security.*;
import java.net.*;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
@ -134,23 +133,6 @@ class SecuritySupport {
} }
} }
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) { boolean doesFileExist(final File f) {
return ((Boolean) return ((Boolean)
AccessController.doPrivileged(new PrivilegedAction() { AccessController.doPrivileged(new PrivilegedAction() {

View file

@ -27,7 +27,6 @@ package javax.xml.xpath;
import java.net.URL; import java.net.URL;
import java.security.*; import java.security.*;
import java.net.*;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
@ -131,23 +130,6 @@ class SecuritySupport {
} }
} }
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) { boolean doesFileExist(final File f) {
return ((Boolean) return ((Boolean)
AccessController.doPrivileged(new PrivilegedAction() { AccessController.doPrivileged(new PrivilegedAction() {

View file

@ -87,7 +87,7 @@ class SecuritySupport {
public Object run() { public Object run() {
InputStream ris; InputStream ris;
if (cl == null) { if (cl == null) {
ris = Object.class.getResourceAsStream(name); ris = SecuritySupport.class.getResourceAsStream(name);
} else { } else {
ris = cl.getResourceAsStream(name); ris = cl.getResourceAsStream(name);
} }

View file

@ -134,6 +134,9 @@ public class LSSerializerTest {
DOMImplementation impl = doc.getImplementation(); DOMImplementation impl = doc.getImplementation();
DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0"); DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
LSSerializer writer = implLS.createLSSerializer(); LSSerializer writer = implLS.createLSSerializer();
System.out.println("Serializer is: " + implLS.getClass().getName() + " " + implLS);
DOMErrorHandlerImpl eh = new DOMErrorHandlerImpl(); DOMErrorHandlerImpl eh = new DOMErrorHandlerImpl();
writer.getDomConfig().setParameter("error-handler", eh); writer.getDomConfig().setParameter("error-handler", eh);
@ -200,6 +203,8 @@ public class LSSerializerTest {
DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation; DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation;
LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
System.out.println("Serializer is: " + lsSerializer.getClass().getName() + " " + lsSerializer);
// get configuration // get configuration
DOMConfiguration domConfiguration = lsSerializer.getDomConfig(); DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
@ -294,6 +299,8 @@ public class LSSerializerTest {
DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation; DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation;
LSSerializer lsSerializer = domImplementationLS.createLSSerializer(); LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
System.out.println("Serializer is: " + lsSerializer.getClass().getName() + " " + lsSerializer);
// get default serialization // get default serialization
String defaultSerialization = lsSerializer.writeToString(document); String defaultSerialization = lsSerializer.writeToString(document);