Commit 75fdc163 by Jessica Hawkwell

fixed classloader, made unlinked loading work

1 parent fca8b3a4
......@@ -3,9 +3,12 @@ package me.felinewith.lang_toolkit.apps.bootstrap;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import me.felinewith.lang_toolkit.apps.bootstrap.util.LoaderHelper;
/**
*
......@@ -18,6 +21,10 @@ public class BootstrapApp {
public static void init(String[] args) {
ClassLoader cl = BootstrapApp.class.getClassLoader();
System.out.print("ClassLoader: ");
System.out.println(cl.toString());
// fetch all available bootstrap descriptors
Enumeration<URL> urls = null;
try { urls = cl.getResources("bootstrap/bootstrap.properties"); }
catch (IOException ex) { Logger.getLogger(BootstrapApp.class.getName()).log(Level.SEVERE, null, ex); }
......@@ -25,6 +32,7 @@ public class BootstrapApp {
Properties props = new Properties();
URL u;
if ( urls != null ) {
// load up all the descriptors
while ( urls.hasMoreElements() ) {
u = urls.nextElement();
......@@ -34,14 +42,67 @@ public class BootstrapApp {
}
}
// TODO: initialize app then iterate plugins
}
}
props.list(System.out);
LoaderHelper<IStrappedApp> appHelper = new LoaderHelper<>(cl, IStrappedApp.class);
LoaderHelper<IStrappedPlugin> plugHelper = new LoaderHelper<>(cl, IStrappedPlugin.class);
public BootstrapApp(ClassLoader classloader, String[] arguments) {
cl = classloader;
args = arguments;
}
IStrappedApp app = null;
IStrappedPlugin plug = null;
HashMap<String, IStrappedPlugin> plugins = new HashMap<>();
// get the app
if ( props.containsKey("app") ) {
try {
app = appHelper.fetchClass(props.getProperty("app"));
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
Logger.getLogger(BootstrapApp.class.getName()).log(Level.SEVERE, null, ex);
return;
}
}
else {
Logger.getLogger(BootstrapApp.class.getName()).log(Level.SEVERE, null,
new NoClassDefFoundError("Application did not provide an application class."));
return;
}
if ( app == null ) {
Logger.getLogger(BootstrapApp.class.getName()).log(Level.SEVERE, null,
new ClassNotFoundException("Application class could not be loaded."));
return;
}
// run through plugins
Iterator<String> it = props.stringPropertyNames().iterator();
String name;
while ( it.hasNext() ) {
name = it.next();
if ( !name.equals("app") ) {
try { plug = plugHelper.fetchClass(props.getProperty(name)); }
catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
Logger.getLogger(BootstrapApp.class.getName()).log(Level.WARNING, null, ex);
}
if ( plug != null ) { plugins.put(plug.getName(), plug); }
plug = null;
}
}
// start it up
app.preStart(args);
// check plugins
if ( !plugins.isEmpty() ) {
// register all the plugins
Iterator<IStrappedPlugin> itp = plugins.values().iterator();
while ( itp.hasNext() ) {
plug = itp.next();
app.registerPlugin(plug);
}
}
// readySetGO!
app.start();
}
}
}
......@@ -92,7 +92,7 @@ public class BootstrapClassLoader extends ClassLoader
public void init()
{
Iterator<String> paths;
paths = cpDirs(System.getProperty("java.class.path"));
paths = cpDirs(System.getProperty("java.class.path").concat(" ./libs"));
File f;
String s;
......@@ -198,15 +198,17 @@ public class BootstrapClassLoader extends ClassLoader
Iterator<File> it = jars.values().iterator();
File f;
JarFile jf;
ZipEntry ze;
JarEntry je;
while ( it.hasNext() )
{
f = it.next();
jf = new JarFile(f);
ze = jf.getEntry(name);
je = jf.getJarEntry(name);
if ( je != null ) {
ar.add(new URL("jar:file:".concat(f.getCanonicalPath()).concat("!/").concat(name)));
}
jf.close();
if ( ze != null ) { ar.add(new URL("jar:file:".concat(f.getCanonicalPath()).concat("!/").concat(name))); }
}
return new Iterator2Enum<>(ar.iterator());
......
......@@ -5,7 +5,7 @@ package me.felinewith.lang_toolkit.apps.bootstrap;
* @author jlhawkwell
*/
public interface IStrappedApp {
public void preStart();
public void preStart(String[] args);
public void registerPlugin(IStrappedPlugin plugin);
public void start();
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package me.felinewith.lang_toolkit.apps.bootstrap.util;
/**
*
* @author jlhawkwell
*/
public class LoaderHelper<T> {
ClassLoader cl;
Class<T> t;
public LoaderHelper(ClassLoader classLoader, Class<T> test) {
cl = classLoader;
t = test;
}
public T fetchClass(String className)
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class<?> clz = null;
Object o = null;
clz = cl.loadClass(className);
if ( clz == null ) { return null; }
o = clz.newInstance();
if ( o == null ) { return null; }
if ( !t.isInstance(o) ) { throw new ClassCastException("Discovered class does not match type"); }
return t.cast(o);
}
}
......@@ -11,10 +11,12 @@ import me.felinewith.lang_toolkit.apps.bootstrap.IStrappedPlugin;
*/
public class LangBuilder implements IStrappedApp {
private String[] args;
private HashMap<String, IStrappedPlugin> plugins;
private JFrame window;
@Override public void preStart() {
@Override public void preStart(String[] argsStrings) {
args = argsStrings;
plugins = new HashMap<>();
window = new JFrame("LangBuilder");
......
......@@ -50,7 +50,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
</snapshotRepository>
<site>
<id>Main</id>
<name>Candle POS</name>
<name>${project.name}</name>
<url>${gl.pages}</url>
</site>
</distributionManagement>
......@@ -112,9 +112,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
</snapshots>
</repository>
<repository>
<id>internal</id>
<name>SPM Internal</name>
<url>https://mvn.felinewith.me/repository/internal</url>
<id>external</id>
<name>SPM Externals</name>
<url>https://mvn.felinewith.me/repository/external</url>
<releases>
<enabled>true</enabled>
</releases>
......@@ -159,7 +159,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
</dependencies>
<build>
<defaultGoal>clean test package site</defaultGoal>
<defaultGoal>clean test package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!