2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jcore.factory.database.backend;
19 import java.lang.reflect.Constructor;
20 import java.lang.reflect.InvocationTargetException;
21 import java.text.MessageFormat;
22 import org.mxchange.jcore.BaseFrameworkSystem;
23 import org.mxchange.jcore.database.backend.DatabaseBackend;
24 import org.mxchange.jcore.database.frontend.DatabaseFrontend;
27 * A factory class for database backends
29 * @author Roland Haeder
31 public class BackendFactory extends BaseFrameworkSystem {
34 * Creates instance of a database backend as configured in properties file
36 * @param frontend Frontend instance
37 * @return An instance of a DatabaseBackend class
38 * @throws java.lang.ClassNotFoundException If the configured class was not found
39 * @throws java.lang.NoSuchMethodException If the constructor with a frontend instance is not found
40 * @throws java.lang.InstantiationException
41 * @throws java.lang.IllegalAccessException
42 * @throws java.lang.reflect.InvocationTargetException
44 public static final DatabaseBackend createInstance (final DatabaseFrontend frontend) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
45 // Get factory instance
46 BackendFactory factory = new BackendFactory();
49 factory.getLogger().trace(MessageFormat.format("frontend={0} - CALLED!", frontend)); //NOI18N
51 // frontend must be set
52 if (frontend == null) {
54 throw new NullPointerException("frontend is null");
58 String className = factory.getProperty("database.backend.class"); //NOI18N
61 factory.getLogger().debug(MessageFormat.format("className={0}", className)); //NOI18N
64 if (className == null) {
66 throw new NullPointerException("className is null"); //NOI18N
69 // Try to get the instance
70 Class<?> reflection = Class.forName(className);
73 factory.getLogger().debug(MessageFormat.format("reflection={0}", reflection)); //NOI18N
76 Constructor<?> constructor = reflection.getConstructor(DatabaseFrontend.class);
79 factory.getLogger().debug(MessageFormat.format("constructor={0}", constructor)); //NOI18N
82 DatabaseBackend backend = (DatabaseBackend) constructor.newInstance(frontend);
85 factory.getLogger().trace(MessageFormat.format("backend={0} - EXIT!", backend)); //NOI18N