-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved database-core to core and added credential reader
- Loading branch information
Showing
20 changed files
with
279 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
core/src/main/java/org/geysermc/databaseutils/CredentialsFileHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright (c) 2024 GeyserMC <https://geysermc.org> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
* @author GeyserMC | ||
* @link https://github.com/GeyserMC/DatabaseUtils | ||
*/ | ||
package org.geysermc.databaseutils; | ||
|
||
import static org.geysermc.databaseutils.util.StringUtils.nullToEmpty; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Properties; | ||
import org.geysermc.databaseutils.sql.SqlDialect; | ||
|
||
final class CredentialsFileHandler { | ||
public DatabaseConfig handle(SqlDialect dialect, Path credentialsFile) { | ||
DatabaseConfig config = defaultValuesFor(dialect); | ||
if (credentialsFile != null) { | ||
if (Files.exists(credentialsFile)) { | ||
config = readConfig(credentialsFile); | ||
} else { | ||
createConfig(config, credentialsFile); | ||
} | ||
} | ||
return config; | ||
} | ||
|
||
private DatabaseConfig readConfig(Path credentialsFile) { | ||
var properties = new Properties(); | ||
try (var reader = Files.newBufferedReader(credentialsFile)) { | ||
properties.load(reader); | ||
} catch (IOException exception) { | ||
throw new IllegalStateException("Failed to load credentials!", exception); | ||
} | ||
return new DatabaseConfig( | ||
properties.getProperty("url"), | ||
properties.getProperty("username"), | ||
properties.getProperty("password"), | ||
Integer.parseInt(properties.getProperty("connectionPoolSize"))); | ||
} | ||
|
||
private void createConfig(DatabaseConfig defaults, Path toStore) { | ||
// not using properties here so that the values aren't escaped and there isn't a timestamp comment | ||
var lines = new ArrayList<String>(); | ||
lines.add("# Database configuration"); | ||
lines.add("url=" + defaults.url()); | ||
lines.add("username=" + nullToEmpty(defaults.username())); | ||
lines.add("password=" + nullToEmpty(defaults.password())); | ||
lines.add("connectionPoolSize=" + defaults.connectionPoolSize()); | ||
try { | ||
Files.write(toStore, lines); | ||
} catch (IOException exception) { | ||
throw new IllegalStateException("Failed to save credential defaults!", exception); | ||
} | ||
} | ||
|
||
private DatabaseConfig defaultValuesFor(SqlDialect dialect) { | ||
return switch (dialect) { | ||
case H2 -> configFor("jdbc:h2:./database", "sa"); | ||
case SQL_SERVER -> configFor("jdbc:sqlserver://localhost;encrypt=true;integratedSecurity=true;"); | ||
case MYSQL -> configFor("jdbc:mysql://localhost/database"); | ||
case ORACLE_DATABASE -> configFor("jdbc:oracle:thin:@//localhost/service"); | ||
case POSTGRESQL -> configFor("jdbc:postgresql://localhost/database"); | ||
case SQLITE -> configFor("jdbc:sqlite:./database"); | ||
}; | ||
} | ||
|
||
private DatabaseConfig configFor(String url) { | ||
return configFor(url, null); | ||
} | ||
|
||
private DatabaseConfig configFor(String url, String username) { | ||
// default pool size is 5 | ||
return new DatabaseConfig(url, username, null, 5); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
core/src/main/java/org/geysermc/databaseutils/DatabaseConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (c) 2024 GeyserMC <https://geysermc.org> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
* @author GeyserMC | ||
* @link https://github.com/GeyserMC/DatabaseUtils | ||
*/ | ||
package org.geysermc.databaseutils; | ||
|
||
public record DatabaseConfig(String url, String username, String password, int connectionPoolSize) { | ||
public DatabaseConfig { | ||
if (username != null && (username.isEmpty() || "null".equals(username))) { | ||
username = null; | ||
} | ||
if (password != null && (password.isEmpty() || "null".equals(password))) { | ||
password = null; | ||
} | ||
|
||
if (url == null || url.isEmpty()) throw new IllegalArgumentException("url cannot be null or empty"); | ||
if (connectionPoolSize <= 0) throw new IllegalArgumentException("connectionPoolSize has to be at least 1"); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
core/src/main/java/org/geysermc/databaseutils/DatabaseContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (c) 2024 GeyserMC <https://geysermc.org> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
* @author GeyserMC | ||
* @link https://github.com/GeyserMC/DatabaseUtils | ||
*/ | ||
package org.geysermc.databaseutils; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import org.geysermc.databaseutils.codec.TypeCodecRegistry; | ||
import org.geysermc.databaseutils.sql.SqlDialect; | ||
|
||
public record DatabaseContext( | ||
String url, | ||
String username, | ||
String password, | ||
String poolName, | ||
int connectionPoolSize, | ||
SqlDialect dialect, | ||
ExecutorService service, | ||
TypeCodecRegistry registry) { | ||
public DatabaseContext { | ||
if (poolName == null || poolName.isEmpty()) | ||
throw new IllegalArgumentException("poolName cannot be null or empty"); | ||
if (dialect == null) throw new IllegalArgumentException("dialect cannot be null"); | ||
} | ||
|
||
public DatabaseContext( | ||
DatabaseConfig config, | ||
String poolName, | ||
SqlDialect dialect, | ||
ExecutorService service, | ||
TypeCodecRegistry registry) { | ||
this( | ||
config.url(), | ||
config.username(), | ||
config.password(), | ||
poolName, | ||
config.connectionPoolSize(), | ||
dialect, | ||
service, | ||
registry); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.