From 4e0dfe9503ace4e645521eb5af8839853c6330e5 Mon Sep 17 00:00:00 2001 From: Parnassian Date: Sun, 23 Feb 2014 15:16:56 +0100 Subject: [PATCH] Abstract Library System --- .../src/org/parabot/core/jython/Jython.java | 91 ------------------- .../src/org/parabot/core/lib/Library.java | 61 +++++++++++++ .../org/parabot/core/lib/javafx/JavaFX.java | 71 +++++++++++++++ .../org/parabot/core/lib/jython/Jython.java | 72 +++++++++++++++ .../core/parsers/scripts/ScriptParser.java | 2 +- .../org/parabot/environment/Environment.java | 30 ++++-- 6 files changed, 226 insertions(+), 101 deletions(-) delete mode 100644 parabotv2/src/org/parabot/core/jython/Jython.java create mode 100644 parabotv2/src/org/parabot/core/lib/Library.java create mode 100644 parabotv2/src/org/parabot/core/lib/javafx/JavaFX.java create mode 100644 parabotv2/src/org/parabot/core/lib/jython/Jython.java diff --git a/parabotv2/src/org/parabot/core/jython/Jython.java b/parabotv2/src/org/parabot/core/jython/Jython.java deleted file mode 100644 index 4f879bf..0000000 --- a/parabotv2/src/org/parabot/core/jython/Jython.java +++ /dev/null @@ -1,91 +0,0 @@ -package org.parabot.core.jython; - -import java.io.File; -import java.net.URL; - -import org.parabot.core.Core; -import org.parabot.core.Directories; -import org.parabot.core.build.BuildPath; - -/** - * - * Jython util class - * - * @author Everel - * - */ -public class Jython { - private static boolean valid; - - /** - * Determines if jython jar has been downloaded. - * @return false if jython jar has not been downloaded..... - */ - public static final boolean hasJar() { - return getJarFile().exists(); - } - - /** - * Adds the jython jar to the build path - */ - public static void init() { - if(!hasJar()) { - System.err.println("Failed to load jython... [jar missing]"); - return; - } - Core.verbose("Adding jyton jar file to build path: " + getLocalJarFile().getPath()); - BuildPath.add(getLocalJarFile()); - - try { - Class.forName("org.python.Version"); - valid = true; - } catch (ClassNotFoundException e) { - System.err.println("Failed to add jython to build path, or incorrupt download"); - } - - Core.verbose("Jython initialized."); - } - - /** - * Determines if jython jar has been successfully added to the buildpath - * @return if jython jar was successfully added to the buildpath - */ - public static boolean isValid() { - return valid; - } - - /** - * Gets jython's local jar file - * @return file - */ - public static final File getJarFile() { - return new File(Directories.getCachePath(), "jython.jar"); - } - - /** - * Gets jython's local jar file in URL format - * @return URL - */ - public static final URL getLocalJarFile() { - try { - return getJarFile().toURI().toURL(); - } catch (Throwable t ) { - t.printStackTrace(); - } - return null; - } - - /** - * Location where jython jar is located on the parabot site - * @return URL - */ - public static final URL getDownloadLink() { - try { - return new URL("http://bot.parabot.org/libs/jython.jar"); - } catch (Throwable t ) { - t.printStackTrace(); - } - return null; - } - -} diff --git a/parabotv2/src/org/parabot/core/lib/Library.java b/parabotv2/src/org/parabot/core/lib/Library.java new file mode 100644 index 0000000..03417d8 --- /dev/null +++ b/parabotv2/src/org/parabot/core/lib/Library.java @@ -0,0 +1,61 @@ +package org.parabot.core.lib; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; + +/** + * + * @author Everel + * + */ +public abstract class Library { + + /** + * Determines if this library jar has already been downloaded. + * @return false if library jar has not been downloaded, otherwise true + */ + public boolean hasJar() { + return getJarFile().exists(); + } + + /** + * Adds the library to the buildpath and validates if it has been added + */ + public abstract void init(); + + /** + * Determines if library has been added to the buildpath + * @return true if library has been added to the buildpath, otherwise false. + */ + public abstract boolean isAdded(); + + /** + * Gets the local file target/location of the jar file + * @return local file (target) to library + */ + public abstract File getJarFile(); + + /** + * Gets download url to the library + * @return url + */ + public abstract URL getDownloadLink(); + + + /** + * Fetches URL from {@link Library#getJarFile()} + * @return URL to local library jar file + */ + public URL getJarFileURL() { + try { + return getJarFile().toURI().toURL(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + return null; + } + + public abstract String getLibraryName(); + +} diff --git a/parabotv2/src/org/parabot/core/lib/javafx/JavaFX.java b/parabotv2/src/org/parabot/core/lib/javafx/JavaFX.java new file mode 100644 index 0000000..fca8b06 --- /dev/null +++ b/parabotv2/src/org/parabot/core/lib/javafx/JavaFX.java @@ -0,0 +1,71 @@ +package org.parabot.core.lib.javafx; + +import java.io.File; +import java.net.URL; + +import org.parabot.core.Core; +import org.parabot.core.Directories; +import org.parabot.core.build.BuildPath; +import org.parabot.core.lib.Library; + +/** + * + * Jython util class + * + * @author Everel + * + */ +public class JavaFX extends Library { + private static boolean valid; + + @Override + public void init() { + if (!hasJar()) { + System.err.println("Failed to load javafx... [jar missing]"); + return; + } + Core.verbose("Adding javafx jar file to build path: " + + getJarFileURL().getPath()); + BuildPath.add(getJarFileURL()); + + try { + Class.forName("javafx.application.Application"); + valid = true; + } catch (ClassNotFoundException e) { + System.err + .println("Failed to add javafx to build path, or incorrupt download"); + } + + Core.verbose("JavaFX initialized."); + } + + @Override + public boolean isAdded() { + return valid; + } + + @Override + public File getJarFile() { + return new File(Directories.getCachePath(), "javafx.jar"); + } + + @Override + public URL getDownloadLink() { + try { + return new URL("http://bot.parabot.org/libs/jfxrt.jar"); + } catch (Throwable t) { + t.printStackTrace(); + } + return null; + } + + @Override + public String getLibraryName() { + return "JavaFX"; + } + + public static boolean isValid() { + return valid; + } + +} diff --git a/parabotv2/src/org/parabot/core/lib/jython/Jython.java b/parabotv2/src/org/parabot/core/lib/jython/Jython.java new file mode 100644 index 0000000..b724841 --- /dev/null +++ b/parabotv2/src/org/parabot/core/lib/jython/Jython.java @@ -0,0 +1,72 @@ +package org.parabot.core.lib.jython; + +import java.io.File; +import java.net.URL; + +import org.parabot.core.Core; +import org.parabot.core.Directories; +import org.parabot.core.build.BuildPath; +import org.parabot.core.lib.Library; + +/** + * + * Jython util class + * + * @author Everel + * + */ +public class Jython extends Library { + private static boolean valid; + + + @Override + public void init() { + if (!hasJar()) { + System.err.println("Failed to load jython... [jar missing]"); + return; + } + Core.verbose("Adding jyton jar file to build path: " + + getJarFileURL().getPath()); + BuildPath.add(getJarFileURL()); + + try { + Class.forName("org.python.Version"); + valid = true; + } catch (ClassNotFoundException e) { + System.err + .println("Failed to add jython to build path, or incorrupt download"); + } + + Core.verbose("Jython initialized."); + } + + @Override + public boolean isAdded() { + return valid; + } + + @Override + public File getJarFile() { + return new File(Directories.getCachePath(), "jython.jar"); + } + + @Override + public URL getDownloadLink() { + try { + return new URL("http://bot.parabot.org/libs/jython.jar"); + } catch (Throwable t) { + t.printStackTrace(); + } + return null; + } + + @Override + public String getLibraryName() { + return "Jython"; + } + + public static boolean isValid() { + return valid; + } + +} diff --git a/parabotv2/src/org/parabot/core/parsers/scripts/ScriptParser.java b/parabotv2/src/org/parabot/core/parsers/scripts/ScriptParser.java index 431fb86..3787c90 100644 --- a/parabotv2/src/org/parabot/core/parsers/scripts/ScriptParser.java +++ b/parabotv2/src/org/parabot/core/parsers/scripts/ScriptParser.java @@ -2,7 +2,7 @@ package org.parabot.core.parsers.scripts; import org.parabot.core.Core; import org.parabot.core.desc.ScriptDescription; -import org.parabot.core.jython.Jython; +import org.parabot.core.lib.jython.Jython; import org.parabot.environment.scripts.ScriptExecuter; import java.util.ArrayList; diff --git a/parabotv2/src/org/parabot/environment/Environment.java b/parabotv2/src/org/parabot/environment/Environment.java index 401a1c3..8ddafd0 100644 --- a/parabotv2/src/org/parabot/environment/Environment.java +++ b/parabotv2/src/org/parabot/environment/Environment.java @@ -1,12 +1,17 @@ package org.parabot.environment; +import java.util.LinkedList; + import org.parabot.core.Core; import org.parabot.core.desc.ServerDescription; -import org.parabot.core.jython.Jython; +import org.parabot.core.lib.Library; +import org.parabot.core.lib.javafx.JavaFX; +import org.parabot.core.lib.jython.Jython; import org.parabot.core.parsers.servers.ServerParser; import org.parabot.core.ui.components.VerboseLoader; import org.parabot.environment.api.utils.WebUtil; + /** * * Initiliazes the bot environment @@ -22,15 +27,22 @@ public class Environment { * @param url */ public static void load(final ServerDescription desc) { - if (!Jython.hasJar()) { - Core.verbose("Downloading jython..."); - VerboseLoader.setState("Downloading jython..."); - WebUtil.downloadFile(Jython.getDownloadLink(), Jython.getJarFile(), - VerboseLoader.get()); - Core.verbose("Downloaded jython."); + + LinkedList libs = new LinkedList(); + libs.add(new Jython()); + libs.add(new JavaFX()); + + for(Library lib : libs) { + if(!lib.hasJar()) { + Core.verbose("Downloading " + lib.getLibraryName() + "..."); + VerboseLoader.setState("Downloading " + lib.getLibraryName() + "..."); + WebUtil.downloadFile(lib.getDownloadLink(), lib.getJarFile(), VerboseLoader.get()); + Core.verbose("Downloaded " + lib.getLibraryName() + "."); + } + Core.verbose("Initializing " + lib.getLibraryName()); + lib.init(); } - Core.verbose("Initializing jython..."); - Jython.init(); + Core.verbose("Loading server: " + desc.toString());