Merge pull request #64 from TheBiblMan/master

kind of unacceptable.
This commit is contained in:
Kalen (Konloch) Kinloch 2015-07-20 19:47:47 -06:00
commit 25df7fa362
21 changed files with 210 additions and 27 deletions

View file

@ -0,0 +1,61 @@
package the.bytecode.club.bootloader;
import java.util.HashMap;
import java.util.Map;
import the.bytecode.club.bootloader.resource.ExternalResource;
/**
* @author Bibl (don't ban me pls)
* @created 21 Jul 2015 00:18:07
*/
public final class AbstractLoaderFactory {
private static final String DEFAULT_KEY = "default-factory";
private static final Map<String, LoaderFactory<?>> FACTORYCACHE = new HashMap<String, LoaderFactory<?>>();
public static void register(LoaderFactory<?> factory) {
register(DEFAULT_KEY, factory);
}
public static void register(String key, LoaderFactory<?> factory) {
if(key == null || factory == null) {
throw new IllegalArgumentException("null key or factory");
}
if(FACTORYCACHE.containsKey(key)) {
throw new IllegalArgumentException("factory already registered with key: " + key);
}
FACTORYCACHE.put(key, factory);
}
public static void unregister(String key) {
if(key == null) {
throw new IllegalArgumentException("null key");
}
if(!FACTORYCACHE.containsKey(key)) {
throw new IllegalArgumentException("factory doesn't key for key: " + key);
}
FACTORYCACHE.remove(key);
}
public static <T extends ExternalResource<?>> LoaderFactory<T> find() {
return find(DEFAULT_KEY);
}
@SuppressWarnings("unchecked")
public static <T extends ExternalResource<?>> LoaderFactory<T> find(String key) {
if(key == null) {
throw new IllegalArgumentException("null key");
}
if(!FACTORYCACHE.containsKey(key)) {
throw new IllegalArgumentException("factory doesn't key for key: " + key);
}
return (LoaderFactory<T>) FACTORYCACHE.get(key);
}
}

View file

@ -11,6 +11,8 @@ import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import me.konloch.kontainer.io.HTTPRequest;
import the.bytecode.club.bootloader.resource.EmptyExternalResource;
import the.bytecode.club.bootloader.resource.ExternalResource;
/**
* @author Bibl (don't ban me pls)
@ -21,7 +23,8 @@ public class Boot {
private static InitialBootScreen screen;
public static void main(String[] args) throws Exception {
ILoader loader = findLoader();
bootstrap();
ILoader<?> loader = findLoader();
screen = new InitialBootScreen();
SwingUtilities.invokeLater(new Runnable() {
@ -41,7 +44,7 @@ public class Boot {
klass.getDeclaredMethod("main", new Class<?>[] { String[].class }).invoke(null, new Object[] { args });
}
private static void create(ILoader loader, boolean clean) throws Exception {
private static void create(ILoader<?> loader, boolean clean) throws Exception {
setState("Bytecode Viewer Boot Screen - Checking Libraries...");
final File libsDirectory = libsDir();
@ -159,9 +162,8 @@ public class Boot {
System.out.println("Loading library " + f.getName());
try {
JarInfo jar = new JarInfo(f);
ExternalLibrary lib = new ExternalLibrary(jar);
loader.bind(lib);
ExternalResource res = new EmptyExternalResource<Object>(f.toURI().toURL());
loader.bind(res);
System.out.println("Succesfully loaded " + f.getName());
} catch (Exception e) {
e.printStackTrace();
@ -191,8 +193,20 @@ public class Boot {
screen.setTitle(s);
}
private static ILoader findLoader() {
private static ILoader<?> findLoader() {
// TODO: Find from providers
return new LibraryClassLoader();
// return new LibraryClassLoader();
// TODO: Catch
return AbstractLoaderFactory.find().spawnLoader();
}
private static void bootstrap() {
AbstractLoaderFactory.register(new LoaderFactory<Object>() {
@Override
public ILoader<Object> spawnLoader() {
return new ClassPathLoader();
}
});
}
}

View file

@ -0,0 +1,67 @@
package the.bytecode.club.bootloader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import the.bytecode.club.bootloader.resource.ExternalResource;
/**
* @author Bibl (don't ban me pls)
* @created 21 Jul 2015 00:09:53
*/
public class ClassPathLoader implements ILoader<Object> {
void extendClassPath(URL url) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class<URLClassLoader> urlClass = URLClassLoader.class;
Method method = urlClass.getDeclaredMethod("addURL", new Class[] { URL.class });
method.setAccessible(true);
method.invoke(urlClassLoader, new Object[] { url });
}
/*
* (non-Javadoc)
*
* @see the.bytecode.club.bootloader.ILoader#bind(the.bytecode.club.bootloader .resource.ExternalResource)
*/
@Override
public void bind(ExternalResource<Object> resource) {
try {
if (resource != null) {
URL url = resource.getLocation();
if (url != null) {
extendClassPath(url);
}
}
}/* catch (IOException e) {
System.err.println("Error loading resource.");
e.printStackTrace();
}*/ catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
System.err.println("Error reflecting URLClassLoader.addURL(URL) ?");
e.printStackTrace();
}
}
/*
* (non-Javadoc)
*
* @see the.bytecode.club.bootloader.ILoader#findClass(java.lang.String)
*/
@Override
public Class<?> findClass(String name) throws ClassNotFoundException, NoClassDefFoundError {
return Class.forName(name);
}
/*
* (non-Javadoc)
*
* @see the.bytecode.club.bootloader.ILoader#loadClass(java.lang.String)
*/
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException, NoClassDefFoundError {
return findClass(name);
}
}

