From 7d591f015e77e51b9e37b02a050e8df7911035b9 Mon Sep 17 00:00:00 2001 From: Emma Stone Date: Fri, 3 Feb 2017 15:56:57 +0000 Subject: [PATCH 01/14] [BUGFIX] Fixed a bug in RandomType MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The id’s for the types were all the same --- .../java/org/parabot/environment/randoms/RandomType.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/parabot/environment/randoms/RandomType.java b/src/main/java/org/parabot/environment/randoms/RandomType.java index 250bd9a..3743812 100644 --- a/src/main/java/org/parabot/environment/randoms/RandomType.java +++ b/src/main/java/org/parabot/environment/randoms/RandomType.java @@ -6,9 +6,9 @@ package org.parabot.environment.randoms; public enum RandomType { SCRIPT(0, "Script"), - ON_SCRIPT_START(0, "On script start"), - ON_SERVER_START(0, "On server start"), - ON_SCRIPT_FINISH(0, "On script finish"); + ON_SCRIPT_START(1, "On script start"), + ON_SERVER_START(2, "On server start"), + ON_SCRIPT_FINISH(3, "On script finish"); private int id; private String name; From 5a2e12eb5496e203c1cbe999e63044b46944e5e7 Mon Sep 17 00:00:00 2001 From: Emma Stone Date: Fri, 3 Feb 2017 17:34:28 +0000 Subject: [PATCH 02/14] [BUGFIX] Remade validateCache Closes issue #167. --- src/main/java/org/parabot/core/Core.java | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/parabot/core/Core.java b/src/main/java/org/parabot/core/Core.java index 16f0204..ff70069 100644 --- a/src/main/java/org/parabot/core/Core.java +++ b/src/main/java/org/parabot/core/Core.java @@ -20,6 +20,7 @@ import java.net.URISyntaxException; import java.net.URLEncoder; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.Date; /** * The core of parabot @@ -206,31 +207,20 @@ public class Core { } /** - * Validates the cache and removes the cache contents if required + * Method that removes the cache contents after 3 days */ private static void validateCache() { File[] cache = Directories.getCachePath().listFiles(); - Integer lowest = null; + if (cache != null) { for (File f : cache) { - int date = (int) (f.lastModified() / 1000); - if (lowest == null || date < lowest) { - lowest = date; + long age = new Date().getTime() - f.lastModified(); + + if (age > 3 * 24 * 60 * 60 * 1000) { + f.delete(); } } } - - try { - JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(WebUtil.getContents("http://bdn.parabot.org/api/v2/bot/cache", "date=" + lowest)); - if ((boolean) object.get("result")) { - Core.verbose("Making space for the latest cache files"); - Directories.clearCache(); - } else { - Core.verbose("Cache is up to date"); - } - } catch (MalformedURLException | ParseException e) { - e.printStackTrace(); - } } public static void downloadNewVersion() { From 6be4aaf8999a2f39fb0fad377c143d87ac03a4b2 Mon Sep 17 00:00:00 2001 From: Emma Stone Date: Fri, 3 Feb 2017 19:02:22 +0000 Subject: [PATCH 03/14] [FEATURE] Added redirectErrorStream --- .../org/parabot/core/asm/redirect/ProcessBuilderRedirect.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/parabot/core/asm/redirect/ProcessBuilderRedirect.java b/src/main/java/org/parabot/core/asm/redirect/ProcessBuilderRedirect.java index 9df397d..ddee308 100644 --- a/src/main/java/org/parabot/core/asm/redirect/ProcessBuilderRedirect.java +++ b/src/main/java/org/parabot/core/asm/redirect/ProcessBuilderRedirect.java @@ -2,4 +2,7 @@ package org.parabot.core.asm.redirect; public class ProcessBuilderRedirect { + public static ProcessBuilder redirectErrorStream(ProcessBuilder pb, boolean redirectErrorStream) { + return pb; + } } From e18e59f502750a4943559f29a7728cc2626bfca2 Mon Sep 17 00:00:00 2001 From: JKetelaar Date: Fri, 3 Feb 2017 21:02:13 +0100 Subject: [PATCH 04/14] [TASK] Updated internal API to 1.4.5 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c356eb2..a97cdad 100755 --- a/pom.xml +++ b/pom.xml @@ -73,7 +73,7 @@ org.parabot internal-api - 1.4.46 + 1.4.5 From 801cd9f6d112aa8153176ccb68d3342871654978 Mon Sep 17 00:00:00 2001 From: JKetelaar Date: Fri, 3 Feb 2017 21:02:31 +0100 Subject: [PATCH 05/14] [TASK] Removed invalid cache clearing --- src/main/java/org/parabot/core/Core.java | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/parabot/core/Core.java b/src/main/java/org/parabot/core/Core.java index ff70069..240b8be 100644 --- a/src/main/java/org/parabot/core/Core.java +++ b/src/main/java/org/parabot/core/Core.java @@ -210,17 +210,8 @@ public class Core { * Method that removes the cache contents after 3 days */ private static void validateCache() { - File[] cache = Directories.getCachePath().listFiles(); - - if (cache != null) { - for (File f : cache) { - long age = new Date().getTime() - f.lastModified(); - - if (age > 3 * 24 * 60 * 60 * 1000) { - f.delete(); - } - } - } + // Already handled by Directories initiating + // Method will be used once BDN V3 has a functionality for this } public static void downloadNewVersion() { From c451bb6d29226c49fc2cd33393c012462ed26ca3 Mon Sep 17 00:00:00 2001 From: JKetelaar Date: Fri, 3 Feb 2017 21:02:45 +0100 Subject: [PATCH 06/14] [TASK] Moved StringUtils to Internal API StringUtil --- .../environment/api/utils/StringUtils.java | 48 ++----------------- 1 file changed, 3 insertions(+), 45 deletions(-) diff --git a/src/main/java/org/parabot/environment/api/utils/StringUtils.java b/src/main/java/org/parabot/environment/api/utils/StringUtils.java index 892c75b..8911317 100644 --- a/src/main/java/org/parabot/environment/api/utils/StringUtils.java +++ b/src/main/java/org/parabot/environment/api/utils/StringUtils.java @@ -1,52 +1,10 @@ package org.parabot.environment.api.utils; +import org.parabot.api.misc.StringUtil; + /** * @author mkyong, JKetelaar */ -public class StringUtils { +public class StringUtils extends StringUtil { - private static java.util.Random random = new java.util.Random(); - private static char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray(); - - public static String convertHexToString(String hex) { - - StringBuilder sb = new StringBuilder(); - StringBuilder temp = new StringBuilder(); - - for (int i = 0; i < hex.length() - 1; i += 2) { - - // grab the hex in pairs - String output = hex.substring(i, (i + 2)); - // convert hex to decimal - int decimal = Integer.parseInt(output, 16); - // convert the decimal to character - sb.append((char) decimal); - - temp.append(decimal); - } - - return sb.toString(); - } - - public static String implode(String separator, String... data) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < data.length - 1; i++) { - //data.length - 1 => to not add separator at the end - if (!data[i].matches(" *")) {//empty string are ""; " "; " "; and so on - sb.append(data[i]); - sb.append(separator); - } - } - sb.append(data[data.length - 1].trim()); - return sb.toString(); - } - - public static String randomString(final int length) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < 20; i++) { - char c = chars[random.nextInt(chars.length)]; - sb.append(c); - } - return sb.toString(); - } } From f77aa730eed27e26e580a60f8bf0d069259ac478 Mon Sep 17 00:00:00 2001 From: JKetelaar Date: Fri, 3 Feb 2017 21:03:01 +0100 Subject: [PATCH 07/14] [FEATURE] Added CacheValidation unit test --- .../java/org/parabot/CacheValidation.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/test/java/org/parabot/CacheValidation.java diff --git a/src/test/java/org/parabot/CacheValidation.java b/src/test/java/org/parabot/CacheValidation.java new file mode 100644 index 0000000..c9b8e8d --- /dev/null +++ b/src/test/java/org/parabot/CacheValidation.java @@ -0,0 +1,34 @@ +package org.parabot; + +import org.junit.Assert; +import org.junit.Test; +import org.parabot.core.Directories; + +import java.io.File; +import java.io.IOException; + +/** + * @author JKetelaar + */ +public class CacheValidation { + + @Test + public void test(){ + try { + File fileOne = new File(Directories.getCachePath(), "should-exist.tmp"); + File fileTwo = new File(Directories.getCachePath(), "should-not-exist.tmp"); + + fileOne.createNewFile(); + fileTwo.createNewFile(); + + fileTwo.setLastModified(System.currentTimeMillis() / 1000 - 350000); + + Directories.clearCache(259200, false); + + Assert.assertTrue(fileOne.exists()); + Assert.assertTrue(!fileTwo.exists()); + } catch (IOException e) { + e.printStackTrace(); + } + } +} From 296025cbb2ce7b001672d6e8d7de63c29b75edfd Mon Sep 17 00:00:00 2001 From: JKetelaar Date: Fri, 3 Feb 2017 21:10:05 +0100 Subject: [PATCH 08/14] [BUGFIX] Renamed CacheValidation to CacheValidationTest Allowing JUnit to understand it has to run this test too --- .../parabot/{CacheValidation.java => CacheValidationTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/test/java/org/parabot/{CacheValidation.java => CacheValidationTest.java} (95%) diff --git a/src/test/java/org/parabot/CacheValidation.java b/src/test/java/org/parabot/CacheValidationTest.java similarity index 95% rename from src/test/java/org/parabot/CacheValidation.java rename to src/test/java/org/parabot/CacheValidationTest.java index c9b8e8d..e966b78 100644 --- a/src/test/java/org/parabot/CacheValidation.java +++ b/src/test/java/org/parabot/CacheValidationTest.java @@ -10,7 +10,7 @@ import java.io.IOException; /** * @author JKetelaar */ -public class CacheValidation { +public class CacheValidationTest { @Test public void test(){ From 0badb2f0947cffb22d8aff895371cd78866d8f4e Mon Sep 17 00:00:00 2001 From: JKetelaar Date: Fri, 3 Feb 2017 21:19:39 +0100 Subject: [PATCH 09/14] [BUGFIX] Added maven test requirement --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 780a3d9..e3121ac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,9 @@ before_install: install: - sudo apt-get update && sudo apt-get --assume-yes install zip unzip -script: "./.travis/maven-build.sh" +script: + - mvn test + - "./.travis/maven-build.sh" after_deploy: "./.travis/call-creation.sh" From 3198b3aff9dec835d8f0b6be69eded80c6e2eff7 Mon Sep 17 00:00:00 2001 From: JKetelaar Date: Fri, 3 Feb 2017 21:22:19 +0100 Subject: [PATCH 10/14] [BUGFIX] Added throwable to method, instead of catching --- .../java/org/parabot/CacheValidationTest.java | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/test/java/org/parabot/CacheValidationTest.java b/src/test/java/org/parabot/CacheValidationTest.java index e966b78..cecaed6 100644 --- a/src/test/java/org/parabot/CacheValidationTest.java +++ b/src/test/java/org/parabot/CacheValidationTest.java @@ -13,22 +13,18 @@ import java.io.IOException; public class CacheValidationTest { @Test - public void test(){ - try { - File fileOne = new File(Directories.getCachePath(), "should-exist.tmp"); - File fileTwo = new File(Directories.getCachePath(), "should-not-exist.tmp"); + public void test() throws IOException { + File fileOne = new File(Directories.getCachePath(), "should-exist.tmp"); + File fileTwo = new File(Directories.getCachePath(), "should-not-exist.tmp"); - fileOne.createNewFile(); - fileTwo.createNewFile(); + fileOne.createNewFile(); + fileTwo.createNewFile(); - fileTwo.setLastModified(System.currentTimeMillis() / 1000 - 350000); + fileTwo.setLastModified(System.currentTimeMillis() / 1000 - 350000); - Directories.clearCache(259200, false); + Directories.clearCache(259200, false); - Assert.assertTrue(fileOne.exists()); - Assert.assertTrue(!fileTwo.exists()); - } catch (IOException e) { - e.printStackTrace(); - } + Assert.assertTrue(fileOne.exists()); + Assert.assertTrue(!fileTwo.exists()); } } From 1b2375145ca5cfc802dfa00fe20e49cc81d21ca4 Mon Sep 17 00:00:00 2001 From: JKetelaar Date: Fri, 3 Feb 2017 21:22:46 +0100 Subject: [PATCH 11/14] [TASK] Removed mvn test --- .travis.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index e3121ac..780a3d9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,9 +12,7 @@ before_install: install: - sudo apt-get update && sudo apt-get --assume-yes install zip unzip -script: - - mvn test - - "./.travis/maven-build.sh" +script: "./.travis/maven-build.sh" after_deploy: "./.travis/call-creation.sh" From f58eef3c62662913e0450f973e13e8cc498769fe Mon Sep 17 00:00:00 2001 From: JKetelaar Date: Fri, 3 Feb 2017 21:25:34 +0100 Subject: [PATCH 12/14] [BUGFIX] Added validate to create required directories --- src/test/java/org/parabot/CacheValidationTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/org/parabot/CacheValidationTest.java b/src/test/java/org/parabot/CacheValidationTest.java index cecaed6..24aa1de 100644 --- a/src/test/java/org/parabot/CacheValidationTest.java +++ b/src/test/java/org/parabot/CacheValidationTest.java @@ -14,6 +14,8 @@ public class CacheValidationTest { @Test public void test() throws IOException { + Directories.validate(); + File fileOne = new File(Directories.getCachePath(), "should-exist.tmp"); File fileTwo = new File(Directories.getCachePath(), "should-not-exist.tmp"); From 3f5aaf42b64d4474ea800bf9056f95382e98d145 Mon Sep 17 00:00:00 2001 From: JKetelaar Date: Fri, 3 Feb 2017 22:49:34 +0100 Subject: [PATCH 13/14] [TASK] Seperated library loading Allowing to load libraries into the environment, seperated from loading the entire environment --- .../org/parabot/environment/Environment.java | 73 +++++++++++-------- 1 file changed, 41 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/parabot/environment/Environment.java b/src/main/java/org/parabot/environment/Environment.java index e17d0fd..b3ba652 100644 --- a/src/main/java/org/parabot/environment/Environment.java +++ b/src/main/java/org/parabot/environment/Environment.java @@ -12,40 +12,49 @@ import java.util.LinkedList; /** - * - * Initiliazes the bot environment - * - * @author Everel - * + * Initializes the bot environment + * + * @author Everel, JKetelaar */ public class Environment { - /** - * Loads a new environment - * - * @param desc - */ - public static void load(final ServerDescription desc) { - - LinkedList libs = new LinkedList<>(); - libs.add(new JavaFX()); - - for(Library lib : libs) { - if (lib.requiresJar()) { - 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("Loading server: " + desc.toString() + "..."); + /** + * Loads a new environment + * + * @param desc + */ + public static void load(final ServerDescription desc) { - ServerParser.SERVER_CACHE.get(desc).run(); - - } + LinkedList libs = new LinkedList<>(); + libs.add(new JavaFX()); + + for (Library lib : libs) { + loadLibrary(lib, true); + } + + Core.verbose("Loading server: " + desc.toString() + "..."); + + ServerParser.SERVER_CACHE.get(desc).run(); + } + + /** + * Loads library into environment + * + * @param library + * @param verboseLoader defines if verboseLoader should be enabled + */ + public static void loadLibrary(Library library, boolean verboseLoader) { + if (library.requiresJar()) { + if (!library.hasJar()) { + Core.verbose("Downloading " + library.getLibraryName() + "..."); + if (verboseLoader) { + VerboseLoader.setState("Downloading " + library.getLibraryName() + "..."); + } + WebUtil.downloadFile(library.getDownloadLink(), library.getJarFile(), VerboseLoader.get()); + Core.verbose("Downloaded " + library.getLibraryName() + "."); + } + Core.verbose("Initializing " + library.getLibraryName()); + library.init(); + } + } } From e7f4c6a15ce34979974c9336ce93f77c492986c8 Mon Sep 17 00:00:00 2001 From: JKetelaar Date: Sun, 5 Feb 2017 18:47:07 +0100 Subject: [PATCH 14/14] [BUGFIX] Removed user.home property --- .../org/parabot/core/asm/redirect/SystemRedirect.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/parabot/core/asm/redirect/SystemRedirect.java b/src/main/java/org/parabot/core/asm/redirect/SystemRedirect.java index f18fe1f..5ddfe88 100644 --- a/src/main/java/org/parabot/core/asm/redirect/SystemRedirect.java +++ b/src/main/java/org/parabot/core/asm/redirect/SystemRedirect.java @@ -26,14 +26,12 @@ public class SystemRedirect { public static String getProperty(String s) { String value; switch (s) { - case "user.home": - value = Directories.getCachePath().getAbsolutePath(); - break; case "java.class.path": value = "."; break; default: value = System.getProperty(s); + break; } System.out.printf("GetSystemProp %s = %s\n", s, value); return value; @@ -42,9 +40,6 @@ public class SystemRedirect { public static String getProperty(String s, String s2) { String value = null; switch (s2) { - case "user.home": - value = Directories.getCachePath().getAbsolutePath(); - break; case "java.class.path": value = "."; break; @@ -52,14 +47,12 @@ public class SystemRedirect { if (value == null) { switch (s) { - case "user.home": - value = Directories.getCachePath().getAbsolutePath(); - break; case "java.class.path": value = "."; break; default: value = System.getProperty(s); + break; } } System.out.printf("GetSystemProp %s = %s\n", s, value);