[jboss-cvs] container/src/main/org/jboss/vfs/classloading ...
Scott Stark
scott.stark at jboss.com
Fri Jul 14 11:10:18 EDT 2006
User: starksm
Date: 06/07/14 11:10:18
Added: src/main/org/jboss/vfs/classloading VFSClassLoader.java
SecurityActions.java VFSClassLoaderFactory.java
Log:
Move the class loader to a classloding package
Revision Changes Path
1.1 date: 2006/07/14 15:10:18; author: starksm; state: Exp;container/src/main/org/jboss/vfs/classloading/VFSClassLoader.java
Index: VFSClassLoader.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.vfs.classloading;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.SecureClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Vector;
import org.jboss.classloading.spi.ClassLoadingDomain;
import org.jboss.classloading.spi.DomainClassLoader;
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$
*/
public class VFSClassLoader extends SecureClassLoader
implements DomainClassLoader
{
protected static class ClassPathVFS
{
private ArrayList<String> searchCtxs = new ArrayList<String>();
private ReadOnlyVFS vfs;
protected ClassPathVFS(String[] searchCtxs, ReadOnlyVFS vfs)
{
this.searchCtxs.addAll(Arrays.asList(searchCtxs));
this.vfs = vfs;
}
}
protected ArrayList<ClassPathVFS> classpath = new ArrayList<ClassPathVFS>();
/**
* Create a class loader given a search path and VFS
* @param searchCtxs - the paths from the VFS that make up the class loader path
* @param vfs - the VFS used to resolve and load classes and resources
*/
public VFSClassLoader(String[] searchCtxs, ReadOnlyVFS vfs)
{
ClassPathVFS cp = new ClassPathVFS(searchCtxs, vfs);
classpath.add(cp);
}
/**
* Find and define the given java class
*
* @param name - the binary class name
* @return the defined Class object
* @throws ClassNotFoundException thrown if the class could not be found
* or defined
*/
protected Class<?> findClass(String name) throws ClassNotFoundException
{
String resName = name.replace('.', '/');
URL classRes = findResource(resName+".class");
if( classRes == null )
throw new ClassNotFoundException(name);
try
{
byte[] tmp = new byte[128];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = classRes.openStream();
int length;
while ((length = is.read(tmp)) > 0)
{
baos.write(tmp, 0, length);
}
is.close();
tmp = baos.toByteArray();
Class c = super.defineClass(name, tmp, 0, tmp.length);
return c;
}
catch (IOException e)
{
throw new ClassNotFoundException(name, e);
}
}
/**
* Search for the resource in the VFS contraining the search to the
* class loader paths.
* @param name - the resource name
* @return the resource URL if found, null otherwise
*/
public URL findResource(String name)
{
URL res = null;
try
{
for(ClassPathVFS cp : classpath)
{
VirtualFile vf = cp.vfs.resolveFile(name, cp.searchCtxs);
if( vf != null )
{
res = vf.toURL();
break;
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
return res;
}
/**
* Search for the resource in the VFS contraining the search to the
* class loader paths.
* @param name - the resource name
* @return A possibly empty enumeration of the matching resources
*/
public Enumeration<URL> findResources(String name) throws IOException
{
Vector<URL> resources = new Vector<URL>();
/*for(ClassPathVFS cp : classpath)
{
List<VirtualFile> matches = null;//cp.vfs.resolveFiles(name, cp.searchCtxs);
for(VirtualFile vf : matches)
{
URL resURL = vf.toURL();
resources.add(resURL);
}
}*/
return resources.elements();
}
public ClassLoadingDomain getDomain()
{
return null;
}
public void setDomain(ClassLoadingDomain domain)
{
}
public Class loadClassLocally(String name, boolean resolve)
throws ClassNotFoundException
{
return findClass(name);
}
public URL loadResourceLocally(String name)
{
return this.findResource(name);
}
/**
* Get the packages defined by the classloader
*
* @return the packages
*/
public Package[] getPackages()
{
return super.getPackages();
}
/**
* Get a package defined by the classloader
*
* @param name the name of the package
* @return the package
*/
public Package getPackage(String name)
{
return super.getPackage(name);
}
}
1.1 date: 2006/07/14 15:10:18; author: starksm; state: Exp;container/src/main/org/jboss/vfs/classloading/SecurityActions.java
Index: SecurityActions.java
===================================================================
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.vfs.classloading;
import java.security.PrivilegedAction;
import java.security.AccessController;
import org.jboss.vfs.spi.ReadOnlyVFS;
/**
Package priviledged actions
@author Scott.Stark at jboss.org
@version $Revision$
*/
public class SecurityActions
{
interface ClassLoaderActions
{
ClassLoaderActions PRIVILEGED = new ClassLoaderActions()
{
public VFSClassLoader newClassLoader(final String[] paths, final ReadOnlyVFS vfs)
{
PrivilegedAction<VFSClassLoader> action = new PrivilegedAction<VFSClassLoader>()
{
public VFSClassLoader run()
{
return new VFSClassLoader(paths, vfs);
}
};
return AccessController.doPrivileged(action);
}
};
ClassLoaderActions NON_PRIVILEGED = new ClassLoaderActions()
{
public VFSClassLoader newClassLoader(final String[] paths, final ReadOnlyVFS vfs)
{
return new VFSClassLoader(paths, vfs);
}
};
VFSClassLoader newClassLoader(final String[] paths, final ReadOnlyVFS vfs);
}
static VFSClassLoader newClassLoader(final String[] paths, final ReadOnlyVFS vfs)
{
if(System.getSecurityManager() == null)
{
return ClassLoaderActions.NON_PRIVILEGED.newClassLoader(paths, vfs);
}
else
{
return ClassLoaderActions.PRIVILEGED.newClassLoader(paths, vfs);
}
}
}
1.1 date: 2006/07/14 15:10:18; author: starksm; state: Exp;container/src/main/org/jboss/vfs/classloading/VFSClassLoaderFactory.java
Index: VFSClassLoaderFactory.java
===================================================================
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.vfs.classloading;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import org.jboss.vfs.VFSFactory;
import org.jboss.vfs.spi.ReadOnlyVFS;
public class VFSClassLoaderFactory
{
public static VFSClassLoader newClassLoader(URL rootURL, String[] paths, VFSFactory factory)
throws IOException
{
ReadOnlyVFS vfs = factory.getVFS(rootURL);
return SecurityActions.newClassLoader(paths, vfs);
}
public static VFSClassLoader newClassLoader(String[] paths, ReadOnlyVFS vfs)
{
return SecurityActions.newClassLoader(paths, vfs);
}
}
More information about the jboss-cvs-commits
mailing list