psmith      2003/09/17 16:30:14

  Modified:    src/java/org/apache/log4j/plugins Plugin.java
                        PluginSkeleton.java
               tests/src/java/org/apache/log4j/plugins PluginTestCase.java
  Log:
  added PropertyChangeListener support to Plugin and
  it's associated Skeleton class.
  
  Also modified the PluginTestCase and ensured
  the Name and LoggerRepository property change events
  are fired correctly.
  
  The Active property is still under discussion as to how the
  setter may or may not be exposed via the class hierarchy.
  
  Revision  Changes    Path
  1.6       +37 -1     jakarta-log4j/src/java/org/apache/log4j/plugins/Plugin.java
  
  Index: Plugin.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/plugins/Plugin.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Plugin.java       24 Jun 2003 08:24:57 -0000      1.5
  +++ Plugin.java       17 Sep 2003 23:30:14 -0000      1.6
  @@ -52,6 +52,8 @@
   import org.apache.log4j.spi.LoggerRepository;
   import org.apache.log4j.spi.OptionHandler;
   
  +import java.beans.PropertyChangeListener;
  +
   
   /**
     Defines the required interface for all Plugin objects.
  @@ -100,11 +102,45 @@
     public void setLoggerRepository(LoggerRepository repository);
   
     /**
  +   * Adds a PropertyChangeListener to this instance which is
  +   * notified only by changes of the property with name propertyName
  +   * @param propertyName the name of the property in standard JavaBean syntax (e.g. 
for setName(), property="name")
  +   * @param l
  +   */
  +  public void addPropertyChangeListener(
  +    String propertyName, PropertyChangeListener l);
  +
  +  /**
  +   * Adds a PropertyChangeListener that will be notified of all property
  +   * changes.
  +   * @param l
  +   */
  +  public void addPropertyChangeListener(PropertyChangeListener l);
  +
  +  /**
  +   * Removes a specific PropertyChangeListener from this instances
  +   * registry that has been mapped to be notified of all property
  +   * changes..
  +   * @param l
  +   */
  +  public void removePropertyChangeListener(PropertyChangeListener l);
  +
  +  /**
  +   * Removes a specific PropertyChangeListener from this instance's
  +   * registry which has been previously registered to be notified
  +   * of only a specific property change.
  +   * @param propertyName
  +   * @param l
  +   */
  +  public void removePropertyChangeListener(
  +    String propertyName, PropertyChangeListener l);
  +
  +  /**
       True if the plugin is active and running.
   
       @return boolean true if the plugin is currently active. */
     public boolean isActive();
  -  
  +
     /**
       Call when the plugin should be stopped. */
     public void shutdown();
  
  
  
  1.7       +88 -4     
