import org.mxchange.jcore.application.Application;
import org.mxchange.jcore.client.Client;
import org.mxchange.jcore.contact.Contact;
+import org.mxchange.jcore.database.backend.DatabaseBackend;
import org.mxchange.jcore.database.frontend.DatabaseFrontend;
import org.mxchange.jcore.manager.Manageable;
* @author Roland Haeder
*/
public class BaseFrameworkSystem implements FrameworkInterface {
+
/**
* Bundle instance
*/
*/
private Application application;
+ /**
+ * Instance for database backend
+ */
+ private DatabaseBackend backend;
/**
* Client instance
*/
private DatabaseFrontend wrapper;
-
/**
* Initialize object
*/
LOG = LogManager.getLogger(this);
}
+ /**
+ * No instances can be created of this class
+ */
+ protected BaseFrameworkSystem () {
+ // Set own instance
+ this.setSelfInstance();
+ }
+
/**
* Getter for this application
*
return selfInstance;
}
- /**
- * No instances can be created of this class
- */
- protected BaseFrameworkSystem () {
- // Init properties file
- this.initProperties();
-
- // Set own instance
- this.setSelfInstance();
- }
-
/**
* Application instance
*
*
* @param columnName Column name
* @return Value from field
- * @throws IllegalArgumentException Some implementations may throw this.
+ * @throws IllegalArgumentException Some implementations may throw this.
*/
@Override
public Object getValueFromColumn (final String columnName) throws IllegalArgumentException {
private Class<? extends FrameworkInterface> getClassFromTarget (final FrameworkInterface instance, final String targetClass) {
// Trace message
this.getLogger().debug(MessageFormat.format("instance={0},targetClass={1}", instance, targetClass)); //NOI18N
-
+
// Instance reflaction of this class
Class<? extends FrameworkInterface> c = instance.getClass();
-
+
// Analyze class
while (!targetClass.equals(c.getSimpleName())) {
// Debug message
this.getLogger().debug(MessageFormat.format("c={0}", c.getSimpleName())); //NOI18N
-
+
// Get super class (causes unchecked warning)
c = (Class<? extends FrameworkInterface>) c.getSuperclass();
}
// Trace message
this.getLogger().trace(MessageFormat.format("c={0} - EXIT!", c)); //NOI18N
-
+
// Return it
return c;
}
private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName) {
// Trace messahe
this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
-
+
// Get target class instance
Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
-
+
// Init field instance
Method method = null;
-
+
// Use reflection to get all attributes
try {
method = c.getDeclaredMethod(methodName, new Class<?>[0]);
- } catch (final SecurityException ex) {
+ } catch (final SecurityException | NoSuchMethodException ex) {
// Security problem
this.abortProgramWithException(ex);
- } catch (final NoSuchMethodException ex) {
- // Method not found
- this.abortProgramWithException(ex);
}
-
+
// Assert on field
assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
-
+
// Trace message
this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N
-
+
// Return it
return method;
}
protected final void abortProgramWithException (final Throwable throwable) {
// Log exception ...
this.getLogger().catching(throwable);
-
+
// .. and exit
System.exit(1);
-
+
}
/**
this.client = client;
}
+ /**
+ * Name of used database table, handled over to backend
+ *
+ * @return the tableName
+ */
+ public final String getTableName () {
+ return this.tableName;
+ }
+
/**
* Checks if given boolean field is available and set to same value
*
this.getLogger().catching(exception);
}
- /**
- * Prepares all properties, the file is written if it is not found
- */
- private void initProperties () {
- // Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("{0} properties are loaded already.", BaseFrameworkSystem.properties.size())); //NOI18N
-
- // Are some properties loaded?
- if (!BaseFrameworkSystem.properties.isEmpty()) {
- // Some are already loaded, abort here
- return;
- }
-
- try {
- // Try to read it
- BaseFrameworkSystem.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE))));
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("{0} properties has been loaded.", BaseFrameworkSystem.properties.size())); //NOI18N
- } catch (final FileNotFoundException ex) {
- // Debug message
- this.getLogger().debug(MessageFormat.format("Properties file {0} not found: {1}", FrameworkInterface.PROPERTIES_CONFIG_FILE, ex)); //NOI18N
-
- /*
- * The file is not found which is normal for first run, so
- * initialize default values.
- */
- this.initPropertiesWithDefault();
-
- // Write file
- this.writePropertiesFile();
- } catch (final IOException ex) {
- // Something else didn't work
- this.abortProgramWithException(ex);
- }
-
- // Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
- }
-
/**
* Initializes properties with default values
*/
// Init default values:
// Default database backend
- BaseFrameworkSystem.properties.put("org.mxchange.database.backendType", "base64csv"); //NOI18N
+ BaseFrameworkSystem.properties.put("org.mxchange.database.backend.class", "org.mxchange.jcore.database.backend.base64.Base64CsvDatabaseBackend"); //NOI18N
// For MySQL backend
BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.host", "localhost"); //NOI18N
protected String[] getArrayFromString (final String str, final String delimiter, final int size) {
// Trace message
this.getLogger().trace(MessageFormat.format("str={0},delimiter={1},size={2} - CALLED!", str, delimiter, size)); //NOI18N
-
+
// Get tokenizer
StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
-
+
// Init array and index
String[] tokens = new String[size];
int index = 0;
-
+
// Run through all tokens
while (tokenizer.hasMoreTokens()) {
// Get current token and add it
tokens[index] = tokenizer.nextToken();
-
+
// Debug message
this.getLogger().debug(MessageFormat.format("Token at index{0}: {1}", index, tokens[1])); //NOI18N
-
+
// Increment index
index++;
}
-
+
// Trace message
this.getLogger().trace(MessageFormat.format("tokens({0})={1} - EXIT!", tokens.length, Arrays.toString(tokens))); //NOI18N
try {
value = (Boolean) method.invoke(instance);
- } catch (final IllegalArgumentException ex) {
- // Other problem
- this.abortProgramWithException(ex);
- } catch (final IllegalAccessException ex) {
- // Other problem
- this.abortProgramWithException(ex);
- } catch (final InvocationTargetException ex) {
+ } catch (final IllegalArgumentException | IllegalAccessException | InvocationTargetException ex) {
// Other problem
this.abortProgramWithException(ex);
}
protected Object getField (final FrameworkInterface instance, final String targetClass, final String methodName) {
// Trace messahe
this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
-
+
// Get method to call
Method method = this.getMethodFromName(instance, targetClass, methodName);
-
+
// Get value from field
Object object = null;
-
+
try {
object = method.invoke(instance);
- } catch (final IllegalArgumentException ex) {
- // Other problem
- this.abortProgramWithException(ex);
- } catch (final IllegalAccessException ex) {
- // Other problem
- this.abortProgramWithException(ex);
- } catch (final InvocationTargetException ex) {
+ } catch (final IllegalArgumentException | IllegalAccessException | InvocationTargetException ex) {
// Other problem
this.abortProgramWithException(ex);
}
/**
* Name of used database table, handled over to backend
*
- * @return the tableName
+ * @param tableName the tableName to set
*/
- protected final String getTableName () {
- return this.tableName;
+ protected final void setTableName (final String tableName) {
+ this.tableName = tableName;
}
/**
- * Name of used database table, handled over to backend
+ * Converts null to empty string or leaves original string.
*
- * @param tableName the tableName to set
+ * @param str Any string
+ * @return Empty string if null or original string
*/
- protected final void setTableName (final String tableName) {
- this.tableName = tableName;
+ protected Object convertNullToEmpty (final Object str) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("str={0}", str));
+
+ // Is it null?
+ if (str == null) {
+ // Return empty string
+ return "";
+ }
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("str={0} - EXIT!", str));
+
+ // Return it
+ return str;
}
/**
- * Getter for DatabaseFrontend instance
+ * Creates an iterator from given instance and class name.
*
- * @return DatabaseFrontend instance
+ * @param instance Instance to run getter calls on
+ * @param className Class name to iterate over
+ * @return An iterator over all object's fields
*/
- protected final DatabaseFrontend getWrapper () {
- return this.wrapper;
+ protected Iterator<Map.Entry<Field, Object>> fieldIterator (final FrameworkInterface instance, final String className) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("instance={0},className={1} - CALLED!", instance, className));
+
+ // Get all attributes from given instance
+ Field[] fields = this.getClassFromTarget(instance, className).getDeclaredFields();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("Found {0} fields.", fields.length));
+
+ // A simple map with K=fieldName and V=Value is fine
+ Map<Field, Object> map = new HashMap<>(fields.length);
+
+ // Walk through all
+ for (final Field field : fields) {
+ // Debug log
+ this.getLogger().debug(MessageFormat.format("field={0}", field.getName()));
+
+ // Does the field start with "$"?
+ if (field.getName().startsWith("$")) {
+ // Skip it silently
+ continue;
+ }
+
+ // Get value from it
+ Object value = this.getValueFromColumn(field.getName());
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("value={0}", value));
+
+ // Add it to list
+ map.put(field, value);
+ }
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("Returning iterator for {0} entries ...", map.size()));
+
+ // Return list iterator
+ return map.entrySet().iterator();
}
/**
- * Setter for wrapper instance
+ * Instance for database backend
*
- * @param wrapper A DatabaseFrontend instance
+ * @return the backend
*/
- protected final void setWrapper (final DatabaseFrontend wrapper) {
- this.wrapper = wrapper;
+ protected final DatabaseBackend getBackend () {
+ return this.backend;
+ }
+
+ /**
+ * Instance for database backend
+ *
+ * @param backend the backend to set
+ */
+ protected final void setBackend (final DatabaseBackend backend) {
+ this.backend = backend;
}
/**
this.contact = contact;
}
+ /**
+ * Getter for DatabaseFrontend instance
+ *
+ * @return DatabaseFrontend instance
+ */
+ protected final DatabaseFrontend getFrontend () {
+ return this.wrapper;
+ }
+
+ /**
+ * Setter for wrapper instance
+ *
+ * @param wrapper A DatabaseFrontend instance
+ */
+ protected final void setFrontend (final DatabaseFrontend wrapper) {
+ this.wrapper = wrapper;
+ }
+
/**
* Initializes i18n bundles
*/
// Set instance
bundle = ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE); // NOI18N
}
+
+ /**
+ * Prepares all properties, the file is written if it is not found
+ */
+ protected void initProperties () {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("{0} properties are loaded already.", BaseFrameworkSystem.properties.size())); //NOI18N
+
+ // Are some properties loaded?
+ if (!BaseFrameworkSystem.properties.isEmpty()) {
+ // Some are already loaded, abort here
+ return;
+ }
+
+ try {
+ // Try to read it
+ BaseFrameworkSystem.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE))));
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("{0} properties has been loaded.", BaseFrameworkSystem.properties.size())); //NOI18N
+ } catch (final FileNotFoundException ex) {
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("Properties file {0} not found: {1}", FrameworkInterface.PROPERTIES_CONFIG_FILE, ex)); //NOI18N
+
+ /*
+ * The file is not found which is normal for first run, so
+ * initialize default values.
+ */
+ this.initPropertiesWithDefault();
+
+ // Write file
+ this.writePropertiesFile();
+ } catch (final IOException ex) {
+ // Something else didn't work
+ this.abortProgramWithException(ex);
+ }
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
+ }
/**
* Checks whether the given field is a boolean field by probing it.
- *
+ *
* @param instance Instance to call
* @param targetClass Target class
* @param columnName Column name to check
return isBool;
}
- /**
- * Creates an iterator from given instance and class name.
- *
- * @param instance Instance to run getter calls on
- * @param className Class name to iterate over
- * @return An iterator over all object's fields
- */
- protected Iterator<Map.Entry<Field, Object>> fieldIterator (final FrameworkInterface instance, final String className) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("instance={0},className={1} - CALLED!", instance, className));
-
- // Get all attributes from given instance
- Field[] fields = this.getClassFromTarget(instance, className).getDeclaredFields();
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("Found {0} fields.", fields.length));
-
- // A simple map with K=fieldName and V=Value is fine
- Map<Field, Object> map = new HashMap<>(fields.length);
-
- // Walk through all
- for (final Field field : fields) {
- // Debug log
- this.getLogger().debug(MessageFormat.format("field={0}", field.getName()));
-
- // Does the field start with "$"?
- if (field.getName().startsWith("$")) {
- // Skip it silently
- continue;
- }
-
- // Get value from it
- Object value = this.getValueFromColumn(field.getName());
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("value={0}", value));
-
- // Add it to list
- map.put(field, value);
- }
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("Returning iterator for {0} entries ...", map.size()));
-
- // Return list iterator
- return map.entrySet().iterator();
- }
-
- /**
- * Converts null to empty string or leaves original string.
- *
- * @param str Any string
- * @return Empty string if null or original string
- */
- protected Object convertNullToEmpty (final Object str) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("str={0}", str));
-
- // Is it null?
- if (str == null) {
- // Return empty string
- return "";
- }
-
- // Trace message
- this.getLogger().trace(MessageFormat.format("str={0} - EXIT!", str));
-
- // Return it
- return str;
- }
}
--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcore.factory.database.backend;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.text.MessageFormat;
+import org.mxchange.jcore.BaseFrameworkSystem;
+import org.mxchange.jcore.database.backend.DatabaseBackend;
+import org.mxchange.jcore.database.frontend.DatabaseFrontend;
+
+/**
+ * A factory class for database backends
+ *
+ * @author Roland Haeder
+ */
+public class BackendFactory extends BaseFrameworkSystem {
+
+ /**
+ * Creates instance of a database backend as configured in properties file
+ *
+ * @param frontend Frontend instance
+ * @return An instance of a DatabaseBackend class
+ * @throws java.lang.ClassNotFoundException If the configured class was not found
+ * @throws java.lang.NoSuchMethodException If the constructor with a frontend instance is not found
+ * @throws java.lang.InstantiationException
+ * @throws java.lang.IllegalAccessException
+ * @throws java.lang.reflect.InvocationTargetException
+ */
+ public static final DatabaseBackend createInstance (final DatabaseFrontend frontend) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+ // Get factory instance
+ BackendFactory factory = new BackendFactory();
+
+ // Trace message
+ factory.getLogger().trace(MessageFormat.format("frontend={0} - CALLED!", frontend)); //NOI18N
+
+ // Get property
+ String className = factory.getProperty("database.backend.class"); //NOI18N
+
+ // Debug message
+ factory.getLogger().debug(MessageFormat.format("className={0}", className)); //NOI18N
+
+ // Try to get the instance
+ Class<?> reflection = Class.forName(className);
+
+ // Debug message
+ factory.getLogger().debug(MessageFormat.format("reflection={0}", reflection)); //NOI18N
+
+ // Get constructor
+ Constructor<?> constructor = reflection.getConstructor(frontend.getClass());
+
+ // Debug message
+ factory.getLogger().debug(MessageFormat.format("constructor={0}", constructor)); //NOI18N
+
+ // Now invoke it
+ DatabaseBackend backend = (DatabaseBackend) constructor.newInstance(frontend);
+
+ // Trace message
+ factory.getLogger().trace(MessageFormat.format("backend={0} - EXIT!", backend)); //NOI18N
+
+ // Return it
+ return backend;
+ }
+
+}