📄 jarclassloader.java
字号:
package org.j3de.server;
import java.io.InputStream;
import java.io.IOException;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSource;
import java.security.PermissionCollection;
import java.security.SecureClassLoader;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.Enumeration;
import java.util.List;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import org.j3de.exception.ExceptionHandler;
public class JarClassLoader extends SecureClassLoader {
private static JarClassLoader classLoader;
private List jarFiles;
private Set jarFileNames;
private Map codeSources;
private Map permissions;
private JarClassLoader() {
this.jarFiles = new Vector();
this.jarFileNames = new HashSet();
this.codeSources = new HashMap();
this.permissions = new HashMap();
}
public Class findClass(String classname) throws ClassNotFoundException {
byte[] b = null;
JarFile jarfile = null;
for (int i=0; i<jarFiles.size(); i++) {
jarfile = (JarFile)jarFiles.get(i);
try {
b = loadClassFromJarFile(jarfile, classname);
break;
} catch (ClassNotFoundException e) {
}
}
if (b == null) {
ClassNotFoundException e = new ClassNotFoundException("Class " + classname + " not found !");
ExceptionHandler.handleException(e);
throw e;
}
return defineClass(classname, b, 0, b.length, (CodeSource)codeSources.get(jarfile));
}
private byte[] loadClassFromJarFile(JarFile jarfile, String classname) throws ClassNotFoundException {
try {
ZipEntry entry = jarfile.getEntry(classname.replace('.', '/') + ".class");
if (entry == null)
throw new ClassNotFoundException("Class " + classname + " not found in " + jarfile.getName());
InputStream in = jarfile.getInputStream(entry);
int readBytes = 0;
int size = (int)entry.getSize();
int chunk;
byte[] data = new byte[size];
while (readBytes < size) {
chunk = in.read(data, readBytes, size - readBytes);
if (chunk == -1)
break;
readBytes += chunk;
}
return data;
} catch (ClassFormatError e) {
throw new ClassNotFoundException("Damaged class in jarfile : " + jarfile.getName() + " : " + classname);
} catch (IOException e) {
throw new ClassNotFoundException("IOException while loading class : " + jarfile.getName() + " : " + classname +
" " + e.getMessage());
}
}
private String getPropertyValue(String property) {
if (property.equals("/"))
return File.separator;
else
return "";
}
private String expandProperties(String path) {
int pos, endPos;
while ((pos = path.indexOf("${")) > 0) {
String prefix = path.substring(0, pos);
path = path.substring(pos + 2);
pos = path.indexOf("}");
String propertyName = path.substring(0, pos);
path = prefix + getPropertyValue(propertyName) + path.substring(pos + 1);
}
return path;
}
public void addJarFile(String jarFileName, PermissionCollection permissionCollection) {
try {
jarFileName = expandProperties(jarFileName);
if (!jarFileNames.contains(jarFileName)) {
jarFileNames.add(jarFileName);
JarFile file = new JarFile(jarFileName);
jarFiles.add(file);
CodeSource cs = new CodeSource(new URL("file://" + jarFileName), null);
codeSources.put(file, cs);
permissions.put(cs, permissionCollection);
}
} catch (IOException e) {
ExceptionHandler.handleException(e);
}
}
public static JarClassLoader getClassLoader() {
if (classLoader == null)
classLoader = new JarClassLoader();
return classLoader;
}
protected PermissionCollection getPermissions(CodeSource codesource) {
return (PermissionCollection)permissions.get(codesource);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -