-
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.
- Loading branch information
Showing
6 changed files
with
410 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
### IDEA ### | ||
.idea | ||
*.iml | ||
|
||
### MAVEN ### | ||
target/ |
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,88 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>de.garrus.cloudnet</groupId> | ||
<artifactId>cloudnet-postgres-database-provider</artifactId> | ||
<version>1.0</version> | ||
|
||
|
||
<properties> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
<!-- Repository --> | ||
<repositories> | ||
<!-- New Maven repository for releases --> | ||
<repository> | ||
<id>releases</id> | ||
<url>https://repo.cloudnetservice.eu/repository/releases/</url> | ||
</repository> | ||
|
||
<!-- New Maven repository for snapshots --> | ||
<repository> | ||
<id>snapshots</id> | ||
<url>https://repo.cloudnetservice.eu/repository/snapshots/</url> | ||
</repository> | ||
</repositories> | ||
|
||
<distributionManagement> | ||
<repository> | ||
<id>garrus-repository</id> | ||
<url>ftp://repo-upload.garrus.de/repo/releases</url> | ||
</repository> | ||
<snapshotRepository> | ||
<id>garrus-repository</id> | ||
<url>ftp://repo-upload.garrus.de/repo/shapshot</url> | ||
</snapshotRepository> | ||
</distributionManagement> | ||
<build> | ||
<extensions> | ||
<!-- Enabling the use of FTP --> | ||
<extension> | ||
<groupId>org.apache.maven.wagon</groupId> | ||
<artifactId>wagon-ftp</artifactId> | ||
<version>3.4.1</version> | ||
</extension> | ||
</extensions> | ||
</build> | ||
|
||
<dependencies> | ||
<!-- cloudnet application for modules --> | ||
<dependency> | ||
<groupId>de.dytanic.cloudnet</groupId> | ||
<artifactId>cloudnet</artifactId> | ||
<version>3.3.0-RELEASE</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- cloudnet driver for plugins and modules --> | ||
<dependency> | ||
<groupId>de.dytanic.cloudnet</groupId> | ||
<artifactId>cloudnet-driver</artifactId> | ||
<version>3.3.0-RELEASE</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
|
||
<dependency> | ||
<groupId>org.postgresql</groupId> | ||
<artifactId>postgresql</artifactId> | ||
<version>42.2.12</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.zaxxer</groupId> | ||
<artifactId>HikariCP</artifactId> | ||
<version>3.4.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.javassist</groupId> | ||
<artifactId>javassist</artifactId> | ||
<version>3.27.0-GA</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
40 changes: 40 additions & 0 deletions
40
...in/java/de/garrus/cloudnet/database/postgres/provider/CloudNetPostgresDatabaseModule.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,40 @@ | ||
package de.garrus.cloudnet.database.postgres.provider; | ||
|
||
import de.dytanic.cloudnet.database.AbstractDatabaseProvider; | ||
import de.dytanic.cloudnet.driver.module.ModuleLifeCycle; | ||
import de.dytanic.cloudnet.driver.module.ModuleTask; | ||
import de.dytanic.cloudnet.module.NodeCloudNetModule; | ||
|
||
public class CloudNetPostgresDatabaseModule extends NodeCloudNetModule { | ||
private static CloudNetPostgresDatabaseModule instance; | ||
|
||
public static CloudNetPostgresDatabaseModule getInstance() { | ||
return instance; | ||
} | ||
|
||
@ModuleTask(order = 127, event = ModuleLifeCycle.LOADED) | ||
public void init() { | ||
instance = this; | ||
} | ||
|
||
@ModuleTask(order = 126, event = ModuleLifeCycle.LOADED) | ||
public void initConfig() { | ||
this.getConfig().getString("addresse", "jdbc:postgresql://127.0.0.1:5432/database"); | ||
this.getConfig().getString("username", "root"); | ||
this.getConfig().getString("password", "root"); | ||
this.getConfig().getInt("connectionPoolSize", 15); | ||
this.getConfig().getInt("connectionTimeout", 5000); | ||
this.getConfig().getInt("validationTimeout", 5000); | ||
this.saveConfig(); | ||
} | ||
|
||
@ModuleTask(order = 125, event = ModuleLifeCycle.LOADED) | ||
public void registerDatabaseProvider() { | ||
this.getRegistry().registerService(AbstractDatabaseProvider.class, "postgres", new PostgresSQLDatabaseProvider(getConfig(),null)); | ||
} | ||
|
||
@ModuleTask(order = 127, event = ModuleLifeCycle.STOPPED) | ||
public void unregisterDatabaseProvider() { | ||
this.getRegistry().unregisterService(AbstractDatabaseProvider.class, "postgres"); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/de/garrus/cloudnet/database/postgres/provider/PostgresDatabase.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,12 @@ | ||
package de.garrus.cloudnet.database.postgres.provider; | ||
|
||
import de.dytanic.cloudnet.database.sql.SQLDatabase; | ||
import de.dytanic.cloudnet.database.sql.SQLDatabaseProvider; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
|
||
public class PostgresDatabase extends SQLDatabase { | ||
public PostgresDatabase(SQLDatabaseProvider databaseProvider, String name, ExecutorService executorService) { | ||
super(databaseProvider, name, executorService); | ||
} | ||
} |
221 changes: 221 additions & 0 deletions
221
src/main/java/de/garrus/cloudnet/database/postgres/provider/PostgresSQLDatabaseProvider.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,221 @@ | ||
package de.garrus.cloudnet.database.postgres.provider; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import de.dytanic.cloudnet.common.collection.NetorHashMap; | ||
import de.dytanic.cloudnet.common.collection.Pair; | ||
import de.dytanic.cloudnet.common.concurrent.IThrowableCallback; | ||
import de.dytanic.cloudnet.common.document.gson.JsonDocument; | ||
import de.dytanic.cloudnet.database.IDatabase; | ||
import de.dytanic.cloudnet.database.sql.SQLDatabaseProvider; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ExecutorService; | ||
|
||
public class PostgresSQLDatabaseProvider extends SQLDatabaseProvider { | ||
private static final long NEW_CREATION_DELAY = 600000L; | ||
protected final NetorHashMap<String, Long, IDatabase> cachedDatabaseInstances = new NetorHashMap<>(); | ||
protected final HikariDataSource hikariDataSource = new HikariDataSource(); | ||
private final JsonDocument config; | ||
private String address; | ||
|
||
public PostgresSQLDatabaseProvider(JsonDocument config,ExecutorService executorService) { | ||
super(executorService); | ||
this.config = config; | ||
} | ||
|
||
public boolean init() { | ||
this.address = this.config.getString("addresse"); | ||
this.hikariDataSource.setJdbcUrl(address); | ||
this.hikariDataSource.setUsername(this.config.getString("username")); | ||
this.hikariDataSource.setPassword(this.config.getString("password")); | ||
this.hikariDataSource.setDriverClassName("org.postgresql.Driver"); | ||
this.hikariDataSource.setMaximumPoolSize(this.config.getInt("connectionPoolSize")); | ||
this.hikariDataSource.setConnectionTimeout(this.config.getInt("connectionTimeout")); | ||
this.hikariDataSource.setValidationTimeout(this.config.getInt("validationTimeout")); | ||
this.hikariDataSource.validate(); | ||
return true; | ||
} | ||
|
||
public IDatabase getDatabase(String name) { | ||
Preconditions.checkNotNull(name); | ||
this.removedOutdatedEntries(); | ||
if (!this.cachedDatabaseInstances.contains(name)) { | ||
this.cachedDatabaseInstances.add(name, System.currentTimeMillis() + NEW_CREATION_DELAY, new PostgresDatabase(this, name,super.executorService) { | ||
}); | ||
} | ||
|
||
return this.cachedDatabaseInstances.getSecond(name); | ||
} | ||
|
||
public boolean containsDatabase(String name) { | ||
Preconditions.checkNotNull(name); | ||
this.removedOutdatedEntries(); | ||
|
||
return getDatabaseNames().contains(name); | ||
} | ||
|
||
public boolean deleteDatabase(String name) { | ||
Preconditions.checkNotNull(name); | ||
this.cachedDatabaseInstances.remove(name); | ||
if (this.containsDatabase(name)) { | ||
return false; | ||
} | ||
|
||
try { | ||
Connection connection = getConnection(); | ||
PreparedStatement preparedStatement = connection.prepareStatement("DROP TABLE " + name); | ||
boolean deleted = preparedStatement.executeUpdate() != -1; | ||
|
||
if (!preparedStatement.isClosed()) { | ||
preparedStatement.close(); | ||
} | ||
|
||
if (!connection.isClosed()) { | ||
connection.close(); | ||
} | ||
|
||
return deleted; | ||
} catch (SQLException throwables) { | ||
throwables.printStackTrace(); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public List<String> getDatabaseNames() { | ||
return this.executeQuery("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='PUBLIC'", resultSet -> { | ||
List<String> collection = new ArrayList<>(); | ||
|
||
while (resultSet.next()) { | ||
collection.add(resultSet.getString("table_name")); | ||
} | ||
|
||
return collection; | ||
}); | ||
} | ||
|
||
public String getName() { | ||
return this.config.getString("database"); | ||
} | ||
|
||
public void close() { | ||
this.hikariDataSource.close(); | ||
} | ||
|
||
private void removedOutdatedEntries() { | ||
Iterator<Map.Entry<String, Pair<Long, IDatabase>>> var1 = this.cachedDatabaseInstances.entrySet().iterator(); | ||
|
||
while (var1.hasNext()) { | ||
Map.Entry<String, Pair<Long, IDatabase>> entry = var1.next(); | ||
if ((Long) ((Pair) entry.getValue()).getFirst() < System.currentTimeMillis()) { | ||
this.cachedDatabaseInstances.remove(entry.getKey()); | ||
} | ||
} | ||
|
||
} | ||
|
||
public Connection getConnection() throws SQLException { | ||
return this.hikariDataSource.getConnection(); | ||
} | ||
|
||
public NetorHashMap<String, Long, IDatabase> getCachedDatabaseInstances() { | ||
return this.cachedDatabaseInstances; | ||
} | ||
|
||
public HikariDataSource getHikariDataSource() { | ||
return this.hikariDataSource; | ||
} | ||
|
||
public JsonDocument getConfig() { | ||
return this.config; | ||
} | ||
|
||
public String getAddress() { | ||
return this.address; | ||
} | ||
|
||
public int executeUpdate(String query, Object... objects) { | ||
Preconditions.checkNotNull(query); | ||
Preconditions.checkNotNull(objects); | ||
|
||
try { | ||
Connection connection = getConnection(); | ||
PreparedStatement preparedStatement = connection.prepareStatement(query); | ||
|
||
for (int i = 0; i < objects.length; i++) { | ||
preparedStatement.setString(i + 1, objects[i].toString()); | ||
} | ||
int i = preparedStatement.executeUpdate(); | ||
|
||
if (!preparedStatement.isClosed()) { | ||
preparedStatement.close(); | ||
} | ||
if (!connection.isClosed()) { | ||
connection.close(); | ||
} | ||
|
||
return i; | ||
} catch (SQLException throwables) { | ||
throwables.printStackTrace(); | ||
return -1; | ||
} | ||
|
||
} | ||
|
||
public <T> T executeQuery(String query, IThrowableCallback<ResultSet, T> callback, Object... objects) { | ||
Preconditions.checkNotNull(query); | ||
Preconditions.checkNotNull(callback); | ||
Preconditions.checkNotNull(objects); | ||
|
||
try { | ||
Connection connection = getConnection(); | ||
PreparedStatement preparedStatement = connection.prepareStatement(query); | ||
|
||
for (int i = 0; i < objects.length; i++) { | ||
preparedStatement.setString(i + 1, objects[i].toString()); | ||
} | ||
|
||
ResultSet resultSet = preparedStatement.executeQuery(); | ||
|
||
try { | ||
T call = callback.call(resultSet); | ||
|
||
if (!resultSet.isClosed()) { | ||
resultSet.close(); | ||
} | ||
if (!preparedStatement.isClosed()) { | ||
preparedStatement.close(); | ||
} | ||
if (!connection.isClosed()) { | ||
connection.close(); | ||
} | ||
return call; | ||
|
||
} catch (Throwable throwable) { | ||
if (!resultSet.isClosed()) { | ||
resultSet.close(); | ||
} | ||
if (!preparedStatement.isClosed()) { | ||
preparedStatement.close(); | ||
} | ||
if (!connection.isClosed()) { | ||
connection.close(); | ||
} | ||
throwable.printStackTrace(); | ||
return null; | ||
} | ||
} catch (SQLException throwables) { | ||
throwables.printStackTrace(); | ||
return null; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.