maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ogusa...@apache.org
Subject svn commit: r719683 - in /maven/mercury/trunk/mercury-util/src: main/java/org/apache/maven/mercury/util/event/ test/java/org/apache/maven/mercury/util/event/
Date Fri, 21 Nov 2008 19:28:11 GMT
Author: ogusakov
Date: Fri Nov 21 11:28:11 2008
New Revision: 719683

URL: http://svn.apache.org/viewvc?rev=719683&view=rev
Log: (empty)

Added:
    maven/mercury/trunk/mercury-util/src/test/java/org/apache/maven/mercury/util/event/EventFrameworkTest.java
Modified:
    maven/mercury/trunk/mercury-util/src/main/java/org/apache/maven/mercury/util/event/EventManager.java
    maven/mercury/trunk/mercury-util/src/main/java/org/apache/maven/mercury/util/event/MercuryEvent.java

Modified: maven/mercury/trunk/mercury-util/src/main/java/org/apache/maven/mercury/util/event/EventManager.java
URL: http://svn.apache.org/viewvc/maven/mercury/trunk/mercury-util/src/main/java/org/apache/maven/mercury/util/event/EventManager.java?rev=719683&r1=719682&r2=719683&view=diff
==============================================================================
--- maven/mercury/trunk/mercury-util/src/main/java/org/apache/maven/mercury/util/event/EventManager.java
(original)
+++ maven/mercury/trunk/mercury-util/src/main/java/org/apache/maven/mercury/util/event/EventManager.java
Fri Nov 21 11:28:11 2008
@@ -22,27 +22,73 @@
  */
 public class EventManager
 {
+  public static final int THREAD_COUNT = 4;
+
+  /**
+   * this property may contain comma separated list of bit numbers defined in MercuryEvent.EventTypeEnum.
It supersedes 
+   * any bits set by the appropriate EventManager constructor by OR operation with those
+   */
+  public static final String SYSTEM_PROPERTY_EVENT_MASK = "maven.mercury.events";
+  public static final String systemPropertyEventMask = System.getProperty( SYSTEM_PROPERTY_EVENT_MASK,
null );
+
   private static final IMercuryLogger _log = MercuryLoggerManager.getLogger( EventManager.class
);
   private static final Language _lang = new DefaultLanguage( EventManager.class );
   
-  public static final int THREAD_COUNT = 4;
-  
   final List<MercuryEventListener> _listeners = new ArrayList<MercuryEventListener>(8);
   
   final LinkedBlockingQueue<UnitOfWork> _queue = new LinkedBlockingQueue<UnitOfWork>(
512 );
   
-  ExecutorService execService;
+  private ExecutorService _execService;
   
+  private MercuryEvent.EventMask _eventMask;
+  
+  /**
+   * default initialization - create thread pool
+   */
   public EventManager()
   {
-    execService = Executors.newFixedThreadPool( THREAD_COUNT );
+    _execService = Executors.newFixedThreadPool( THREAD_COUNT );
+
     for( int i = 0; i < THREAD_COUNT; i++ )
-      execService.execute( new Runner( _queue ) );
+      _execService.execute( new Runner( _queue ) );
+    
+    processSystemOptions();
+  }
+
+  /**
+   * default initialization - create thread pool
+   */
+  public EventManager( MercuryEvent.EventMask eventMask )
+  {
+    this();
+    
+    this._eventMask = eventMask;
+    
+    processSystemOptions();
+  }
+  
+  private final void processSystemOptions()
+  {
+    if( systemPropertyEventMask == null )
+      return;
+    
+    if( _eventMask == null )
+      _eventMask = new MercuryEvent.EventMask( systemPropertyEventMask );
+    else
+      _eventMask.setBits( systemPropertyEventMask );
   }
   
+  /**
+   * add listener only if it meets the criteria
+   * 
+   * @param listener
+   */
   public void register( MercuryEventListener listener )
   {
-    _listeners.add( listener );
+    MercuryEvent.EventMask lMask = listener.getMask();
+    
+    if( lMask == null || _eventMask == null || _eventMask.intersects( lMask ) )
+      _listeners.add( listener );
   }
   
   public void unRegister( MercuryEventListener listener )
@@ -85,7 +131,19 @@
     {
       try
       {
-        listener.fire( event );
+        MercuryEvent.EventMask lMask = listener.getMask();
+        
+        if( _eventMask != null )
+        {
+          if( lMask == null )
+            lMask = _eventMask;
+          else 
+            lMask.and( _eventMask );
+        }
+        
+        
+        if( lMask == null || lMask.get( event.getType().bitNo ) )
+          listener.fire( event );
       }
       catch( Throwable th )
       {

Modified: maven/mercury/trunk/mercury-util/src/main/java/org/apache/maven/mercury/util/event/MercuryEvent.java
URL: http://svn.apache.org/viewvc/maven/mercury/trunk/mercury-util/src/main/java/org/apache/maven/mercury/util/event/MercuryEvent.java?rev=719683&r1=719682&r2=719683&view=diff
==============================================================================
--- maven/mercury/trunk/mercury-util/src/main/java/org/apache/maven/mercury/util/event/MercuryEvent.java
(original)
+++ maven/mercury/trunk/mercury-util/src/main/java/org/apache/maven/mercury/util/event/MercuryEvent.java
Fri Nov 21 11:28:11 2008
@@ -2,6 +2,7 @@
 
 import java.util.BitSet;
 import java.util.Map;
+import java.util.StringTokenizer;
 
 /**
  * 
@@ -50,6 +51,30 @@
       for( EventTypeEnum bit : bits )
         set( bit.bitNo );
     }
+
+    public EventMask( String bits )
+    {
+      super();
+      
+      setBits( bits );
+    }
+    
+    public final void setBits( String bits )
+    {
+      if( bits == null )
+        return;
+      
+      StringTokenizer st = new StringTokenizer( bits, ",");
+      
+      while( st.hasMoreTokens() )
+      {
+        String bit = st.nextToken();
+        
+        int bitNo = Integer.valueOf( bit );
+        
+        set( bitNo, true );
+      }
+    }
   }
 
   /**

Added: maven/mercury/trunk/mercury-util/src/test/java/org/apache/maven/mercury/util/event/EventFrameworkTest.java
URL: http://svn.apache.org/viewvc/maven/mercury/trunk/mercury-util/src/test/java/org/apache/maven/mercury/util/event/EventFrameworkTest.java?rev=719683&view=auto
==============================================================================
--- maven/mercury/trunk/mercury-util/src/test/java/org/apache/maven/mercury/util/event/EventFrameworkTest.java
(added)
+++ maven/mercury/trunk/mercury-util/src/test/java/org/apache/maven/mercury/util/event/EventFrameworkTest.java
Fri Nov 21 11:28:11 2008
@@ -0,0 +1,233 @@
+package org.apache.maven.mercury.util.event;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+import junit.framework.TestCase;
+
+import org.apache.maven.mercury.util.event.MercuryEvent.EventMask;
+
+/**
+ *
+ *
+ * @author Oleg Gusakov
+ * @version $Id$
+ *
+ */
+public class EventFrameworkTest
+extends TestCase
+{
+  static final int THREAD_COUNT = 5;
+  static final int EVENT_COUNT  = 10;
+  
+  ExecutorService es;
+  
+  EventManager em;
+  
+  Listener listener;
+  
+  @Override
+  protected void setUp()
+  throws Exception
+  {
+    es = Executors.newFixedThreadPool( THREAD_COUNT );
+  }
+  public void testListenAllEvents()
+  throws InterruptedException
+  {
+    em = new EventManager();
+    
+    // testing this one - receive all events
+    listener = new Listener( null );
+    
+    em.register( listener );
+
+    for( int i=0; i<THREAD_COUNT; i++ )
+    {
+      es.execute( new Generator( em, MercuryEvent.EventTypeEnum.localRepository, ""+i ) );
+    }
+
+    for( int i=0; i<THREAD_COUNT; i++ )
+    {
+      es.execute( new Generator( em, MercuryEvent.EventTypeEnum.remoteRepository, ""+i )
);
+    }
+    
+    es.awaitTermination( 2, TimeUnit.SECONDS );
+    
+    assertEquals( THREAD_COUNT * EventFrameworkTest.EVENT_COUNT, listener.localRepoCount
);
+    assertEquals( THREAD_COUNT * EventFrameworkTest.EVENT_COUNT, listener.remoteRepoCount
);
+  }
+
+  public void testListenMaskedListenerEvents()
+  throws InterruptedException
+  {
+    em = new EventManager();
+    
+    // test this - receive only local repo events
+    listener = new Listener( new MercuryEvent.EventMask(MercuryEvent.EventTypeEnum.localRepository)
);
+    
+    em.register( listener );
+
+    for( int i=0; i<THREAD_COUNT; i++ )
+    {
+      es.execute( new Generator( em, MercuryEvent.EventTypeEnum.localRepository, ""+i ) );
+    }
+
+    for( int i=0; i<THREAD_COUNT; i++ )
+    {
+      es.execute( new Generator( em, MercuryEvent.EventTypeEnum.remoteRepository, ""+i )
);
+    }
+    
+    es.awaitTermination( 2, TimeUnit.SECONDS );
+    
+    assertEquals( THREAD_COUNT * EventFrameworkTest.EVENT_COUNT, listener.localRepoCount
);
+    assertEquals( 0, listener.remoteRepoCount );
+  }
+
+  public void testListenMaskedManagerEvents()
+  throws InterruptedException
+  {
+    // test this - propagate only remote repo events
+    em = new EventManager( new MercuryEvent.EventMask(MercuryEvent.EventTypeEnum.remoteRepository)
);
+    
+    // listen to all
+    listener = new Listener( null );
+    
+    em.register( listener );
+
+    for( int i=0; i<THREAD_COUNT; i++ )
+    {
+      es.execute( new Generator( em, MercuryEvent.EventTypeEnum.localRepository, ""+i ) );
+    }
+
+    for( int i=0; i<THREAD_COUNT; i++ )
+    {
+      es.execute( new Generator( em, MercuryEvent.EventTypeEnum.remoteRepository, ""+i )
);
+    }
+    
+    es.awaitTermination( 2, TimeUnit.SECONDS );
+    
+    assertEquals( 0, listener.localRepoCount );
+    assertEquals( THREAD_COUNT * EventFrameworkTest.EVENT_COUNT, listener.remoteRepoCount
);
+  }
+
+  public void testListenMismatchedMaskEvents()
+  throws InterruptedException
+  {
+    // test this - propagate only remote repo events
+    em = new EventManager( new MercuryEvent.EventMask(MercuryEvent.EventTypeEnum.remoteRepository)
);
+    
+    // and this - listen only to local events
+    listener = new Listener( new MercuryEvent.EventMask(MercuryEvent.EventTypeEnum.localRepository)
);
+    
+    em.register( listener );
+
+    for( int i=0; i<THREAD_COUNT; i++ )
+    {
+      es.execute( new Generator( em, MercuryEvent.EventTypeEnum.localRepository, ""+i ) );
+    }
+
+    for( int i=0; i<THREAD_COUNT; i++ )
+    {
+      es.execute( new Generator( em, MercuryEvent.EventTypeEnum.remoteRepository, ""+i )
);
+    }
+    
+    es.awaitTermination( 2, TimeUnit.SECONDS );
+    
+    assertEquals( 0, listener.localRepoCount );
+    assertEquals( 0, listener.remoteRepoCount );
+  }
+}
+
+//=====================  helper classes  =====================
+class Listener
+implements MercuryEventListener
+{
+  MercuryEvent.EventMask _mask;
+  
+  int localRepoCount = 0;
+  
+  int remoteRepoCount = 0;
+  
+  public Listener( MercuryEvent.EventMask mask )
+  {
+    _mask = mask;
+  }
+
+  public void fire( MercuryEvent event )
+  {
+    System.out.println( EventManager.toString( event ) );
+    System.out.flush();
+    
+    if( event.getType().equals( MercuryEvent.EventTypeEnum.localRepository ) )
+      ++localRepoCount;
+    else
+      if( event.getType().equals( MercuryEvent.EventTypeEnum.remoteRepository ) )
+        ++remoteRepoCount;
+  }
+
+  public EventMask getMask()
+  {
+    return _mask;
+  }
+  
+}
+
+class Generator
+implements Runnable, EventGenerator
+{
+  
+  EventManager _eventManager;
+  
+  String _msg;
+  
+  MercuryEvent.EventTypeEnum _myType;
+  
+  public Generator( EventManager em, MercuryEvent.EventTypeEnum type, String msg  )
+  {
+    _eventManager = em;
+    _msg = msg;
+    _myType = type;
+  }
+  
+  public void run()
+  {
+    for( int i=0; i< EventFrameworkTest.EVENT_COUNT; i++ )
+      try
+      {
+        GenericEvent event = new GenericEvent( _myType, _msg );
+        Thread.sleep( (int)(100.0*Math.random()) );
+        event.stop();
+        _eventManager.fireEvent( event );
+      }
+      catch( InterruptedException e )
+      {
+        return;
+      }
+  }
+
+  public void register( MercuryEventListener listener )
+  {
+    if( _eventManager == null )
+      _eventManager = new EventManager();
+      
+    _eventManager.register( listener );
+  }
+
+  public void unRegister( MercuryEventListener listener )
+  {
+    if( _eventManager != null )
+      _eventManager.unRegister( listener );
+  }
+  
+  public void setEventManager( EventManager eventManager )
+  {
+    if( _eventManager == null )
+      _eventManager = eventManager;
+    else
+      _eventManager.getListeners().addAll( eventManager.getListeners() );
+      
+  }
+}
+



Mime
View raw message