diff --git a/src/the/bytecode/club/bootloader/AbstractLoaderFactory.java b/src/the/bytecode/club/bootloader/AbstractLoaderFactory.java new file mode 100644 index 00000000..911e773b --- /dev/null +++ b/src/the/bytecode/club/bootloader/AbstractLoaderFactory.java @@ -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> FACTORYCACHE = new HashMap>(); + + 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 > LoaderFactory find() { + return find(DEFAULT_KEY); + } + + @SuppressWarnings("unchecked") + public static > LoaderFactory 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) FACTORYCACHE.get(key); + } +} \ No newline at end of file diff --git a/src/the/bytecode/club/bootloader/Boot.java b/src/the/bytecode/club/bootloader/Boot.java index 7b5812b0..364c3026 100644 --- a/src/the/bytecode/club/bootloader/Boot.java +++ b/src/the/bytecode/club/bootloader/Boot.java @@ -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(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() { + @Override + public ILoader spawnLoader() { + return new ClassPathLoader(); + } + }); } } \ No newline at end of file diff --git a/src/the/bytecode/club/bootloader/ClassPathLoader.java b/src/the/bytecode/club/bootloader/ClassPathLoader.java new file mode 100644 index 00000000..586ca2bc --- /dev/null +++ b/src/the/bytecode/club/bootloader/ClassPathLoader.java @@ -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 { + + void extendClassPath(URL url) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, + InvocationTargetException { + URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); + Class 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 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); + } +} \ No newline at end of file diff --git a/src/the/bytecode/club/bootloader/ILoader.java b/src/the/bytecode/club/bootloader/ILoader.java index 4a32679e..a79004d9 100644 --- a/src/the/bytecode/club/bootloader/ILoader.java +++ b/src/the/bytecode/club/bootloader/ILoader.java @@ -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 { - public abstract void bind(ExternalResource> resource); + public abstract void bind(ExternalResource resource); abstract Class findClass(String name) throws ClassNotFoundException, NoClassDefFoundError; diff --git a/src/the/bytecode/club/bootloader/LibraryClassLoader.java b/src/the/bytecode/club/bootloader/LibraryClassLoader.java index 98675ba0..f98cba90 100644 --- a/src/the/bytecode/club/bootloader/LibraryClassLoader.java +++ b/src/the/bytecode/club/bootloader/LibraryClassLoader.java @@ -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> { private final Set> binded; private final Map> classCache; diff --git a/src/the/bytecode/club/bootloader/LoaderFactory.java b/src/the/bytecode/club/bootloader/LoaderFactory.java new file mode 100644 index 00000000..056fd327 --- /dev/null +++ b/src/the/bytecode/club/bootloader/LoaderFactory.java @@ -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 { + + public abstract ILoader spawnLoader(); +} \ No newline at end of file diff --git a/src/the/bytecode/club/bootloader/DataContainer.java b/src/the/bytecode/club/bootloader/resource/DataContainer.java similarity index 86% rename from src/the/bytecode/club/bootloader/DataContainer.java rename to src/the/bytecode/club/bootloader/resource/DataContainer.java index 7ed158c1..1d748ab8 100644 --- a/src/the/bytecode/club/bootloader/DataContainer.java +++ b/src/the/bytecode/club/bootloader/resource/DataContainer.java @@ -1,4 +1,4 @@ -package the.bytecode.club.bootloader; +package the.bytecode.club.bootloader.resource; import java.util.ArrayList; import java.util.Collection; diff --git a/src/the/bytecode/club/bootloader/resource/EmptyExternalResource.java b/src/the/bytecode/club/bootloader/resource/EmptyExternalResource.java new file mode 100644 index 00000000..d88e958b --- /dev/null +++ b/src/the/bytecode/club/bootloader/resource/EmptyExternalResource.java @@ -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 extends ExternalResource { + + /** + * @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(); + } +} \ No newline at end of file diff --git a/src/the/bytecode/club/bootloader/ExternalLibrary.java b/src/the/bytecode/club/bootloader/resource/ExternalLibrary.java similarity index 94% rename from src/the/bytecode/club/bootloader/ExternalLibrary.java rename to src/the/bytecode/club/bootloader/resource/ExternalLibrary.java index b8b7194a..efc98868 100644 --- a/src/the/bytecode/club/bootloader/ExternalLibrary.java +++ b/src/the/bytecode/club/bootloader/resource/ExternalLibrary.java @@ -1,4 +1,4 @@ -package the.bytecode.club.bootloader; +package the.bytecode.club.bootloader.resource; import java.io.ByteArrayOutputStream; import java.io.IOException; diff --git a/src/the/bytecode/club/bootloader/ExternalResource.java b/src/the/bytecode/club/bootloader/resource/ExternalResource.java similarity index 91% rename from src/the/bytecode/club/bootloader/ExternalResource.java rename to src/the/bytecode/club/bootloader/resource/ExternalResource.java index 0832c268..aa185125 100644 --- a/src/the/bytecode/club/bootloader/ExternalResource.java +++ b/src/the/bytecode/club/bootloader/resource/ExternalResource.java @@ -1,4 +1,4 @@ -package the.bytecode.club.bootloader; +package the.bytecode.club.bootloader.resource; import java.io.IOException; import java.net.URL; diff --git a/src/the/bytecode/club/bootloader/JarContents.java b/src/the/bytecode/club/bootloader/resource/JarContents.java similarity index 94% rename from src/the/bytecode/club/bootloader/JarContents.java rename to src/the/bytecode/club/bootloader/resource/JarContents.java index 47748317..37c59b1e 100644 --- a/src/the/bytecode/club/bootloader/JarContents.java +++ b/src/the/bytecode/club/bootloader/resource/JarContents.java @@ -1,4 +1,4 @@ -package the.bytecode.club.bootloader; +package the.bytecode.club.bootloader.resource; import java.util.ArrayList; import java.util.Collection; diff --git a/src/the/bytecode/club/bootloader/JarInfo.java b/src/the/bytecode/club/bootloader/resource/JarInfo.java similarity index 93% rename from src/the/bytecode/club/bootloader/JarInfo.java rename to src/the/bytecode/club/bootloader/resource/JarInfo.java index 765be06d..394913e3 100644 --- a/src/the/bytecode/club/bootloader/JarInfo.java +++ b/src/the/bytecode/club/bootloader/resource/JarInfo.java @@ -1,4 +1,4 @@ -package the.bytecode.club.bootloader; +package the.bytecode.club.bootloader.resource; import java.io.File; import java.net.JarURLConnection; diff --git a/src/the/bytecode/club/bootloader/JarResource.java b/src/the/bytecode/club/bootloader/resource/JarResource.java similarity index 90% rename from src/the/bytecode/club/bootloader/JarResource.java rename to src/the/bytecode/club/bootloader/resource/JarResource.java index a79a3fbf..6a19c78f 100644 --- a/src/the/bytecode/club/bootloader/JarResource.java +++ b/src/the/bytecode/club/bootloader/resource/JarResource.java @@ -1,4 +1,4 @@ -package the.bytecode.club.bootloader; +package the.bytecode.club.bootloader.resource; import java.util.Arrays; diff --git a/src/the/bytecode/club/bootloader/JarType.java b/src/the/bytecode/club/bootloader/resource/JarType.java similarity index 83% rename from src/the/bytecode/club/bootloader/JarType.java rename to src/the/bytecode/club/bootloader/resource/JarType.java index 2a27a59f..a06f7609 100644 --- a/src/the/bytecode/club/bootloader/JarType.java +++ b/src/the/bytecode/club/bootloader/resource/JarType.java @@ -1,4 +1,4 @@ -package the.bytecode.club.bootloader; +package the.bytecode.club.bootloader.resource; /** * Type of Jar Stored. diff --git a/src/the/bytecode/club/bootloader/LocateableJarContents.java b/src/the/bytecode/club/bootloader/resource/LocateableJarContents.java similarity index 88% rename from src/the/bytecode/club/bootloader/LocateableJarContents.java rename to src/the/bytecode/club/bootloader/resource/LocateableJarContents.java index c598dbfd..da150d73 100644 --- a/src/the/bytecode/club/bootloader/LocateableJarContents.java +++ b/src/the/bytecode/club/bootloader/resource/LocateableJarContents.java @@ -1,4 +1,4 @@ -package the.bytecode.club.bootloader; +package the.bytecode.club.bootloader.resource; import java.net.URL; diff --git a/src/the/bytecode/club/bootloader/ClassHelper.java b/src/the/bytecode/club/bootloader/util/ClassHelper.java similarity index 90% rename from src/the/bytecode/club/bootloader/ClassHelper.java rename to src/the/bytecode/club/bootloader/util/ClassHelper.java index 9a7cdd04..50089657 100644 --- a/src/the/bytecode/club/bootloader/ClassHelper.java +++ b/src/the/bytecode/club/bootloader/util/ClassHelper.java @@ -1,4 +1,4 @@ -package the.bytecode.club.bootloader; +package the.bytecode.club.bootloader.util; import java.util.Collection; import java.util.HashMap; diff --git a/src/the/bytecode/club/bootloader/ClassTree.java b/src/the/bytecode/club/bootloader/util/ClassTree.java similarity index 93% rename from src/the/bytecode/club/bootloader/ClassTree.java rename to src/the/bytecode/club/bootloader/util/ClassTree.java index 75521fbc..2e2bd464 100644 --- a/src/the/bytecode/club/bootloader/ClassTree.java +++ b/src/the/bytecode/club/bootloader/util/ClassTree.java @@ -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; diff --git a/src/the/bytecode/club/bootloader/NullCreator.java b/src/the/bytecode/club/bootloader/util/NullCreator.java similarity index 75% rename from src/the/bytecode/club/bootloader/NullCreator.java rename to src/the/bytecode/club/bootloader/util/NullCreator.java index 91fc45cf..8e942e99 100644 --- a/src/the/bytecode/club/bootloader/NullCreator.java +++ b/src/the/bytecode/club/bootloader/util/NullCreator.java @@ -1,4 +1,4 @@ -package the.bytecode.club.bootloader; +package the.bytecode.club.bootloader.util; /** * @author Bibl (don't ban me pls) diff --git a/src/the/bytecode/club/bootloader/NullPermeableHashMap.java b/src/the/bytecode/club/bootloader/util/NullPermeableHashMap.java similarity index 87% rename from src/the/bytecode/club/bootloader/NullPermeableHashMap.java rename to src/the/bytecode/club/bootloader/util/NullPermeableHashMap.java index 9a93edf4..9a63c591 100644 --- a/src/the/bytecode/club/bootloader/NullPermeableHashMap.java +++ b/src/the/bytecode/club/bootloader/util/NullPermeableHashMap.java @@ -1,4 +1,4 @@ -package the.bytecode.club.bootloader; +package the.bytecode.club.bootloader.util; import java.util.HashMap; diff --git a/src/the/bytecode/club/bootloader/SetCreator.java b/src/the/bytecode/club/bootloader/util/SetCreator.java similarity index 82% rename from src/the/bytecode/club/bootloader/SetCreator.java rename to src/the/bytecode/club/bootloader/util/SetCreator.java index fd161495..6facaa95 100644 --- a/src/the/bytecode/club/bootloader/SetCreator.java +++ b/src/the/bytecode/club/bootloader/util/SetCreator.java @@ -1,4 +1,4 @@ -package the.bytecode.club.bootloader; +package the.bytecode.club.bootloader.util; import java.util.HashSet; import java.util.Set; diff --git a/src/the/bytecode/club/bootloader/ValueCreator.java b/src/the/bytecode/club/bootloader/util/ValueCreator.java similarity index 72% rename from src/the/bytecode/club/bootloader/ValueCreator.java rename to src/the/bytecode/club/bootloader/util/ValueCreator.java index 3e6970a9..c722510d 100644 --- a/src/the/bytecode/club/bootloader/ValueCreator.java +++ b/src/the/bytecode/club/bootloader/util/ValueCreator.java @@ -1,4 +1,4 @@ -package the.bytecode.club.bootloader; +package the.bytecode.club.bootloader.util; /** * @author Bibl (don't ban me pls)