jakarta-log4j/src/java/org/apache/log4j/plugins/PluginSkeleton.java
  
  Index: PluginSkeleton.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-log4j/src/java/org/apache/log4j/plugins/PluginSkeleton.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- PluginSkeleton.java       24 Jun 2003 08:24:57 -0000      1.6
  +++ PluginSkeleton.java       17 Sep 2003 23:30:14 -0000      1.7
  @@ -51,6 +51,10 @@
   
   import org.apache.log4j.spi.LoggerRepository;
   
  +import java.beans.PropertyChangeEvent;
  +import java.beans.PropertyChangeListener;
  +import java.beans.PropertyChangeSupport;
  +
   
   /**
     A convienent abstract class for plugin subclasses that implements
  @@ -60,7 +64,7 @@
   
     <p>Developers are not required to subclass PluginSkeleton to
     develop their own plugins (they are only required to implement the
  -  Plugin interface), but it provides a convienent base class to start
  +  Plugin interface), but it provides a convenient base class to start
     from.
   
     Contributors: Nicko Cadell
  @@ -78,6 +82,13 @@
     protected boolean active;
   
     /**
  +   * This is a delegate that does all the PropertyChangeListener
  +   * support.
  +   */
  +  private PropertyChangeSupport propertySupport =
  +    new PropertyChangeSupport(this);
  +
  +  /**
       Gets the name of the plugin.
   
       @return String the name of the plugin. */
  @@ -86,11 +97,13 @@
     }
   
     /**
  -    Sets the name of the plugin.
  +    Sets the name of the plugin and notifies PropertyChangeListeners of the change
   
       @param name the name of the plugin to set. */
     public void setName(String name) {
  +    String oldName = this.name;
       this.name = name;
  +    propertySupport.firePropertyChange("name", oldName, this.name);
     }
   
     /**
  @@ -102,20 +115,91 @@
     }
   
     /**
  -    Sets the logger repository used by this plugin. This
  +    Sets the logger repository used by this plugin and notifies an relevant 
PropertyChangeListeners registered. This
       repository will be used by the plugin functionality.
   
       @param repository the logger repository that this plugin should affect. */
     public void setLoggerRepository(LoggerRepository repository) {
  +    Object oldValue = this.repository;
       this.repository = repository;
  +    firePropertyChange("loggerRepository", oldValue, this.repository);
     }
   
     /**
      * Returns whether this plugin is Active or not
  -   * @return true/false 
  +   * @return true/false
      */
     public synchronized boolean isActive() {
       return active;
     }
   
  +  /**
  +   * @param listener
  +   */
  +  public final void addPropertyChangeListener(PropertyChangeListener listener) {
  +    propertySupport.addPropertyChangeListener(listener);
  +  }
  +
  +  /**
  +   * @param propertyName
  +   * @param listener
  +   */
  +  public final void addPropertyChangeListener(
  +    String propertyName, PropertyChangeListener listener) {
  +    propertySupport.addPropertyChangeListener(propertyName, listener);
  +  }
  +
  +  /**
  +   * @param listener
  +   */
  +  public final void removePropertyChangeListener(
  +    PropertyChangeListener listener) {
  +    propertySupport.removePropertyChangeListener(listener);
  +  }
  +
  +  /**
  +   * @param propertyName
  +   * @param listener
  +   */
  +  public final void removePropertyChangeListener(
  +    String propertyName, PropertyChangeListener listener) {
  +    propertySupport.removePropertyChangeListener(propertyName, listener);
  +  }
  +
  +  /**
  +   * @param evt
  +   */
  +  protected final void firePropertyChange(PropertyChangeEvent evt) {
  +    propertySupport.firePropertyChange(evt);
  +  }
  +
  +  /**
  +   * @param propertyName
  +   * @param oldValue
  +   * @param newValue
  +   */
  +  protected final void firePropertyChange(
  +    String propertyName, boolean oldValue, boolean newValue) {
  +    propertySupport.firePropertyChange(propertyName, oldValue, newValue);
  +  }
  +
  +  /**
  +   * @param propertyName
  +   * @param oldValue
  +   * @param newValue
  +   */
  +  protected final void firePropertyChange(
  +    String propertyName, int oldValue, int newValue) {
  +    propertySupport.firePropertyChange(propertyName, oldValue, newValue);
  +  }
  +
  +  /**
  +   * @param propertyName
  +   * @param oldValue
  +   * @param newValue
  +   */
  +  protected final void firePropertyChange(
  +    String propertyName, Object oldValue, Object newValue) {
  +    propertySupport.firePropertyChange(propertyName, oldValue, newValue);
  +  }
   }
  
  
  
  1.7       +117 -0    
