*/
package org.mxchange.jcore;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.text.MessageFormat;
-import java.util.Arrays;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
-import org.apache.logging.log4j.LogManager;
-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.manager.Manageable;
+import org.mxchange.jcore.model.contact.Contact;
/**
* General class
- *
- * @author Roland Haeder
+ * <p>
+ * @author Roland Haeder<roland@mxchange.org>
*/
-public class BaseFrameworkSystem implements FrameworkInterface {
+public abstract class BaseFrameworkSystem implements FrameworkInterface {
/**
* Bundle instance
*/
private static FrameworkInterface selfInstance;
- /**
- * Class' logger
- */
- private final Logger LOG;
-
/**
* Application instance
*/
*/
private Manageable manager;
- /**
- * Name of used database table, handled over to backend
- */
- private String tableName;
-
/**
* Initialize object
*/
{
- // Init logger
- this.LOG = LogManager.getLogger(this);
-
// Need to set it here
selfInstance = this;
}
/**
* Getter for this application
- *
+ * <p>
* @return Instance from this application
*/
- public static final FrameworkInterface getInstance () {
+ public static FrameworkInterface getInstance () {
// Return it
return selfInstance;
}
@Override
- public final Application getApplication () {
+ public Application getApplication () {
return this.application;
}
@Override
- public final Logger getLogger () {
- return this.LOG;
- }
-
- @Override
- public final Manageable getManager () {
- return this.manager;
- }
-
- @Override
- public final String getMessageStringFromKey (final String key) {
- // Return message
- return this.getBundle().getString(key);
- }
-
- /**
- * Some "getter" for target class instance from given name.
- *
- * @param instance Instance to iterate on
- * @param targetClass Class name to look for
- * @return Class instance
- */
- @SuppressWarnings ("unchecked")
- 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;
- }
-
- /**
- * Some "getter" for a Method instance from given method name
- *
- * @param instance Actual instance to call
- * @param targetClass Target class name
- * @param methodName Method name
- * @return A Method instance
- */
- private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException {
- // Trace messahe
- this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
-
- // Init method instance
- Method method = null;
-
- // Try it from target class
- try {
- // Get target class instance
- Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
-
- // Init field instance
- method = c.getDeclaredMethod(methodName, new Class<?>[0]);
- } catch (final NoSuchMethodException e) {
- // Didn't found it
- this.getLogger().debug(e);
-
- // So try it from super class
- Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, "BaseFrameworkSystem"); //NOI18N
-
- // Init field instance
- method = c.getDeclaredMethod(methodName, new Class<?>[0]);
- }
-
- // 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;
- }
-
- /**
- * Some "getter" for a Method instance from given method name
- *
- * @param instance Actual instance to call
- * @param targetClass Target class name
- * @param methodName Method name
- * @param type Type reflection to check type from
- * @return A Method instance
- */
- private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName, final Class<?> type) throws NoSuchMethodException {
- // Trace messahe
- this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1},type={2}", targetClass, methodName, type)); //NOI18N
-
- // Init method instance
- Method method = null;
-
- // Try it from target class
- try {
- // Get target class instance
- Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
-
- // Init field instance
- method = c.getDeclaredMethod(methodName, type);
- } catch (final NoSuchMethodException e) {
- // Didn't found it
- this.getLogger().debug(e);
-
- // So try it from super class
- Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, "BaseFrameworkSystem"); //NOI18N
-
- // Init field instance
- method = c.getDeclaredMethod(methodName, type);
- }
-
- // 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;
- }
-
- /**
- * Aborts program with given exception
- *
- * @param throwable Any type of Throwable
- */
- protected final void abortProgramWithException (final Throwable throwable) {
- // Log exception ...
- this.logException(throwable);
-
- // .. and exit
- System.exit(1);
- }
-
- /**
- * Application instance
- *
- * @param application the application to set
- */
- protected final void setApplication (final Application application) {
- this.application = application;
- }
-
- @Override
- public final Client getClient () {
+ public Client getClient () {
return this.client;
}
/**
- * Getter for bundle instance
- *
- * @return Resource bundle
- */
- protected final ResourceBundle getBundle () {
- return BaseFrameworkSystem.bundle;
- }
-
- /**
- * Setter for bundle instance
- *
- * @param bundle the bundle to set
- */
- protected static void setBundle (final ResourceBundle bundle) {
- BaseFrameworkSystem.bundle = bundle;
- }
-
- /**
- * Client instance
- *
- * @param client the client to set
- */
- protected final void setClient (final Client client) {
- this.client = client;
- }
-
- /**
- * Name of used database table, handled over to backend
- *
- * @return the tableName
- */
- public final String getTableName () {
- return this.tableName;
- }
-
- @Override
- public boolean isFieldValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
- // Not implemented
- throw new UnsupportedOperationException(MessageFormat.format("Not implemented. columnName={0},bool={1}", columnName, bool)); //NOI18N
- }
-
- @Override
- public final void logException (final Throwable exception) {
- // Log this exception
- this.getLogger().catching(exception);
- }
-
- /**
- * Converts a column name like "foo_bar" to an attribute name like "fooBar"
- *
- * @param columnName Column name to convert
- * @return Attribute name
+ * Converts null to empty string or leaves original object untouched.
+ * <p>
+ * @param object Any string
+ * @return Empty string if null or original string TODO: Move to own utility
+ * class
*/
- protected String convertColumnNameToFieldName (final String columnName) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
-
- // Split on "_"
- StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
-
- // Resulting string
- StringBuilder builder = new StringBuilder(tokenizer.countTokens());
-
- // Init counter
- int count = 0;
-
- // Walk through all
- while (tokenizer.hasMoreTokens()) {
- // Get token
- String token = tokenizer.nextToken();
-
- // Is later than first element?
- if (count > 0) {
- // Make first character upper-case
- char c = token.charAt(0);
- token = String.valueOf(c).toUpperCase() + token.substring(1);
- }
-
- // Add token
- builder.append(token);
-
- // Increment counter
- count++;
- }
-
- // Trace message
- this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
-
- // Return result
- return builder.toString();
- }
-
- /**
- * Converts a column name like "foo_bar" to a method name like "getFooBar"
- * for non-booleans and to "isFooBar" for boolean fields.
- *
- * @param columnName Column name to convert
- * @param isBool Whether the parameter is boolean
- * @return Attribute name
- */
- protected String convertColumnNameToGetterMethod (final String columnName, boolean isBool) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("columnName={0},isBool={1} - CALLED!", columnName, isBool)); //NOI18N
-
- // Then split on "_"
- StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
-
- // Resulting string
- StringBuilder builder = new StringBuilder(tokenizer.countTokens());
-
- // Is it boolean?
- if (isBool) {
- // Append "is"
- builder.append("is"); //NOI18N
- } else {
- // Append "get"
- builder.append("get"); //NOI18N
- }
-
- // Walk through all
- while (tokenizer.hasMoreTokens()) {
- // Get token
- String token = tokenizer.nextToken();
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("token={0}", token)); //NOI18N
-
- // Make it upper-case
- char c = token.charAt(0);
- token = String.valueOf(c).toUpperCase() + token.substring(1);
-
- // Add token
- builder.append(token);
- }
-
- // Trace message
- this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
-
- // Return result
- return builder.toString();
- }
-
- /**
- * Converts a column name like "foo_bar" to a method name like "getFooBar"
- * for non-booleans and to "isFooBar" for boolean fields.
- *
- * @param columnName Column name to convert
- * @return Attribute name
- */
- protected String convertColumnNameToSetterMethod (final String columnName) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
-
- // Then split on "_"
- StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
-
- // Resulting string
- StringBuilder builder = new StringBuilder(tokenizer.countTokens());
-
- // Append "set"
- builder.append("set"); //NOI18N
-
- // Walk through all
- while (tokenizer.hasMoreTokens()) {
- // Get token
- String token = tokenizer.nextToken();
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("token={0} - BEFORE", token)); //NOI18N
-
- // Make it upper-case
- char c = token.charAt(0);
- token = String.valueOf(c).toUpperCase() + token.substring(1);
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("token={0} - AFTER", token)); //NOI18N
-
- // Add token
- builder.append(token);
+ protected Object convertNullToEmpty (final Object object) {
+ // Is it null?
+ if (null == object) {
+ // Return empty string
+ return ""; //NOI18N
}
- // Trace message
- this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
-
- // Return result
- return builder.toString();
+ // Return it
+ return object;
}
/**
* Some "getter" for an array from given string and tokenizer
- *
+ * <p>
* @param str String to tokenize and get array from
* @param delimiter Delimiter
- * @return Array from tokenized string TODO Get rid of size parameter
+ * @return Array from tokenized string TODO Get rid of size parameter TODO:
+ * Move to own utility class
*/
protected String[] getArrayFromString (final String str, final String delimiter) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("str={0},delimiter={1} - CALLED!", str, delimiter)); //NOI18N
-
// Get tokenizer
StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
// 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
-
// Return it
return tokens;
}
/**
- * Returns boolean field value from given method name by invoking it
- *
- * @param instance The instance to call
- * @param targetClass Target class to look in
- * @param methodName Method name to look for
- * @return Boolean value from field
- * @throws java.lang.NoSuchMethodException If the method was not found
- * @throws java.lang.IllegalAccessException If the method cannot be accessed
- * @throws java.lang.reflect.InvocationTargetException Some other problems?
+ * Client instance
+ * <p>
+ * @param client the client to set
*/
- protected boolean getBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
- // Trace messahe
- this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
-
- // Get method instance
- Method method = this.getMethodFromName(instance, targetClass, methodName);
-
- // Get value from field
- Boolean value = (Boolean) method.invoke(instance);
-
- // Trace message
- this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
-
- // Return value
- return value;
+ protected void setClient (final Client client) {
+ this.client = client;
}
/**
- * Manager instance
- *
- * @param manager the manager instance to set
+ * Application instance
+ * <p>
+ * @param application the application to set
*/
- protected final void setContactManager (final Manageable manager) {
- this.manager = manager;
+ protected void setApplication (final Application application) {
+ this.application = application;
+ }
+
+ @Override
+ public Manageable getManager () {
+ return this.manager;
}
/**
- * Returns any field value from given method name by invoking it
- *
- * @param instance The instance to call
- * @param targetClass Target class to look in
- * @param methodName Method name to look for
- * @return Any value from field
- * @throws java.lang.NoSuchMethodException If the method was not found
- * @throws java.lang.IllegalAccessException If the method cannot be accessed
- * @throws java.lang.reflect.InvocationTargetException Some other problems?
+ * Getter for Contact instance
+ * <p>
+ * @return Contact instance
*/
- protected Object getField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
- // Trace messahe
- this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},methodName={2}", instance, targetClass, methodName)); //NOI18N
-
- // Get method to call
- Method method = this.getMethodFromName(instance, targetClass, methodName);
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("method={0},instance={1}", method, instance)); //NOI18N
-
- // Get value from field
- Object value = method.invoke(instance);
-
- // Trace messahe
- this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
-
- // Return value
- return value;
+ protected Contact getContact () {
+ return this.contact;
}
/**
- * Name of used database table, handled over to backend
- *
- * @param tableName the tableName to set
+ * Setter for Contact instance
+ * <p>
+ * @param contact A Contact instance
*/
- protected final void setTableName (final String tableName) {
- this.tableName = tableName;
+ protected void setContact (final Contact contact) {
+ this.contact = contact;
}
/**
- * Converts null to empty string or leaves original string.
- *
- * @param str Any string
- * @return Empty string if null or original string
+ * Manager instance
+ * <p>
+ * @param manager the manager instance to set
*/
- protected Object convertNullToEmpty (final Object str) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("str={0}", str)); //NOI18N
-
- // Is it null?
- if (null == str) {
- // Return empty string
- return ""; //NOI18N
- }
-
- // Trace message
- this.getLogger().trace(MessageFormat.format("str={0} - EXIT!", str)); //NOI18N
+ protected void setManager (final Manageable manager) {
+ this.manager = manager;
+ }
- // Return it
- return str;
+ @Override
+ public String getMessageStringFromKey (final String key) {
+ // Return message
+ return this.getBundle().getString(key);
}
/**
- * Getter for Contact instance
- *
- * @return Contact instance
+ * Getter for bundle instance
+ * <p>
+ * @return Resource bundle
*/
- protected final Contact getContact () {
- return this.contact;
+ protected ResourceBundle getBundle () {
+ return BaseFrameworkSystem.bundle;
}
/**
- * Setter for Contact instance
- *
- * @param contact A Contact instance
+ * Setter for bundle instance
+ * <p>
+ * @param bundle the bundle to set
*/
- protected final void setContact (final Contact contact) {
- this.contact = contact;
+ protected static void setBundle (final ResourceBundle bundle) {
+ BaseFrameworkSystem.bundle = bundle;
}
/**
* Initializes i18n bundles
*/
protected void initBundle () {
- // Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
-
// Is the bundle set?
- if (isBundledInitialized()) {
+ if (BaseFrameworkSystem.isBundledInitialized()) {
// Is already set
throw new IllegalStateException("called twice"); //NOI18N
}
// Set instance
setBundle(ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE)); // NOI18N
-
- // Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
}
/**
* Checks if the bundle is initialized
- *
+ * <p>
* @return Whether the bundle has been initialized
*/
protected static boolean isBundledInitialized () {