]> git.mxchange.org Git - jcore.git/commitdiff
Added backend (unfinished) for datasources (JNDI). The usage of a Table Data Gateway...
authorRoland Haeder <roland@mxchange.org>
Thu, 20 Aug 2015 12:16:55 +0000 (14:16 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 20 Aug 2015 12:16:55 +0000 (14:16 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

src/org/mxchange/jcore/BaseFrameworkSystem.java
src/org/mxchange/jcore/contact/Contact.java
src/org/mxchange/jcore/database/backend/DatabaseBackend.java
src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java [new file with mode: 0644]
src/org/mxchange/jcore/database/frontend/BaseDatabaseFrontend.java

index 9e74ec3748ac2f75d5268a2315693b6d36322310..d3e7e924b58b390505cffcfafe12cd6a6c5352eb 100644 (file)
@@ -365,6 +365,7 @@ public class BaseFrameworkSystem implements FrameworkInterface {
                // Default database backend
                BaseFrameworkSystem.properties.put("org.mxchange.database.backend.class", "org.mxchange.jcore.database.backend.base64.Base64CsvDatabaseBackend"); //NOI18N
                BaseFrameworkSystem.properties.put("org.mxchange.database.backend.storagepath", "data/"); //NOI18N
+               BaseFrameworkSystem.properties.put("org.mxchange.database.datasource.name", ""); //NOI18N
 
                // For MySQL backend
                BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.host", "localhost"); //NOI18N
index 11664e6a42aa3b298a60579cdd7f2e8f7a43e9a7..cf78950760b0a2a312e01dc24fc14fd8381e92a6 100644 (file)
@@ -61,18 +61,18 @@ public interface Contact extends Storeable {
        public void setGender (final Gender gender);
 
        /**
-        * Surname
+        * First name
         *
-        * @return the surname
+        * @return the first name
         */
        public String getFirstName ();
 
        /**
-        * Surname
+        * First name
         *
-        * @param surname the surname to set
+        * @param firstName the first name to set
         */
-       public void setFirstName (final String surname);
+       public void setFirstName (final String firstName);
 
        /**
         * Family name
index 3ed3038b8133c60d2cb82fa24b3a2145ed1f49db..80d4c9cd33f347b4458b85eb9cdf1e5cec1711bb 100644 (file)
@@ -20,6 +20,7 @@ import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
 import java.sql.SQLException;
 import java.util.Map;
+import javax.naming.NamingException;
 import org.mxchange.jcore.FrameworkInterface;
 import org.mxchange.jcore.criteria.searchable.SearchableCriteria;
 import org.mxchange.jcore.database.result.Result;
@@ -38,11 +39,13 @@ public interface DatabaseBackend extends FrameworkInterface {
         * Tries a connection to the database
         * 
         * @throws java.sql.SQLException If the connection attempt fails
+        * @throws javax.naming.NamingException May be thrown by a backend implementation
         */
-       public void connectToDatabase () throws SQLException;
+       public void connectToDatabase () throws SQLException, NamingException;
 
        /**
-        * Inserts given dataset instance and returns a Result instance on success
+        * Inserts given dataset instance and returns a Result instance on success.
+        * The callee should not modify any content of the dataset instance.
         *
         * @param dataset A dataset instance
         * @return An instance of Result
@@ -56,6 +59,8 @@ public interface DatabaseBackend extends FrameworkInterface {
         * instance. The result instance then provides methods to iterate over all
         * found entries.
         *
+        * The callee should not modify any content of the criteria instance.
+        *
         * @param critera Search critera
         * @return A result instance
         * @throws java.io.IOException If any IO error occurs
diff --git a/src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java
new file mode 100644 (file)
index 0000000..a02d1f5
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcore.database.backend.datasource;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.text.MessageFormat;
+import java.util.Map;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+import org.mxchange.jcore.criteria.searchable.SearchableCriteria;
+import org.mxchange.jcore.database.backend.BaseDatabaseBackend;
+import org.mxchange.jcore.database.backend.DatabaseBackend;
+import org.mxchange.jcore.database.frontend.DatabaseFrontend;
+import org.mxchange.jcore.database.result.Result;
+import org.mxchange.jcore.database.storage.Storeable;
+import org.mxchange.jcore.exceptions.BadTokenException;
+import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
+import org.mxchange.jcore.exceptions.UnsupportedDatabaseDriverException;
+
+/**
+ * A database backend to a data source
+ *
+ * @author Roland Haeder
+ */
+public class DataSourceDatabaseBackend extends BaseDatabaseBackend implements DatabaseBackend {
+       /**
+        * Connection instance
+        */
+       private static Connection connection;
+
+       /**
+        * Prepared statement for total row count
+        */
+       private PreparedStatement totalRows;
+
+       /**
+        * Constructor with table name
+        * 
+        * @param frontend An instance of the frontend
+        * @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseDriverException If the requested driver is not supported
+        */
+       public DataSourceDatabaseBackend (final DatabaseFrontend frontend) throws UnsupportedDatabaseDriverException {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("frontend={0} - CALLED!", frontend)); //NOI18N
+
+               // Get table name
+               String tableName = frontend.getTableName();
+
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("tableName={0}", tableName)); //NOI18N
+
+               // Now that the driver is there, set the table name
+               this.setTableName(tableName);
+
+               // Set frontend
+               this.setFrontend(frontend);
+       }
+
+       @Override
+       public void connectToDatabase () throws SQLException, NamingException {
+               // Trace message
+               this.getLogger().trace("CALLED!");
+
+               // Is the connection given?
+               if (connection == null) {
+                       // Initial context
+                       Context context = new InitialContext();
+
+                       // Get data source instance
+                       DataSource source = (DataSource) context.lookup(this.getProperty("database.datasource.name"));
+
+                       // Debug message
+                       this.getLogger().debug(MessageFormat.format("source={0}", source));
+
+                       // Now as all access data is stored in data source, get a connection from it
+                       connection = source.getConnection();
+
+                       // Debug log
+                       this.getLogger().debug(MessageFormat.format("connection={0}", connection));
+               }
+
+               // Debug message
+               this.getLogger().debug("Connection is up, preparing some statements ..."); //NOI18N
+
+               // Set prepared statement
+               this.totalRows = connection.prepareStatement(String.format("SELECT COUNT(`%s`) AS `cnt` FROM `%s` LIMIT 1", this.getFrontend().getIdName(), this.getTableName())); //NOI18N
+
+               // Trace message
+               this.getLogger().trace("EXIT!");
+       }
+
+       @Override
+       public Result<? extends Storeable> doInsertDataSet (final Map<String, Object> dataset) throws SQLException, IOException {
+               throw new UnsupportedOperationException("Not supported yet: dataset=" + dataset); //To change body of generated methods, choose Tools | Templates.
+       }
+
+       @Override
+       public Result<? extends Storeable> doSelectByCriteria (final SearchableCriteria critera) throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+               throw new UnsupportedOperationException("Not supported yet: criteria=" + critera); //To change body of generated methods, choose Tools | Templates.
+       }
+
+       @Override
+       public void doShutdown () throws SQLException, IOException {
+               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+       }
+
+       @Override
+       public Long getTotalRows () throws IOException, SQLException {
+               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+       }
+}
index 7de1749910bdf0488bd116096e73feaa291d3f5a..7be4a11fa40838312aef054ed2b910c22b2cfaa2 100644 (file)
@@ -23,6 +23,7 @@ import java.sql.SQLException;
 import java.text.MessageFormat;
 import java.util.HashMap;
 import java.util.Map;
+import javax.naming.NamingException;
 import org.mxchange.jcore.BaseFrameworkSystem;
 import org.mxchange.jcore.database.backend.DatabaseBackend;
 import org.mxchange.jcore.database.result.DatabaseResult;
@@ -215,7 +216,7 @@ public abstract class BaseDatabaseFrontend extends BaseFrameworkSystem implement
 
                        // All fine so far, then set it here
                        this.setBackend(backend);
-               } catch (final ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
+               } catch (final ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NamingException ex) {
                        // Continue to throw
                        throw new UnsupportedDatabaseBackendException(ex);
                }