mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-27 09:00:28 +08:00
Fix bugs & vulns reported by Sonar
This commit is contained in:
parent
58f2d7f02c
commit
bc17486fdb
Plan/src
main/java/com/djrapitops/plan
system
database/databases/sql
processing
utilities
test/java/com/djrapitops/plan/system/database/databases
@ -57,19 +57,21 @@ public class SQLiteDB extends SQLDB {
|
||||
}
|
||||
|
||||
String dbFilePath = new File(PlanPlugin.getInstance().getDataFolder(), dbName + ".db").getAbsolutePath();
|
||||
Connection connection;
|
||||
|
||||
try {
|
||||
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 connection = getConnectionFor(dbFilePath);
|
||||
connection.setAutoCommit(false);
|
||||
|
||||
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() {
|
||||
stopConnectionPingTask();
|
||||
try {
|
||||
@ -84,6 +86,7 @@ public class SQLiteDB extends SQLDB {
|
||||
statement.execute("/* ping */ SELECT 1");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
Log.debug("Something went wrong during Ping task.");
|
||||
try {
|
||||
connection = getNewConnection(dbName);
|
||||
} catch (SQLException e1) {
|
||||
|
@ -73,6 +73,7 @@ public abstract class Importer {
|
||||
service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
Log.toLog(this.getClass().getName(), e);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
Benchmark.stop(benchmarkName);
|
||||
|
@ -11,6 +11,7 @@ import com.djrapitops.plan.system.cache.SessionCache;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.processing.Processor;
|
||||
import com.djrapitops.plan.system.processing.processors.ObjectProcessor;
|
||||
import com.djrapitops.plan.utilities.NullCheck;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
|
||||
import java.util.UUID;
|
||||
@ -41,6 +42,7 @@ public class RegisterProcessor extends PlayerProcessor {
|
||||
public void process() {
|
||||
UUID uuid = getUUID();
|
||||
Database db = Database.getActive();
|
||||
NullCheck.check(uuid, new IllegalStateException("UUID was null"));
|
||||
try {
|
||||
if (!db.check().isPlayerRegistered(uuid)) {
|
||||
db.save().registerNewUser(uuid, registered, name);
|
||||
|
@ -123,7 +123,7 @@ public class AnalysisUtils {
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -134,7 +134,7 @@ public class AnalysisUtils {
|
||||
test += day;
|
||||
value++;
|
||||
}
|
||||
return value;
|
||||
return value == 0 ? 1 : value;
|
||||
}
|
||||
|
||||
public static void addMissingWorlds(WorldTimes worldTimes) {
|
||||
@ -196,7 +196,7 @@ public class AnalysisUtils {
|
||||
stickM++;
|
||||
}
|
||||
}
|
||||
probability *= (stickM / similarM.size());
|
||||
probability *= ((double) stickM / similarM.size());
|
||||
}
|
||||
|
||||
if (!similarW.isEmpty()) {
|
||||
@ -207,7 +207,7 @@ public class AnalysisUtils {
|
||||
}
|
||||
}
|
||||
|
||||
probability *= (stickW / similarW.size());
|
||||
probability *= ((double) stickW / similarW.size());
|
||||
}
|
||||
|
||||
return probability;
|
||||
|
@ -167,7 +167,6 @@ public class HtmlExport extends SpecificExport {
|
||||
copyFromJar(resources, true);
|
||||
}
|
||||
|
||||
|
||||
private void copyFromJar(String[] resources, boolean overwrite) {
|
||||
for (String resource : resources) {
|
||||
try {
|
||||
@ -185,8 +184,9 @@ public class HtmlExport extends SpecificExport {
|
||||
File to = new File(outputFolder, outputFile);
|
||||
to.getParentFile().mkdirs();
|
||||
if (to.exists()) {
|
||||
to.delete();
|
||||
to.createNewFile();
|
||||
if (!to.delete() || !to.createNewFile()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
export(to, lines);
|
||||
}
|
||||
|
@ -855,6 +855,16 @@ public class SQLiteTest {
|
||||
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
|
||||
public void testWorldTableGetWorldNamesNoException() throws SQLException {
|
||||
Set<String> worldNames = db.getWorldTable().getWorldNames();
|
||||
|
Loading…
Reference in New Issue
Block a user