From: Roland Haeder Date: Thu, 20 Aug 2015 12:16:55 +0000 (+0200) Subject: Added backend (unfinished) for datasources (JNDI). The usage of a Table Data Gateway... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=bd206047d5e1bb1d0e604f9f4bb9f9bc571e75d0;p=jcore.git Added backend (unfinished) for datasources (JNDI). The usage of a Table Data Gateway Pattern allows the application to never "know" how the data is being stored or retrived from the database, it only gets its wanted objects back and stores them. Also the used pattern is simple (or simplier than) to understand. It also makes sure that the "business logic" classes is free of any SQL which makes it lesser dependent on SQL-based databases. Signed-off-by:Roland Häder --- diff --git a/src/org/mxchange/jcore/BaseFrameworkSystem.java b/src/org/mxchange/jcore/BaseFrameworkSystem.java index 9e74ec3..d3e7e92 100644 --- a/src/org/mxchange/jcore/BaseFrameworkSystem.java +++ b/src/org/mxchange/jcore/BaseFrameworkSystem.java @@ -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 diff --git a/src/org/mxchange/jcore/contact/Contact.java b/src/org/mxchange/jcore/contact/Contact.java index 11664e6..cf78950 100644 --- a/src/org/mxchange/jcore/contact/Contact.java +++ b/src/org/mxchange/jcore/contact/Contact.java @@ -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 diff --git a/src/org/mxchange/jcore/database/backend/DatabaseBackend.java b/src/org/mxchange/jcore/database/backend/DatabaseBackend.java index 3ed3038..80d4c9c 100644 --- a/src/org/mxchange/jcore/database/backend/DatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/DatabaseBackend.java @@ -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 index 0000000..a02d1f5 --- /dev/null +++ b/src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java @@ -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 . + */ +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 doInsertDataSet (final Map dataset) throws SQLException, IOException { + throw new UnsupportedOperationException("Not supported yet: dataset=" + dataset); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Result 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. + } +} diff --git a/src/org/mxchange/jcore/database/frontend/BaseDatabaseFrontend.java b/src/org/mxchange/jcore/database/frontend/BaseDatabaseFrontend.java index 7de1749..7be4a11 100644 --- a/src/org/mxchange/jcore/database/frontend/BaseDatabaseFrontend.java +++ b/src/org/mxchange/jcore/database/frontend/BaseDatabaseFrontend.java @@ -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); }