[jboss-cvs] container/src/main/org/jboss/vfs/classloading ...
Scott Stark
scott.stark at jboss.com
Tue Jul 18 00:29:46 EDT 2006
User: starksm
Date: 06/07/18 00:29:46
Modified: src/main/org/jboss/vfs/classloading VFSClassLoader.java
Log:
Define the classes ProtectionDomain based on the VirtualFile url.
Revision Changes Path
1.2 +66 -17 container/src/main/org/jboss/vfs/classloading/VFSClassLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: VFSClassLoader.java
===================================================================
RCS file: /cvsroot/jboss/container/src/main/org/jboss/vfs/classloading/VFSClassLoader.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- VFSClassLoader.java 14 Jul 2006 15:10:18 -0000 1.1
+++ VFSClassLoader.java 18 Jul 2006 04:29:46 -0000 1.2
@@ -9,8 +9,14 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.net.MalformedURLException;
import java.net.URL;
+import java.security.CodeSource;
+import java.security.PermissionCollection;
+import java.security.Policy;
+import java.security.ProtectionDomain;
import java.security.SecureClassLoader;
+import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
@@ -18,17 +24,20 @@
import org.jboss.classloading.spi.ClassLoadingDomain;
import org.jboss.classloading.spi.DomainClassLoader;
+import org.jboss.logging.Logger;
import org.jboss.vfs.spi.ReadOnlyVFS;
import org.jboss.vfs.spi.VirtualFile;
/** A class loader that obtains classes and resources from a VFS.
*
* @author Scott.Stark at jboss.org
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public class VFSClassLoader extends SecureClassLoader
implements DomainClassLoader
{
+ private static Logger log = Logger.getLogger(VFSClassLoader.class);
+
protected static class ClassPathVFS
{
private ArrayList<String> searchCtxs = new ArrayList<String>();
@@ -63,14 +72,14 @@
protected Class<?> findClass(String name) throws ClassNotFoundException
{
String resName = name.replace('.', '/');
- URL classRes = findResource(resName+".class");
- if( classRes == null )
+ VirtualFile classFile = findResourceFile(resName+".class");
+ if( classFile == null )
throw new ClassNotFoundException(name);
try
{
byte[] tmp = new byte[128];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
- InputStream is = classRes.openStream();
+ InputStream is = classFile.openStream();
int length;
while ((length = is.read(tmp)) > 0)
{
@@ -78,7 +87,8 @@
}
is.close();
tmp = baos.toByteArray();
- Class c = super.defineClass(name, tmp, 0, tmp.length);
+ ProtectionDomain pd = getProtectionDomain(classFile);
+ Class c = super.defineClass(name, tmp, 0, tmp.length, pd);
return c;
}
catch (IOException e)
@@ -96,21 +106,18 @@
public URL findResource(String name)
{
URL res = null;
- try
- {
- for(ClassPathVFS cp : classpath)
- {
- VirtualFile vf = cp.vfs.resolveFile(name, cp.searchCtxs);
+ VirtualFile vf = findResourceFile(name);
if( vf != null )
{
+ try
+ {
res = vf.toURL();
- break;
}
- }
- }
- catch (IOException e)
+ catch(IOException e)
{
- e.printStackTrace();
+ if( log.isTraceEnabled() )
+ log.trace("Failed to obtain vf URL: "+vf, e);
+ }
}
return res;
}
@@ -175,4 +182,46 @@
{
return super.getPackage(name);
}
+
+ protected VirtualFile findResourceFile(String name)
+ {
+ VirtualFile vf = null;
+ try
+ {
+ for(ClassPathVFS cp : classpath)
+ {
+ vf = cp.vfs.resolveFile(name, cp.searchCtxs);
+ if( vf != null )
+ {
+ break;
+ }
+ }
+ }
+ catch (IOException e)
+ {
+ if( log.isTraceEnabled() )
+ log.trace("Failed to find resource: "+name, e);
+ }
+ return vf;
+ }
+
+ /**
+ * Determine the protection domain. If we are a copy of the original
+ * deployment, use the original url as the codebase.
+ * @return the protection domain
+ * @throws MalformedURLException
+ * @todo certificates and principles?
+ */
+ protected ProtectionDomain getProtectionDomain(VirtualFile classFile)
+ throws MalformedURLException
+ {
+ Certificate certs[] = null;
+ URL codesourceUrl = classFile.toURL();
+ CodeSource cs = new CodeSource(codesourceUrl, certs);
+ PermissionCollection permissions = SecurityActions.getPolicy().getPermissions(cs);
+ if (log.isTraceEnabled())
+ log.trace("getProtectionDomain, url=" + codesourceUrl +
+ " codeSource=" + cs + " permissions=" + permissions);
+ return new ProtectionDomain(cs, permissions);
+ }
}
More information about the jboss-cvs-commits
mailing list