hammant     2002/11/29 13:30:07

  Added:       src/java/org/apache/avalon/cornerstone/blocks/event
                        DefaultEventManager.java
                        InvalidEventTypeException.java
               src/java/org/apache/avalon/cornerstone/services/event
                        Event.java EventManager.java Filter.java
                        Publisher.java Register.java Subscriber.java
  Log:
  Mauro Talevi suppies "Event Notifier pattern by Gupta et al. (Java Report, 
Vol. 3, No. 7, July 1998, 19-36)" block.
  
  Revision  Changes    Path
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/blocks/event/DefaultEventManager.java
  
  Index: DefaultEventManager.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.cornerstone.blocks.event;
  
  import java.util.Hashtable;
  import java.util.Enumeration;
  
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.context.Contextualizable;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.ContextException;
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.service.Serviceable;
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.avalon.framework.service.ServiceException;
  import org.apache.avalon.framework.activity.Initializable;
  import org.apache.avalon.framework.activity.Disposable;
  
  import org.apache.avalon.phoenix.BlockContext;
  
  import org.apache.avalon.cornerstone.services.event.Event;
  import org.apache.avalon.cornerstone.services.event.Filter;
  import org.apache.avalon.cornerstone.services.event.EventManager;
  import org.apache.avalon.cornerstone.services.event.Publisher;
  import org.apache.avalon.cornerstone.services.event.Register;
  import org.apache.avalon.cornerstone.services.event.Subscriber;
  
  /**
   * EventManager Block
   *
   * @phoenix:block
   * @phoenix:service 
name="org.apache.avalon.cornerstone.services.event.EventManager"
   *
   * @author Mauro Talevi
   */
  public class DefaultEventManager extends AbstractLogEnabled
      implements EventManager,
                 Contextualizable, Configurable, Serviceable, Initializable, 
