Added some exception handling.
This commit is contained in:
@@ -24,21 +24,22 @@ import com.nijikokun.register.payment.Methods;
|
||||
|
||||
import de.sockenklaus.XmlStats.XmlStats;
|
||||
import de.sockenklaus.XmlStats.XmlStatsRegistry;
|
||||
import de.sockenklaus.XmlStats.Exceptions.XmlStatsException;
|
||||
|
||||
/**
|
||||
* The Class MoneyDS.
|
||||
*/
|
||||
public class MoneyDS extends Datasource {
|
||||
public class BalancesDS extends Datasource {
|
||||
|
||||
private ArrayList<String> allPlayers;
|
||||
private XmlStats xmlstats;
|
||||
|
||||
public MoneyDS(){
|
||||
public BalancesDS(){
|
||||
this.allPlayers = fetchAllPlayers();
|
||||
this.xmlstats = (XmlStats)XmlStatsRegistry.get("xmlstats");
|
||||
}
|
||||
|
||||
public HashMap<String, Double> getBalances(){
|
||||
public HashMap<String, Double> getBalances() throws XmlStatsException {
|
||||
HashMap<String, Double> result = new HashMap<String, Double>();
|
||||
|
||||
for (String playerName : allPlayers){
|
||||
@@ -48,7 +49,7 @@ public class MoneyDS extends Datasource {
|
||||
return result;
|
||||
}
|
||||
|
||||
public Double getBalance(String playerName){
|
||||
public Double getBalance(String playerName) throws XmlStatsException {
|
||||
Double result = 0.0;
|
||||
|
||||
if (xmlstats.checkRegister()){
|
||||
@@ -61,13 +62,11 @@ public class MoneyDS extends Datasource {
|
||||
if (account != null){
|
||||
result = account.balance();
|
||||
}
|
||||
else XmlStats.LogWarn("The player \""+playerName+"\" has an account but it isn't valid. Bad data will return.");
|
||||
else throw new XmlStatsException("The player \""+playerName+"\" has an account but it isn't valid.");
|
||||
}
|
||||
else XmlStats.LogWarn("The player \""+playerName+"\" doesn't have a bank account and this action will return bad data");
|
||||
}
|
||||
else {
|
||||
XmlStats.LogError("Something went wrong! /user_balances.xml shouldn't be enabled but it's datasource was called! This will return bad results.");
|
||||
else throw new XmlStatsException("The player \""+playerName+"\" doesn't have a bank account.");
|
||||
}
|
||||
else throw new XmlStatsException("Something went wrong! /user_balances.xml shouldn't be enabled but it's datasource was called!");
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -76,7 +75,7 @@ public class MoneyDS extends Datasource {
|
||||
* @param list
|
||||
* @return
|
||||
*/
|
||||
public int getSum(List<String> list) {
|
||||
public int getSum(List<String> list) throws XmlStatsException {
|
||||
int result = 0;
|
||||
|
||||
for(String playerName : list){
|
||||
@@ -18,7 +18,7 @@ import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import de.sockenklaus.XmlStats.Exceptions.UserNotFoundException;
|
||||
import de.sockenklaus.XmlStats.Exceptions.XmlStatsException;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
/**
|
||||
@@ -52,13 +52,13 @@ public abstract class Datasource {
|
||||
return fetchAllPlayers().contains(player);
|
||||
}
|
||||
|
||||
public static List<String> fetchValidUsers(List<String> list) throws UserNotFoundException{
|
||||
public static List<String> fetchValidUsers(List<String> list) throws XmlStatsException{
|
||||
ArrayList<String> output = new ArrayList<String>();
|
||||
|
||||
for (String possibleUser : list){
|
||||
if(Datasource.userExists(possibleUser)) output.add(possibleUser);
|
||||
}
|
||||
if(output.isEmpty()) throw new UserNotFoundException("No valid user has been found!");
|
||||
if(output.isEmpty()) throw new XmlStatsException("No valid user has been found!");
|
||||
else return output;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,13 +7,13 @@ package de.sockenklaus.XmlStats.Exceptions;
|
||||
* @author socrates
|
||||
*
|
||||
*/
|
||||
public class UserNotFoundException extends Exception {
|
||||
public class XmlStatsException extends Exception {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -6660078004710596491L;
|
||||
|
||||
public UserNotFoundException(String s){
|
||||
public XmlStatsException(String s){
|
||||
super(s);
|
||||
}
|
||||
}
|
||||
@@ -86,6 +86,7 @@ public class Webserver {
|
||||
}
|
||||
|
||||
protected void startRegister(){
|
||||
XmlStats.LogDebug("Casting startRegister()");
|
||||
if (this.isRunning() && xmlstats.checkRegister()){
|
||||
server.createContext("/user_balances.xml", new UserBalances());
|
||||
XmlStats.LogInfo("Register seems to be loaded correctly. Enabling /user_balances.xml");
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package de.sockenklaus.XmlStats;
|
||||
|
||||
/**
|
||||
* @author socrates
|
||||
*
|
||||
*/
|
||||
public class XmlStatsException extends Throwable {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
}
|
||||
@@ -17,10 +17,12 @@ package de.sockenklaus.XmlStats.XmlWorkers;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.w3c.dom.DOMException;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import de.sockenklaus.XmlStats.XmlStats;
|
||||
import de.sockenklaus.XmlStats.Datasource.MoneyDS;
|
||||
import de.sockenklaus.XmlStats.Datasource.BalancesDS;
|
||||
import de.sockenklaus.XmlStats.Exceptions.XmlStatsException;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
/**
|
||||
@@ -28,14 +30,14 @@ import de.sockenklaus.XmlStats.Datasource.MoneyDS;
|
||||
*/
|
||||
public class UserBalances extends XmlWorker {
|
||||
|
||||
private MoneyDS moneyDS;
|
||||
private BalancesDS moneyDS;
|
||||
|
||||
public UserBalances(){
|
||||
this.moneyDS = new MoneyDS();
|
||||
this.moneyDS = new BalancesDS();
|
||||
}
|
||||
|
||||
|
||||
protected Element getUserXml(List<String> playerList, Map<String, List<String>> parameters){
|
||||
protected Element getUserXml(List<String> playerList, Map<String, List<String>> parameters) throws XmlStatsException {
|
||||
|
||||
Element elem_users = this.doc.createElement("users");
|
||||
elem_users.setAttribute("count", String.valueOf(playerList.size()));
|
||||
@@ -55,12 +57,13 @@ public class UserBalances extends XmlWorker {
|
||||
* @see de.sockenklaus.XmlStats.XmlWorkers.XmlWorker#getXML(java.util.Map)
|
||||
*/
|
||||
@Override
|
||||
public Element getXml(Map<String, List<String>> parameters) {
|
||||
Element elem_error = this.doc.createElement("error");
|
||||
public Element getXml(Map<String, List<String>> parameters) throws XmlStatsException {
|
||||
/*Element elem_error = this.doc.createElement("error");
|
||||
elem_error.setAttribute("code", "1");
|
||||
elem_error.setTextContent("No data provided with this query!");
|
||||
|
||||
return elem_error;
|
||||
return elem_error;*/
|
||||
throw new XmlStatsException("No data provided with this query!");
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +71,7 @@ public class UserBalances extends XmlWorker {
|
||||
* @see de.sockenklaus.XmlStats.XmlWorkers.XmlWorker#getSumXml(java.util.List, java.util.Map)
|
||||
*/
|
||||
@Override
|
||||
protected Element getSumXml(List<String> userList, Map<String, List<String>> parameters) {
|
||||
protected Element getSumXml(List<String> userList, Map<String, List<String>> parameters) throws XmlStatsException {
|
||||
|
||||
Element elem_sum = this.doc.createElement("sum");
|
||||
Element elem_users = this.doc.createElement("users");
|
||||
|
||||
@@ -47,7 +47,7 @@ import com.sun.net.httpserver.HttpExchange;
|
||||
import de.sockenklaus.XmlStats.XmlStats;
|
||||
import de.sockenklaus.XmlStats.XmlStatsRegistry;
|
||||
import de.sockenklaus.XmlStats.Datasource.Datasource;
|
||||
import de.sockenklaus.XmlStats.Exceptions.UserNotFoundException;
|
||||
import de.sockenklaus.XmlStats.Exceptions.XmlStatsException;
|
||||
|
||||
// TODO: Auto-generated Javadoc
|
||||
/**
|
||||
@@ -151,7 +151,7 @@ public abstract class XmlWorker implements HttpHandler {
|
||||
* Build string from XML
|
||||
*/
|
||||
}
|
||||
catch(UserNotFoundException e){
|
||||
catch(XmlStatsException e){
|
||||
root.setAttribute("status", "error");
|
||||
root.appendChild(getTextElem("error", e.getMessage()));
|
||||
|
||||
@@ -212,21 +212,21 @@ public abstract class XmlWorker implements HttpHandler {
|
||||
* @param parameters
|
||||
* @return
|
||||
*/
|
||||
protected abstract Element getXml(Map<String, List<String>> parameters);
|
||||
protected abstract Element getXml(Map<String, List<String>> parameters) throws XmlStatsException;
|
||||
|
||||
/**
|
||||
* @param playerList
|
||||
* @param parameters
|
||||
* @return
|
||||
*/
|
||||
protected abstract Element getSumXml(List<String> playerList, Map<String, List<String>> parameters);
|
||||
protected abstract Element getSumXml(List<String> playerList, Map<String, List<String>> parameters) throws XmlStatsException;
|
||||
|
||||
/**
|
||||
* @param playerList
|
||||
* @param parameters
|
||||
* @return
|
||||
*/
|
||||
protected abstract Element getUserXml(List<String> playerList, Map<String, List<String>> parameters);
|
||||
protected abstract Element getUserXml(List<String> playerList, Map<String, List<String>> parameters) throws XmlStatsException;
|
||||
|
||||
/**
|
||||
* Parses the parameters.
|
||||
|
||||
Reference in New Issue
Block a user