View file

@ -1,14 +1,14 @@
package the.bytecode.club.bootloader;
import org.objectweb.asm.tree.ClassNode;
import the.bytecode.club.bootloader.resource.ExternalResource;
/**
* @author Bibl (don't ban me pls)
* @created 19 Jul 2015 02:29:43
*/
public abstract interface ILoader {
public abstract interface ILoader<T> {
public abstract void bind(ExternalResource<JarContents<ClassNode>> resource);
public abstract void bind(ExternalResource<T> resource);
abstract Class<?> findClass(String name) throws ClassNotFoundException, NoClassDefFoundError;

View file

@ -11,13 +11,18 @@ import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
import the.bytecode.club.bootloader.resource.ExternalResource;
import the.bytecode.club.bootloader.resource.JarContents;
import the.bytecode.club.bootloader.util.ClassTree;
/**
* @author Bibl (don't ban me pls)
* @created 19 Jul 2015 02:48:41
*
* TODO: Resource loading
*/
public class LibraryClassLoader extends ClassLoader implements ILoader {
@Deprecated
public class LibraryClassLoader extends ClassLoader implements ILoader<JarContents<ClassNode>> {
private final Set<JarContents<ClassNode>> binded;
private final Map<String, Class<?>> classCache;

View file

@ -0,0 +1,10 @@
package the.bytecode.club.bootloader;
/**
* @author Bibl (don't ban me pls)
* @created 21 Jul 2015 00:14:53
*/
public abstract interface LoaderFactory<T> {
public abstract ILoader<T> spawnLoader();
}

View file

@ -1,4 +1,4 @@
package the.bytecode.club.bootloader;
package the.bytecode.club.bootloader.resource;
import java.util.ArrayList;
import java.util.Collection;

View file

@ -0,0 +1,26 @@
package the.bytecode.club.bootloader.resource;
import java.io.IOException;
import java.net.URL;
/**
* @author Bibl (don't ban me pls)
* @created 21 Jul 2015 00:29:11
*/
public class EmptyExternalResource<T> extends ExternalResource<T> {
/**
* @param location
*/
public EmptyExternalResource(URL location) {
super(location);
}
/* (non-Javadoc)
* @see the.bytecode.club.bootloader.resource.ExternalResource#load()
*/
@Override
public T load() throws IOException {
throw new UnsupportedOperationException();
}
}

View file

@ -1,4 +1,4 @@
package the.bytecode.club.bootloader;
package the.bytecode.club.bootloader.resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

View file

@ -1,4 +1,4 @@
package the.bytecode.club.bootloader;
package the.bytecode.club.bootloader.resource;
import java.io.IOException;
import java.net.URL;

View file

@ -1,4 +1,4 @@
package the.bytecode.club.bootloader;
package the.bytecode.club.bootloader.resource;
import java.util.ArrayList;
import java.util.Collection;

View file

@ -1,4 +1,4 @@
package the.bytecode.club.bootloader;
package the.bytecode.club.bootloader.resource;
import java.io.File;
import java.net.JarURLConnection;

View file

@ -1,4 +1,4 @@
package the.bytecode.club.bootloader;
package the.bytecode.club.bootloader.resource;
import java.util.Arrays;

View file

@ -1,4 +1,4 @@
package the.bytecode.club.bootloader;
package the.bytecode.club.bootloader.resource;
/**
* Type of Jar Stored.

View file

@ -1,4 +1,4 @@
package the.bytecode.club.bootloader;
package the.bytecode.club.bootloader.resource;
import java.net.URL;

View file

@ -1,4 +1,4 @@
package the.bytecode.club.bootloader;
package the.bytecode.club.bootloader.util;
import java.util.Collection;
import java.util.HashMap;

View file

@ -1,7 +1,7 @@
package the.bytecode.club.bootloader;
package the.bytecode.club.bootloader.util;
import static the.bytecode.club.bootloader.ClassHelper.convertToMap;
import static the.bytecode.club.bootloader.ClassHelper.copyOf;
import static the.bytecode.club.bootloader.util.ClassHelper.convertToMap;
import static the.bytecode.club.bootloader.util.ClassHelper.copyOf;
import java.util.Collection;
import java.util.Collections;

View file

@ -1,4 +1,4 @@
package the.bytecode.club.bootloader;
package the.bytecode.club.bootloader.util;
/**
* @author Bibl (don't ban me pls)

View file

@ -1,4 +1,4 @@
package the.bytecode.club.bootloader;
package the.bytecode.club.bootloader.util;
import java.util.HashMap;

View file

@ -1,4 +1,4 @@
package the.bytecode.club.bootloader;
package the.bytecode.club.bootloader.util;
import java.util.HashSet;
import java.util.Set;

View file

@ -1,4 +1,4 @@
package the.bytecode.club.bootloader;
package the.bytecode.club.bootloader.util;
/**
* @author Bibl (don't ban me pls)