diff --git a/.classpath b/.classpath index d54800d..bb81e2f 100644 --- a/.classpath +++ b/.classpath @@ -1,10 +1,20 @@ - + - + - - + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore index 5cb20e8..cd1444b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /bin/ /settings.properties +/target/ diff --git a/.project b/.project index b72cc40..d387340 100644 --- a/.project +++ b/.project @@ -10,8 +10,14 @@ + + org.eclipse.m2e.core.maven2Builder + + + + org.eclipse.m2e.core.maven2Nature org.eclipse.jdt.core.javanature diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index e256158..e21538b 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,12 +1,13 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=11 +org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.release=enabled -org.eclipse.jdt.core.compiler.source=11 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7a4ab0c --- /dev/null +++ b/pom.xml @@ -0,0 +1,41 @@ + + 4.0.0 + org.leolo.internal.transport + nrdata-2 + 0.0.1-SNAPSHOT + + src + + + maven-compiler-plugin + 3.8.0 + + 1.7 + 1.7 + + + + + + + org.mariadb.jdbc + mariadb-java-client + 2.7.2 + + + org.json + json + 20210307 + + + org.apache.logging.log4j + log4j-core + 2.13.3 + + + org.apache.logging.log4j + log4j-api + 2.13.3 + + + \ No newline at end of file diff --git a/src/log4j2.xml b/src/log4j2.xml new file mode 100644 index 0000000..6e6f5f9 --- /dev/null +++ b/src/log4j2.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/org/leolo/transport/nr2/ConfigurationManager.java b/src/org/leolo/transport/nr2/ConfigurationManager.java new file mode 100644 index 0000000..17e7a7b --- /dev/null +++ b/src/org/leolo/transport/nr2/ConfigurationManager.java @@ -0,0 +1,103 @@ +package org.leolo.transport.nr2; + +import java.io.FileReader; +import java.io.IOException; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.util.Collection; +import java.util.Enumeration; +import java.util.Properties; +import java.util.Set; +import java.util.function.BiConsumer; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class ConfigurationManager { + + private static ConfigurationManager instance; + private Logger log = LogManager.getLogger(); + + private Properties properties; + + public static synchronized final ConfigurationManager getInstance() { + if(instance==null) { + instance = new ConfigurationManager(); + } + return instance; + } + + private ConfigurationManager() { + properties = new Properties(); + try { + properties.load(new FileReader("settings.properties")); + } catch (IOException e) { + log.atError().log(e.getMessage(), e); + } + } + + public String getProperty(String key) { + return properties.getProperty(key); + } + + public String getProperty(String key, String defaultValue) { + return properties.getProperty(key, defaultValue); + } + + public Enumeration propertyNames() { + return properties.propertyNames(); + } + + public void list(PrintStream out) { + properties.list(out); + } + + public void list(PrintWriter out) { + properties.list(out); + } + + public int size() { + return properties.size(); + } + + public boolean isEmpty() { + return properties.isEmpty(); + } + + public Enumeration keys() { + return properties.keys(); + } + + public boolean contains(Object value) { + return properties.contains(value); + } + + public boolean containsValue(Object value) { + return properties.containsValue(value); + } + + public boolean containsKey(Object key) { + return properties.containsKey(key); + } + + public Object get(Object key) { + return properties.get(key); + } + + public Set keySet() { + return properties.keySet(); + } + + public Collection values() { + return properties.values(); + } + + public Object getOrDefault(Object key, Object defaultValue) { + return properties.getOrDefault(key, defaultValue); + } + + public void forEach(BiConsumer action) { + properties.forEach(action); + } + +} diff --git a/src/org/leolo/transport/nr2/Constants.java b/src/org/leolo/transport/nr2/Constants.java new file mode 100644 index 0000000..c51e169 --- /dev/null +++ b/src/org/leolo/transport/nr2/Constants.java @@ -0,0 +1,14 @@ +package org.leolo.transport.nr2; + +public class Constants { + + public static final int RV_NORMAL = 0x00000000; + public static final int RV_FATAL_ERROR = 0xfeedbeef; + public static final int RV_DB_CONN_ERROR = 0xfeedbeee; + + public static final int DB_MIN_CONN = 5; + public static final int DB_MAX_CONN = 100; + + public static final int MAX_THREAD = 20; + +} diff --git a/src/org/leolo/transport/nr2/DataLoader.java b/src/org/leolo/transport/nr2/DataLoader.java new file mode 100644 index 0000000..3fc387a --- /dev/null +++ b/src/org/leolo/transport/nr2/DataLoader.java @@ -0,0 +1,22 @@ +package org.leolo.transport.nr2; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class DataLoader { + + private Logger log = LogManager.getLogger(); + + public static final void main(String [] args) { + new DataLoader().start(); + } + + private DataLoader() { + + } + + private void start() { + log.always().log("Started"); + DatabaseManager.getInstance(); + } +} diff --git a/src/org/leolo/transport/nr2/DatabaseManager.java b/src/org/leolo/transport/nr2/DatabaseManager.java new file mode 100644 index 0000000..693c473 --- /dev/null +++ b/src/org/leolo/transport/nr2/DatabaseManager.java @@ -0,0 +1,64 @@ +package org.leolo.transport.nr2; + +import java.sql.Connection; +import java.sql.SQLException; + +import javax.sql.XAConnection; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.mariadb.jdbc.MariaDbPoolDataSource; + +public class DatabaseManager { + + private static DatabaseManager instance; + private static Logger log = LogManager.getLogger(DatabaseManager.class); + private MariaDbPoolDataSource ds; + + + public synchronized final static DatabaseManager getInstance() { + if(instance==null) { + instance = new DatabaseManager(); + } + return instance; + } + + private DatabaseManager() { + ConfigurationManager cm = ConfigurationManager.getInstance(); + log.atInfo().log("Creating Database Manager"); + if( + !cm.containsKey("host") || + !cm.containsKey("user") || + !cm.containsKey("pass") || + !cm.containsKey("name") + ) { + log.atFatal().log("Missing required parameter!"); + System.exit(Constants.RV_FATAL_ERROR); + } + String url = "jdbc:mariadb://"+cm.getProperty("host")+ + ":3306"+ + "/"+cm.getProperty("name"); + log.info("Connecting to DB {} as {}", url, cm.get("user")); + try { + ds = new MariaDbPoolDataSource(url); + ds.setMaxPoolSize(Constants.DB_MAX_CONN); + ds.setMinPoolSize(Constants.DB_MIN_CONN); + ds.setUser(cm.getProperty("user").toString()); + ds.setPassword(cm.getProperty("pwd").toString()); + } catch (SQLException e) { + log.fatal("Cannot connect to DB",e); + System.exit(Constants.RV_DB_CONN_ERROR); + } + } + + public Connection getConnection() throws SQLException { + Connection conn = ds.getConnection(); + conn.setAutoCommit(false); + return conn; + } + + public XAConnection getXAConnection() throws SQLException { + XAConnection conn = ds.getXAConnection(); + return conn; + } +}