From 0f31ea295301aa5b0ac3c87853e349d029496d6f Mon Sep 17 00:00:00 2001 From: StevenLawson Date: Tue, 27 Aug 2013 09:01:12 -0400 Subject: [PATCH] Make it thread safe. --- .../HTTPD/TFM_HTTPD_Manager.java | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/src/me/StevenLawson/TotalFreedomMod/HTTPD/TFM_HTTPD_Manager.java b/src/me/StevenLawson/TotalFreedomMod/HTTPD/TFM_HTTPD_Manager.java index 41460d20..9feb55ec 100644 --- a/src/me/StevenLawson/TotalFreedomMod/HTTPD/TFM_HTTPD_Manager.java +++ b/src/me/StevenLawson/TotalFreedomMod/HTTPD/TFM_HTTPD_Manager.java @@ -2,8 +2,12 @@ package me.StevenLawson.TotalFreedomMod.HTTPD; import java.io.IOException; import java.util.Map; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; import me.StevenLawson.TotalFreedomMod.TFM_Log; +import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; import org.apache.commons.lang.StringUtils; +import org.bukkit.Bukkit; public class TFM_HTTPD_Manager { @@ -45,26 +49,45 @@ public class TFM_HTTPD_Manager } @Override - public Response serve(String uri, Method method, Map headers, Map params, Map files) + public Response serve(final String uri, final Method method, final Map headers, final Map params, final Map files) { Response response = null; final String[] args = StringUtils.split(uri, "/"); if (args.length >= 1) { - if ("dump".equalsIgnoreCase(args[0])) + // Hop onto the Bukkit thread, so we're safe to access the Bukkit API + Future responseCall = Bukkit.getScheduler().callSyncMethod(TotalFreedomMod.plugin, new Callable() { - response = new Module_dump(uri, method, headers, params, files).getResponse(); + @Override + public Response call() throws Exception + { + if ("dump".equalsIgnoreCase(args[0])) + { + return new Module_dump(uri, method, headers, params, files).getResponse(); + } + else if ("list".equalsIgnoreCase(args[0])) + { + return new Module_list(uri, method, headers, params, files).getResponse(); + } + else if ("help".equalsIgnoreCase(args[0])) + { + //The issue is that plugin.getDescription().getCommands() only shows commands in the plugin.yml file. + //I need to make another version of this that uses the CommandMap. + return new Module_help(uri, method, headers, params, files).getResponse(); + } + + return null; + } + }); + + try + { + response = responseCall.get(); } - else if ("list".equalsIgnoreCase(args[0])) + catch (Exception ex) { - response = new Module_list(uri, method, headers, params, files).getResponse(); - } - else if ("help".equalsIgnoreCase(args[0])) - { - //The issue is that plugin.getDescription().getCommands() only shows commands in the plugin.yml file. - //I need to make another version of this that uses the CommandMap. - response = new Module_help(uri, method, headers, params, files).getResponse(); + TFM_Log.severe(ex); } }