Implemented optional gzip compression, some verbose output and the
registry pattern to organize object references.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# XmlStats
|
||||
|
||||
This plugin for the Bukkit Minecraft Server API ([Homepage](http://bukkig.org) | [Github](https://github.com/Bukkit/Bukkit )) offers the possibility to access some playerstats via xml files. The provided data is basically generated by [Stats](https://github.com/nidefawl/Stats).
|
||||
This plugin for the Bukkit Minecraft Server API ([Homepage](http://bukkit.org) | [Github](https://github.com/Bukkit/Bukkit )) offers the possibility to access some playerstats via xml files. The provided data is basically generated by [Stats](https://github.com/nidefawl/Stats).
|
||||
|
||||
## Install
|
||||
|
||||
|
||||
@@ -23,8 +23,6 @@ import java.util.ArrayList;
|
||||
*/
|
||||
public abstract class Datasource {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Fetch all players.
|
||||
*
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.iConomy.system.Account;
|
||||
import com.iConomy.system.Holdings;
|
||||
|
||||
import de.sockenklaus.XmlStats.XmlStats;
|
||||
import de.sockenklaus.XmlStats.XmlStatsRegistry;
|
||||
|
||||
/**
|
||||
* The Class MoneyDS.
|
||||
@@ -32,7 +33,7 @@ public class MoneyDS extends Datasource {
|
||||
private ArrayList<String> allPlayers;
|
||||
|
||||
public MoneyDS(){
|
||||
this.iConomy = XmlStats.getiConomyPlugin();
|
||||
this.iConomy = (iConomy)XmlStatsRegistry.get("iconomy");
|
||||
this.allPlayers = fetchAllPlayers();
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ import com.nidefawl.Stats.datasource.Category;
|
||||
import com.nidefawl.Stats.datasource.PlayerStat;
|
||||
import com.nidefawl.Stats.datasource.PlayerStatSQL;
|
||||
|
||||
import de.sockenklaus.XmlStats.XmlStats;
|
||||
import de.sockenklaus.XmlStats.XmlStatsRegistry;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
/**
|
||||
@@ -39,7 +39,7 @@ public class UserstatsDS extends Datasource {
|
||||
* Instantiates a new stats ds.
|
||||
*/
|
||||
public UserstatsDS() {
|
||||
this.statsPlugin = XmlStats.getStatsPlugin();
|
||||
this.statsPlugin = (Stats)XmlStatsRegistry.get("stats");
|
||||
this.allPlayerNames = fetchAllPlayers();
|
||||
this.stats = fetchPlayerStats(allPlayerNames);
|
||||
}
|
||||
|
||||
@@ -42,6 +42,8 @@ public class Settings {
|
||||
else {
|
||||
conf.setProperty("options.webserver-enabled", false);
|
||||
conf.setProperty("options.webserver-port", 9123);
|
||||
conf.setProperty("options.gzip-enabled", false);
|
||||
conf.setProperty("options.verbose-enabled", false);
|
||||
conf.save();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import de.sockenklaus.XmlStats.XmlWorkers.*;
|
||||
* The Class WebServer.
|
||||
*/
|
||||
@SuppressWarnings("restriction")
|
||||
public class WebServer {
|
||||
public class Webserver {
|
||||
|
||||
private InetSocketAddress address;
|
||||
private HttpServer server = null;
|
||||
@@ -37,7 +37,7 @@ public class WebServer {
|
||||
* @param port the port
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public WebServer(int port) throws IOException {
|
||||
public Webserver(int port) throws IOException {
|
||||
this.address = new InetSocketAddress(port);
|
||||
|
||||
server = HttpServer.create(address, 0);
|
||||
@@ -38,24 +38,18 @@ public class XmlStats extends JavaPlugin {
|
||||
private final static double version = 0.01;
|
||||
private final static String logprefix = "[XmlStats]";
|
||||
private boolean enabled = false;
|
||||
private static Stats Stats = null;
|
||||
private static iConomy iConomy = null;
|
||||
private static Server serverRef;
|
||||
private WebServer webServer;
|
||||
private Settings settings;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.bukkit.plugin.Plugin#onDisable()
|
||||
*/
|
||||
@Override
|
||||
public void onDisable() {
|
||||
if(this.enabled && this.webServer.isRunning()){
|
||||
Webserver webserverTemp = (Webserver)XmlStatsRegistry.get("webserver");
|
||||
|
||||
if(this.enabled && webserverTemp.isRunning()){
|
||||
this.enabled = false;
|
||||
|
||||
iConomy = null;
|
||||
Stats = null;
|
||||
|
||||
this.webServer.stopServer();
|
||||
webserverTemp.stopServer();
|
||||
|
||||
getServer().getScheduler().cancelTasks(this);
|
||||
|
||||
@@ -71,14 +65,22 @@ public class XmlStats extends JavaPlugin {
|
||||
|
||||
getDataFolder().mkdirs();
|
||||
|
||||
serverRef = getServer();
|
||||
this.settings = new Settings(this);
|
||||
XmlStatsRegistry.put("settings", new Settings(this));
|
||||
XmlStatsRegistry.put("xmlstats", this);
|
||||
|
||||
Settings settingsTemp = (Settings)XmlStatsRegistry.get("settings");
|
||||
|
||||
LogDebug("Settings read:");
|
||||
LogDebug("options.webserver-enabled: "+settingsTemp.getBoolean("options.webserver-enabled"));
|
||||
LogDebug("options.webserver-port: "+settingsTemp.getInt("options.webserver-port"));
|
||||
LogDebug("options.gzip-enabled: "+settingsTemp.getBoolean("options.gzip-enabled"));
|
||||
LogDebug("options.verbose-enabled: "+settingsTemp.getBoolean("options.verbose-enabled"));
|
||||
|
||||
this.hookPlugins();
|
||||
|
||||
if (this.settings.getBoolean("options.webserver-enabled")){
|
||||
if (settingsTemp.getBoolean("options.webserver-enabled")){
|
||||
try {
|
||||
this.webServer = new WebServer(settings.getInt("options.webserver-port"));
|
||||
XmlStatsRegistry.put("webserver", new Webserver(settingsTemp.getInt("options.webserver-port")));
|
||||
|
||||
this.enabled = true;
|
||||
LogInfo("Plugin Enabled");
|
||||
@@ -90,7 +92,7 @@ public class XmlStats extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
else {
|
||||
LogWarn("Webserver ist derzeit in der "+settings.getSettingsFilename()+" deaktiviert.");
|
||||
LogWarn("Webserver ist derzeit in der "+settingsTemp.getSettingsFilename()+" deaktiviert.");
|
||||
}
|
||||
|
||||
|
||||
@@ -123,55 +125,20 @@ public class XmlStats extends JavaPlugin {
|
||||
log.log(Level.WARNING, logprefix + " "+ Message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the stats plugin.
|
||||
*
|
||||
* @return the stats plugin
|
||||
*/
|
||||
public static Stats getStatsPlugin(){
|
||||
return Stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the server ref.
|
||||
*
|
||||
* @return the server ref
|
||||
*/
|
||||
public static Server getServerRef(){
|
||||
return serverRef;
|
||||
}
|
||||
|
||||
public static iConomy getiConomyPlugin(){
|
||||
return iConomy;
|
||||
}
|
||||
|
||||
public void onPluginDisable(PluginDisableEvent event){
|
||||
if(iConomy != null){
|
||||
if(event.getPlugin().getDescription().getName().equals("iConomy")){
|
||||
iConomy = null;
|
||||
LogInfo("iConomy is disabled now. Unhooking.");
|
||||
}
|
||||
public static void LogDebug(String Message){
|
||||
Settings settingsTemp = (Settings)XmlStatsRegistry.get("settings");
|
||||
if(settingsTemp.getBoolean("options.verbose-enabled")){
|
||||
log.log(Level.INFO, logprefix+"[DEBUG] "+Message);
|
||||
}
|
||||
if(Stats != null){
|
||||
if(event.getPlugin().getDescription().getName().equals("Stats")){
|
||||
Stats = null;
|
||||
LogInfo("Stats is disabled now. Unhooking.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void onPluginEnable(PluginEnableEvent event){
|
||||
this.hookPlugins();
|
||||
}
|
||||
|
||||
private void hookPlugins(){
|
||||
protected void hookPlugins(){
|
||||
Plugin StatsTemp = getServer().getPluginManager().getPlugin("Stats");
|
||||
Plugin iConomyTemp = getServer().getPluginManager().getPlugin("iConomy");
|
||||
|
||||
if(StatsTemp != null){
|
||||
if(StatsTemp.isEnabled() && StatsTemp.getClass().getName().equals("com.nidefawl.Stats.Stats")){
|
||||
Stats = (Stats)StatsTemp;
|
||||
XmlStatsRegistry.put("stats", (Stats)StatsTemp);
|
||||
LogInfo("Hooked into Stats!");
|
||||
}
|
||||
}
|
||||
@@ -180,7 +147,7 @@ public class XmlStats extends JavaPlugin {
|
||||
}
|
||||
if (iConomyTemp != null) {
|
||||
if (iConomyTemp.isEnabled() && iConomyTemp.getClass().getName().equals("com.iConomy.iConomy")) {
|
||||
iConomy = (iConomy)iConomyTemp;
|
||||
XmlStatsRegistry.put("iconomy", (iConomy)iConomyTemp);
|
||||
LogInfo("Hooked into iConomy");
|
||||
}
|
||||
}
|
||||
@@ -188,17 +155,20 @@ public class XmlStats extends JavaPlugin {
|
||||
LogError("iConomy not found! Can't hook into it.");
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isStatsHooked(){
|
||||
if (Stats != null){
|
||||
if(Stats.getClass().getName().equals("com.nidefawl.Stats.Stats") && Stats.isEnabled()) return true;
|
||||
Stats StatsTemp = (Stats)XmlStatsRegistry.get("stats");
|
||||
|
||||
if (StatsTemp != null){
|
||||
if(StatsTemp.getClass().getName().equals("com.nidefawl.Stats.Stats") && StatsTemp.isEnabled()) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isiConomyHooked(){
|
||||
if (iConomy != null){
|
||||
if (iConomy.getClass().getName().equals("com.iConomy.iConomy") && iConomy.isEnabled()) return true;
|
||||
iConomy iConomyTemp = (iConomy)XmlStatsRegistry.get("iconomy");
|
||||
|
||||
if (iConomyTemp != null){
|
||||
if (iConomyTemp.getClass().getName().equals("com.iConomy.iConomy") && iConomyTemp.isEnabled()) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
*/
|
||||
package de.sockenklaus.XmlStats.XmlWorkers;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.HttpURLConnection;
|
||||
@@ -22,11 +23,15 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import com.sun.net.httpserver.Headers;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
import de.sockenklaus.XmlStats.Settings;
|
||||
import de.sockenklaus.XmlStats.XmlStats;
|
||||
import de.sockenklaus.XmlStats.XmlStatsRegistry;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
/**
|
||||
@@ -41,10 +46,13 @@ public abstract class XmlWorker implements HttpHandler {
|
||||
public void handle(HttpExchange exchange) {
|
||||
Map<String, List<String>> parameters = new HashMap<String, List<String>>();
|
||||
|
||||
Headers headers = exchange.getRequestHeaders();
|
||||
Settings settingsTemp = (Settings)XmlStatsRegistry.get("settings");
|
||||
|
||||
if("get".equalsIgnoreCase(exchange.getRequestMethod())){
|
||||
String queryString = exchange.getRequestURI().getRawQuery();
|
||||
String xmlResponse = "";
|
||||
byte[] byteResponse;
|
||||
byte[] byteResponse = null;
|
||||
|
||||
try {
|
||||
|
||||
@@ -57,7 +65,48 @@ public abstract class XmlWorker implements HttpHandler {
|
||||
|
||||
xmlResponse = getXML(parameters);
|
||||
|
||||
byteResponse = xmlResponse.getBytes();
|
||||
/*
|
||||
* Check if the clients sends the header "Accept-encoding", the option "gzip-enabled" is true and the clients supports gzip.
|
||||
*/
|
||||
if(headers.containsKey("Accept-encoding") && settingsTemp.getBoolean("options.gzip-enabled")){
|
||||
XmlStats.LogDebug("Compression seems to be accepted by the client and activated in this plugin.");
|
||||
List<String> header = headers.get("Accept-encoding");
|
||||
try {
|
||||
XmlStats.LogDebug("There are "+header.size()+" values in the header");
|
||||
XmlStats.LogDebug("Let's take a look at the headers values:");
|
||||
}
|
||||
catch (Exception e){
|
||||
XmlStats.LogError(e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
for(String val : header){
|
||||
XmlStats.LogDebug("Accept-encoding: "+val);
|
||||
if(val.toLowerCase().indexOf("gzip") > -1){
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
try {
|
||||
XmlStats.LogDebug("OK... let's try gzip compression...");
|
||||
XmlStats.LogDebug("Actual size of the xml file: "+xmlResponse.getBytes().length+"Bytes");
|
||||
GZIPOutputStream gzip = new GZIPOutputStream(out);
|
||||
gzip.write(xmlResponse.getBytes());
|
||||
gzip.close();
|
||||
byteResponse = out.toByteArray();
|
||||
XmlStats.LogDebug("Compressed size of the xml file: "+byteResponse.length+"Bytes");
|
||||
exchange.getResponseHeaders().add("Content-encoding", "gzip");
|
||||
|
||||
} catch (Exception e) {
|
||||
XmlStats.LogError("GZIP-Compression failed! Falling back to non-compressed output.");
|
||||
XmlStats.LogError(e.getMessage());
|
||||
e.printStackTrace();
|
||||
byteResponse = xmlResponse.getBytes();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (byteResponse == null || byteResponse.length == 0) {
|
||||
XmlStats.LogDebug("Compression is not enabled or doesn't work properly. Fallback to uncompressed output.");
|
||||
byteResponse = xmlResponse.getBytes();
|
||||
}
|
||||
|
||||
try {
|
||||
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, byteResponse.length);
|
||||
|
||||
Reference in New Issue
Block a user