jakarta-log4j/tests/src/java/org/apache/log4j/plugins/PluginTestCase.java
  
  Index: PluginTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-log4j/tests/src/java/org/apache/log4j/plugins/PluginTestCase.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- PluginTestCase.java       13 May 2003 16:33:16 -0000      1.6
  +++ PluginTestCase.java       17 Sep 2003 23:30:14 -0000      1.7
  @@ -63,6 +63,9 @@
   import org.apache.log4j.spi.RootCategory;
   import org.apache.log4j.util.Compare;
   
  +import java.beans.PropertyChangeEvent;
  +import java.beans.PropertyChangeListener;
  +
   import java.io.File;
   import java.io.IOException;
   
  @@ -365,14 +368,125 @@
         Compare.compare(getOutputFile(testName), getWitnessFile(testName)));
     }
   
  +  public void testPropertyChangeListeners() {
  +    Plugin plugin = new PluginTester1("PluginTest1", 1);
  +
  +    final PropertyChangeListenerLatch l = new PropertyChangeListenerLatch();
  +    plugin.addPropertyChangeListener(l);
  +
  +    /**
  +     * Test the basic properties and ensure they get latched by notification
  +     */
  +    plugin.setName("NewName");
  +    assertTrue(
  +      "PropertyChange latch should have been detected", l.isLatched());
  +    assertTrue(
  +      "Old value unexpected: '" + l.getLastEvent().getOldValue() + "'",
  +      l.getLastEvent().getOldValue().equals("PluginTest1"));
  +    assertTrue(
  +      "New value unexpected: '" + l.getLastEvent().getNewValue() + "'",
  +      l.getLastEvent().getNewValue().equals("NewName"));
  +
  +    l.reset();
  +
  +    plugin.removePropertyChangeListener(l);
  +    plugin.setName("SecondNewName");
  +
  +    assertTrue("Should not have been notified/latched", !l.isLatched());
  +
  +    l.reset();
  +
  +    /**
  +     * Test when only listening for specific property
  +     */
  +    plugin.addPropertyChangeListener("name", l);
  +
  +    plugin.setName("NewName2");
  +    assertTrue(
  +      "PropertyChange latch should have been detected", l.isLatched());
  +    assertTrue(
  +      "Old value unexpected: '" + l.getLastEvent().getOldValue() + "'",
  +      l.getLastEvent().getOldValue().equals("SecondNewName"));
  +    assertTrue(
  +      "New value unexpected: '" + l.getLastEvent().getNewValue() + "'",
  +      l.getLastEvent().getNewValue().equals("NewName2"));
  +
  +    plugin.removePropertyChangeListener("name", l);
  +
  +    l.reset();
  +
  +    /**
  +     * setup some assertions before continuing testing to make sure the test code 
isn't broken
  +     */
  +    assertTrue("Plugin should not be active just yet", !plugin.isActive());
  +    assertTrue("Latch should not be latched", !l.isLatched());
  +
  +    plugin.addPropertyChangeListener("active", l);
  +
  +    PluginRegistry.startPlugin(plugin);
  +    assertTrue(
  +      "Should have been notified of activation when PluginRegistry.start(plugin)",
  +      l.isLatched());
  +    assertTrue(
  +      "Active old value should have been false",
  +      l.getLastEvent().getOldValue().equals(Boolean.FALSE));
  +    assertTrue(
  +      "Active New value should have been true",
  +      l.getLastEvent().getNewValue().equals(Boolean.TRUE));
  +      
  +      PluginRegistry.stopAllPlugins();
  +      l.reset();
  +      assertTrue("Latch should have been reset", !l.isLatched());
  +      /**
  +       * start afresh
  +       */
  +     plugin = new PluginTester1("LoggerRepositoryProperty", 2);
  +     LoggerRepository oldValue = plugin.getLoggerRepository();
  +     plugin.addPropertyChangeListener("loggerRepository", l);
  +     LoggerRepository rep = new Hierarchy(new RootCategory(Level.DEBUG));
  +     plugin.setLoggerRepository(rep);
  +     
  +     assertTrue("Should be notified of LoggerRepository property change", 
l.isLatched());
  +     assertTrue("LoggerRepository Old value mismatch", 
l.getLastEvent().getOldValue() == oldValue);
  +     assertTrue("LoggerRepository New vale mismatch", 
l.getLastEvent().getNewValue() == rep);
  +  }
  +
     public static Test suite() {
       TestSuite suite = new TestSuite();
       suite.addTest(new PluginTestCase("test1"));
       suite.addTest(new PluginTestCase("test2"));
  +    suite.addTest(new PluginTestCase("testPropertyChangeListeners"));
   
       return suite;
     }
   
  +  private static class PropertyChangeListenerLatch
  +    implements PropertyChangeListener {
  +    boolean latch = false;
  +    PropertyChangeEvent lastEvent = null;
  +
  +    /* (non-Javadoc)
  +     * @see 
java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
  +     */
  +    public void propertyChange(PropertyChangeEvent evt) {
  +      latch = true;
  +      lastEvent = evt;
  +    }
  +
  +    boolean isLatched() {
  +      return latch;
  +    }
  +
  +    void reset() {
  +      latch = false;
  +      lastEvent = null;
  +    }
  +
  +    PropertyChangeEvent getLastEvent() {
  +      return lastEvent;
  +    }
  +  }
  +
     /**
       Class to test the Plugin and PluginRegistry functionality. */
     private static class PluginTester extends PluginSkeleton {
  @@ -424,8 +538,11 @@
       }
   
       private synchronized boolean setActive(boolean _active) {
  +      boolean oldValue = active;
  +
         if (active != _active) {
           active = _active;
  +        firePropertyChange("active", oldValue, active);
   
           return true;
         } else {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to