Author: ron.sigal(a)jboss.com
Date: 2009-04-14 06:15:03 -0400 (Tue, 14 Apr 2009)
New Revision: 5003
Modified:
remoting2/branches/2.x/src/main/org/jboss/remoting/loading/ClassByteClassLoader.java
remoting2/branches/2.x/src/main/org/jboss/remoting/loading/ObjectInputStreamWithClassLoader.java
Log:
JBREM-1116: Eliminated dependence on SecurityUtility.
Modified:
remoting2/branches/2.x/src/main/org/jboss/remoting/loading/ClassByteClassLoader.java
===================================================================
---
remoting2/branches/2.x/src/main/org/jboss/remoting/loading/ClassByteClassLoader.java 2009-04-14
10:14:29 UTC (rev 5002)
+++
remoting2/branches/2.x/src/main/org/jboss/remoting/loading/ClassByteClassLoader.java 2009-04-14
10:15:03 UTC (rev 5003)
@@ -26,12 +26,19 @@
import org.jboss.remoting.util.SecurityUtility;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -180,7 +187,7 @@
{
return cl;
}
- cl = Class.forName(className, false, SecurityUtility.getSystemClassLoader());
+ cl = Class.forName(className, false, getSystemClassLoaderPrivate());
if(cl != null)
{
return cl;
@@ -208,12 +215,12 @@
File file = null;
try
{
- file = SecurityUtility.createTempFile("cbc", ".class",
true);
+ file = createTempFile("cbc", ".class", true);
if(log.isTraceEnabled())
{
log.trace("adding resource at: " + name + " to file: " +
file);
}
- out = SecurityUtility.getFileOutputStream(file);
+ out = getFileOutputStream(file);
out.write(buf);
out.flush();
}
@@ -253,11 +260,11 @@
{
log.trace("getResourceAsStream =>" + denormalized + " = "
+ file);
}
- if(file != null && SecurityUtility.fileExists(file))
+ if(file != null && fileExists(file))
{
try
{
- InputStream is = SecurityUtility.getFileInputStream(file);
+ InputStream is = getFileInputStream(file);
return new java.io.BufferedInputStream(is);
}
catch(Exception ex)
@@ -424,4 +431,113 @@
return loadedClass;
}
+
+ static private File createTempFile(final String prefix, final String suffix, final
boolean deleteOnExit) throws IOException
+ {
+ if (SecurityUtility.skipAccessControl())
+ {
+ File file = File.createTempFile(prefix, suffix);
+ if (deleteOnExit) file.deleteOnExit();
+ return file;
+ }
+
+ try
+ {
+ return (File)AccessController.doPrivileged( new PrivilegedExceptionAction()
+ {
+ public Object run() throws IOException
+ {
+ File file = File.createTempFile(prefix, suffix);
+ if (deleteOnExit) file.deleteOnExit();
+ return file;
+ }
+ });
+ }
+ catch (PrivilegedActionException e)
+ {
+ throw (IOException) e.getCause();
+ }
+ }
+
+ static private boolean fileExists(final File file)
+ {
+ if (file == null)
+ return false;
+
+ if (SecurityUtility.skipAccessControl())
+ {
+ return file.exists();
+ }
+
+ return ((Boolean)AccessController.doPrivileged( new PrivilegedAction()
+ {
+ public Object run()
+ {
+ return new Boolean(file.exists());
+ }
+ })).booleanValue();
+ }
+
+ static private FileInputStream getFileInputStream(final File file) throws
FileNotFoundException
+ {
+ if (SecurityUtility.skipAccessControl())
+ {
+ return new FileInputStream(file);
+ }
+
+ try
+ {
+ return (FileInputStream)AccessController.doPrivileged( new
PrivilegedExceptionAction()
+ {
+ public Object run() throws FileNotFoundException
+ {
+ return new FileInputStream(file);
+ }
+ });
+ }
+ catch (PrivilegedActionException e)
+ {
+ throw (FileNotFoundException) e.getCause();
+ }
+ }
+
+ static private FileOutputStream getFileOutputStream(final File file)
+ throws FileNotFoundException
+ {
+ if (SecurityUtility.skipAccessControl())
+ {
+ return new FileOutputStream(file);
+ }
+
+ try
+ {
+ return (FileOutputStream)AccessController.doPrivileged( new
PrivilegedExceptionAction()
+ {
+ public Object run() throws FileNotFoundException
+ {
+ return new FileOutputStream(file);
+ }
+ });
+ }
+ catch (PrivilegedActionException e)
+ {
+ throw (FileNotFoundException) e.getCause();
+ }
+ }
+
+ static private ClassLoader getSystemClassLoaderPrivate()
+ {
+ if (SecurityUtility.skipAccessControl())
+ {
+ return ClassLoader.getSystemClassLoader();
+ }
+
+ return (ClassLoader)AccessController.doPrivileged( new PrivilegedAction()
+ {
+ public Object run()
+ {
+ return ClassLoader.getSystemClassLoader();
+ }
+ });
+ }
}
Modified:
remoting2/branches/2.x/src/main/org/jboss/remoting/loading/ObjectInputStreamWithClassLoader.java
===================================================================
---
remoting2/branches/2.x/src/main/org/jboss/remoting/loading/ObjectInputStreamWithClassLoader.java 2009-04-14
10:14:29 UTC (rev 5002)
+++
remoting2/branches/2.x/src/main/org/jboss/remoting/loading/ObjectInputStreamWithClassLoader.java 2009-04-14
10:15:03 UTC (rev 5003)
@@ -26,6 +26,9 @@
import java.io.ObjectInputStream;
import java.io.StreamCorruptedException;
import java.lang.reflect.Method;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
import java.util.HashMap;
import org.jboss.logging.Logger;
@@ -52,7 +55,7 @@
{
try
{
- clearMethod = SecurityUtility.getDeclaredMethod(ObjectInputStream.class,
"clear", new Class[]{});
+ clearMethod = getDeclaredMethod(ObjectInputStream.class, "clear", new
Class[]{});
} catch (SecurityException e) {
log.error(e.getMessage(), e);
@@ -259,4 +262,32 @@
}
}
}
+
+ static private Method getDeclaredMethod(final Class c, final String name, final
Class[] parameterTypes)
+ throws NoSuchMethodException
+ {
+ if (SecurityUtility.skipAccessControl())
+ {
+ Method m = c.getDeclaredMethod(name, parameterTypes);
+ m.setAccessible(true);
+ return m;
+ }
+
+ try
+ {
+ return (Method) AccessController.doPrivileged( new PrivilegedExceptionAction()
+ {
+ public Object run() throws NoSuchMethodException
+ {
+ Method m = c.getDeclaredMethod(name, parameterTypes);
+ m.setAccessible(true);
+ return m;
+ }
+ });
+ }
+ catch (PrivilegedActionException e)
+ {
+ throw (NoSuchMethodException) e.getCause();
+ }
+ }
}
\ No newline at end of file
Show replies by date