]> git.mxchange.org Git - jcore.git/blob - src/org/mxchange/jcore/factory/database/backend/BackendFactory.java
Added getStoreableAtRow() which is a more generalized approach than e.g. readSingleCo...
[jcore.git] / src / org / mxchange / jcore / factory / database / backend / BackendFactory.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jcore.factory.database.backend;
18
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;
25
26 /**
27  * A factory class for database backends
28  *
29  * @author Roland Haeder
30  */
31 public class BackendFactory extends BaseFrameworkSystem {
32
33         /**
34          * Creates instance of a database backend as configured in properties file
35          *
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
43          */
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();
47
48                 // Trace message
49                 factory.getLogger().trace(MessageFormat.format("frontend={0} - CALLED!", frontend)); //NOI18N
50
51                 // frontend must be set
52                 if (frontend == null) {
53                         // Is null
54                         throw new NullPointerException("frontend is null");
55                 }
56
57                 // Get property
58                 String className = factory.getProperty("database.backend.class"); //NOI18N
59
60                 // Debug message
61                 factory.getLogger().debug(MessageFormat.format("className={0}", className)); //NOI18N
62
63                 // Is it null?
64                 if (className == null) {
65                         // Not valid
66                         throw new NullPointerException("className is null"); //NOI18N
67                 }
68
69                 // Try to get the instance
70                 Class<?> reflection = Class.forName(className);
71
72                 // Debug message
73                 factory.getLogger().debug(MessageFormat.format("reflection={0}", reflection)); //NOI18N
74
75                 // Get constructor
76                 Constructor<?> constructor = reflection.getConstructor(DatabaseFrontend.class);
77
78                 // Debug message
79                 factory.getLogger().debug(MessageFormat.format("constructor={0}", constructor)); //NOI18N
80
81                 // Now invoke it
82                 DatabaseBackend backend = (DatabaseBackend) constructor.newInstance(frontend);
83
84                 // Trace message
85                 factory.getLogger().trace(MessageFormat.format("backend={0} - EXIT!", backend)); //NOI18N
86
87                 // Return it
88                 return backend;
89         }
90         
91 }