Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added jar support to WeavingClassLoader

	modified:   src/kilim/WeavingClassLoader.java
	modified:   src/kilim/analysis/FileLister.java
  • Loading branch information...
commit 63b5ea75d0d9f197f1e994b4ab58b4290807d26b 1 parent 0e50160
@kilim authored
Showing with 69 additions and 29 deletions.
  1. +16 −19 src/kilim/WeavingClassLoader.java
  2. +53 −10 src/kilim/analysis/FileLister.java
View
35 src/kilim/WeavingClassLoader.java
@@ -2,12 +2,12 @@
import java.io.DataInputStream;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import kilim.analysis.ClassInfo;
+import kilim.analysis.FileLister;
import kilim.tools.Weaver;
/**
@@ -19,7 +19,7 @@
/**
* List of paths in kilim.class.path
*/
- ArrayList<File> paths;
+ ArrayList<FileLister> fileContainers;
/**
* Weaver instance. There is a mutually recursive dependency between the weaver and
* this class loader. See {@link #findClass(String)}
@@ -30,19 +30,17 @@ public WeavingClassLoader(ClassLoader parent) {
super(parent);
String classPath = System.getProperty(KILIM_CLASSPATH, "");
String[] classPaths = classPath.split(":");
- paths = new ArrayList<File>(classPaths.length);
+ fileContainers = new ArrayList<FileLister>(classPaths.length);
for (String name : classPaths) {
name = name.trim();
if (name.equals(""))
continue;
- File f = new File(name.trim());
- if (f.exists() && (f.isDirectory() || name.endsWith(".jar"))) {
- paths.add(f);
+ try {
+ fileContainers.add(new FileLister(name));
+ } catch (IOException ioe) {
+ // System.err.println( "'" + name + "' does not exist. See property " +
+ // KILIM_CLASSPATH);
}
- // else {
- // System.err.println( "'" + name + "' does not exist. See property " +
- // KILIM_CLASSPATH);
- // }
}
weaver = new Weaver(this); // mutually recursive dependency.
}
@@ -54,13 +52,12 @@ public WeavingClassLoader(ClassLoader parent) {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
Class<?> ret = null;
- for (File path : paths) {
+ for (FileLister container : fileContainers) {
try {
- File f = new File(path.getAbsolutePath() + File.separatorChar
- + name.replace('.', File.separatorChar) + ".class");
- if (!f.exists())
- continue;
- byte[] code = readFile(f);
+ String classFileName = name.replace('.', File.separatorChar) + ".class";
+ FileLister.Entry fe = container.open(classFileName);
+ if (fe == null) continue;
+ byte[] code = readFully(fe);
List<ClassInfo> cis = weaver.weave(new ClassInfo(name, code));
for (ClassInfo ci : cis) {
@@ -92,9 +89,9 @@ public WeavingClassLoader(ClassLoader parent) {
return ret;
}
- private static byte[] readFile(File f) throws IOException {
- DataInputStream in = new DataInputStream(new FileInputStream(f));
- byte[] contents = new byte[in.available()];
+ private static byte[] readFully(FileLister.Entry fe) throws IOException {
+ DataInputStream in = new DataInputStream(fe.getInputStream());
+ byte[] contents = new byte[(int)fe.getSize()];
in.readFully(contents);
in.close();
return contents;
View
63 src/kilim/analysis/FileLister.java
@@ -16,51 +16,72 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
-/**
- * Utility class to paper over the differences between jar files and
- * directories
+/**
+ * Utility class to present a uniform iterator interface for file containers; presently
+ * includes directories and jar files.
*/
+
public class FileLister implements Iterable<FileLister.Entry> {
public static abstract class Entry {
public abstract String getFileName();
+ public abstract long getSize();
public abstract InputStream getInputStream() throws IOException;
};
- Iterator<FileLister.Entry> iter;
+ FileContainer container;
public FileLister(String dirOrJarName) throws IOException {
if (dirOrJarName.endsWith(".jar")) {
- iter = openJar(dirOrJarName);
+ container = openJar(dirOrJarName);
} else {
File f = new File(dirOrJarName);
if (f.exists() && f.isDirectory()) {
- iter = new DirIterator(f);
+ container = new DirIterator(f);
} else {
throw new IOException("Expected jar file or directory name");
}
}
}
+
+ /**
+ * @param relativeFileName
+ * @return if the relativeFileName exists in the directory or jar represented by FileLister object
+ * open it. If not return null.
+ * @throws IOException
+ */
+ public Entry open(String relativeFileName) throws IOException {
+ return container.open(relativeFileName);
+ }
- private Iterator<FileLister.Entry> openJar(String jarFile) throws IOException {
+ private FileContainer openJar(String jarFile) throws IOException {
return new JarIterator(new JarFile(jarFile));
}
public Iterator<FileLister.Entry> iterator() {
- return iter;
+ return container;
}
}
+abstract class FileContainer implements Iterator<FileLister.Entry> {
+ abstract FileLister.Entry open(String relativeFileName) throws IOException;
+}
+
/**
* Preorder traversal of a directory. Returns everything including directory
* names.
*/
-class DirIterator implements Iterator<FileLister.Entry> {
+class DirIterator extends FileContainer {
+ final File root;
private static class DirEntry extends FileLister.Entry {
final File file;
DirEntry(File f) {file = f;}
@Override
+ public long getSize() {
+ return file.length();
+ }
+ @Override
public String getFileName() {
try {
return file.getCanonicalPath();
@@ -76,7 +97,9 @@ public InputStream getInputStream() throws IOException {
Stack<File> stack = new Stack<File>();
+
DirIterator(File f) {
+ root = f;
stack.push(f);
}
@@ -110,9 +133,18 @@ public boolean hasNext() {
public void remove() {
throw new RuntimeException("FileLister does not remove files");
}
+
+ @Override
+ FileLister.Entry open(String fileName) throws IOException {
+ File ret = new File(root.getAbsolutePath() + File.separatorChar + fileName);
+ if (ret.exists() && ret.isFile()) {
+ return new DirEntry(ret);
+ }
+ return null;
+ }
}
-class JarIterator implements Iterator<FileLister.Entry> {
+class JarIterator extends FileContainer {
Enumeration<JarEntry> jarEnum;
JarFile jarFile;
String nextName;
@@ -130,6 +162,11 @@ public String getFileName() {
public InputStream getInputStream() throws IOException {
return jarFile.getInputStream(jarEntry);
}
+
+ @Override
+ public long getSize() {
+ return jarEntry.getSize();
+ }
}
JarIterator(JarFile f) {
@@ -148,4 +185,10 @@ public boolean hasNext() {
public void remove() {
throw new RuntimeException("FileLister does not remove files");
}
+
+ @Override
+ FileLister.Entry open(String relativeFileName) throws IOException {
+ JarEntry e = jarFile.getJarEntry(relativeFileName);
+ return e == null ? null : new JEntry(e);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.