Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Search Host

Monthly Fee($)
Disk Space (MB)
Register With us for Newsletter!
Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Have tutorials?
Add your tutorial to our Java Resource and get tons of hits.

We offer free hosting for your tutorials. and exposure for thousands of readers. drop a mail
roseindia_net@yahoo.com
 
   

Tutorials

Java Server Pages

JAXB

Java Beans

JDBC

MySQL

Java Servlets

Struts

Bioinformatics

Java Code Examples

Interview Questions

 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Web Promotion

Web Submission

Submit Sites

Manual Submission?

Web Promotion Guide

Hosting Companies

Web Hosting Guide

Web Hosting

Linux

Beginner Guide to Linux Server

Frameworks

Persistence Framework

Web Frameworks

Free EAI Tools

Web Servers

Aspect Oriented Programming

Free Proxy Servers

Softwares

Adware & Spyware Remover

Open Source Softwares

Self-reloading XML Property Files

       

2004-10-01 The Java Specialists' Newsletter [Issue 095b] - Follow-up: Self-reloading XML Property Files

Author: Dr. Heinz M. Kabutz

JDK version: Sun JDK 1.5.0-rc

If you are reading this, and have not subscribed, please consider doing it now by going to our subscribe page. You can subscribe either via email or RSS.


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 maintain

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

This material from The Java(tm) Specialists' Newsletter by Maximum Solutions (South Africa). Please contact Maximum Solutions for more information.

       

Useful Links
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.