onami-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject svn commit: r1454750 - /incubator/onami/trunk/lifecycle/core/main/java/org/apache/onami/lifecycle/core/DefaultStager.java
Date Sat, 09 Mar 2013 18:17:27 GMT
Author: randgalt
Date: Sat Mar  9 18:17:26 2013
New Revision: 1454750

URL: http://svn.apache.org/r1454750
Log:
1. Use add() instead of offer()
2. Use switch statement in DefaultStager
3. Make DefaultStager constructor protected
4. Better synchronization in stage()

Modified:
    incubator/onami/trunk/lifecycle/core/main/java/org/apache/onami/lifecycle/core/DefaultStager.java

Modified: incubator/onami/trunk/lifecycle/core/main/java/org/apache/onami/lifecycle/core/DefaultStager.java
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/lifecycle/core/main/java/org/apache/onami/lifecycle/core/DefaultStager.java?rev=1454750&r1=1454749&r2=1454750&view=diff
==============================================================================
--- incubator/onami/trunk/lifecycle/core/main/java/org/apache/onami/lifecycle/core/DefaultStager.java
(original)
+++ incubator/onami/trunk/lifecycle/core/main/java/org/apache/onami/lifecycle/core/DefaultStager.java
Sat Mar  9 18:17:26 2013
@@ -20,8 +20,10 @@ package org.apache.onami.lifecycle.core;
  */
 
 import java.lang.annotation.Annotation;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Queue;
 
 /**
@@ -41,27 +43,51 @@ public class DefaultStager<A extends Ann
      * @param stage the annotation that specifies this stage
      * @param mode  execution order
      */
-    public DefaultStager( Class<A> stage, Order mode )
+    protected DefaultStager( Class<A> stage, Order mode )
     {
         this.stage = stage;
 
         Queue<Stageable> localStageables;
-        if ( mode == Order.FIRST_IN_LAST_OUT )
+        switch ( mode )
         {
-            localStageables = Collections.asLifoQueue( new LinkedList<Stageable>()
);
-        }
-        else
-        {
-            localStageables = new LinkedList<Stageable>();
+            case FIRST_IN_FIRST_OUT:
+            {
+                localStageables = new LinkedList<Stageable>();
+                break;
+            }
+
+            case FIRST_IN_LAST_OUT:
+            {
+                localStageables = Collections.asLifoQueue( new LinkedList<Stageable>()
);
+                break;
+            }
+
+            default:
+            {
+                throw new IllegalArgumentException( "Unknown mode: " + mode );
+            }
         }
         stageables = localStageables;
     }
 
+    /**
+     * Allocate a new stager
+     *
+     * @param stage the stage annotation
+     * @return stager
+     */
     public static <A extends Annotation> Stager<A> newStager( Class<A>
stage )
     {
         return newStager( stage, Order.FIRST_IN_FIRST_OUT );
     }
 
+    /**
+     * Allocate a new stager
+     *
+     * @param stage the stage annotation
+     * @param mode  execution order
+     * @return stager
+     */
     public static <A extends Annotation> Stager<A> newStager( Class<A>
stage, Order mode )
     {
         return new DefaultStager<A>( stage, mode );
@@ -72,7 +98,7 @@ public class DefaultStager<A extends Ann
      */
     public synchronized void register( Stageable stageable )
     {
-        stageables.offer( stageable );
+        stageables.add( stageable );
     }
 
     /**
@@ -86,16 +112,23 @@ public class DefaultStager<A extends Ann
     /**
      * {@inheritDoc}
      */
-    public synchronized void stage( StageHandler stageHandler )
+    public void stage( StageHandler stageHandler )
     {
         if ( stageHandler == null )
         {
             stageHandler = new NoOpStageHandler();
         }
 
-        while ( !stageables.isEmpty() )
+        List<Stageable> localStageables;
+        synchronized ( this )
+        {
+            localStageables = new ArrayList<Stageable>(stageables);
+            stageables.clear();
+        }
+
+        for ( Stageable stageable : localStageables )
         {
-            stageables.remove().stage( stageHandler );
+            stageable.stage( stageHandler );
         }
     }
 



Mime
View raw message