Fix bugs & vulns reported by Sonar

This commit is contained in:
Rsl1122 2018-01-28 15:35:11 +02:00
parent 58f2d7f02c
commit bc17486fdb
6 changed files with 31 additions and 15 deletions

View File

@ -57,19 +57,21 @@ public class SQLiteDB extends SQLDB {
} }
String dbFilePath = new File(PlanPlugin.getInstance().getDataFolder(), dbName + ".db").getAbsolutePath(); String dbFilePath = new File(PlanPlugin.getInstance().getDataFolder(), dbName + ".db").getAbsolutePath();
Connection connection;
try { Connection connection = getConnectionFor(dbFilePath);
connection = DriverManager.getConnection("jdbc:sqlite:" + dbFilePath + "?journal_mode=WAL");
} catch (SQLException ignored) {
connection = DriverManager.getConnection("jdbc:sqlite:" + dbFilePath);
Log.info("SQLite WAL mode not supported on this server version, using default. This may or may not affect performance.");
}
connection.setAutoCommit(false); connection.setAutoCommit(false);
return connection; return connection;
} }
private Connection getConnectionFor(String dbFilePath) throws SQLException {
try {
return DriverManager.getConnection("jdbc:sqlite:" + dbFilePath + "?journal_mode=WAL");
} catch (SQLException ignored) {
Log.info("SQLite WAL mode not supported on this server version, using default. This may or may not affect performance.");
return DriverManager.getConnection("jdbc:sqlite:" + dbFilePath);
}
}
private void startConnectionPingTask() { private void startConnectionPingTask() {
stopConnectionPingTask(); stopConnectionPingTask();
try { try {
@ -84,6 +86,7 @@ public class SQLiteDB extends SQLDB {
statement.execute("/* ping */ SELECT 1"); statement.execute("/* ping */ SELECT 1");
} }
} catch (SQLException e) { } catch (SQLException e) {
Log.debug("Something went wrong during Ping task.");
try { try {
connection = getNewConnection(dbName); connection = getNewConnection(dbName);
} catch (SQLException e1) { } catch (SQLException e1) {

View File

@ -73,6 +73,7 @@ public abstract class Importer {
service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Log.toLog(this.getClass().getName(), e); Log.toLog(this.getClass().getName(), e);
Thread.currentThread().interrupt();
} }
Benchmark.stop(benchmarkName); Benchmark.stop(benchmarkName);

View File

@ -11,6 +11,7 @@ import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.database.databases.Database; import com.djrapitops.plan.system.database.databases.Database;
import com.djrapitops.plan.system.processing.Processor; import com.djrapitops.plan.system.processing.Processor;
import com.djrapitops.plan.system.processing.processors.ObjectProcessor; import com.djrapitops.plan.system.processing.processors.ObjectProcessor;
import com.djrapitops.plan.utilities.NullCheck;
import com.djrapitops.plugin.api.utility.log.Log; import com.djrapitops.plugin.api.utility.log.Log;
import java.util.UUID; import java.util.UUID;
@ -41,6 +42,7 @@ public class RegisterProcessor extends PlayerProcessor {
public void process() { public void process() {
UUID uuid = getUUID(); UUID uuid = getUUID();
Database db = Database.getActive(); Database db = Database.getActive();
NullCheck.check(uuid, new IllegalStateException("UUID was null"));
try { try {
if (!db.check().isPlayerRegistered(uuid)) { if (!db.check().isPlayerRegistered(uuid)) {
db.save().registerNewUser(uuid, registered, name); db.save().registerNewUser(uuid, registered, name);

View File

@ -123,7 +123,7 @@ public class AnalysisUtils {
} }
public static double getAveragePerDay(long after, long before, long total) { public static double getAveragePerDay(long after, long before, long total) {
return total / getNumberOfDaysBetween(after, before); return (double) total / getNumberOfDaysBetween(after, before);
} }
public static long getNumberOfDaysBetween(long start, long end) { public static long getNumberOfDaysBetween(long start, long end) {
@ -134,7 +134,7 @@ public class AnalysisUtils {
test += day; test += day;
value++; value++;
} }
return value; return value == 0 ? 1 : value;
} }
public static void addMissingWorlds(WorldTimes worldTimes) { public static void addMissingWorlds(WorldTimes worldTimes) {
@ -196,7 +196,7 @@ public class AnalysisUtils {
stickM++; stickM++;
} }
} }
probability *= (stickM / similarM.size()); probability *= ((double) stickM / similarM.size());
} }
if (!similarW.isEmpty()) { if (!similarW.isEmpty()) {
@ -207,7 +207,7 @@ public class AnalysisUtils {
} }
} }
probability *= (stickW / similarW.size()); probability *= ((double) stickW / similarW.size());
} }
return probability; return probability;

View File

@ -167,7 +167,6 @@ public class HtmlExport extends SpecificExport {
copyFromJar(resources, true); copyFromJar(resources, true);
} }
private void copyFromJar(String[] resources, boolean overwrite) { private void copyFromJar(String[] resources, boolean overwrite) {
for (String resource : resources) { for (String resource : resources) {
try { try {
@ -185,8 +184,9 @@ public class HtmlExport extends SpecificExport {
File to = new File(outputFolder, outputFile); File to = new File(outputFolder, outputFile);
to.getParentFile().mkdirs(); to.getParentFile().mkdirs();
if (to.exists()) { if (to.exists()) {
to.delete(); if (!to.delete() || !to.createNewFile()) {
to.createNewFile(); return;
}
} }
export(to, lines); export(to, lines);
} }

View File

@ -855,6 +855,16 @@ public class SQLiteTest {
assertTrue(db.getUserInfoTable().isRegistered(playerUUID)); assertTrue(db.getUserInfoTable().isRegistered(playerUUID));
} }
@Test
public void testRegister() throws DBException {
assertFalse(db.check().isPlayerRegistered(playerUUID));
assertFalse(db.check().isPlayerRegisteredOnThisServer(playerUUID));
db.save().registerNewUser(playerUUID, 1000L, "name");
db.save().registerNewUserOnThisServer(playerUUID, 500L);
assertTrue(db.check().isPlayerRegistered(playerUUID));
assertTrue(db.check().isPlayerRegisteredOnThisServer(playerUUID));
}
@Test @Test
public void testWorldTableGetWorldNamesNoException() throws SQLException { public void testWorldTableGetWorldNamesNoException() throws SQLException {
Set<String> worldNames = db.getWorldTable().getWorldNames(); Set<String> worldNames = db.getWorldTable().getWorldNames();