Disposable
  {
      private final String m_rootEventType = "Event";
      private Class m_eventClass;
      private Publisher m_publisher = new DefaultPublisher();
      private Register m_register = new DefaultRegister();
      private Hashtable m_subscribers = new Hashtable();
      
      public Publisher getPublisher(){
          return m_publisher;
      }
      public Register getRegister(){
          return m_register;
      }
      
      public void contextualize( final Context context )
      {
      }
      
      public void configure( final Configuration configuration )
          throws ConfigurationException
      {
      }
  
      /**
       * ServiceManager dependencies
       */
      public void service( final ServiceManager serviceManager )
          throws ServiceException
      {
      }
  
      public void initialize()
          throws Exception
      {            
          m_eventClass = Class.forName( m_rootEventType );
          getLogger().info("Initialising eventClass " + m_eventClass);
      }
  
      public void dispose()
      {
      }
      
      class DefaultPublisher implements Publisher
      {
          public void publish( final Event event ) 
          {
              getLogger().info("Publishing event " + event.getClass());
              System.out.println("Publishing event " + event.getClass());
              for ( Enumeration e = m_subscribers.elements(); 
e.hasMoreElements(); ){
                  Subscriber subscriber = (Subscriber)e.nextElement();
                  if 
(subscriber.getEventType().isAssignableFrom(event.getClass())
                  && (subscriber.getFilter() != null ||
                  subscriber.getFilter().filter(event))){
                      getLogger().info("Informing subscriber "+subscriber+" of 
event "+event.getClass());
                      subscriber.inform(event);
                  }
              }
          }
      }
      
      class DefaultRegister implements Register
      {
          public void subscribe( final Subscriber subscriber )
              throws InvalidEventTypeException
          {
              if ( !m_eventClass.isAssignableFrom( subscriber.getEventType() ) )
                  throw new InvalidEventTypeException();
              
              getLogger().info( "Subscribing event " + 
subscriber.getEventType().getName() );
              // Add to list but prevent duplicate subscriptions
              if ( !m_subscribers.containsKey( subscriber.getUID() ) ){
                  m_subscribers.put( subscriber.getUID(), subscriber );
                  getLogger().info( "Subscribed Event " + 
subscriber.getEventType().getName() );
                  if ( getLogger().isDebugEnabled() ){
                      getLogger().debug( "Subscribers now active: " + 
m_subscribers.size() );
                  }
              }
          }
          public void unsubscribe( Subscriber subscriber )
              throws InvalidEventTypeException 
          {
              if ( !m_eventClass.isAssignableFrom( subscriber.getEventType() ) )
                  throw new InvalidEventTypeException();
              if ( m_subscribers.containsKey( subscriber.getUID() ) ){
                  m_subscribers.remove( subscriber.getUID() );
                  getLogger().info( "Unsubscribed Event " + 
subscriber.getEventType().getName() );
                  if ( getLogger().isDebugEnabled() ){
                      getLogger().debug( "Subscribers now active: " + 
m_subscribers.size() );
                  }
              } else {
                  getLogger().warn( "Subscriber " + subscriber.getUID() + " not 
found" );
              }
          }
      }
      
  }
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/blocks/event/InvalidEventTypeException.java
  
  Index: InvalidEventTypeException.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.cornerstone.blocks.event;
  
  /**
   * InvalidEventTypeException is thrown whenever the event type of a 
<tt>Subscriber</tt>
   * is not assignable from the root event type.
   *
   * @author Mauro Talevi
   */
  public class InvalidEventTypeException extends RuntimeException 
  {
  }
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/services/event/Event.java
  
  Index: Event.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.cornerstone.services.event;
  
  import java.io.Serializable;
  /**
   * <tt>Event</tt> is a common ancestor type for all events.
   *
   * It is Serializable to allow events to be published and received 
   * in a distributed architecture.
   *
   * @author Mauro Talevi
   */
  public interface Event extends Serializable
  {
      
     /** 
       * Returns the event type
       * @return int representing the event type
       */
      public int getType();
      
      /** 
       * Returns the event source
       * @return Object representing the event source
       */
      public Object getSource();
  
  }
  
  
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/services/event/EventManager.java
  
  Index: EventManager.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.cornerstone.services.event;
  
  /**
   * <p>Service to manage event notification. The designed has been inspired by 
the paper by 
   * Gupta, S., J. M. Hartkopf, and S. Ramaswamy, in Java Report, Vol. 3, No. 
7, July 1998, 19-36,
   * "Event Notifier: A Pattern for Event Notification".</p>  
   * 
   * <p>EventManager brokers events between a <tt>Publisher</tt>, which 
produces events,
   * and a <tt>Subscriber</tt>, which handles the notification of events.
   * A <tt>Filter</tt> discards events not of interest to a subscriber.
   * All Events have a common ancestor type <tt>Event</tt> and the event types 
are 
   * identified by a <tt>Class</tt>.</p>
   * 
   * @author Mauro Talevi
   */
  public interface EventManager
  {
   
      /**
       * Represents Role of the service
       */
       String ROLE = EventManager.class.getName(); 
      
       /**
        *  Returns the Publisher with which events can be published.
        */
       public Publisher getPublisher();
      
       /**
        *  Returns the Register with which subscribers can 
        *  subscribe and unsubscribe interest to given Events.
        */
       public Register getRegister();
       
  }
  
  
  
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/services/event/Filter.java
  
  Index: Filter.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.cornerstone.services.event;
  
  import java.io.Serializable;
  /**
   * A Filter allows subscribers to specify which events
   * they should be informed of.  
   * It is Serializable to allow events to be published and received 
   * in a distributed architecture.
   *
   * @author Mauro Talevi
   */
  public interface Filter extends Serializable
  {
       /**
        * Filters event, discarding those not of interest to the subscriber.
        * 
        * @param event the <tt>Event</tt>
        * @return boolean <code>true</code> if Event passes filter
        */
       public boolean filter( Event event );
  
  }
  
  
  
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/services/event/Publisher.java
  
  Index: Publisher.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.cornerstone.services.event;
  
  /**
   * <tt>Publisher</tt> produces or publishes events that are brokered by 
<tt>EventManager</tt>
   * and of which the appropriate subscribers are informed of.
   *
   * @author Mauro Talevi  
   */
  public interface Publisher
  {
      /**
       *  Publishes an event for subscribers to be informed of.
       *
       *  @param event the <tt>Event</tt> being published
       */
      public void publish( Event event );
  
  } 
  
  
  
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/services/event/Register.java
  
  Index: Register.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.cornerstone.services.event;
  
  /**
   * <tt>Register</tt> allows a <tt>Subscriber</tt> to subscribe to 
   * and unsubscribe from <tt>EventManager</tt>.
   *  
   * @author Mauro Talevi
   */
  public interface Register
  {
      
       /**
        * Subscribes a Subscriber to the EventManager.  
        * The Subscriber abstracts all the information needed for the 
subscription.
        * @param subscriber the Subscriber
        * @see Subscriber
        */
       public void subscribe( Subscriber subscriber );
      
       /**
        * Unsubscribes an Subscriber from the EventManager.
        * @param subscriber the Subscriber 
        * @see Subscriber
        */
       public void unsubscribe( Subscriber subscriber );
       
  } 
  
  
  
  
  
  1.1                  
jakarta-avalon-cornerstone/src/java/org/apache/avalon/cornerstone/services/event/Subscriber.java
  
  Index: Subscriber.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.cornerstone.services.event;
  
  /**
   * <tt>Subscriber</tt> registers its interest in a class of events and 
   * filters the events of which it should be notified.
   *
   * @author Mauro Talevi
   */
  public interface Subscriber
  {
      /** 
       *  Returns UID (Unique ID) set when the subscriber is created.
       *  The UID is required when the subscriber is remote, since the
       *  <tt>Filter</tt> will in general be deep-copied to a new object.
       * 
       *  @return the String encoding the UID
       */
      public String getUID();
      
      /**
       *  Returns the event type of the event on which the Subscriber is 
interested.
       *  The event type is encoded by a <tt>Class</tt>.
       *
       *  @return the <tt>Class</tt> encoding the event type  
       */
       public Class getEventType();
  
      /**
       *  Returns the filter used to select the events in which the subscriber 
is 
       *  interested.
       *
       *  @return the <tt>Filter</tt> 
       */
       public Filter getFilter();
       
      /**
       * Callback method informing the Subscriber of the occurence of an event.
       *
       * @param event the <tt>Event</tt> of which the <tt>Subscriber</tt> is 
informed
       */
       public void inform( Event event );
  }
  
  
  
  
  

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

Reply via email to