Reformats Code

Uses the Logger for some Exceptions which were still using e.printStackTrace()
This commit is contained in:
Fuzzlemann 2017-08-03 19:50:01 +02:00
parent 7e1e7d80e9
commit d45b0f426e
27 changed files with 96 additions and 95 deletions

View File

@ -168,6 +168,7 @@ public class Plan extends BukkitPlugin<Plan> {
if (bootAnalysisIsEnabled) {
startBootAnalysisTask();
}
if (analysisRefreshTaskIsEnabled) {
startAnalysisRefreshTask(analysisRefreshMinutes);
}

View File

@ -18,36 +18,29 @@ import java.util.stream.Collectors;
*/
public class UserData {
private final List<SessionData> sessions;
private int accessing;
private boolean clearAfterSave;
private UUID uuid;
private String name;
private Set<String> nicknames;
private String lastNick;
private String geolocation;
private Set<InetAddress> ips;
private int loginTimes;
private int timesKicked;
private boolean isOp;
private boolean isBanned;
private boolean isOnline;
private int mobKills;
private List<KillData> playerKills;
private int deaths;
private long registered;
private long lastPlayed;
private long playTime;
private GMTimes gmTimes;
private WorldTimes worldTimes;
private final List<SessionData> sessions;
/**
* Creates a new UserData object with given values and default values.
* <p>
@ -491,6 +484,14 @@ public class UserData {
return gmTimes;
}
public void setGmTimes(Map<String, Long> times) {
if (Verify.notNull(times)) {
for (Map.Entry<String, Long> entry : times.entrySet()) {
gmTimes.setTime(entry.getKey(), entry.getValue());
}
}
}
/**
* Set the GM Times object containing playtime in each gamemode.
*
@ -502,14 +503,6 @@ public class UserData {
}
}
public void setGmTimes(Map<String, Long> times) {
if (Verify.notNull(times)) {
for (Map.Entry<String, Long> entry : times.entrySet()) {
gmTimes.setTime(entry.getKey(), entry.getValue());
}
}
}
/**
* Is the user Operator?
*

View File

@ -158,7 +158,7 @@ public class DataCacheHandler extends SessionCache {
periodicTaskIsSaving = false;
}
}
}).runTaskTimerAsynchronously(60 * 20 * minutes, 60 * 20 * minutes);
}).runTaskTimerAsynchronously(60L * 20L * minutes, 60L * 20L * minutes);
}
/**

View File

@ -23,6 +23,9 @@ import java.util.Map;
*/
public class GeolocationCacheHandler {
private static final Cache<String, String> geolocationCache = CacheBuilder.newBuilder()
.build();
/**
* Constructor used to hide the public constructor
*/
@ -30,9 +33,6 @@ public class GeolocationCacheHandler {
throw new IllegalStateException("Utility class");
}
private static final Cache<String, String> geolocationCache = CacheBuilder.newBuilder()
.build();
/**
* Retrieves the country in full length (e.g. United States) from the IP Address.
* <p>

View File

@ -18,6 +18,9 @@ import main.java.com.djrapitops.plan.ui.webserver.response.Response;
*/
public class PageCacheHandler {
private static final Cache<String, Response> pageCache = CacheBuilder.newBuilder()
.build();
/**
* Constructor used to hide the public constructor
*/
@ -25,9 +28,6 @@ public class PageCacheHandler {
throw new IllegalStateException("Utility class");
}
private static final Cache<String, Response> pageCache = CacheBuilder.newBuilder()
.build();
/**
* Loads the page from the page cache.
* <p>

View File

@ -55,7 +55,7 @@ public class DataCacheClearQueue extends Queue<UUID> {
try {
queue.addAll(uuids);
} catch (IllegalStateException e) {
Log.error(Phrase.ERROR_TOO_SMALL_QUEUE.parse("Clear Queue", Settings.PROCESS_CLEAR_LIMIT.getNumber() + ""));
Log.error(Phrase.ERROR_TOO_SMALL_QUEUE.parse("Clear Queue", Settings.PROCESS_CLEAR_LIMIT.getNumber()));
}
}
}

View File

@ -45,7 +45,7 @@ public class DataCacheGetQueue extends Queue<Map<UUID, List<DBCallableProcessor>
map.put(uuid, Arrays.asList(processors));
queue.add(map);
} catch (IllegalStateException e) {
Log.error(Phrase.ERROR_TOO_SMALL_QUEUE.parse("Get Queue", String.valueOf(Settings.PROCESS_GET_LIMIT.getNumber())));
Log.error(Phrase.ERROR_TOO_SMALL_QUEUE.parse("Get Queue", Settings.PROCESS_GET_LIMIT.getNumber()));
}
}

View File

@ -45,7 +45,7 @@ public class DataCacheSaveQueue extends Queue<UserData> {
try {
queue.add(data);
} catch (IllegalStateException e) {
Log.error(Phrase.ERROR_TOO_SMALL_QUEUE.parse("Save Queue", Settings.PROCESS_SAVE_LIMIT.getNumber() + ""));
Log.error(Phrase.ERROR_TOO_SMALL_QUEUE.parse("Save Queue", Settings.PROCESS_SAVE_LIMIT.getNumber()));
}
}
@ -59,7 +59,7 @@ public class DataCacheSaveQueue extends Queue<UserData> {
try {
queue.addAll(data);
} catch (IllegalStateException e) {
Log.error(Phrase.ERROR_TOO_SMALL_QUEUE.parse("Save Queue", Settings.PROCESS_SAVE_LIMIT.getNumber() + ""));
Log.error(Phrase.ERROR_TOO_SMALL_QUEUE.parse("Save Queue", Settings.PROCESS_SAVE_LIMIT.getNumber()));
}
}
@ -73,7 +73,7 @@ public class DataCacheSaveQueue extends Queue<UserData> {
try {
queue.add(data);
} catch (IllegalStateException e) {
Log.error(Phrase.ERROR_TOO_SMALL_QUEUE.parse("Save Queue", Settings.PROCESS_SAVE_LIMIT.getNumber() + ""));
Log.error(Phrase.ERROR_TOO_SMALL_QUEUE.parse("Save Queue", Settings.PROCESS_SAVE_LIMIT.getNumber()));
}
}

View File

@ -45,9 +45,10 @@ public class PlanGamemodeChangeListener implements Listener {
Player p = event.getPlayer();
UUID uuid = p.getUniqueId();
String gameMode = event.getNewGameMode().name();
long time = MiscUtils.getTime();
String gameMode = event.getNewGameMode().name();
String worldName = p.getWorld().getName();
handler.addToPool(new PlaytimeDependentInfo(uuid, InfoType.GM, time, event.getNewGameMode().name(), p.getWorld().getName()));
handler.addToPool(new PlaytimeDependentInfo(uuid, InfoType.GM, time, gameMode, worldName));
}
}

View File

@ -109,30 +109,30 @@ public abstract class TimeKeeper {
return times.values().stream().mapToLong(i -> i).sum();
}
public void setTimes(Map<String, Long> times) {
this.times = times;
}
public Map<String, Long> getTimes() {
return times;
}
public void setState(String state) {
this.state = state;
}
public void setLastStateChange(long lastStateChange) {
this.lastStateChange = lastStateChange;
public void setTimes(Map<String, Long> times) {
this.times = times;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public long getLastStateChange() {
return lastStateChange;
}
public void setLastStateChange(long lastStateChange) {
this.lastStateChange = lastStateChange;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@ -155,7 +155,7 @@ public abstract class TimeKeeper {
@Override
public String toString() {
return getClass().getSimpleName()+"{" +
return getClass().getSimpleName() + "{" +
"times=" + times +
", state='" + state + '\'' +
", lastStateChange=" + lastStateChange +

View File

@ -19,6 +19,8 @@ import java.util.Map.Entry;
*/
public class DBUtils {
private static final int BATCH_SIZE = 2048;
/**
* Constructor used to hide the public constructor
*/
@ -26,8 +28,6 @@ public class DBUtils {
throw new IllegalStateException("Utility class");
}
private static final int BATCH_SIZE = 2048;
/**
* Splits a collection of objects into lists with the size defined by
* BATCH_SIZE.

View File

@ -86,7 +86,7 @@ public abstract class SQLDB extends Database {
}
}
}
}).runTaskTimerAsynchronously(60 * 20, 60 * 20);
}).runTaskTimerAsynchronously(60L * 20L, 60L * 20L);
}
/**

View File

@ -50,7 +50,7 @@ public class SQLiteDB extends SQLDB {
try {
Class.forName("org.sqlite.JDBC");
Connection connection = DriverManager.getConnection("jdbc:sqlite:" + new File(plugin.getDataFolder(), dbName + ".db").getAbsolutePath());
Connection connection = DriverManager.getConnection("jdbc:sqlite:" + new File(plugin.getDataFolder(), dbName + ".db").getAbsolutePath());
connection.setAutoCommit(false);
return connection;
} catch (ClassNotFoundException | SQLException e) {

View File

@ -302,7 +302,7 @@ public class GMTimesTable extends Table {
try {
addNewGMTimesBatch(batch);
} catch (SQLException e) {
e.printStackTrace();
Log.toLog("GMTimesTable.addNewGMTimesRows", e);
}
});

View File

@ -222,7 +222,7 @@ public class SessionsTable extends Table {
try {
saveSessionBatch(batch);
} catch (SQLException e) {
e.printStackTrace();
Log.toLog("SessionsTable.saveSessionData", e);
}
});

View File

@ -120,12 +120,12 @@ public class TPSTable extends Table {
public void saveTPSData(List<TPS> data) {
List<List<TPS>> batches = DBUtils.splitIntoBatches(data);
batches.forEach(batch -> {
try {
saveTPSBatch(batch);
} catch (SQLException e) {
Log.toLog("UsersTable.saveUserDataInformationBatch", e);
}
});
try {
saveTPSBatch(batch);
} catch (SQLException e) {
Log.toLog("UsersTable.saveUserDataInformationBatch", e);
}
});
}
private void saveTPSBatch(List<TPS> batch) throws SQLException {

View File

@ -10,8 +10,8 @@ import java.util.stream.Collectors;
* Class for creating scatter graph data from RAM Usage snapshots with TPS task.
*
* @author Rsl1122
* @since 3.6.0
* @see main.java.com.djrapitops.plan.data.listeners.TPSCountTimer
* @since 3.6.0
*/
public class RamGraphCreator {

View File

@ -10,8 +10,8 @@ import java.util.stream.Collectors;
* Class for creating scatter graph data from Chunk & Entity load snapshots with TPS task.
*
* @author Rsl1122
* @since 3.6.0
* @see main.java.com.djrapitops.plan.data.listeners.TPSCountTimer
* @since 3.6.0
*/
public class WorldLoadGraphCreator {

View File

@ -11,6 +11,7 @@ import main.java.com.djrapitops.plan.data.cache.PageCacheHandler;
import main.java.com.djrapitops.plan.database.tables.SecurityTable;
import main.java.com.djrapitops.plan.ui.html.DataRequestHandler;
import main.java.com.djrapitops.plan.ui.webserver.response.*;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import main.java.com.djrapitops.plan.utilities.HtmlUtils;
import main.java.com.djrapitops.plan.utilities.PassEncryptUtil;
import main.java.com.djrapitops.plan.utilities.uuid.UUIDUtility;
@ -39,10 +40,9 @@ public class WebServer {
private final Plan plugin;
private final DataRequestHandler dataReqHandler;
private final int port;
private boolean enabled = false;
private HttpServer server;
private final int port;
private boolean usingHttps;
/**
@ -98,7 +98,7 @@ public class WebServer {
}
}
responseHeaders.set("Content-Encoding", "gzip");
responseHeaders.set("Content-Encoding", "gzip");
Response response = getResponse(target, user);
@ -135,6 +135,7 @@ public class WebServer {
}
private WebUser getUser(Headers requestHeaders) {
Benchmark.start("getUser");
try {
List<String> authorization = requestHeaders.get("Authorization");
if (Verify.isEmpty(authorization)) {
@ -169,6 +170,8 @@ public class WebServer {
if (!correctPass) {
throw new IllegalArgumentException("User and Password do not match");
}
Benchmark.stop("getUser: " + requestHeaders);
return webUser;
} catch (IllegalArgumentException e) {
Log.debug("WebServer: " + e.getMessage());
@ -184,6 +187,7 @@ public class WebServer {
if (!Paths.get(keyStorePath).isAbsolute()) {
keyStorePath = plugin.getDataFolder() + File.separator + keyStorePath;
}
char[] storepass = Settings.WEBSERVER_CERTIFICATE_STOREPASS.toString().toCharArray();
char[] keypass = Settings.WEBSERVER_CERTIFICATE_KEYPASS.toString().toCharArray();
String alias = Settings.WEBSERVER_CERTIFICATE_ALIAS.toString();
@ -241,6 +245,7 @@ public class WebServer {
if ("/favicon.ico".equals(target)) {
return PageCacheHandler.loadPage("Redirect: favicon", () -> new RedirectResponse("https://puu.sh/tK0KL/6aa2ba141b.ico"));
}
if (usingHttps) {
if (user == null) {
return PageCacheHandler.loadPage("promptAuthorization", PromptAuthorizationResponse::new);

View File

@ -27,14 +27,14 @@ public abstract class Response {
return content;
}
public void setHeader(String header) {
this.header = header;
}
public void setContent(String content) {
this.content = content;
}
public void setHeader(String header) {
this.header = header;
}
public int getCode() {
return header == null ? 500 : Integer.parseInt(header.split(" ")[1]);
}

View File

@ -17,13 +17,6 @@ import java.security.spec.InvalidKeySpecException;
*/
public class PassEncryptUtil {
/**
* Constructor used to hide the public constructor
*/
private PassEncryptUtil() {
throw new IllegalStateException("Utility class");
}
public static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA1";
// These constants may be changed without breaking existing hashes.
public static final int SALT_BYTE_SIZE = 24;
@ -36,6 +29,12 @@ public class PassEncryptUtil {
public static final int HASH_SIZE_INDEX = 2;
public static final int SALT_INDEX = 3;
public static final int PBKDF2_INDEX = 4;
/**
* Constructor used to hide the public constructor
*/
private PassEncryptUtil() {
throw new IllegalStateException("Utility class");
}
public static String createHash(String password) throws CannotPerformOperationException {
return createHash(password.toCharArray());

View File

@ -11,6 +11,8 @@ import java.util.stream.Stream;
*/
public class MathUtils {
private static final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
private static final DecimalFormat decimalFormat = new DecimalFormat("#.##", decimalFormatSymbols);
/**
* Constructor used to hide the public constructor
*/
@ -18,9 +20,6 @@ public class MathUtils {
throw new IllegalStateException("Utility class");
}
private static final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
private static final DecimalFormat decimalFormat = new DecimalFormat("#.##", decimalFormatSymbols);
/**
* Gets the average of a Stream of Integers.
* If there are no components in the Stream, it will return 0.

View File

@ -37,7 +37,7 @@ public class DataCacheClearQueueTest {
public void setUp() throws Exception {
TestInit t = TestInit.init();
Plan plan = t.getPlanMock();
DataCacheHandler handler = new DataCacheHandler(plan) {
new DataCacheHandler(plan) {
@Override
public boolean getCommandUseFromDb() {
return true;

View File

@ -18,7 +18,7 @@ public class InfoUuidCorrectionTest {
public void testAllInfoBooleanReturn() {
long now = MiscUtils.getTime();
HandlingInfo[] h = new HandlingInfo[]{
new ChatInfo(test, ""),
new ChatInfo(test, ""),
new DeathInfo(test),
new KickInfo(test),
new KillInfo(test, now, null, ""),

View File

@ -34,7 +34,7 @@ public class FormatUtilsTest {
*
*/
@Before
public void setUp() throws Exception{
public void setUp() throws Exception {
TestInit.init();
}

View File

@ -16,8 +16,11 @@ import org.bukkit.entity.Player;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
@ -153,9 +156,9 @@ public class MockUtils {
return PowerMockito.mock(CommandSender.class);
}
public static HttpServer mockHTTPServer() {
public static HttpServer mockHTTPServer() throws IOException {
HttpServer httpServer = PowerMockito.mock(HttpServer.class);
when(httpServer.getAddress()).thenReturn(InetSocketAddress.createUnresolved("127.0.0.1", 80));
when(httpServer.getAddress()).thenReturn(new ServerSocket(80, 0, InetAddress.getByName(null));
when(httpServer.getExecutor()).thenReturn(command -> System.out.println("HTTP Server command received"));
return httpServer;
}

View File

@ -47,6 +47,24 @@ public class TestInit {
return t;
}
private static File getTestFolder() {
File testFolder = new File("temporaryTestFolder");
testFolder.mkdir();
return testFolder;
}
public static void clean() throws IOException {
clean(getTestFolder());
}
public static void clean(File testFolder) throws IOException {
if (testFolder.exists() && testFolder.isDirectory()) {
for (File f : testFolder.listFiles()) {
Files.deleteIfExists(f.toPath());
}
}
}
@Deprecated // Use Test.init instead.
public void setUp(boolean clearOnStart) throws Exception {
planMock = PowerMockito.mock(Plan.class);
@ -100,24 +118,6 @@ public class TestInit {
return mockServer;
}
private static File getTestFolder() {
File testFolder = new File("temporaryTestFolder");
testFolder.mkdir();
return testFolder;
}
public static void clean() throws IOException {
clean(getTestFolder());
}
public static void clean(File testFolder) throws IOException {
if (testFolder.exists() && testFolder.isDirectory()) {
for (File f : testFolder.listFiles()) {
Files.deleteIfExists(f.toPath());
}
}
}
private YamlConfiguration mockConfig() throws IOException, InvalidConfigurationException {
File configFile = new File(getClass().getResource("/config.yml").getPath());
YamlConfiguration configuration = new YamlConfiguration();