From 2d54e2b8310001dd41ec8ad50ad3d66b91585110 Mon Sep 17 00:00:00 2001 From: jenz Date: Sat, 3 Jun 2023 19:34:48 +0200 Subject: [PATCH] finally redid the settings file so DB connections just depend on json file on disk --- .../main/java/DataMapper/DBCPDataSource.java | 15 +++---- .../main/java/DataMapper/DBCPDataSource2.java | 15 +++---- .../main/java/DataMapper/settingsInit.java | 43 +++++++++++++++++++ 3 files changed, 55 insertions(+), 18 deletions(-) create mode 100644 RaceTimer/racetimer_endpoints/src/main/java/DataMapper/settingsInit.java diff --git a/RaceTimer/racetimer_endpoints/src/main/java/DataMapper/DBCPDataSource.java b/RaceTimer/racetimer_endpoints/src/main/java/DataMapper/DBCPDataSource.java index 251f52e6..d2ce02a8 100644 --- a/RaceTimer/racetimer_endpoints/src/main/java/DataMapper/DBCPDataSource.java +++ b/RaceTimer/racetimer_endpoints/src/main/java/DataMapper/DBCPDataSource.java @@ -17,14 +17,14 @@ import org.apache.commons.dbcp2.BasicDataSource; */ public class DBCPDataSource { - private static BasicDataSource ds = new BasicDataSource(); - - static { + public static Connection getConnection() throws SQLException { + BasicDataSource ds = new BasicDataSource(); try { + settingsInit settingsInit = new settingsInit(); ds.setDriver(new com.mysql.cj.jdbc.Driver()); - ds.setUrl(settings.racetimerURL); - ds.setUsername(settings.racetimerUser); - ds.setPassword(settings.racetimerPassword); + ds.setUrl(settingsInit.racetimerURL); + ds.setUsername(settingsInit.racetimerUser); + ds.setPassword(settingsInit.racetimerPassword); ds.setMaxTotal(-1); ds.setMinIdle(5); ds.setMaxIdle(-1); @@ -32,9 +32,6 @@ public class DBCPDataSource { } catch (SQLException ex) { Logger.getLogger(DBCPDataSource.class.getName()).log(Level.SEVERE, null, ex); } - } - - public static Connection getConnection() throws SQLException { return ds.getConnection(); } diff --git a/RaceTimer/racetimer_endpoints/src/main/java/DataMapper/DBCPDataSource2.java b/RaceTimer/racetimer_endpoints/src/main/java/DataMapper/DBCPDataSource2.java index e02c4a24..2d666794 100644 --- a/RaceTimer/racetimer_endpoints/src/main/java/DataMapper/DBCPDataSource2.java +++ b/RaceTimer/racetimer_endpoints/src/main/java/DataMapper/DBCPDataSource2.java @@ -17,14 +17,14 @@ import org.apache.commons.dbcp2.BasicDataSource; */ public class DBCPDataSource2 { - private static BasicDataSource ds = new BasicDataSource(); - - static { + public static Connection getConnection() throws SQLException { + BasicDataSource ds = new BasicDataSource(); try { + settingsInit settingsInit = new settingsInit(); ds.setDriver(new com.mysql.cj.jdbc.Driver()); - ds.setUrl(settings.forumURL); - ds.setUsername(settings.forumUser); - ds.setPassword(settings.forumPassword); + ds.setUrl(settingsInit.forumURL); + ds.setUsername(settingsInit.forumUser); + ds.setPassword(settingsInit.forumPassword); ds.setMaxTotal(-1); ds.setMinIdle(5); ds.setMaxIdle(-1); @@ -32,9 +32,6 @@ public class DBCPDataSource2 { } catch (SQLException ex) { Logger.getLogger(DBCPDataSource.class.getName()).log(Level.SEVERE, null, ex); } - } - - public static Connection getConnection() throws SQLException { return ds.getConnection(); } diff --git a/RaceTimer/racetimer_endpoints/src/main/java/DataMapper/settingsInit.java b/RaceTimer/racetimer_endpoints/src/main/java/DataMapper/settingsInit.java new file mode 100644 index 00000000..11d125fd --- /dev/null +++ b/RaceTimer/racetimer_endpoints/src/main/java/DataMapper/settingsInit.java @@ -0,0 +1,43 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package DataMapper; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.json.JSONObject; + +/** + * + * @author Christian + */ +public class settingsInit { + String text; + public static String racetimerURL = ""; + static String racetimerUser = ""; + static String racetimerPassword = ""; + static String forumURL = ""; + static String forumUser = ""; + static String forumPassword = ""; + + public settingsInit() { + try { + this.text = new String(Files.readAllBytes(Paths.get("/opt/tomcat/race_backend_settings.json")), StandardCharsets.UTF_8); + JSONObject obj = new JSONObject(text); + this.racetimerURL = obj.getString("racetimerURL"); + this.racetimerUser = obj.getString("racetimerUser"); + this.racetimerPassword = obj.getString("racetimerPassword"); + this.forumURL = obj.getString("forumURL"); + this.forumUser = obj.getString("forumUser"); + this.forumPassword = obj.getString("forumPassword"); + } catch (IOException ex) { + Logger.getLogger(settingsInit.class.getName()).log(Level.SEVERE, null, ex); + } + } +}