From bd206047d5e1bb1d0e604f9f4bb9f9bc571e75d0 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Thu, 20 Aug 2015 14:16:55 +0200 Subject: [PATCH] =?utf8?q?Added=20backend=20(unfinished)=20for=20datasourc?= =?utf8?q?es=20(JNDI).=20The=20usage=20of=20a=20Table=20Data=20Gateway=20P?= =?utf8?q?attern=20allows=20the=20application=20to=20never=20"know"=20how?= =?utf8?q?=20the=20data=20is=20being=20stored=20or=20retrived=20from=20the?= =?utf8?q?=20database,=20it=20only=20gets=20its=20wanted=20objects=20back?= =?utf8?q?=20and=20stores=20them.=20Also=20the=20used=20pattern=20is=20sim?= =?utf8?q?ple=20(or=20simplier=20than)=20to=20understand.=20It=20also=20ma?= =?utf8?q?kes=20sure=20that=20the=20"business=20logic"=20classes=20is=20fr?= =?utf8?q?ee=20of=20any=20SQL=20which=20makes=20it=20lesser=20dependent=20?= =?utf8?q?on=20SQL-based=20databases.=20Signed-off-by:Roland=20H=C3=A4der?= =?utf8?q?=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../mxchange/jcore/BaseFrameworkSystem.java | 1 + src/org/mxchange/jcore/contact/Contact.java | 10 +- .../database/backend/DatabaseBackend.java | 9 +- .../datasource/DataSourceDatabaseBackend.java | 131 ++++++++++++++++++ .../frontend/BaseDatabaseFrontend.java | 3 +- 5 files changed, 146 insertions(+), 8 deletions(-) create mode 100644 src/org/mxchange/jcore/database/backend/datasource/DataSourceDatabaseBackend.java 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); } -- 2.39.5