From: Roland Haeder Date: Wed, 12 Aug 2015 10:28:35 +0000 (+0200) Subject: Continued with project: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=8e7f3f38af176d01d0cff04ff98b6ba84022ef4a;p=pizzaservice-war.git Continued with project: - Moved some classes to proper location - Removed no longer glassfish-specific character encoding, because .. - Added filter for setting character encoding to UTF-8 - Added "data" directory + ignored all files in it - Rewrote initialization of properties so the context parameter from web.xml can be used to set needed properties for jcore database backend. Still this is not so satifying, maybe the method needs to be moved to jcore? Signed-off-by:Roland Häder --- diff --git a/.gitignore b/.gitignore index d73b7d0f..4bb68df3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /build/ /dist/ *.properties +/data/*.* diff --git a/src/java/org/mxchange/pizzaapplication/BasePizzaServiceSystem.java b/src/java/org/mxchange/pizzaapplication/BasePizzaServiceSystem.java index 84f1e1c5..0e0e7541 100644 --- a/src/java/org/mxchange/pizzaapplication/BasePizzaServiceSystem.java +++ b/src/java/org/mxchange/pizzaapplication/BasePizzaServiceSystem.java @@ -16,6 +16,8 @@ */ package org.mxchange.pizzaapplication; +import java.text.MessageFormat; +import javax.servlet.ServletContext; import org.mxchange.jcore.BaseFrameworkSystem; /** @@ -24,5 +26,30 @@ import org.mxchange.jcore.BaseFrameworkSystem; * @author Roland Haeder */ public class BasePizzaServiceSystem extends BaseFrameworkSystem { - + /** + * Initializes properties from given servlet configuration + * @param context Servlet context instance + */ + protected void initProperties (final ServletContext context) { + // Trace message + this.getLogger().trace(MessageFormat.format("context={0} - CALLED!", context)); + + // We need some properties that needs to be set + for (final String name : this.getPropertyNames()) { + // Debug log + this.getLogger().debug(MessageFormat.format("name={0}", name)); + + // Get value + String value = context.getInitParameter(name).trim(); + + // Debug log + this.getLogger().debug(MessageFormat.format("{0}={1}", name, value)); + + // Set property + this.setProperty(name, value); + } + + // Trace message + this.getLogger().trace("EXIT!"); + } } diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java index b3199752..9161e1ab 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java @@ -30,7 +30,8 @@ import org.mxchange.jcore.contact.Gender; import org.mxchange.pizzaapplication.BasePizzaServiceSystem; import org.mxchange.pizzaapplication.customer.Customer; import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer; -import org.mxchange.pizzaapplication.product.PizzaProduct; +import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend; +import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend; import org.mxchange.pizzaapplication.product.Product; /** @@ -47,13 +48,13 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * Some singleton getter for this instance. If the instance is not set in * given application, it will be created. * - * @param application Servlet context + * @param context Servlet context * @return This instance * @throws javax.servlet.ServletException If object is not set correctly */ - public static final PizzaApplication getInstance (final ServletContext application) throws ServletException { + public static final PizzaApplication getInstance (final ServletContext context) throws ServletException { // Check application instance - if (application == null) { + if (context == null) { // Not set throw new NullPointerException("application is null"); //NOI18N } @@ -62,7 +63,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P PizzaApplication instance = null; // Get instance from servlet application (aka. "application scope") - Object object = application.getAttribute("app"); //NOI18N + Object object = context.getAttribute("app"); //NOI18N // Is it set? if (object instanceof PizzaApplication) { @@ -73,10 +74,10 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P throw new ServletException("app is not set correctly"); //NOI18N } else { // "service" is null, so initialize it - instance = new PizzaServiceApplication(); + instance = new PizzaServiceApplication(context); // And set it here - application.setAttribute("app", instance); //NOI18N + context.setAttribute("app", instance); //NOI18N } // Trace message @@ -107,11 +108,20 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P private PizzaServiceApplication () { // Init products instance this.products = new TreeMap<>(); + } - // Init bundle - this.initBundle(); + /** + * Constructor with servet configuration + * @param context Servlet context + */ + private PizzaServiceApplication (final ServletContext context) { + // Call other constructor first + this(); - // Fill products list + // Initialize properties from config + this.initProperties(context); + + // Load available products this.fillProductsList(); } @@ -828,13 +838,14 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P /** * Adds given product to list or throws an exception if name is already found * - * @param name Internal name of product - * @param title Product's title - * @param price Price + * @param product Product instance to add */ - private void addProduct (final String name, final String title, final float price) { + private void addProduct (final Product product) { // Trace message - this.getLogger().trace(MessageFormat.format("name={0},title={1},price={2} - CALLED!", name, title, price)); //NOI18N + this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N + + // Get all data from it + String name = product.getName(); // Is the name already used? if (this.isProductNameUsed(name)) { @@ -842,9 +853,6 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P throw new IllegalArgumentException(MessageFormat.format("product {0} is already used.", name)); //NOI18N } - // Instance product - Product product = new PizzaProduct(name, title, price); - // Debug message this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N @@ -882,10 +890,20 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P // Trace message this.getLogger().trace("CALLED!"); //NOI18N - // Add products - this.addProduct("italia", "Pizza Italia", 5.50f); //NOI18N - this.addProduct("diablo", "Pizza Diablo", 7.80f); //NOI18N - this.addProduct("bolognese", "Spagetti Bolognese", 11.95f); //NOI18N + // Get a product frontend + ProductFrontend frontend = new PizzaProductDatabaseFrontend(); + + // Get iterator + Iterator iterator = frontend.getProducts(); + + // Get all products + while (iterator.hasNext()) { + // Get it + Product product = iterator.next(); + + // Add it + this.addProduct(product); + } // Trace message this.getLogger().trace("EXIT!"); //NOI18N @@ -1063,6 +1081,15 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * Application starter */ private void start () { + // Init bundle + this.initBundle(); + + // Init properties + this.initProperties(); + + // Fill products list + this.fillProductsList(); + // "Walk" over all products for (final Product product : this.getProducts()) { // Output data diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java new file mode 100644 index 00000000..f0a63a78 --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java @@ -0,0 +1,137 @@ +/* + * 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.pizzaapplication.database.frontend.product; + +import java.sql.SQLException; +import java.text.MessageFormat; +import java.util.Iterator; +import org.mxchange.jcore.criteria.searchable.SearchCriteria; +import org.mxchange.jcore.criteria.searchable.SearchableCritera; +import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend; +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.UnsupportedDatabaseBackendException; +import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants; +import org.mxchange.pizzaapplication.product.Product; + +/** + * Stores and retrieves Contact instances + * + * @author Roland Haeder + */ +public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implements ProductFrontend { + + /** + * Default constrcutor + */ + public PizzaProductDatabaseFrontend () { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Set "table" name + this.setTableName("products"); //NOI18N + + try { + // Initalize backend + this.initBackend(); + } catch (final UnsupportedDatabaseBackendException | SQLException ex) { + // Abort program + this.abortProgramWithException(ex); + } + } + + /** + * Shuts down the database layer + */ + @Override + public void doShutdown () { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Shutdown backend + this.getBackend().doShutdown(); + + // Trace message + this.getLogger().trace("EXIT!"); //NOI18N + } + + /** + * An iterator on all products + * + * @return Iterator on all products + */ + @Override + @SuppressWarnings ("unchecked") + public Iterator getProducts () { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + + // Instance search criteria + SearchableCritera critera = new SearchCriteria(); + + // Add criteria + critera.addCriteria(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, true); + + // Run the query + Result result = this.getBackend().doSelectByCriteria(critera); + + // Debug message + this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N + + // Get iterator + Iterator iterator = result.iterator(); + + // Trace message + this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N + + // Get iterator and return it + return (Iterator) iterator; + } + + /** + * Parses given line from database backend into a Storeable instance. Please + * note that not all backends need this. + * + * @param line Line from database backend + * @return A Storeable instance + */ + @Override + public Storeable parseLineToStoreable (final String line) throws BadTokenException { + // Trace message + this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N + + // Call inner method + Product product = this.parseLineToProduct(line); + + // Debug message + this.getLogger().trace(MessageFormat.format("product={0} - EXIT!", product)); //NOI18N + + // Return it + return product; + } + + /** + * Parses given line to a Product instance + * + * @param line + * @return A Product instance from given line + */ + private Product parseLineToProduct (final String line) { + throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N + } +} diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java new file mode 100644 index 00000000..b94d5502 --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2015 Roland Häder + * + * 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.pizzaapplication.database.frontend.product; + +import java.util.Iterator; +import org.mxchange.jcore.database.frontend.DatabaseFrontend; +import org.mxchange.pizzaapplication.product.Product; + +/** + * An interface for product database frontends + * + * @author Roland Häder + */ +public interface ProductFrontend extends DatabaseFrontend { + + /** + * An iterator on all products + * + * @return Iterator on all products + */ + public Iterator getProducts (); +} diff --git a/src/java/org/mxchange/pizzaapplication/database/product/PizzaProductDatabaseConstants.java b/src/java/org/mxchange/pizzaapplication/database/product/PizzaProductDatabaseConstants.java new file mode 100644 index 00000000..16d08d70 --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/database/product/PizzaProductDatabaseConstants.java @@ -0,0 +1,34 @@ +/* + * 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.pizzaapplication.database.product; + +/** + * + * @author Roland Haeder + */ +public final class PizzaProductDatabaseConstants { + /** + * Column name for "available" + */ + public static final String COLUMN_AVAILABLE = "available"; + + /** + * No instance from this class + */ + private PizzaProductDatabaseConstants () { + } +} diff --git a/src/java/org/mxchange/pizzaapplication/filter/http/BaseServletFilter.java b/src/java/org/mxchange/pizzaapplication/filter/http/BaseServletFilter.java new file mode 100644 index 00000000..0e40f85f --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/filter/http/BaseServletFilter.java @@ -0,0 +1,67 @@ +/* + * 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.pizzaapplication.filter.http; + +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import org.mxchange.jcore.BaseFrameworkSystem; + +/** + * A general HTTP servlet filter class + * @author Roland Haeder + */ +public class BaseServletFilter extends BaseFrameworkSystem { + /** + * Configuration instance + */ + private FilterConfig config; + + /** + * Configuration instance + * @return the config + */ + protected FilterConfig getConfig () { + return this.config; + } + + /** + * Configuration instance + * @param config the config to set + */ + protected void setConfig (final FilterConfig config) { + this.config = config; + } + + /** + * Destroys this filter + */ + public void destroy () { + // Unset config instance + this.setConfig(null); + } + + /** + * Initializes this filter + * + * @param filterConfig Filter configuration + * @throws ServletException + */ + public void init (final FilterConfig filterConfig) throws ServletException { + // Set config instance + this.setConfig(filterConfig); + } +} diff --git a/src/java/org/mxchange/pizzaapplication/filter/http/utf8/Utf8HttpFilter.java b/src/java/org/mxchange/pizzaapplication/filter/http/utf8/Utf8HttpFilter.java new file mode 100644 index 00000000..51585e3a --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/filter/http/utf8/Utf8HttpFilter.java @@ -0,0 +1,51 @@ +/* + * 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.pizzaapplication.filter.http.utf8; + +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import org.mxchange.pizzaapplication.filter.http.BaseServletFilter; + +/** + * A HTTP filter for setting UTF-8 character encoding. + * + * @author Roland Haeder + */ +public class Utf8HttpFilter extends BaseServletFilter implements Filter { + /** + * Filter to set UTF-8 encoding + * + * @param request ServletRequest instance + * @param response ServletResponse instance + * @param chain FilterChain instance + * @throws IOException + * @throws ServletException + */ + @Override + public void doFilter (final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { + // Call super method + chain.doFilter(request, response); + + // Set response/request both to UTF-8 + request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding("UTF-8"); + } +} diff --git a/src/java/org/mxchange/pizzaapplication/product/Product.java b/src/java/org/mxchange/pizzaapplication/product/Product.java index a81b9163..bdeea227 100644 --- a/src/java/org/mxchange/pizzaapplication/product/Product.java +++ b/src/java/org/mxchange/pizzaapplication/product/Product.java @@ -16,13 +16,13 @@ */ package org.mxchange.pizzaapplication.product; -import org.mxchange.jcore.FrameworkInterface; +import org.mxchange.jcore.database.storage.Storeable; /** * * @author Roland Haeder */ -public interface Product extends FrameworkInterface { +public interface Product extends Storeable { /** * Getter for name, suitable for form fields. * diff --git a/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/PizzaProductDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/PizzaProductDatabaseFrontend.java deleted file mode 100644 index 97b19cf0..00000000 --- a/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/PizzaProductDatabaseFrontend.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * 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.pizzaapplicationk.database.frontend.contact; - -import java.sql.SQLException; -import java.text.MessageFormat; -import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend; -import org.mxchange.jcore.database.storage.Storeable; -import org.mxchange.jcore.exceptions.BadTokenException; -import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; -import org.mxchange.pizzaapplication.product.Product; - -/** - * Stores and retrieves Contact instances - * - * @author Roland Haeder - */ -public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implements ProductFrontend { - - /** - * Basic constrcutor - */ - protected PizzaProductDatabaseFrontend () { - // Trace message - this.getLogger().trace("CALLED!"); //NOI18N - - // Set "table" name - this.setTableName("contacts"); //NOI18N - - try { - // Initalize backend - this.initBackend(); - } catch (final UnsupportedDatabaseBackendException | SQLException ex) { - // Abort program - this.abortProgramWithException(ex); - } - } - - /** - * Shuts down the database layer - */ - @Override - public void doShutdown () { - // Trace message - this.getLogger().trace("CALLED!"); //NOI18N - - // Shutdown backend - this.getBackend().doShutdown(); - - // Trace message - this.getLogger().trace("EXIT!"); //NOI18N - } - - /** - * Parses given line from database backend into a Storeable instance. Please - * note that not all backends need this. - * - * @param line Line from database backend - * @return A Storeable instance - */ - @Override - public Storeable parseLineToStoreable (final String line) throws BadTokenException { - // Trace message - this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N - - // Call inner method - Product product = this.parseLineToProduct(line); - - // Debug message - this.getLogger().debug(MessageFormat.format("product={0}", product)); - - // Return it - return (Storeable) product; - } - - /** - * Parses given line to a Product instance - * @param line - * @return A Product instance from given line - */ - private Product parseLineToProduct (final String line) { - throw new UnsupportedOperationException("Not supported yet."); - } -} diff --git a/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/ProductFrontend.java b/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/ProductFrontend.java deleted file mode 100644 index a04499ec..00000000 --- a/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/ProductFrontend.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (C) 2015 Roland Häder - * - * 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.pizzaapplicationk.database.frontend.contact; - -import org.mxchange.jcore.database.frontend.DatabaseFrontend; - -/** - * An interface for product database frontends - * - * @author Roland Häder - */ -public interface ProductFrontend extends DatabaseFrontend { -} diff --git a/web/WEB-INF/glassfish-web.xml b/web/WEB-INF/glassfish-web.xml index 0c186a9a..13e0059f 100644 --- a/web/WEB-INF/glassfish-web.xml +++ b/web/WEB-INF/glassfish-web.xml @@ -7,5 +7,4 @@ Keep a copy of the generated servlet class' java code. - diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml index 764689af..2676cd9a 100644 --- a/web/WEB-INF/web.xml +++ b/web/WEB-INF/web.xml @@ -1,5 +1,39 @@ + + Full-qualified back class name, must implement DatabaseBackend interface + database.backend.class + org.mxchange.jcore.database.backend.base64.Base64CsvDatabaseBackend + + + Login name for MySQL database, mostly not root + database.mysql.login + root + + + Password for above login, an empty password can be archived by setting a space + database.mysql.password + root + + + Hostname or IP address for MySQL server + database.mysql.host + localhost + + + Name of MySQL catalog + database.mysql.dbname + test + + + A filter for setting character encoding to UTF-8 + Utf8HttpFilter + org.mxchange.pizzaapplication.filter.http.utf8.Utf8HttpFilter + + + Utf8HttpFilter + /* + 30 @@ -8,4 +42,8 @@ index.jsp + + Exception + /errorHandler.jsp +