Home of The JavaSpecialists' Newsletter

095bFollow-up: Self-reloading XML Property Files

Posted: 2004-10-01Category: LanguageJava Version: 1.5+Dr. Heinz M. Kabutz
 

Abstract: 

 

In my last newsletter, I mentioned that I like generics. To me, they improve the Java language by being more specific as to what is allowed in collections. I find writing code with generics quite easy, because IntelliJ IDEA assists me with it. However, John O'Sullivan mentioned that it took him "MUCH MUCH MUCH longer to read" the code to see what I was doing, and I think that John has a point.

This may be a problem. The cost of maintenance often dwarfs the cost of initially writing the code. Can generics help in maintenance? I cannot recall ever seeing a ClassCastException "in the field" due to incorrect types being put into a collection.

Here is the AbstractConfiguration class written without using generics. By removing generics, I discovered that the getAllProperties() would have been better off returning a copy of a Map, rather than a Set of entries. This "design flaw" was obscured by having so much text as a return code: Set<Map.Entry<String, Object>>.

So, here is your opportunity to have your say. Please think about whether you find that generics will improve maintenance or not. Remember that if the syntax bothers you, that is a matter of time before you are used to it. You can also do clever tricks with syntax highlighting. An IDE could even give you the option of hiding generics in the editor so that they do not bother you. Try thinking of technical reasons, rather than just looks.

  1. Generics make the code easier to maintain
  2. Generics make the code more difficult to maintainlink>

I will send a summary of the results in my next newsletter, and might publish the full results on my webpage. Please indicate if you would like to remain anonymous in the results. If I do reveal your comments, they would only include your name and company, definitely not your email address.

import java.beans.*;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

public abstract class AbstractConfigurationNoGenerics {
  /** A map of all the properties for the configuration */
  private final Map properties = new HashMap();

  private final Collection listeners = new CopyOnWriteArrayList();

  /** Make a daemon timer to check for changes. */
  private final Timer timer = new Timer(true);

  /**
   * This class has a timer to check periodically if the
   * configuration has changed.  If it has, it reloads the
   * properties.  This may cause the property change events to
   * fire.
   *
   * @param period number of milliseconds between checking for
   *               property changes.
   */
  protected AbstractConfigurationNoGenerics(int period) {
    timer.schedule(new TimerTask() {
      public void run() {
        checkForPropertyChanges();
      }
    }, period, period);
  }

  /**
   * This method should be overridden to check whether the
   * properties could maybe have changed, and if yes, to reload
   * them.
   */
  protected abstract void checkForPropertyChanges();

  public final Object getProperty(String propertyName) {
    synchronized (properties) {
      return properties.get(propertyName);
    }
  }

  public Map getAllProperties() {
    synchronized (properties) {
      return new HashMap(properties);
    }
  }

  /**
   * Each time we set a property, we check whether it has changed
   * and if it has, we let the listeners know.
   */
  protected final void setProperty(String propertyName, Object value) {
    synchronized (properties) {
      Object old = properties.get(propertyName);
      if ((value != null && !value.equals(old))
          || value == null && old != null) {
        properties.put(propertyName, value);
        PropertyChangeEvent event = new PropertyChangeEvent(this,
            propertyName, old, value);
        for (Iterator it = listeners.iterator(); it.hasNext();) {
          PropertyChangeListener listener = (PropertyChangeListener) it.next();
          listener.propertyChange(event);
        }
      }
    }
  }

  public void addPropertyChangeListener(PropertyChangeListener l) {
    listeners.add(l);
  }

  public boolean removePropertyChangeListener(PropertyChangeListener l) {
    return listeners.remove(l);
  }
}
  

I am currently sitting in Citrusdal, about 2 hours drive from my home, and this newsletter will reach you via GPRS :-)

Kind regards

Heinz

 

Related Articles

Browse the Newsletter Archive

About the Author

demo

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

Nobody ever wants to call a Java performance consultant, but with first-hand experience repairing and improving commercial Java applications - JavaSpecialists are a good place to start...

Threading Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.