Init code push.

This commit is contained in:
2021-05-10 09:38:04 +02:00
parent 664f81d941
commit ce8596f52a
6 changed files with 483 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
package net.horizoncode.nekorip;
import lombok.Getter;
import net.horizoncode.nekorip.config.Config;
import net.horizoncode.nekorip.mysql.MySQLAPI;
import org.apache.commons.lang3.Validate;
import java.io.File;
/**
* Created by HorizonCode at 10/05/2021
*
* @author HorizonCode
* @since 10/05/2021
**/
@Getter
public class NekoRIP {
private static NekoRIP instance;
private Config config;
private MySQLAPI mySQL;
public static void main(String[] args) {
new NekoRIP().init();
}
private void init() {
Validate.isTrue(instance == null, "Instance is already initialized.");
instance = this;
File config = new File("config.toml");
this.config = new Config(config);
if(this.config.isJustCreated()){
System.out.println("No config found, created one. Please edit the config!");
System.gc();
System.exit(0);
return;
}
String host = this.config.getStringOrDefault("mysql.host", "");
int port = this.config.getIntOrDefault("mysql.port", 3306);
String database = this.config.getStringOrDefault("mysql.database", "nekorip");
String username = this.config.getStringOrDefault("mysql.username", "root");
String password = this.config.getStringOrDefault("mysql.password", "");
mySQL = MySQLAPI.getInstance();
if(!mySQL.connect(host, port, username, password, database))
System.out.println("Failed to connect to mysql.");
}
}

View File

@@ -0,0 +1,70 @@
package net.horizoncode.nekorip.config;
import lombok.AccessLevel;
import lombok.Getter;
import net.horizoncode.nekorip.NekoRIP;
import org.apache.commons.io.FileUtils;
import org.tomlj.Toml;
import org.tomlj.TomlParseResult;
import java.io.File;
import java.io.IOException;
@Getter
public class Config {
@Getter(AccessLevel.NONE)
private final File configFile;
@Getter(AccessLevel.PACKAGE)
private TomlParseResult toml;
private boolean justCreated;
public Config(File cfg) {
configFile = cfg;
if (!configFile.exists()) {
try {
FileUtils.copyInputStreamToFile(NekoRIP.class.getResourceAsStream("/" + cfg.getName()), configFile);
justCreated = true;
} catch (IOException e) {
e.printStackTrace();
}
} else {
if (configFile.isDirectory()) {
try {
FileUtils.copyInputStreamToFile(NekoRIP.class.getResourceAsStream("/" + cfg.getName()), configFile);
justCreated = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (justCreated)
return;
try {
toml = Toml.parse(configFile.toPath());
if (toml.hasErrors()) {
toml.errors().forEach(error -> System.err.println(error.toString()));
System.exit(-1);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public int getIntOrDefault(String key, int defaultValue) {
return getToml().contains(key) && getToml().get(key) instanceof Integer ? (int) getToml().get(key) : defaultValue;
}
public boolean getBooleanOrDefault(String key, boolean defaultValue) {
return getToml().contains(key) && getToml().get(key) instanceof Boolean ? getToml().getBoolean(key) : defaultValue;
}
public String getStringOrDefault(String key, String defaultValue) {
return getToml().contains(key) ? getToml().getString(key) : defaultValue;
}
}

View File

@@ -0,0 +1,116 @@
package net.horizoncode.nekorip.mysql;
import java.sql.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MySQLAPI {
private static final MySQLAPI INSTANCE;
static {
INSTANCE = new MySQLAPI();
}
private String host;
private int port;
private String user;
private String password;
private String database;
private Connection connection;
private MySQLAPI() {
}
public static MySQLAPI getInstance() {
return MySQLAPI.INSTANCE;
}
public boolean connect(final String host, final int port, final String user, final String password, final String database) {
this.host = host;
this.port = port;
this.user = user;
this.password = password;
this.database = database;
return this.openConnection();
}
private boolean openConnection() {
try {
if (this.connection != null) {
this.connection.close();
}
Class.forName("com.mysql.cj.jdbc.Driver");
this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.user, this.password);
establishConnection();
} catch (Exception e) {
System.out.println("MySQL error while trying connecting to " + this.host + ":" + this.port + "/" + this.database + ": " + e.getMessage());
return false;
}
System.out.println("MySQL connected to " + this.host + ":" + this.port + "/" + this.database);
return true;
}
private void establishConnection() {
ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
scheduleTaskExecutor.scheduleAtFixedRate(() -> {
try {
Statement s = getConnection().createStatement();
s.executeQuery("SELECT 1+1");
s.close();
} catch (SQLException e) {
e.printStackTrace();
}
}, 0, 10, TimeUnit.MINUTES);
}
public void closeConnection() {
try {
System.out.println("MySQL connection closed!");
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void closeRessources(final ResultSet rs, final PreparedStatement st, final Statement s) {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
}
}
if (st != null) {
try {
st.close();
} catch (SQLException ex2) {
}
}
if (s != null) {
try {
s.close();
} catch (SQLException ex2) {
}
}
}
public void closeRessources(final ResultSet rs) {
this.closeRessources(rs, null, null);
}
public void closeRessources(final PreparedStatement st) {
this.closeRessources(null, st, null);
}
public void closeRessources(final Statement s) {
this.closeRessources(null, null, s);
}
public Connection getConnection() {
return connection;
}
}

View File

@@ -0,0 +1,9 @@
[web]
port = 62011
[mysql]
host = ""
port = 3306
database = ""
username = ""
password = ""