Abstract Library System

This commit is contained in:
Parnassian
2014-02-23 15:16:56 +01:00
parent b3c5653452
commit 4e0dfe9503
6 changed files with 226 additions and 101 deletions
@@ -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 <b>false</b> 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;
}
}
@@ -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 <b>false</b> if library jar has not been downloaded, otherwise <b>true</b>
*/
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 <b>true</b> if library has been added to the buildpath, otherwise <b>false</b>.
*/
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();
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
@@ -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<Library> libs = new LinkedList<Library>();
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());