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; ...@@ -3,9 +3,12 @@ package me.felinewith.lang_toolkit.apps.bootstrap;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import me.felinewith.lang_toolkit.apps.bootstrap.util.LoaderHelper;
/** /**
* *
...@@ -18,6 +21,10 @@ public class BootstrapApp { ...@@ -18,6 +21,10 @@ public class BootstrapApp {
public static void init(String[] args) { public static void init(String[] args) {
ClassLoader cl = BootstrapApp.class.getClassLoader(); ClassLoader cl = BootstrapApp.class.getClassLoader();
System.out.print("ClassLoader: ");
System.out.println(cl.toString());
// fetch all available bootstrap descriptors
Enumeration<URL> urls = null; Enumeration<URL> urls = null;
try { urls = cl.getResources("bootstrap/bootstrap.properties"); } try { urls = cl.getResources("bootstrap/bootstrap.properties"); }
catch (IOException ex) { Logger.getLogger(BootstrapApp.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(BootstrapApp.class.getName()).log(Level.SEVERE, null, ex); }
...@@ -25,6 +32,7 @@ public class BootstrapApp { ...@@ -25,6 +32,7 @@ public class BootstrapApp {
Properties props = new Properties(); Properties props = new Properties();
URL u; URL u;
if ( urls != null ) { if ( urls != null ) {
// load up all the descriptors
while ( urls.hasMoreElements() ) { while ( urls.hasMoreElements() ) {
u = urls.nextElement(); u = urls.nextElement();
...@@ -34,14 +42,67 @@ public class BootstrapApp { ...@@ -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) { IStrappedApp app = null;
cl = classloader; IStrappedPlugin plug = null;
args = arguments; 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 ...@@ -92,7 +92,7 @@ public class BootstrapClassLoader extends ClassLoader
public void init() public void init()
{ {
Iterator<String> paths; Iterator<String> paths;
paths = cpDirs(System.getProperty("java.class.path")); paths = cpDirs(System.getProperty("java.class.path").concat(" ./libs"));
File f; File f;
String s; String s;
...@@ -198,15 +198,17 @@ public class BootstrapClassLoader extends ClassLoader ...@@ -198,15 +198,17 @@ public class BootstrapClassLoader extends ClassLoader
Iterator<File> it = jars.values().iterator(); Iterator<File> it = jars.values().iterator();
File f; File f;
JarFile jf; JarFile jf;
ZipEntry ze; JarEntry je;
while ( it.hasNext() ) while ( it.hasNext() )
{ {
f = it.next(); f = it.next();
jf = new JarFile(f); 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(); jf.close();
if ( ze != null ) { ar.add(new URL("jar:file:".concat(f.getCanonicalPath()).concat("!/").concat(name))); }
} }
return new Iterator2Enum<>(ar.iterator()); return new Iterator2Enum<>(ar.iterator());
......
...@@ -5,7 +5,7 @@ package me.felinewith.lang_toolkit.apps.bootstrap; ...@@ -5,7 +5,7 @@ package me.felinewith.lang_toolkit.apps.bootstrap;
* @author jlhawkwell * @author jlhawkwell
*/ */
public interface IStrappedApp { public interface IStrappedApp {
public void preStart(); public void preStart(String[] args);
public void registerPlugin(IStrappedPlugin plugin); public void registerPlugin(IStrappedPlugin plugin);
public void start(); 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; ...@@ -11,10 +11,12 @@ import me.felinewith.lang_toolkit.apps.bootstrap.IStrappedPlugin;
*/ */
public class LangBuilder implements IStrappedApp { public class LangBuilder implements IStrappedApp {
private String[] args;
private HashMap<String, IStrappedPlugin> plugins; private HashMap<String, IStrappedPlugin> plugins;
private JFrame window; private JFrame window;
@Override public void preStart() { @Override public void preStart(String[] argsStrings) {
args = argsStrings;
plugins = new HashMap<>(); plugins = new HashMap<>();
window = new JFrame("LangBuilder"); window = new JFrame("LangBuilder");
......
...@@ -50,7 +50,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma ...@@ -50,7 +50,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
</snapshotRepository> </snapshotRepository>
<site> <site>
<id>Main</id> <id>Main</id>
<name>Candle POS</name> <name>${project.name}</name>
<url>${gl.pages}</url> <url>${gl.pages}</url>
</site> </site>
</distributionManagement> </distributionManagement>
...@@ -112,9 +112,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma ...@@ -112,9 +112,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
</snapshots> </snapshots>
</repository> </repository>
<repository> <repository>
<id>internal</id> <id>external</id>
<name>SPM Internal</name> <name>SPM Externals</name>
<url>https://mvn.felinewith.me/repository/internal</url> <url>https://mvn.felinewith.me/repository/external</url>
<releases> <releases>
<enabled>true</enabled> <enabled>true</enabled>
</releases> </releases>
...@@ -159,7 +159,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma ...@@ -159,7 +159,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
</dependencies> </dependencies>
<build> <build>
<defaultGoal>clean test package site</defaultGoal> <defaultGoal>clean test package</defaultGoal>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <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!