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.Storable;
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
451 StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
454 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
460 while (tokenizer.hasMoreTokens()) {
462 String token = tokenizer.nextToken();
464 // Is later than first element?
466 // Make first character upper-case
467 char c = token.charAt(0);
468 token = String.valueOf(c).toUpperCase() + token.substring(1);
472 builder.append(token);
479 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
482 return builder.toString();
486 * Converts a column name like "foo_bar" to a method name like "getFooBar"
487 * for non-booleans and to "isFooBar" for boolean fields.
489 * @param columnName Column name to convert
490 * @param isBool Whether the parameter is boolean
491 * @return Attribute name
493 protected String convertColumnNameToGetterMethod (final String columnName, boolean isBool) {
495 this.getLogger().trace(MessageFormat.format("columnName={0},isBool={1} - CALLED!", columnName, isBool)); //NOI18N
498 StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
501 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
506 builder.append("is"); //NOI18N
509 builder.append("get"); //NOI18N
513 while (tokenizer.hasMoreTokens()) {
515 String token = tokenizer.nextToken();
518 this.getLogger().debug(MessageFormat.format("token={0}", token)); //NOI18N
520 // Make it upper-case
521 char c = token.charAt(0);
522 token = String.valueOf(c).toUpperCase() + token.substring(1);
525 builder.append(token);
529 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
532 return builder.toString();
536 * Converts a column name like "foo_bar" to a method name like "getFooBar"
537 * for non-booleans and to "isFooBar" for boolean fields.
539 * @param columnName Column name to convert
540 * @return Attribute name
542 protected String convertColumnNameToSetterMethod (final String columnName) {
544 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
547 StringTokenizer tokenizer = new StringTokenizer(columnName, "_"); //NOI18N
550 StringBuilder builder = new StringBuilder(tokenizer.countTokens());
553 builder.append("set"); //NOI18N
556 while (tokenizer.hasMoreTokens()) {
558 String token = tokenizer.nextToken();
561 this.getLogger().debug(MessageFormat.format("token={0} - BEFORE", token)); //NOI18N
563 // Make it upper-case
564 char c = token.charAt(0);
565 token = String.valueOf(c).toUpperCase() + token.substring(1);
568 this.getLogger().debug(MessageFormat.format("token={0} - AFTER", token)); //NOI18N
571 builder.append(token);
575 this.getLogger().trace(MessageFormat.format("builder={0} - EXIT!", builder)); //NOI18N
578 return builder.toString();
582 * Some "getter" for an array from given string and tokenizer
584 * @param str String to tokenize and get array from
585 * @param delimiter Delimiter
586 * @return Array from tokenized string
587 * TODO Get rid of size parameter
589 protected String[] getArrayFromString (final String str, final String delimiter) {
591 this.getLogger().trace(MessageFormat.format("str={0},delimiter={1} - CALLED!", str, delimiter)); //NOI18N
594 StringTokenizer tokenizer = new StringTokenizer(str, delimiter);
596 // Init array and index
597 String[] tokens = new String[tokenizer.countTokens()];
600 // Run through all tokens
601 while (tokenizer.hasMoreTokens()) {
602 // Get current token and add it
603 tokens[index] = tokenizer.nextToken();
606 this.getLogger().debug(MessageFormat.format("Token at index{0}: {1}", index, tokens[1])); //NOI18N
613 this.getLogger().trace(MessageFormat.format("tokens({0})={1} - EXIT!", tokens.length, Arrays.toString(tokens))); //NOI18N
620 * Returns boolean field value from given method name by invoking it
622 * @param instance The instance to call
623 * @param targetClass Target class to look in
624 * @param methodName Method name to look for
625 * @return Boolean value from field
626 * @throws java.lang.NoSuchMethodException If the method was not found
627 * @throws java.lang.IllegalAccessException If the method cannot be accessed
628 * @throws java.lang.reflect.InvocationTargetException Some other problems?
630 protected boolean getBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
632 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
634 // Get method instance
635 Method method = this.getMethodFromName(instance, targetClass, methodName);
637 // Get value from field
638 Boolean value = (Boolean) method.invoke(instance);
641 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
648 * Sets boolean field value with given method name by invoking it
650 * @param instance The instance to call
651 * @param targetClass Target class to look in
652 * @param methodName Method name to look for
653 * @param columnName Column name
654 * @param value Boolean value from field
655 * @throws java.lang.NoSuchMethodException If the method was not found
656 * @throws java.lang.IllegalAccessException If the method cannot be accessed
657 * @throws java.lang.reflect.InvocationTargetException Some other problems?
659 protected void setBooleanField (final FrameworkInterface instance, final String targetClass, final String methodName, final String columnName, final Boolean value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
661 this.getLogger().trace(MessageFormat.format("targetClass={0},methodName={1}", targetClass, methodName)); //NOI18N
664 Class<?> type = this.getType(instance, targetClass, columnName);
666 // Get method instance
667 Method method = this.getMethodFromName(instance, targetClass, methodName, type);
669 // Try to get the value by invoking the method
670 method.invoke(instance, value);
673 this.getLogger().trace("EXIT!"); //NOI18N
679 * @param manager the manager instance to set
681 protected final void setContactManager (final Manageable manager) {
682 this.manager = manager;
686 * Returns any field value from given method name by invoking it
688 * @param instance The instance to call
689 * @param targetClass Target class to look in
690 * @param methodName Method name to look for
691 * @return Any value from field
692 * @throws java.lang.NoSuchMethodException If the method was not found
693 * @throws java.lang.IllegalAccessException If the method cannot be accessed
694 * @throws java.lang.reflect.InvocationTargetException Some other problems?
696 protected Object getField (final FrameworkInterface instance, final String targetClass, final String methodName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
698 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},methodName={2}", instance, targetClass, methodName)); //NOI18N
700 // Get method to call
701 Method method = this.getMethodFromName(instance, targetClass, methodName);
704 this.getLogger().debug(MessageFormat.format("method={0},instance={1}", method, instance)); //NOI18N
706 // Get value from field
707 Object value = method.invoke(instance);
710 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
717 * Sets any field value with given method name by invoking it
719 * @param instance The instance to call
720 * @param targetClass Target class to look in
721 * @param methodName Method name to look for
722 * @param columnName Column name
723 * @param value Any value from field
724 * @throws java.lang.NoSuchMethodException If the method was not found
725 * @throws java.lang.IllegalAccessException If the method cannot be accessed
726 * @throws java.lang.reflect.InvocationTargetException Some other problems?
728 protected void setField (final FrameworkInterface instance, final String targetClass, final String methodName, final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
730 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},methodName={2},value={3}", instance, targetClass, methodName, value)); //NOI18N
733 Class<?> type = this.getType(instance, targetClass, columnName);
736 this.getLogger().debug(MessageFormat.format("type={0}", type)); //NOI18N
739 Object object = value;
741 // Is the value null?
742 if ("null".equals(value)) { //NOI18N
744 this.getLogger().warn(MessageFormat.format("columnName={0} has null value.", columnName)); //NOI18N
749 // Hard-coded "cast" again ... :-(
750 // TODO Can't we get rid of this???
751 switch (type.getSimpleName()) {
752 case "Long": // Long object //NOI18N
753 object = Long.parseLong((String) value);
756 case "Float": // Float object //NOI18N
757 object = Float.parseFloat((String) value);
760 case "Boolean": // Boolean object //NOI18N
761 object = Boolean.parseBoolean((String) value);
764 case "String": // String object //NOI18N
767 default: // Unsupported type
768 throw new IllegalArgumentException(MessageFormat.format("value {0} has unsupported type {1}", value, type.getSimpleName())); //NOI18N
773 this.getLogger().debug(MessageFormat.format("object={0}", object)); //NOI18N
775 // Get method to call
776 Method method = this.getMethodFromName(instance, targetClass, methodName, type);
779 this.getLogger().debug(MessageFormat.format("method={0},instance={1},value[{2}]={3}", method, instance, value.getClass().getSimpleName(), value)); //NOI18N
781 // Get value from field
782 method.invoke(instance, object);
785 this.getLogger().trace("EXIT!"); //NOI18N
789 * Getter for property which must exist
791 * @param key Key to get
792 * @return Propety value
794 protected final String getProperty (final String key) {
795 return BaseFrameworkSystem.properties.getProperty(String.format("org.mxchange.%s", key)); //NOI18N
799 * Name of used database table, handled over to backend
801 * @param tableName the tableName to set
803 protected final void setTableName (final String tableName) {
804 this.tableName = tableName;
808 * Converts null to empty string or leaves original string.
810 * @param str Any string
811 * @return Empty string if null or original string
813 protected Object convertNullToEmpty (final Object str) {
815 this.getLogger().trace(MessageFormat.format("str={0}", str)); //NOI18N
819 // Return empty string
824 this.getLogger().trace(MessageFormat.format("str={0} - EXIT!", str)); //NOI18N
831 * Creates an iterator from given instance and class name.
833 * @param instance Instance to run getter calls on
834 * @param className Class name to iterate over
835 * @return An iterator over all object's fields
836 * @throws java.lang.NoSuchMethodException If the called method does not exist
837 * @throws java.lang.IllegalAccessException If the method cannot be accessed
838 * @throws java.lang.reflect.InvocationTargetException Any other problems?
840 protected Iterator<Map.Entry<Field, Object>> fieldIterator (final Storable instance, final String className) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
842 this.getLogger().trace(MessageFormat.format("instance={0},className={1} - CALLED!", instance, className)); //NOI18N
844 // Get all attributes from given instance
845 Field[] fields = this.getClassFromTarget(instance, className).getDeclaredFields();
848 this.getLogger().debug(MessageFormat.format("Found {0} fields.", fields.length)); //NOI18N
850 // A simple map with K=fieldName and V=Value is fine
851 Map<Field, Object> map = new HashMap<>(fields.length);
854 for (final Field field : fields) {
856 this.getLogger().debug(MessageFormat.format("field={0}", field.getName())); //NOI18N
858 // Does the field start with "$"?
859 if (field.getName().startsWith("$")) { //NOI18N
861 this.getLogger().debug(MessageFormat.format("Skipping field={0} as it starts with a dollar character.", field.getName())); //NOI18N
868 this.getLogger().debug(MessageFormat.format("Calling getValueFromColumn({0}) on instance {1} ...", field.getName(), instance)); //NOI18N
871 Object value = instance.getValueFromColumn(field.getName());
874 this.getLogger().debug(MessageFormat.format("Adding field={0},value={1}", field.getName(), value)); //NOI18N
877 map.put(field, value);
881 this.getLogger().debug(MessageFormat.format("Returning iterator for {0} entries ...", map.size())); //NOI18N
883 // Return list iterator
884 return map.entrySet().iterator();
888 * Instance for database backend
890 * @return the backend
892 protected final DatabaseBackend getBackend () {
897 * Instance for database backend
899 * @param backend the backend to set
901 protected final void setBackend (final DatabaseBackend backend) {
902 this.backend = backend;
906 * Getter for Contact instance
908 * @return Contact instance
910 protected final Contact getContact () {
915 * Setter for Contact instance
917 * @param contact A Contact instance
919 protected final void setContact (final Contact contact) {
920 this.contact = contact;
924 * Getter for DatabaseFrontend instance
926 * @return DatabaseFrontend instance
928 protected final DatabaseFrontend getFrontend () {
929 return this.frontend;
933 * Setter for wrapper instance
935 * @param frontend A DatabaseFrontend instance
937 protected final void setFrontend (final DatabaseFrontend frontend) {
938 this.frontend = frontend;
942 * Setter for session id
944 * @param sessionId Session id
947 public final void setSessionId (final String sessionId) {
948 this.sessionId = sessionId;
952 * Getter for session id
957 public final String getSessionId () {
958 return this.sessionId;
962 * Checks if the bundle is initialized
964 * @return Whether the bundle has been initialized
966 protected boolean isBundledInitialized () {
968 return (bundle instanceof ResourceBundle);
972 * Initializes i18n bundles
974 protected void initBundle () {
976 this.getLogger().trace("CALLED!"); //NOI18N
978 // Is the bundle set?
979 if (this.isBundledInitialized()) {
981 throw new IllegalStateException("called twice"); //NOI18N
985 setBundle(ResourceBundle.getBundle(FrameworkInterface.I18N_BUNDLE_FILE)); // NOI18N
988 this.getLogger().trace("EXIT!"); //NOI18N
992 * Prepares all properties, the file is written if it is not found
994 * @throws java.io.IOException If any IO problem occurs
996 protected void initProperties () throws IOException {
998 this.getLogger().trace("CALLED!"); //NOI18N
1001 this.getLogger().debug(MessageFormat.format("{0} properties are loaded already.", BaseFrameworkSystem.properties.size())); //NOI18N
1003 // Are some properties loaded?
1004 if (!BaseFrameworkSystem.properties.isEmpty()) {
1005 // Some are already loaded, abort here
1011 BaseFrameworkSystem.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE))));
1014 this.getLogger().debug(MessageFormat.format("{0} properties has been loaded.", BaseFrameworkSystem.properties.size())); //NOI18N
1015 } catch (final FileNotFoundException ex) {
1017 this.getLogger().debug(MessageFormat.format("Properties file {0} not found: {1}", FrameworkInterface.PROPERTIES_CONFIG_FILE, ex)); //NOI18N
1020 * The file is not found which is normal for first run, so
1021 * initialize default values.
1023 this.initPropertiesWithDefault();
1026 this.writePropertiesFile();
1030 this.getLogger().trace("EXIT!"); //NOI18N
1034 * Checks whether the given field is a boolean field by probing it.
1036 * @param instance Instance to call
1037 * @param targetClass Target class
1038 * @param columnName Column name to check
1039 * @return Whether the given column name represents a boolean field
1041 protected boolean isBooleanField (final FrameworkInterface instance, final String targetClass, final String columnName) {
1043 this.getLogger().trace(MessageFormat.format("instance={0},targetCLass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); //NOI18N
1045 // Convert column name to getter name (boolean)
1046 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
1048 // Get class instance
1049 Class<? extends FrameworkInterface> c = this.getClassFromTarget(instance, targetClass);
1051 // Defauzlt is boolean
1052 boolean isBool = true;
1055 // Now try to instance the method
1056 Method method = c.getDeclaredMethod(methodName, new Class<?>[0]);
1057 } catch (final NoSuchMethodException ex) {
1059 this.getLogger().debug(MessageFormat.format("Method {0} does not exist, field {1} cannot be boolean: {2}", methodName, columnName, ex)); //NOI18N
1066 this.getLogger().trace(MessageFormat.format("isBool={0} - EXIT!", isBool)); //NOI18N
1073 * Sets value in properties instance.
1075 * @param key Property key (part) to set
1076 * @param value Property value to set
1078 protected void setProperty (final String key, final String value) {
1080 this.getLogger().trace(MessageFormat.format("key={0},value={1} - CALLED!", key, value)); //NOI18N
1082 // Both should not be null
1085 throw new NullPointerException("key is null"); //NOI18N
1086 } else if (null == value) {
1088 throw new NullPointerException("value is null"); //NOI18N
1092 properties.setProperty(String.format("org.mxchange.%s", key), value); //NOI18N
1095 this.getLogger().trace("EXIT!"); //NOI18N
1099 * Some getter for properties names (without org.mxchange)
1101 * @return An array with property names
1103 protected String[] getPropertyNames () {
1106 "database.backend.class", //NOI18N
1107 "database.backend.storagepath", //NOI18N
1108 "database.mysql.login", //NOI18N
1109 "database.mysql.host", //NOI18N
1110 "database.mysql.password", //NOI18N
1111 "database.mysql.dbname", //NOI18N
1119 * Some "setter" for a value in given Storable instance and target class
1121 * @param instance An instance of a Storable class
1122 * @param targetClass The target class (where the field resides)
1123 * @param columnName Column name (= field name)
1124 * @param value Value to set
1125 * @throws java.lang.NoSuchMethodException If the setter is not found
1126 * @throws java.lang.IllegalAccessException If the setter cannot be accessed
1127 * @throws java.lang.reflect.InvocationTargetException Any other problem?
1129 protected void setValueInStorableFromColumn (final Storable instance, final String targetClass, final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1131 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2},value={3} - CALLED!", instance, targetClass, columnName, value)); //NOI18N
1133 // A '$' means not our field
1134 if (columnName.startsWith("$")) { //NOI18N
1135 // Don't handle these
1136 throw new IllegalArgumentException("columnsName contains $"); //NOI18N
1139 // Determine if the given column is boolean
1140 if (this.isBooleanField(instance, targetClass, columnName)) {
1142 this.getLogger().debug(MessageFormat.format("Column {0} represents a boolean field.", columnName)); //NOI18N
1144 // Yes, then call other method
1145 this.setBooleanField(instance, targetClass, this.convertColumnNameToSetterMethod(columnName), columnName, (Boolean) value);
1148 // Convert column name to field name
1149 String methodName = this.convertColumnNameToSetterMethod(columnName);
1152 this.getLogger().debug(MessageFormat.format("methodName={0}", methodName)); //NOI18N
1155 this.setField(instance, targetClass, methodName, columnName, value);
1158 this.getLogger().trace("EXIT!"); //NOI18N
1162 * Some "getter" for a value from given Storable instance and target class
1164 * @param instance An instance of a Storable class
1165 * @param targetClass The target class (where the field resides)
1166 * @param columnName Column name (= field name)
1167 * @return value Value to get
1168 * @throws java.lang.NoSuchMethodException If the getter was not found
1169 * @throws java.lang.IllegalAccessException If the getter cannot be accessed
1170 * @throws java.lang.reflect.InvocationTargetException Some other problems?
1172 protected Object getValueInStorableFromColumn (final Storable instance, final String targetClass, final String columnName) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1174 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); //NOI18N
1176 // A '$' means not our field
1177 if (columnName.startsWith("$")) { //NOI18N
1178 // Don't handle these
1179 throw new IllegalArgumentException("columnsName contains $"); //NOI18N
1182 // Determine if the given column is boolean
1183 if (this.isBooleanField(instance, targetClass, columnName)) {
1185 this.getLogger().debug(MessageFormat.format("Column {0} represents a boolean field.", columnName)); //NOI18N
1187 // Yes, then call other method
1188 return this.getBooleanField(instance, targetClass, this.convertColumnNameToGetterMethod(columnName, true));
1191 // Convert column name to field name
1192 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
1195 this.getLogger().debug(MessageFormat.format("methodName={0}", methodName)); //NOI18N
1198 Object value = this.getField(instance, targetClass, methodName);
1201 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
1208 * Some getter for type reflection of given column name
1210 * @param instance The instance to check
1211 * @param targetClass Target class to check
1212 * @param columnName Column name
1213 * @return Type reflection of value
1215 private Class<?> getType (final FrameworkInterface instance, final String targetClass, final String columnName) {
1217 this.getLogger().trace(MessageFormat.format("instance={0},targetClass={1},columnName={2} - CALLED!", instance, targetClass, columnName)); //NOI18N
1220 Class<?> type = null;
1222 // Get super class fields
1223 Field[] superFields = this.getClassFromTarget(instance, "BaseFrameworkSystem").getDeclaredFields(); //NOI18N
1225 // Get all attributes from given instance
1226 Field[] fields = ArrayUtils.addAll(superFields, this.getClassFromTarget(instance, targetClass).getDeclaredFields());
1229 this.getLogger().debug(MessageFormat.format("fields()={0}", fields.length)); //NOI18N
1231 // Convert column_name to fieldName ;-)
1232 String fieldName = this.convertColumnNameToFieldName(columnName);
1235 this.getLogger().debug(MessageFormat.format("fieldName={0}", fieldName)); //NOI18N
1237 // Search for proper field instance
1238 for (final Field field : fields) {
1239 // Is a dollar character there?
1240 if (field.getName().startsWith("$")) { //NOI18N
1242 this.getLogger().debug(MessageFormat.format("Field name {0} starts with a dollar, skipped.", field.getName())); //NOI18N
1248 this.getLogger().debug(MessageFormat.format("field.getName()={0},fieldName={1}", field.getName(), fieldName)); //NOI18N
1251 if (field.getName().equals(fieldName)) {
1253 type = field.getType();
1256 this.getLogger().debug(MessageFormat.format("Found fieldName={0}: setting type={1}", fieldName, type.getSimpleName())); //NOI18N
1258 // Don't continue with searching
1264 this.getLogger().debug(MessageFormat.format("type={0}", type)); //NOI18N
1266 // type should not be null
1269 throw new NullPointerException("type is null"); //NOI18N
1273 this.getLogger().debug(MessageFormat.format("type={0} - EXIT!", type)); //NOI18N