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;
19 import java.io.BufferedReader;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.io.PrintWriter;
25 import java.lang.reflect.Field;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.text.MessageFormat;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.Iterator;
33 import java.util.Properties;
34 import java.util.ResourceBundle;
35 import java.util.StringTokenizer;
36 import org.apache.commons.lang3.ArrayUtils;
37 import org.apache.logging.log4j.LogManager;
38 import org.apache.logging.log4j.Logger;
39 import org.mxchange.jcore.application.Application;
40 import org.mxchange.jcore.client.Client;
41 import org.mxchange.jcore.contact.Contact;
42 import org.mxchange.jcore.database.backend.DatabaseBackend;
43 import org.mxchange.jcore.database.frontend.DatabaseFrontend;
44 import org.mxchange.jcore.database.storage.Storeable;
45 import org.mxchange.jcore.manager.Manageable;
50 * @author Roland Haeder
52 public class BaseFrameworkSystem implements FrameworkInterface {
57 private static ResourceBundle bundle;
60 * Instance for own properties
62 private static final Properties properties = new Properties(System.getProperties());
67 private static FrameworkInterface selfInstance;
72 private final Logger LOG;
75 * Application instance
77 private Application application;
80 * Instance for database backend
82 private DatabaseBackend backend;
87 private Client client;
92 private Contact contact;
97 private Manageable manager;
100 * Name of used database table, handled over to backend
102 private String tableName;
105 * DatabaseFrontend instance
107 private DatabaseFrontend frontend;
110 * Session id assigned with this basket, or any other unique identifier
112 private String sessionId;
118 LOG = LogManager.getLogger(this);
122 * No instances can be created of this class
124 protected BaseFrameworkSystem () {
126 this.setSelfInstance();
130 * Getter for this application
132 * @return Instance from this application
134 public static final FrameworkInterface getInstance () {
140 * Application instance
142 * @return the application
145 public final Application getApplication () {
146 return this.application;
155 public final Logger getLogger () {
162 * @return the contactManager
165 public final Manageable getManager () {
170 * Getter for human-readable string from given key
172 * @param key Key to return
173 * @return Human-readable message
176 public final String getMessageStringFromKey (final String key) {
178 return this.getBundle().getString(key);
182 * Some "getter" for target class instance from given name.
184 * @param instance Instance to iterate on
185 * @param targetClass Class name to look for
186 * @return Class instance
188 @SuppressWarnings("unchecked")
189 private Class<? extends FrameworkInterface> getClassFromTarget (final FrameworkInterface instance, final String targetClass) {
191 this.getLogger().debug(MessageFormat.format("instance={0},targetClass={1}", instance, targetClass)); //NOI18N
193 // Instance reflaction of this class
194 Class<? extends FrameworkInterface> c = instance.getClass();
197 while (!targetClass.equals(c.getSimpleName())) {
199 this.getLogger().debug(MessageFormat.format("c={0}", c.getSimpleName())); //NOI18N
201 // Get super class (causes unchecked warning)
202 c = (Class<? extends FrameworkInterface>) c.getSuperclass();
206 this.getLogger().trace(MessageFormat.format("c={0} - EXIT!", c)); //NOI18N
213 * Some "getter" for a Method instance from given method name
215 * @param instance Actual instance to call
216 * @param targetClass Target class name
217 * @param methodName Method name
218 * @return A Method instance
220 private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException {
222 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
225 // Init method instance
226 Method method = null;
228 // Try it from target class
230 // Get target class instance
231 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
233 // Init field instance
234 method = c.getDeclaredMethod(methodName, new Class<?>[0]);
235 } catch (final NoSuchMethodException e) {
237 this.getLogger().warn(e);
239 // So try it from super class
240 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, "BaseFrameworkSystem"); //NOI18N
242 // Init field instance
243 method = c.getDeclaredMethod(methodName, new Class<?>[0]);
247 assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
250 this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N
257 * Some "getter" for a Method instance from given method name
259 * @param instance Actual instance to call
260 * @param targetClass Target class name
261 * @param methodName Method name
262 * @param type Type reflection to check type from
263 * @return A Method instance
265 private Method getMethodFromName (final FrameworkInterface instance, final String targetClass, final String methodName, final Class<?> type) throws NoSuchMethodException {
267 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1},type={2}", targetClass, methodName, type)); //NOI18N
269 // Init method instance
270 Method method = null;
272 // Try it from target class
274 // Get target class instance
275 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
277 // Init field instance
278 method = c.getDeclaredMethod(methodName, type);
279 } catch (final NoSuchMethodException e) {
281 this.getLogger().warn(e);
283 // So try it from super class
284 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, "BaseFrameworkSystem"); //NOI18N
286 // Init field instance
287 method = c.getDeclaredMethod(methodName, type);
291 assert (method instanceof Method) : "method is not a Method instance"; //NOI18N
294 this.getLogger().trace(MessageFormat.format("method={0} - EXIT!", method)); //NOI18N
301 * Setter for self instance
303 private void setSelfInstance () {
304 // Need to set it here
309 * Aborts program with given exception
311 * @param throwable Any type of Throwable
313 protected final void abortProgramWithException (final Throwable throwable) {
315 this.logException(throwable);
322 * Application instance
324 * @param application the application to set
326 protected final void setApplication (final Application application) {
327 this.application = application;
336 public final Client getClient () {
341 * Getter for bundle instance
343 * @return Resource bundle
345 protected final ResourceBundle getBundle () {
346 return BaseFrameworkSystem.bundle;
350 * Setter for bundle instance
352 * @param bundle the bundle to set
354 protected static void setBundle (final ResourceBundle bundle) {
355 BaseFrameworkSystem.bundle = bundle;
361 * @param client the client to set
363 protected final void setClient (final Client client) {
364 this.client = client;
368 * Name of used database table, handled over to backend
370 * @return the tableName
372 public final String getTableName () {
373 return this.tableName;
377 * Checks if given boolean field is available and set to same value
379 * @param columnName Column name to check
380 * @param bool Boolean value
381 * @return Whether all conditions are met
382 * @throws NoSuchMethodException May be thrown by some implementations
383 * @throws java.lang.IllegalAccessException If the method cannot be accessed
384 * @throws java.lang.reflect.InvocationTargetException Any other problems?
387 public boolean isFieldValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
389 throw new UnsupportedOperationException(MessageFormat.format("Not implemented. columnName={0},bool={1}", columnName, bool)); //NOI18N
395 * @param exception Exception to log
398 public final void logException (final Throwable exception) {
399 // Log this exception
400 this.getLogger().catching(exception);
404 * Initializes properties with default values
406 private void initPropertiesWithDefault () {
408 this.getLogger().trace("CALLED!"); //NOI18N
410 // Init default values:
411 // Default database backend
412 BaseFrameworkSystem.properties.put("org.mxchange.database.backend.class", "org.mxchange.jcore.database.backend.base64.Base64CsvDatabaseBackend"); //NOI18N
413 BaseFrameworkSystem.properties.put("org.mxchange.database.backend.storagepath", "data/"); //NOI18N
414 BaseFrameworkSystem.properties.put("org.mxchange.database.datasource.name", ""); //NOI18N
417 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.host", "localhost"); //NOI18N
418 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.dbname", "test"); //NOI18N
419 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.login", ""); //NOI18N
420 BaseFrameworkSystem.properties.put("org.mxchange.database.mysql.password", ""); //NOI18N
423 this.getLogger().trace("EXIT!"); //NOI18N
427 * Writes the properties file to disk
429 private void writePropertiesFile () throws IOException {
431 this.getLogger().trace("CALLED!"); //NOI18N
434 BaseFrameworkSystem.properties.store(new PrintWriter(FrameworkInterface.PROPERTIES_CONFIG_FILE), "This file is automatically generated. You may wish to alter it."); //NOI18N
437 this.getLogger().trace("EXIT!"); //NOI18N
441 * Converts a column name like "foo_bar" to an attribute name like "fooBar"
443 * @param columnName Column name to convert
444 * @return Attribute name
446 protected String convertColumnNameToFieldName (final String columnName) {
448 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
450 // First all lower case
451 String lower = columnName.toLowerCase();
454 StringTokenizer tokenizer = new StringTokenizer(lower, "_"); //NOI18N
457 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
463 while (tokenizer.hasMoreTokens()) {
465 String token = tokenizer.nextToken();
467 // Is later than first element?
469 // Make first character upper-case
470 char c = token.charAt(0);
471 token = String.valueOf(c).toUpperCase() + token.substring(1);
475 builder.append(token);
482 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
485 return builder.toString();
489 * Converts a column name like "foo_bar" to a method name like "getFooBar"
490 * for non-booleans and to "isFooBar" for boolean fields.
492 * @param columnName Column name to convert
493 * @param isBool Whether the parameter is boolean
494 * @return Attribute name
496 protected String convertColumnNameToGetterMethod (final String columnName, boolean isBool) {
498 this.getLogger().trace(MessageFormat.format("columnName={0},isBool={1} - CALLED!", columnName, isBool)); //NOI18N
501 StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
504 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
509 builder.append("is"); //NOI18N
512 builder.append("get"); //NOI18N
516 while (tokenizer.hasMoreTokens()) {
518 String token = tokenizer.nextToken();
521 this.getLogger().debug(MessageFormat.format("token={0}", token)); //NOI18N
523 // Make it upper-case
524 char c = token.charAt(0);
525 token = String.valueOf(c).toUpperCase() + token.substring(1);
528 builder.append(token);
532 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
535 return builder.toString();
539 * Converts a column name like "foo_bar" to a method name like "getFooBar"
540 * for non-booleans and to "isFooBar" for boolean fields.
542 * @param columnName Column name to convert
543 * @return Attribute name
545 protected String convertColumnNameToSetterMethod (final String columnName) {
547 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
550 StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
553 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
556 builder.append("set"); //NOI18N
559 while (tokenizer.hasMoreTokens()) {
561 String token = tokenizer.nextToken();
564 this.getLogger().debug(MessageFormat.format("token={0} - BEFORE", token)); //NOI18N
566 // Make it upper-case
567 char c = token.charAt(0);
568 token = String.valueOf(c).toUpperCase() + token.substring(1);
571 this.getLogger().debug(MessageFormat.format("token={0} - AFTER", token)); //NOI18N
574 builder.append(token);
578 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
581 return builder.toString();
585 * Some "getter" for an array from given string and tokenizer
587 * @param str String to tokenize and get array from
588 * @param delimiter Delimiter
589 * @param size Size of array
590 * @return Array from tokenized string
591 * @todo Get rid of size parameter
593 protected String[] getArrayFromString (final String str, final String delimiter, final int size) {
595 this.getLogger().trace(MessageFormat.format("str={0},delimiter={1},size={2} - CALLED!", str, delimiter, size)); //NOI18N
598 StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
600 // Init array and index
601 String[] tokens = new String[size];
604 // Run through all tokens
605 while (tokenizer.hasMoreTokens()) {
606 // Get current token and add it
607 tokens[index] = tokenizer.nextToken();
610 this.getLogger().debug(MessageFormat.format("Token at index{0}: {1}", index, tokens[1])); //NOI18N
617 this.getLogger().trace(MessageFormat.format("tokens({0})={1} - EXIT!", tokens.length, Arrays.toString(tokens))); //NOI18N
624 * Returns boolean field value from given method name by invoking it
626 * @param instance The instance to call
627 * @param targetClass Target class to look in
628 * @param methodName Method name to look for
629 * @return Boolean value from field
630 * @throws java.lang.NoSuchMethodException If the method was not found
631 * @throws java.lang.IllegalAccessException If the method cannot be accessed
632 * @throws java.lang.reflect.InvocationTargetException Some other problems?
634 protected boolean getBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
636 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
638 // Get method instance
639 Method method = this.getMethodFromName(instance, targetClass, methodName);
641 // Get value from field
642 Boolean value = (Boolean) method.invoke(instance);
645 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
652 * Sets boolean field value with given method name by invoking it
654 * @param instance The instance to call
655 * @param targetClass Target class to look in
656 * @param methodName Method name to look for
657 * @param columnName Column name
658 * @param value Boolean value from field
659 * @throws java.lang.NoSuchMethodException If the method was not found
660 * @throws java.lang.IllegalAccessException If the method cannot be accessed
661 * @throws java.lang.reflect.InvocationTargetException Some other problems?
663 protected void setBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName, final String columnName, final Boolean value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
665 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
668 Class<?> type = this.getType(instance, targetClass, columnName);
670 // Get method instance
671 Method method = this.getMethodFromName(instance, targetClass, methodName, type);
673 // Try to get the value by invoking the method
674 method.invoke(instance, value);
677 this.getLogger().trace("EXIT!"); //NOI18N
683 * @param manager the manager instance to set
685 protected final void setContactManager (final Manageable manager) {
686 this.manager = manager;
690 * Returns any field value from given method name by invoking it
692 * @param instance The instance to call
693 * @param targetClass Target class to look in
694 * @param methodName Method name to look for
695 * @return Any value from field
696 * @throws java.lang.NoSuchMethodException If the method was not found
697 * @throws java.lang.IllegalAccessException If the method cannot be accessed
698 * @throws java.lang.reflect.InvocationTargetException Some other problems?
700 protected Object getField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
702 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},methodName={2}", instance, targetClass, methodName)); //NOI18N
704 // Get method to call
705 Method method = this.getMethodFromName(instance, targetClass, methodName);
708 this.getLogger().debug(MessageFormat.format("method={0},instance={1}", method, instance)); //NOI18N
710 // Get value from field
711 Object value = method.invoke(instance);
714 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
721 * Sets any field value with given method name by invoking it
723 * @param instance The instance to call
724 * @param targetClass Target class to look in
725 * @param methodName Method name to look for
726 * @param columnName Column name
727 * @param value Any value from field
728 * @throws java.lang.NoSuchMethodException If the method was not found
729 * @throws java.lang.IllegalAccessException If the method cannot be accessed
730 * @throws java.lang.reflect.InvocationTargetException Some other problems?
732 protected void setField (final FrameworkInterface instance, final String targetClass, final String methodName, final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
734 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},methodName={2},value={3}", instance, targetClass, methodName, value)); //NOI18N
737 Class<?> type = this.getType(instance, targetClass, columnName);
740 this.getLogger().debug(MessageFormat.format("type={0}", type)); //NOI18N
743 Object object = value;
745 // Is the value null?
746 if ("null".equals(value)) { //NOI18N
748 this.getLogger().warn(MessageFormat.format("columnName={0} has null value.", columnName)); //NOI18N
753 // Hard-coded "cast" again ... :-(
754 // @TODO Can't we get rid of this???
755 switch (type.getSimpleName()) {
756 case "Long": // Long object //NOI18N
757 object = Long.parseLong((String) value);
760 case "Float": // Float object //NOI18N
761 object = Float.parseFloat((String) value);
764 case "Boolean": // Boolean object //NOI18N
765 object = Boolean.parseBoolean((String) value);
768 case "String": // String object //NOI18N
771 default: // Unsupported type
772 throw new IllegalArgumentException(MessageFormat.format("value {0} has unsupported type {1}", value, type.getSimpleName())); //NOI18N
777 this.getLogger().debug(MessageFormat.format("object={0}", object)); //NOI18N
779 // Get method to call
780 Method method = this.getMethodFromName(instance, targetClass, methodName, type);
783 this.getLogger().debug(MessageFormat.format("method={0},instance={1},value[{2}]={3}", method, instance, value.getClass().getSimpleName(), value)); //NOI18N
785 // Get value from field
786 method.invoke(instance, object);
789 this.getLogger().trace("EXIT!"); //NOI18N
793 * Getter for property which must exist
795 * @param key Key to get
796 * @return Propety value
798 protected final String getProperty (final String key) {
799 return BaseFrameworkSystem.properties.getProperty(String.format("org.mxchange.%s", key)); //NOI18N
803 * Name of used database table, handled over to backend
805 * @param tableName the tableName to set
807 protected final void setTableName (final String tableName) {
808 this.tableName = tableName;
812 * Converts null to empty string or leaves original string.
814 * @param str Any string
815 * @return Empty string if null or original string
817 protected Object convertNullToEmpty (final Object str) {
819 this.getLogger().trace(MessageFormat.format("str={0}", str)); //NOI18N
823 // Return empty string
828 this.getLogger().trace(MessageFormat.format("str={0} - EXIT!", str)); //NOI18N
835 * Creates an iterator from given instance and class name.
837 * @param instance Instance to run getter calls on
838 * @param className Class name to iterate over
839 * @return An iterator over all object's fields
840 * @throws java.lang.NoSuchMethodException If the called method does not exist
841 * @throws java.lang.IllegalAccessException If the method cannot be accessed
842 * @throws java.lang.reflect.InvocationTargetException Any other problems?
844 protected Iterator<Map.Entry<Field, Object>> fieldIterator (final Storeable instance, final String className) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
846 this.getLogger().trace(MessageFormat.format("instance={0},className={1} - CALLED!", instance, className)); //NOI18N
848 // Get all attributes from given instance
849 Field[] fields = this.getClassFromTarget(instance, className).getDeclaredFields();
852 this.getLogger().debug(MessageFormat.format("Found {0} fields.", fields.length)); //NOI18N
854 // A simple map with K=fieldName and V=Value is fine
855 Map<Field, Object> map = new HashMap<>(fields.length);
858 for (final Field field : fields) {
860 this.getLogger().debug(MessageFormat.format("field={0}", field.getName())); //NOI18N
862 // Does the field start with "$"?
863 if (field.getName().startsWith("$")) { //NOI18N
865 this.getLogger().debug(MessageFormat.format("Skipping field={0} as it starts with a dollar character.", field.getName())); //NOI18N
872 this.getLogger().debug(MessageFormat.format("Calling getValueFromColumn({0}) on instance {1} ...", field.getName(), instance)); //NOI18N
875 Object value = instance.getValueFromColumn(field.getName());
878 this.getLogger().debug(MessageFormat.format("Adding field={0},value={1}", field.getName(), value)); //NOI18N
881 map.put(field, value);
885 this.getLogger().debug(MessageFormat.format("Returning iterator for {0} entries ...", map.size())); //NOI18N
887 // Return list iterator
888 return map.entrySet().iterator();
892 * Instance for database backend
894 * @return the backend
896 protected final DatabaseBackend getBackend () {
901 * Instance for database backend
903 * @param backend the backend to set
905 protected final void setBackend (final DatabaseBackend backend) {
906 this.backend = backend;
910 * Getter for Contact instance
912 * @return Contact instance
914 protected final Contact getContact () {
919 * Setter for Contact instance
921 * @param contact A Contact instance
923 protected final void setContact (final Contact contact) {
924 this.contact = contact;
928 * Getter for DatabaseFrontend instance
930 * @return DatabaseFrontend instance
932 protected final DatabaseFrontend getFrontend () {
933 return this.frontend;
937 * Setter for wrapper instance
939 * @param frontend A DatabaseFrontend instance
941 protected final void setFrontend (final DatabaseFrontend frontend) {
942 this.frontend = frontend;
946 * Setter for session id
948 * @param sessionId Session id
951 public final void setSessionId (final String sessionId) {
952 this.sessionId = sessionId;
956 * Getter for session id
961 public final String getSessionId () {
962 return this.sessionId;
966 * Checks if the bundle is initialized
968 * @return Whether the bundle has been initialized
970 protected boolean isBundledInitialized () {
972 return (bundle instanceof ResourceBundle);
976 * Initializes i18n bundles
978 protected void initBundle () {
980 this.getLogger().trace("CALLED!"); //NOI18N
982 // Is the bundle set?
983 if (this.isBundledInitialized()) {
985 throw new IllegalStateException("called twice"); //NOI18N
989 setBundle(ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE)); // NOI18N
992 this.getLogger().trace("EXIT!"); //NOI18N
996 * Prepares all properties, the file is written if it is not found
998 * @throws java.io.IOException If any IO problem occurs
1000 protected void initProperties () throws IOException {
1002 this.getLogger().trace("CALLED!"); //NOI18N
1005 this.getLogger().debug(MessageFormat.format("{0} properties are loaded already.", BaseFrameworkSystem.properties.size())); //NOI18N
1007 // Are some properties loaded?
1008 if (!BaseFrameworkSystem.properties.isEmpty()) {
1009 // Some are already loaded, abort here
1015 BaseFrameworkSystem.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE))));
1018 this.getLogger().debug(MessageFormat.format("{0} properties has been loaded.", BaseFrameworkSystem.properties.size())); //NOI18N
1019 } catch (final FileNotFoundException ex) {
1021 this.getLogger().debug(MessageFormat.format("Properties file {0} not found: {1}", FrameworkInterface.PROPERTIES_CONFIG_FILE, ex)); //NOI18N
1024 * The file is not found which is normal for first run, so
1025 * initialize default values.
1027 this.initPropertiesWithDefault();
1030 this.writePropertiesFile();
1034 this.getLogger().trace("EXIT!"); //NOI18N
1038 * Checks whether the given field is a boolean field by probing it.
1040 * @param instance Instance to call
1041 * @param targetClass Target class
1042 * @param columnName Column name to check
1043 * @return Whether the given column name represents a boolean field
1045 protected boolean isBooleanField (final FrameworkInterface instance, final String targetClass, final String columnName) {
1047 this.getLogger().trace(MessageFormat.format("instance={0},targetCLass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); //NOI18N
1049 // Convert column name to getter name (boolean)
1050 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
1052 // Get class instance
1053 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
1055 // Defauzlt is boolean
1056 boolean isBool = true;
1059 // Now try to instance the method
1060 Method method = c.getDeclaredMethod(methodName, new Class<?>[0]);
1061 } catch (final NoSuchMethodException ex) {
1063 this.getLogger().debug(MessageFormat.format("Method {0} does not exist, field {1} cannot be boolean: {2}", methodName, columnName, ex)); //NOI18N
1070 this.getLogger().trace(MessageFormat.format("isBool={0} - EXIT!", isBool)); //NOI18N
1077 * Sets value in properties instance.
1079 * @param key Property key (part) to set
1080 * @param value Property value to set
1082 protected void setProperty (final String key, final String value) {
1084 this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value)); //NOI18N
1086 // Both should not be null
1089 throw new NullPointerException("key is null"); //NOI18N
1090 } else if (null == value) {
1092 throw new NullPointerException("value is null"); //NOI18N
1096 properties.setProperty(String.format("org.mxchange.%s", key), value); //NOI18N
1099 this.getLogger().trace("EXIT!"); //NOI18N
1103 * Some getter for properties names (without org.mxchange)
1105 * @return An array with property names
1107 protected String[] getPropertyNames () {
1110 "database.backend.class", //NOI18N
1111 "database.backend.storagepath", //NOI18N
1112 "database.mysql.login", //NOI18N
1113 "database.mysql.host", //NOI18N
1114 "database.mysql.password", //NOI18N
1115 "database.mysql.dbname", //NOI18N
1123 * Some "setter" for a value in given Storeable instance and target class
1125 * @param instance An instance of a Storeable class
1126 * @param targetClass The target class (where the field resides)
1127 * @param columnName Column name (= field name)
1128 * @param value Value to set
1129 * @throws java.lang.NoSuchMethodException If the setter is not found
1130 * @throws java.lang.IllegalAccessException If the setter cannot be accessed
1131 * @throws java.lang.reflect.InvocationTargetException Any other problem?
1133 protected void setValueInStoreableFromColumn (final Storeable instance, final String targetClass, final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1135 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2},value={3} - CALLED!", instance, targetClass, columnName, value)); //NOI18N
1137 // A '$' means not our field
1138 if (columnName.startsWith("$")) { //NOI18N
1139 // Don't handle these
1140 throw new IllegalArgumentException("columnsName contains $"); //NOI18N
1143 // Determine if the given column is boolean
1144 if (this.isBooleanField(instance, targetClass, columnName)) {
1146 this.getLogger().debug(MessageFormat.format("Column {0} represents a boolean field.", columnName)); //NOI18N
1148 // Yes, then call other method
1149 this.setBooleanField(instance, targetClass, this.convertColumnNameToSetterMethod(columnName), columnName, (Boolean) value);
1152 // Convert column name to field name
1153 String methodName = this.convertColumnNameToSetterMethod(columnName);
1156 this.getLogger().debug(MessageFormat.format("methodName={0}", methodName)); //NOI18N
1159 this.setField(instance, targetClass, methodName, columnName, value);
1162 this.getLogger().trace("EXIT!"); //NOI18N
1166 * Some "getter" for a value from given Storeable instance and target class
1168 * @param instance An instance of a Storeable class
1169 * @param targetClass The target class (where the field resides)
1170 * @param columnName Column name (= field name)
1171 * @return value Value to get
1172 * @throws java.lang.NoSuchMethodException If the getter was not found
1173 * @throws java.lang.IllegalAccessException If the getter cannot be accessed
1174 * @throws java.lang.reflect.InvocationTargetException Some other problems?
1176 protected Object getValueInStoreableFromColumn (final Storeable instance, final String targetClass, final String columnName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1178 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); //NOI18N
1180 // A '$' means not our field
1181 if (columnName.startsWith("$")) { //NOI18N
1182 // Don't handle these
1183 throw new IllegalArgumentException("columnsName contains $"); //NOI18N
1186 // Determine if the given column is boolean
1187 if (this.isBooleanField(instance, targetClass, columnName)) {
1189 this.getLogger().debug(MessageFormat.format("Column {0} represents a boolean field.", columnName)); //NOI18N
1191 // Yes, then call other method
1192 return this.getBooleanField(instance, targetClass, this.convertColumnNameToGetterMethod(columnName, true));
1195 // Convert column name to field name
1196 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
1199 this.getLogger().debug(MessageFormat.format("methodName={0}", methodName)); //NOI18N
1202 Object value = this.getField(instance, targetClass, methodName);
1205 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
1212 * Some getter for type reflection of given column name
1214 * @param instance The instance to check
1215 * @param targetClass Target class to check
1216 * @param columnName Column name
1217 * @return Type reflection of value
1219 private Class<?> getType (final FrameworkInterface instance, final String targetClass, final String columnName) {
1221 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); //NOI18N
1224 Class<?> type = null;
1226 // Get super class fields
1227 Field[] superFields = this.getClassFromTarget(instance, "BaseFrameworkSystem").getDeclaredFields(); //NOI18N
1229 // Get all attributes from given instance
1230 Field[] fields = ArrayUtils.addAll(superFields, this.getClassFromTarget(instance, targetClass).getDeclaredFields());
1233 this.getLogger().debug(MessageFormat.format("fields()={0}", fields.length)); //NOI18N
1235 // Convert column_name to fieldName ;-)
1236 String fieldName = this.convertColumnNameToFieldName(columnName);
1239 this.getLogger().debug(MessageFormat.format("fieldName={0}", fieldName)); //NOI18N
1241 // Search for proper field instance
1242 for (final Field field : fields) {
1243 // Is a dollar character there?
1244 if (field.getName().startsWith("$")) {
1246 this.getLogger().debug("Field name " + field.getName() + " starts with a dollar, skipped.");
1252 this.getLogger().debug(MessageFormat.format("field.getName()={0},fieldName={1}", field.getName(), fieldName)); //NOI18N
1255 if (field.getName().equals(fieldName)) {
1257 type = field.getType();
1260 this.getLogger().debug(MessageFormat.format("Found fieldName={0}: setting type={1}", fieldName, type.getSimpleName())); //NOI18N
1262 // Don't continue with searching
1268 this.getLogger().debug(MessageFormat.format("type={0}", type)); //NOI18N
1270 // type should not be null
1273 throw new NullPointerException("type is null"); //NOI18N
1277 this.getLogger().debug(MessageFormat.format("type={0} - EXIT!", type)); //NOI18N