--- miscJava/bukkit-minecraft-plugins/HoerupUtils/src/dk/thoerup/bukkit/hoeruputils/web/StatusListener.java 2011/12/22 20:30:28 1671 +++ miscJava/bukkit-minecraft-plugins/HoerupUtils/src/main/java/dk/thoerup/bukkit/hoeruputils/web/StatusListener.java 2013/03/20 14:50:00 1935 @@ -1,22 +1,21 @@ package dk.thoerup.bukkit.hoeruputils.web; -import java.io.File; +import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; -import java.util.Set; -import java.util.Map; + import org.bukkit.Location; -import org.bukkit.Server; import org.bukkit.OfflinePlayer; +import org.bukkit.Server; +import org.bukkit.configuration.Configuration; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; -import org.bukkit.configuration.Configuration; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; -import org.kokakiwi.apicraft.events.ApiEvent; -import org.kokakiwi.apicraft.events.ApiListener; -public class StatusListener extends ApiListener { +public class StatusListener implements HttpHandler{ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @@ -24,7 +23,11 @@ Configuration loginConfig; Configuration homeConfig; - public StatusListener(Server server, Configuration loginConfig, Configuration homeConfig) { + long cachedTime = 0; + String cachedXml = ""; + + public StatusListener(Plugin plugin, Server server, Configuration loginConfig, Configuration homeConfig) { + this.server = server; this.loginConfig = loginConfig; this.homeConfig = homeConfig; @@ -39,10 +42,33 @@ } @Override - public void onApiEvent(ApiEvent event) { - if(!event.path[0].equalsIgnoreCase("webstatus")) { + public void handle(HttpExchange http) throws IOException { +/* if(!event.path[0].equalsIgnoreCase("webstatus")) { return; + }*/ + + String xml; + + synchronized(this) { //is this necessary ? + long now = System.currentTimeMillis(); + if ( (now-cachedTime) > 5000) { + xml = buildXML(); + cachedXml = xml; + cachedTime = now; + } else { + xml = cachedXml; + } } + + +/* event.setResponse(xml); + event.setActionTaken(true);*/ + + http.getResponseBody().write( xml.getBytes() ); + } + + private String buildXML() { + StringBuilder sb = new StringBuilder(); sb.append(" \n"); @@ -121,8 +147,8 @@ sb.append("\n"); sb.append(""); - event.setResponse(sb.toString()); - event.setActionTaken(true); + + return sb.toString(); }