X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Forg%2Fmxchange%2Fjcore%2FBaseFrameworkSystem.java;h=d31239b48a9f25fc822a8f44c953327834af7ba8;hb=978af0fe4301484fe7b7536f1ee06c7af32f6627;hp=7d96be82381006abe65d4df62bab3e66a226f681;hpb=08cf79beba2d1707717cd01d4cd396244841d019;p=jcore.git diff --git a/src/org/mxchange/jcore/BaseFrameworkSystem.java b/src/org/mxchange/jcore/BaseFrameworkSystem.java index 7d96be8..d31239b 100644 --- a/src/org/mxchange/jcore/BaseFrameworkSystem.java +++ b/src/org/mxchange/jcore/BaseFrameworkSystem.java @@ -38,6 +38,7 @@ import org.apache.logging.log4j.Logger; 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; @@ -47,6 +48,7 @@ import org.mxchange.jcore.manager.Manageable; * @author Roland Haeder */ public class BaseFrameworkSystem implements FrameworkInterface { + /** * Bundle instance */ @@ -72,6 +74,10 @@ public class BaseFrameworkSystem implements FrameworkInterface { */ private Application application; + /** + * Instance for database backend + */ + private DatabaseBackend backend; /** * Client instance @@ -98,7 +104,6 @@ public class BaseFrameworkSystem implements FrameworkInterface { */ private DatabaseFrontend wrapper; - /** * Initialize object */ @@ -106,6 +111,14 @@ public class BaseFrameworkSystem implements FrameworkInterface { LOG = LogManager.getLogger(this); } + /** + * No instances can be created of this class + */ + protected BaseFrameworkSystem () { + // Set own instance + this.setSelfInstance(); + } + /** * Getter for this application * @@ -116,17 +129,6 @@ public class BaseFrameworkSystem implements FrameworkInterface { return selfInstance; } - /** - * No instances can be created of this class - */ - protected BaseFrameworkSystem () { - // Init properties file - this.initProperties(); - - // Set own instance - this.setSelfInstance(); - } - /** * Application instance * @@ -175,7 +177,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { * * @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 { @@ -193,22 +195,22 @@ public class BaseFrameworkSystem implements FrameworkInterface { private Class 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 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) c.getSuperclass(); } // Trace message this.getLogger().trace(MessageFormat.format("c={0} - EXIT!", c)); //NOI18N - + // Return it return c; } @@ -224,30 +226,27 @@ public class BaseFrameworkSystem implements FrameworkInterface { 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 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; } @@ -268,10 +267,10 @@ public class BaseFrameworkSystem implements FrameworkInterface { protected final void abortProgramWithException (final Throwable throwable) { // Log exception ... this.getLogger().catching(throwable); - + // .. and exit System.exit(1); - + } /** @@ -311,6 +310,15 @@ public class BaseFrameworkSystem implements FrameworkInterface { 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 * @@ -335,49 +343,6 @@ public class BaseFrameworkSystem implements FrameworkInterface { 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 */ @@ -387,7 +352,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { // 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 @@ -527,26 +492,26 @@ public class BaseFrameworkSystem implements FrameworkInterface { 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 @@ -574,13 +539,7 @@ public class BaseFrameworkSystem implements FrameworkInterface { 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); } @@ -609,22 +568,16 @@ public class BaseFrameworkSystem implements FrameworkInterface { 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); } @@ -646,37 +599,105 @@ public class BaseFrameworkSystem implements FrameworkInterface { /** * 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)); //NOI18N + + // Is it null? + if (str == null) { + // Return empty string + return ""; //NOI18N + } + + // Trace message + this.getLogger().trace(MessageFormat.format("str={0} - EXIT!", str)); //NOI18N + + // 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> fieldIterator (final FrameworkInterface instance, final String className) { + // Trace message + this.getLogger().trace(MessageFormat.format("instance={0},className={1} - CALLED!", instance, className)); //NOI18N + + // 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)); //NOI18N + + // A simple map with K=fieldName and V=Value is fine + Map map = new HashMap<>(fields.length); + + // Walk through all + for (final Field field : fields) { + // Debug log + this.getLogger().debug(MessageFormat.format("field={0}", field.getName())); //NOI18N + + // Does the field start with "$"? + if (field.getName().startsWith("$")) { //NOI18N + // Debug message + this.getLogger().debug(MessageFormat.format("Skipping field={0} as it starts with a dollar character.", field.getName())); //NOI18N + + // Skip it silently + continue; + } + + // Debug message + this.getLogger().debug(MessageFormat.format("Calling getValueFromColumn({0}) on instance {1} ...", field.getName(), instance)); + + // Get value from it + Object value = instance.getValueFromColumn(field.getName()); + + // Debug message + this.getLogger().debug(MessageFormat.format("Adding field={0},value={1}", field.getName(), value)); //NOI18N + + // Add it to list + map.put(field, value); + } + + // Debug message + this.getLogger().debug(MessageFormat.format("Returning iterator for {0} entries ...", map.size())); //NOI18N + + // 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; } /** @@ -697,6 +718,24 @@ public class BaseFrameworkSystem implements FrameworkInterface { 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 */ @@ -704,16 +743,59 @@ public class BaseFrameworkSystem implements FrameworkInterface { // Is the bundle set? if (bundle instanceof ResourceBundle) { // Is already set - throw new IllegalStateException("called twice"); + throw new IllegalStateException("called twice"); //NOI18N } // 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 @@ -753,51 +835,4 @@ public class BaseFrameworkSystem implements FrameworkInterface { 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> 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 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(); - } }