You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2003/12/06 06:29:25 UTC
svn commit: rev 1366 - in incubator/directory/ldap/trunk/eve/frontend/event: impl impl/src impl/src/java impl/src/java/org impl/src/java/org/apache impl/src/java/org/apache/eve impl/src/java/org/apache/eve/event spi spi/src spi/src/java spi/src/java/org spi/src/java/org/apache spi/src/java/org/apache/eve spi/src/java/org/apache/eve/event
Author: akarasulu
Date: Fri Dec 5 21:29:25 2003
New Revision: 1366
Added:
incubator/directory/ldap/trunk/eve/frontend/event/impl/
incubator/directory/ldap/trunk/eve/frontend/event/impl/src/
incubator/directory/ldap/trunk/eve/frontend/event/impl/src/java/
incubator/directory/ldap/trunk/eve/frontend/event/impl/src/java/org/
incubator/directory/ldap/trunk/eve/frontend/event/impl/src/java/org/apache/
incubator/directory/ldap/trunk/eve/frontend/event/impl/src/java/org/apache/eve/
incubator/directory/ldap/trunk/eve/frontend/event/impl/src/java/org/apache/eve/event/
incubator/directory/ldap/trunk/eve/frontend/event/impl/src/java/org/apache/eve/event/EventRouterImpl.java
incubator/directory/ldap/trunk/eve/frontend/event/spi/
incubator/directory/ldap/trunk/eve/frontend/event/spi/src/
incubator/directory/ldap/trunk/eve/frontend/event/spi/src/java/
incubator/directory/ldap/trunk/eve/frontend/event/spi/src/java/org/
incubator/directory/ldap/trunk/eve/frontend/event/spi/src/java/org/apache/
incubator/directory/ldap/trunk/eve/frontend/event/spi/src/java/org/apache/eve/
incubator/directory/ldap/trunk/eve/frontend/event/spi/src/java/org/apache/eve/event/
incubator/directory/ldap/trunk/eve/frontend/event/spi/src/java/org/apache/eve/event/EventRouter.java
incubator/directory/ldap/trunk/eve/frontend/event/spi/src/java/org/apache/eve/event/Filter.java
incubator/directory/ldap/trunk/eve/frontend/event/spi/src/java/org/apache/eve/event/Subscription.java
Log:
Adding the event router module.
Added: incubator/directory/ldap/trunk/eve/frontend/event/impl/src/java/org/apache/eve/event/EventRouterImpl.java
==============================================================================
--- (empty file)
+++ incubator/directory/ldap/trunk/eve/frontend/event/impl/src/java/org/apache/eve/event/EventRouterImpl.java Fri Dec 5 21:29:25 2003
@@ -0,0 +1,199 @@
+/*
+
+ ============================================================================
+ The Apache Software License, Version 1.1
+ ============================================================================
+
+ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modifica-
+ tion, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. The end-user documentation included with the redistribution, if any, must
+ include the following acknowledgment: "This product includes software
+ developed by the Apache Software Foundation (http://www.apache.org/)."
+ Alternately, this acknowledgment may appear in the software itself, if
+ and wherever such third-party acknowledgments normally appear.
+
+ 4. The names "Eve Directory Server", "Apache Directory Project", "Apache Eve"
+ and "Apache Software Foundation" must not be used to endorse or promote
+ products derived from this software without prior written
+ permission. For written permission, please contact apache@apache.org.
+
+ 5. Products derived from this software may not be called "Apache", nor may
+ "Apache" appear in their name, without prior written permission of the
+ Apache Software Foundation.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
+ DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many individuals
+ on behalf of the Apache Software Foundation. For more information on the
+ Apache Software Foundation, please see <http://www.apache.org/>.
+
+*/
+package org.apache.eve.event ;
+
+
+import java.util.Set ;
+import java.util.HashSet ;
+import java.util.Iterator ;
+import java.util.EventObject ;
+
+
+/**
+ * An synchronous implementation of the event router / notification pattern.
+ *
+ * @author <a href="mailto:aok123@bellsouth.net">Alex Karasulu</a>
+ * @author $Author$
+ * @version $Revision$
+ */
+public class EventRouterImpl implements EventRouter
+{
+ /** the set of subscriptions made with this router */
+ private Set m_subscriptions = new HashSet() ;
+
+
+ /**
+ * @see org.apache.eve.event.EventRouter#subscribe(java.lang.Class,
+ * org.apache.eve.event.Filter, org.apache.eve.event.Subscriber)
+ */
+ public void subscribe( Class a_type, Filter a_filter,
+ Subscriber a_subscriber )
+ {
+ if ( ! EventObject.class.isAssignableFrom( a_type ) )
+ {
+ throw new IllegalArgumentException( "Invalid event class: "
+ + a_type.getName() ) ;
+ }
+
+ Subscription l_subscription =
+ new Subscription( a_type, a_filter, a_subscriber ) ;
+
+ if ( ! m_subscriptions.contains( l_subscription ) )
+ {
+ synchronized ( m_subscriptions )
+ {
+ m_subscriptions.add( l_subscription ) ;
+ }
+ }
+ }
+
+
+ /**
+ * @see org.apache.eve.event.EventRouter#unsubscribe(
+ * org.apache.eve.event.Subscriber)
+ */
+ public void unsubscribe( Subscriber a_subscriber )
+ {
+ Iterator l_list = m_subscriptions.iterator() ;
+
+ synchronized ( m_subscriptions )
+ {
+ while ( l_list.hasNext() )
+ {
+ Subscription l_subscription = ( Subscription ) l_list.next() ;
+ if ( a_subscriber == l_subscription.getSubscriber() )
+ {
+ l_list.remove() ;
+ }
+ }
+ }
+ }
+
+
+ /**
+ * (non-Javadoc)
+ * @see org.apache.eve.event.EventRouter#unsubscribe(java.lang.Class,
+ * org.apache.eve.event.Subscriber)
+ */
+ public void unsubscribe( Class a_type, Subscriber a_subscriber )
+ {
+ Iterator l_list = m_subscriptions.iterator() ;
+
+ synchronized ( m_subscriptions )
+ {
+ while ( l_list.hasNext() )
+ {
+ Subscription l_subscription = ( Subscription ) l_list.next() ;
+ if ( a_subscriber == l_subscription.getSubscriber()
+ && a_type.equals( l_subscription.getType() ) )
+ {
+ l_list.remove() ;
+ }
+ }
+ }
+ }
+
+
+ /**
+ * (non-Javadoc)
+ * @see org.apache.eve.event.EventRouter#unsubscribe(java.lang.Class,
+ * org.apache.eve.event.Subscriber)
+ */
+ public void unsubscribe( Class a_type, Filter a_filter,
+ Subscriber a_subscriber )
+ {
+ if ( ! EventObject.class.isAssignableFrom( a_type ) )
+ {
+ throw new IllegalArgumentException( "Invalid event class: "
+ + a_type.getName() ) ;
+ }
+
+ final Subscription l_subscription = new Subscription( a_type, a_filter,
+ a_subscriber ) ;
+
+ synchronized ( m_subscriptions )
+ {
+ m_subscriptions.remove( l_subscription ) ;
+ }
+ }
+
+
+ /**
+ * (non-Javadoc)
+ * @see org.apache.eve.event.EventRouter#publish(org.apache.eve.event.Event)
+ */
+ public void publish( EventObject a_event )
+ {
+ final Subscription [] l_subscriptions ;
+
+ synchronized ( m_subscriptions )
+ {
+ l_subscriptions = ( Subscription [] ) m_subscriptions
+ .toArray( new Subscription [ m_subscriptions.size() ] ) ;
+ }
+
+ for ( int ii = 0; ii < l_subscriptions.length; ii++ )
+ {
+ boolean isAssignable = l_subscriptions[ii].getType()
+ .isAssignableFrom( a_event.getClass() ) ;
+
+ if ( ! isAssignable )
+ {
+ continue ;
+ }
+
+ if ( l_subscriptions[ii].getFilter() != null &&
+ l_subscriptions[ii].getFilter().apply( a_event ) )
+ {
+ l_subscriptions[ii].getSubscriber().inform( a_event ) ;
+ }
+ }
+ }
+}
Added: incubator/directory/ldap/trunk/eve/frontend/event/spi/src/java/org/apache/eve/event/EventRouter.java
==============================================================================
--- (empty file)
+++ incubator/directory/ldap/trunk/eve/frontend/event/spi/src/java/org/apache/eve/event/EventRouter.java Fri Dec 5 21:29:25 2003
@@ -0,0 +1,100 @@
+/*
+
+ ============================================================================
+ The Apache Software License, Version 1.1
+ ============================================================================
+
+ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modifica-
+ tion, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. The end-user documentation included with the redistribution, if any, must
+ include the following acknowledgment: "This product includes software
+ developed by the Apache Software Foundation (http://www.apache.org/)."
+ Alternately, this acknowledgment may appear in the software itself, if
+ and wherever such third-party acknowledgments normally appear.
+
+ 4. The names "Eve Directory Server", "Apache Directory Project", "Apache Eve"
+ and "Apache Software Foundation" must not be used to endorse or promote
+ products derived from this software without prior written
+ permission. For written permission, please contact apache@apache.org.
+
+ 5. Products derived from this software may not be called "Apache", nor may
+ "Apache" appear in their name, without prior written permission of the
+ Apache Software Foundation.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
+ DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many individuals
+ on behalf of the Apache Software Foundation. For more information on the
+ Apache Software Foundation, please see <http://www.apache.org/>.
+
+*/
+package org.apache.eve.event ;
+
+
+import java.util.EventObject ;
+
+
+/**
+ * Event service based on an exact version of the event notifier pattern found
+ * <a href="http://members.ispwest.com/jeffhartkopf/notifier/">here</a>.
+ *
+ * @author <a href="mailto:aok123@bellsouth.net">Alex Karasulu</a>
+ * @author $Author$
+ * @version $Revision$
+ */
+public interface EventRouter
+{
+ /** Avalon compliant service interface role */
+ String ROLE = EventRouter.class.getName() ;
+
+ /**
+ * Subscribes an event subscriber.
+ *
+ * @param a_type an event type enumeration value
+ * @param a_filter an event filter if any to apply
+ * @param a_subscriber the Subscriber to subscribe
+ */
+ void subscribe( Class a_type, Filter a_filter,
+ Subscriber a_subscriber ) ;
+
+ /**
+ * Unsubscribes an event subscriber.
+ *
+ * @param a_subscriber the Subscriber to unsubscribe
+ */
+ void unsubscribe( Subscriber a_subscriber ) ;
+
+ /**
+ * Unsubscribes an event subscriber.
+ *
+ * @param a_subscriber the Subscriber to unsubscribe
+ */
+ void unsubscribe( Class a_type, Subscriber a_subscriber ) ;
+
+ /**
+ * Fires an event synchronously in the thread of the caller to all
+ * subscribers registered for a specific event type.
+ *
+ * @param a_event the event to publish
+ */
+ void publish( EventObject a_event ) ;
+}
Added: incubator/directory/ldap/trunk/eve/frontend/event/spi/src/java/org/apache/eve/event/Filter.java
==============================================================================
--- (empty file)
+++ incubator/directory/ldap/trunk/eve/frontend/event/spi/src/java/org/apache/eve/event/Filter.java Fri Dec 5 21:29:25 2003
@@ -0,0 +1,73 @@
+/*
+
+ ============================================================================
+ The Apache Software License, Version 1.1
+ ============================================================================
+
+ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modifica-
+ tion, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. The end-user documentation included with the redistribution, if any, must
+ include the following acknowledgment: "This product includes software
+ developed by the Apache Software Foundation (http://www.apache.org/)."
+ Alternately, this acknowledgment may appear in the software itself, if
+ and wherever such third-party acknowledgments normally appear.
+
+ 4. The names "Eve Directory Server", "Apache Directory Project", "Apache Eve"
+ and "Apache Software Foundation" must not be used to endorse or promote
+ products derived from this software without prior written
+ permission. For written permission, please contact apache@apache.org.
+
+ 5. Products derived from this software may not be called "Apache", nor may
+ "Apache" appear in their name, without prior written permission of the
+ Apache Software Foundation.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
+ DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many individuals
+ on behalf of the Apache Software Foundation. For more information on the
+ Apache Software Foundation, please see <http://www.apache.org/>.
+
+*/
+package org.apache.eve.event ;
+
+
+import java.util.EventObject ;
+
+
+/**
+ * A filter in the event router is used to further prune the subscriber's
+ * interest list.
+ *
+ * @author <a href="mailto:aok123@bellsouth.net">Alex Karasulu</a>
+ * @author $Author$
+ * @version $Revision$
+ */
+public interface Filter
+{
+ /**
+ * Determines whether or not to inform a subscriber of an event.
+ *
+ * @param a_event the event to test
+ * @return true if the event can be sent, false if it cannot
+ */
+ boolean apply( EventObject a_event ) ;
+}
Added: incubator/directory/ldap/trunk/eve/frontend/event/spi/src/java/org/apache/eve/event/Subscription.java
==============================================================================
--- (empty file)
+++ incubator/directory/ldap/trunk/eve/frontend/event/spi/src/java/org/apache/eve/event/Subscription.java Fri Dec 5 21:29:25 2003
@@ -0,0 +1,186 @@
+/*
+
+ ============================================================================
+ The Apache Software License, Version 1.1
+ ============================================================================
+
+ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modifica-
+ tion, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. The end-user documentation included with the redistribution, if any, must
+ include the following acknowledgment: "This product includes software
+ developed by the Apache Software Foundation (http://www.apache.org/)."
+ Alternately, this acknowledgment may appear in the software itself, if
+ and wherever such third-party acknowledgments normally appear.
+
+ 4. The names "Eve Directory Server", "Apache Directory Project", "Apache Eve"
+ and "Apache Software Foundation" must not be used to endorse or promote
+ products derived from this software without prior written
+ permission. For written permission, please contact apache@apache.org.
+
+ 5. Products derived from this software may not be called "Apache", nor may
+ "Apache" appear in their name, without prior written permission of the
+ Apache Software Foundation.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
+ DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many individuals
+ on behalf of the Apache Software Foundation. For more information on the
+ Apache Software Foundation, please see <http://www.apache.org/>.
+
+*/
+package org.apache.eve.event ;
+
+
+/**
+ * A subscription bean.
+ *
+ * @author <a href="mailto:aok123@bellsouth.net">Alex Karasulu</a>
+ * @author $Author$
+ * @version $Revision$
+ */
+public class Subscription
+{
+ /** the filter if any used to filter out events */
+ private final Filter m_filter ;
+ /** the event class */
+ private final Class m_type ;
+ /** the subscriber */
+ private final Subscriber m_subscriber ;
+
+
+ /**
+ * Creates a subscription for a type of event using a filter and a
+ * subscriber.
+ *
+ * @param a_type the class of event to be informed on
+ * @param a_filter the Filter to use weed out unwanted events
+ * @param a_subscriber the subscriber to deliever the event to
+ */
+ public Subscription( Class a_type, Filter a_filter,
+ Subscriber a_subscriber )
+ {
+ m_type = a_type ;
+ m_filter = a_filter ;
+ m_subscriber = a_subscriber ;
+ }
+
+
+ /**
+ * Get the event class/type
+ *
+ * @return the event class/type
+ */
+ public Class getType()
+ {
+ return m_type ;
+ }
+
+
+ /**
+ * Get the filter used with this subscription.
+ *
+ * @return The filter
+ */
+ public Filter getFilter()
+ {
+ return m_filter ;
+ }
+
+
+ /**
+ * Get the subscriber.
+ *
+ * @return the subscriber
+ */
+ public Subscriber getSubscriber()
+ {
+ return m_subscriber ;
+ }
+
+
+ /**
+ * Compare two Subscriptions to each other.
+ *
+ * @param a_obj the object to compare this Subscription to
+ * @return <code>true</code> if the two Subscription objects are the same
+ */
+ public boolean equals( Object a_obj )
+ {
+ if ( this == a_obj )
+ {
+ return true ;
+ }
+
+ if ( ! ( a_obj instanceof Subscription ) )
+ {
+ return false ;
+ }
+
+ final Subscription l_subscription = ( Subscription ) a_obj ;
+
+ if ( ! m_type.equals( l_subscription.getType() ) )
+ {
+ return false ;
+ }
+
+ if ( m_filter != null )
+ {
+ if ( l_subscription.getFilter() == null )
+ {
+ return false ;
+ }
+
+ if ( ! m_filter.equals( l_subscription.getFilter() ) )
+ {
+ return false ;
+ }
+ }
+
+ if ( ! m_subscriber.equals( l_subscription.getSubscriber() ) )
+ {
+ return false ;
+ }
+
+ return true ;
+ }
+
+
+ /**
+ * Get the hashcode (used in HashMaps).
+ *
+ * hashCode = 37 * (37 * (629 + event.hashCode()) + filter.hashCode())
+ * + subscriber.hashCode()
+ *
+ * This method was borrowed from Berin Loritsch.
+ *
+ * @return the hashCode value
+ */
+ public int hashCode()
+ {
+ int l_result = 17 ;
+ l_result = 37 * l_result + m_type.hashCode() ;
+ l_result = 37 * l_result +
+ ( m_filter != null ? m_filter.hashCode() : 0 ) ;
+ l_result = 37 * l_result + m_subscriber.hashCode() ;
+ return l_result ;
+ }
+}