Print

Print


Author: [log in to unmask]
Date: Fri Apr 17 12:19:49 2015
New Revision: 2739

Log:
Improve error handling when setting properties in AbstractModel.  A missing property is no longer a fatal error and will just print a warning message now.

Modified:
    java/trunk/monitoring-app/src/main/java/org/hps/monitoring/application/model/AbstractModel.java

Modified: java/trunk/monitoring-app/src/main/java/org/hps/monitoring/application/model/AbstractModel.java
 =============================================================================
--- java/trunk/monitoring-app/src/main/java/org/hps/monitoring/application/model/AbstractModel.java	(original)
+++ java/trunk/monitoring-app/src/main/java/org/hps/monitoring/application/model/AbstractModel.java	Fri Apr 17 12:19:49 2015
@@ -10,8 +10,13 @@
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javassist.Modifier;
+
+import org.lcsim.util.log.DefaultLogFormatter;
+import org.lcsim.util.log.LogUtil;
 
 /**
  * An abstract class which updates a set of listeners when there are property changes to a backing model.
@@ -19,6 +24,12 @@
  * @author <a href="mailto:[log in to unmask]">Jeremy McCormick</a>
  */
 public abstract class AbstractModel {
+
+    /**
+     * Setup logging.
+     */
+    private static final Logger LOGGER = LogUtil.create(AbstractModel.class.getName(), new DefaultLogFormatter(),
+            Level.INFO);
 
     /**
      * This method will extract property names from a class, which in this package's conventions are statically
@@ -76,7 +87,9 @@
      * @param properties the list of property names
      */
     void firePropertiesChanged(final Collection<String> properties) {
-        propertyLoop: for (final String property : properties) {
+        for (final String property : properties) {
+
+            // Find the getter for this property.
             Method getMethod = null;
             for (final Method method : this.getClass().getMethods()) {
                 if (method.getName().equals("get" + property)) {
@@ -84,38 +97,44 @@
                     break;
                 }
             }
-            // System.out.println(getMethod.getName());
-            try {
-                Object value = null;
+
+            // Is there a valid get method for the property?
+            if (getMethod != null) {
+                LOGGER.fine("property: " + property + ", method: " + getMethod.getName());
                 try {
-                    value = getMethod.invoke(this, (Object[]) null);
-                    // System.out.println("  value = " + value);
-                } catch (final NullPointerException e) {
-                    // This means there is no get method for the property which is a throwable error.
-                    throw new RuntimeException("Property " + property + " is missing a get method.", e);
-                } catch (final InvocationTargetException e) {
-                    // Is the cause of the problem an illegal argument to the method?
-                    if (e.getCause() instanceof IllegalArgumentException) {
-                        // For this error, assume that the key itself is missing from the configuration which is a
-                        // warning.
-                        System.err.println("The key " + property + " is not set in the configuration.");
-                        continue propertyLoop;
-                    } else {
-                        e.printStackTrace();
-                        throw new RuntimeException(e);
+                    Object value = null;
+                    try {
+                        value = getMethod.invoke(this, (Object[]) null);
+                    } catch (final InvocationTargetException e) {
+                        // Is the cause of the problem an illegal argument to the method?
+                        if (e.getCause() instanceof IllegalArgumentException) {
+                            // Property key is not in the configuration (this should not happen under normal
+                            // circumstances).
+                            LOGGER.log(Level.WARNING, "Property key missing from configuration: " + property, e);
+                            continue;
+                        } else {
+                            // Something else went wrong, which we assume is a fatal error.
+                            LOGGER.log(Level.SEVERE, "Error setting property: " + property, e);
+                            throw new RuntimeException("Error setting property: " + property, e);
+                        }
                     }
+                    if (value != null) {
+                        this.firePropertyChange(property, value, value);
+                        for (final PropertyChangeListener listener : this.propertyChangeSupport
+                                .getPropertyChangeListeners()) {
+                            // FIXME: For some reason calling the propertyChangeSupport methods directly here doesn't
+                            // work!
+                            listener.propertyChange(new PropertyChangeEvent(this, property, value, value));
+                        }
+                    }
+                } catch (IllegalAccessException | IllegalArgumentException e) {
+                    // This should not usually happen.
+                    LOGGER.log(Level.SEVERE, "Error setting property: " + property, e);
+                    throw new RuntimeException("Error setting property: " + property, e);
                 }
-                if (value != null) {
-                    this.firePropertyChange(property, value, value);
-                    for (final PropertyChangeListener listener : this.propertyChangeSupport
-                            .getPropertyChangeListeners()) {
-                        // FIXME: For some reason calling the propertyChangeSupport methods directly here doesn't work!
-                        listener.propertyChange(new PropertyChangeEvent(this, property, value, value));
-                    }
-                }
-            } catch (IllegalAccessException | IllegalArgumentException e) {
-                e.printStackTrace();
-                throw new RuntimeException(e);
+            } else {
+                // There was no getter found for the property which is a non-fatal warning.
+                LOGGER.log(Level.WARNING, "Unknown property in configuration: " + property);
             }
         }
     }