LISTSERV mailing list manager LISTSERV 16.5

Help for HPS-SVN Archives


HPS-SVN Archives

HPS-SVN Archives


HPS-SVN@LISTSERV.SLAC.STANFORD.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

HPS-SVN Home

HPS-SVN Home

HPS-SVN  April 2015

HPS-SVN April 2015

Subject:

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

From:

[log in to unmask]

Reply-To:

Notification of commits to the hps svn repository <[log in to unmask]>

Date:

Fri, 17 Apr 2015 19:19:56 -0000

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (127 lines)

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);
             }
         }
     }

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

November 2017
August 2017
July 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013

ATOM RSS1 RSS2



LISTSERV.SLAC.STANFORD.EDU

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager

Privacy Notice, Security Notice and Terms of Use