* @author Haruaki TAMADA
*/
public interface ClassFileArchive extends Iterable<ClassFileEntry>{
+ String CLASS_FILE_EXTENSION = ".class";
+
/**
* returns the location of this archive.
*/
- public URL getLocation();
+ URL getLocation();
/**
* returns the InputStream object from given entry.
*/
- public InputStream getInputStream(ClassFileEntry entry) throws IOException;
+ InputStream getInputStream(ClassFileEntry entry) throws IOException;
/**
* returns an entries of this archive.
*/
- public Iterator<ClassFileEntry> iterator();
+ Iterator<ClassFileEntry> iterator();
/**
* returns this archive has given class entry or not.
*/
- public boolean hasEntry(String className);
+ boolean hasEntry(String className);
/**
* returns an entry of given class name.
*/
- public ClassFileEntry getEntry(String className) throws ClassNotFoundException;
+ ClassFileEntry getEntry(String className) throws ClassNotFoundException;
/**
* returns the name of this archive.
*/
- public String getName();
+ String getName();
}
return className;
}
- public void setLocation(URL location){
+ public final void setLocation(URL location){
this.location = location;
}
return classpath.iterator();
}
else{
- final Iterator<URL> parentIterator = parent.iterator();
- final Iterator<URL> thisIterator = classpath.iterator();
- return new Iterator<URL>(){
- public boolean hasNext(){
- boolean next = parentIterator.hasNext();
- if(!next){
- next = thisIterator.hasNext();
- }
- return next;
- }
- public URL next(){
- URL nextObject = null;
- if(parentIterator.hasNext()){
- nextObject = parentIterator.next();
- }
- else{
- nextObject = thisIterator.next();
- }
- return nextObject;
- }
- public void remove(){
- }
- };
+ return new ParentIterator(parent.iterator(), classpath.iterator());
}
}
-
/**
* construct and returns a ClassLoader object which loads from classpath list.
*/
parentClassLoader = null;
}
}
- final ClassLoader parent = parentClassLoader;
+ final ClassLoader parentLoader = parentClassLoader;
loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
public ClassLoader run(){
- return new WarClassLoader(list.toArray(new URL[list.size()]), parent);
+ return new WarClassLoader(list.toArray(new URL[list.size()]), parentLoader);
}
});
}
* @return ClassFileEntry object, if not found given class, then returns null.
*/
public synchronized ClassFileEntry findEntry(String className){
- ClassLoader loader = createClassLoader();
+ ClassLoader localLoader = createClassLoader();
- URL resource = loader.getResource(className.replace('.', '/') + ".class");
+ URL resource = localLoader.getResource(
+ className.replace('.', '/') + ClassFileArchive.CLASS_FILE_EXTENSION
+ );
if(resource != null){
return new ClassFileEntry(className, resource);
* returns this context has given class entry or not.
*/
public synchronized boolean hasEntry(String className){
- ClassLoader loader = createClassLoader();
- return loader.getResource(className.replace('.', '/') + ".class") != null;
+ ClassLoader localLoader = createClassLoader();
+ return localLoader.getResource(
+ className.replace('.', '/') + ClassFileArchive.CLASS_FILE_EXTENSION
+ ) != null;
}
/**
*/
public synchronized Class<?> findClass(String className) throws ClassNotFoundException{
try{
- ClassLoader loader = createClassLoader();
+ ClassLoader localLoader = createClassLoader();
- Class<?> clazz = loader.loadClass(className);
+ Class<?> clazz = localLoader.loadClass(className);
return clazz;
} catch(NoClassDefFoundError e){
throw new ClassNotFoundException(e.getMessage(), e);
}
}
+
+ private static class ParentIterator implements Iterator<URL>{
+ private Iterator<URL> parentIterator;
+ private Iterator<URL> thisIterator;
+
+ public ParentIterator(Iterator<URL> parentIterator, Iterator<URL> thisIterator){
+ this.parentIterator = parentIterator;
+ this.thisIterator = thisIterator;
+ }
+
+ public boolean hasNext(){
+ boolean next = parentIterator.hasNext();
+ if(!next){
+ next = thisIterator.hasNext();
+ }
+ return next;
+ }
+ public URL next(){
+ URL nextObject = null;
+ if(parentIterator.hasNext()){
+ nextObject = parentIterator.next();
+ }
+ else{
+ nextObject = thisIterator.next();
+ }
+ return nextObject;
+ }
+ public void remove(){
+ }
+ }
}
public InputStream getInputStream(ClassFileEntry entry) throws IOException{
if(hasEntry(entry.getClassName())){
- return jarfile.getInputStream(jarfile.getEntry(entry.getClassName().replace('.', '/') + ".class"));
+ return jarfile.getInputStream(jarfile.getEntry(
+ entry.getClassName().replace('.', '/') + CLASS_FILE_EXTENSION
+ ));
}
return null;
}
for(Enumeration<JarEntry> e = jarfile.entries(); e.hasMoreElements(); ){
JarEntry entry = e.nextElement();
- if(entry.getName().endsWith(".class")){
+ if(entry.getName().endsWith(CLASS_FILE_EXTENSION)){
URL location = null;
try {
location = new URL("jar:" + getLocation() + "!/" + entry.getName());
String className = entry.getName();
- className = className.substring(0, className.length() - ".class".length());
+ className = className.substring(0, className.length() - CLASS_FILE_EXTENSION.length());
className = className.replace('/', '.');
list.add(new ClassFileEntry(className, location));
} catch (MalformedURLException ex) {
- throw new InternalError(ex.getMessage());
+ throw new IllegalStateException(ex);
}
}
}
}
public boolean hasEntry(String className){
- return jarfile.getEntry(className.replace('.', '/') + ".class") != null;
+ return jarfile.getEntry(className.replace('.', '/') + CLASS_FILE_EXTENSION) != null;
}
public ClassFileEntry getEntry(String className) throws ClassNotFoundException{
if(hasEntry(className)){
- String entryName = className.replace('.', '/') + ".class";
+ String entryName = className.replace('.', '/') + CLASS_FILE_EXTENSION;
try{
URL location = new URL("jar:" + jarfile.getName() + "!/" + entryName);
return new ClassFileEntry(className, location);
} catch(MalformedURLException e){
- throw new InternalError(e.getMessage());
+ throw new IllegalStateException(e);
}
}
return null;
* @author Haruaki TAMADA
*/
public class WarClassFileArchive extends JarClassFileArchive{
+ private static final String WAR_FILE_CLASSPATH = "WEB-INF/classes/";
+
public WarClassFileArchive(String jarfile) throws IOException{
super(jarfile);
}
for(Enumeration<JarEntry> e = jarEntries(); e.hasMoreElements(); ){
JarEntry entry = e.nextElement();
- if(entry.getName().endsWith(".class")){
+ if(entry.getName().endsWith(CLASS_FILE_EXTENSION)){
URL location = null;
try {
location = new URL("jar:" + getLocation() + "!/" + entry.getName());
String className = entry.getName();
- className = className.substring("WEB-INF/classes/".length(), className.length() - ".class".length());
+ className = className.substring(
+ WAR_FILE_CLASSPATH.length(), className.length() - CLASS_FILE_EXTENSION.length()
+ );
className = className.replace('/', '.');
list.add(new ClassFileEntry(className, location));
- } catch (MalformedURLException ex) {
- throw new InternalError(ex.getMessage());
+ } catch(MalformedURLException ex){
+ throw new IllegalStateException(ex);
}
}
}
}
public boolean hasEntry(String className){
- return hasJarEntry("WEB-INF/classes/" + className.replace('.', '/') + ".class");
+ return hasJarEntry(
+ WAR_FILE_CLASSPATH + className.replace('.', '/') + CLASS_FILE_EXTENSION
+ );
}
public ClassFileEntry getEntry(String className) throws ClassNotFoundException{
if(hasEntry(className)){
- String entryName = className.replace('.', '/') + ".class";
+ String entryName = className.replace('.', '/') + CLASS_FILE_EXTENSION;
try{
- URL location = new URL("jar:" + getLocation() + "!/WEB-INF/classes/" + entryName);
+ URL location = new URL("jar:" + getLocation() + "!/" + WAR_FILE_CLASSPATH + entryName);
return new ClassFileEntry(className, location);
} catch(MalformedURLException e){
- throw new InternalError(e.getMessage());
+ throw new IllegalStateException(e);
}
}
return null;
* @author Haruaki Tamada
*/
public class WarClassLoader extends URLClassLoader{
+ private static final int BUFFER_SIZE = 256;
+
public WarClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory){
super(urls, parent, factory);
}
try{
clazz = super.findClass(name);
} catch(ClassNotFoundException e){
- String path = "WEB-INF/classes/" + name.replace('.', '/') + ".class";
+ }
+ if(clazz == null){
+ String path = "WEB-INF/classes/" + name.replace('.', '/')
+ + ClassFileArchive.CLASS_FILE_EXTENSION;
for(URL url: getURLs()){
if(url.toString().endsWith(".war")){
InputStream in = null;
try{
URL newurl = new URL("jar:" + url + "!/" + path);
in = newurl.openStream();
- byte[] data = new byte[256];
+ byte[] data = new byte[BUFFER_SIZE];
int read = 0;
while((read = in.read(data, 0, data.length)) != -1){
out.write(data, 0, read);
byte[] classdata = out.toByteArray();
clazz = defineClass(name, classdata, 0, classdata.length);
} catch(IOException exp){
- throw e;
+ throw new ClassNotFoundException(name, exp);
} finally{
if(in != null){
try{ in.close(); }
catch(IOException exception){
- throw new InternalError(exception.getMessage());
+ throw new IllegalStateException(exception);
}
}
try{
out.close();
} catch(IOException exception){
- throw new InternalError(exception.getMessage());
+ throw new IllegalStateException(exception);
}
}
break;