[jboss-cvs] JBossAS SVN: r87967 - in projects/bootstrap/trunk: impl-base/src/test/java/org/jboss/bootstrap/impl/base/server and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Apr 28 19:17:27 EDT 2009


Author: ALRubinger
Date: 2009-04-28 19:17:27 -0400 (Tue, 28 Apr 2009)
New Revision: 87967

Modified:
   projects/bootstrap/trunk/impl-base/src/main/java/org/jboss/bootstrap/impl/base/server/AbstractServer.java
   projects/bootstrap/trunk/impl-base/src/test/java/org/jboss/bootstrap/impl/base/server/LifecycleStateUpdatingEventHander.java
   projects/bootstrap/trunk/impl-base/src/test/java/org/jboss/bootstrap/impl/base/server/unit/ServerLifecycleEventCallbackTestCase.java
   projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/lifecycle/LifecycleEventHandler.java
   projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/Server.java
Log:
[JBBOOT-57] API improvements around lifecycle registration

Modified: projects/bootstrap/trunk/impl-base/src/main/java/org/jboss/bootstrap/impl/base/server/AbstractServer.java
===================================================================
--- projects/bootstrap/trunk/impl-base/src/main/java/org/jboss/bootstrap/impl/base/server/AbstractServer.java	2009-04-28 23:11:46 UTC (rev 87966)
+++ projects/bootstrap/trunk/impl-base/src/main/java/org/jboss/bootstrap/impl/base/server/AbstractServer.java	2009-04-28 23:17:27 UTC (rev 87967)
@@ -22,11 +22,13 @@
 
 package org.jboss.bootstrap.impl.base.server;
 
-import java.util.ArrayList;
+import java.util.EnumSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CopyOnWriteArraySet;
 
 import org.jboss.bootstrap.spi.Bootstrap;
 import org.jboss.bootstrap.spi.config.ConfigurationInitializer;
@@ -104,7 +106,7 @@
    /**
     * Event handlers for each lifecycle state change
     */
-   private final Map<LifecycleState, List<LifecycleEventHandler<K, T>>> eventHandlers = new ConcurrentHashMap<LifecycleState, List<LifecycleEventHandler<K, T>>>();
+   private final Map<LifecycleState, Set<LifecycleEventHandler<K, T>>> eventHandlers = new ConcurrentHashMap<LifecycleState, Set<LifecycleEventHandler<K, T>>>();
 
    //-------------------------------------------------------------------------------------||
    // Constructors -----------------------------------------------------------------------||
@@ -477,27 +479,97 @@
    }
 
    /* (non-Javadoc)
-    * @see org.jboss.bootstrap.spi.server.Server#registerEventHandler(org.jboss.bootstrap.spi.lifecycle.LifecycleState, org.jboss.bootstrap.spi.lifecycle.LifecycleEventHandler)
+    * @see org.jboss.bootstrap.spi.server.Server#registerEventHandlers(org.jboss.bootstrap.spi.lifecycle.LifecycleState, org.jboss.bootstrap.spi.lifecycle.LifecycleEventHandler)
     */
+   @SuppressWarnings("unchecked")
    public void registerEventHandler(final LifecycleState state, final LifecycleEventHandler<K, T> handler)
          throws IllegalArgumentException
    {
+      // Delegate
+      this.registerEventHandlers(state, handler);
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.bootstrap.spi.server.Server#registerEventHandler(org.jboss.bootstrap.spi.lifecycle.LifecycleEventHandler, java.util.EnumSet)
+    */
+   public void registerEventHandler(final LifecycleEventHandler<K, T> handler, final EnumSet<LifecycleState> states)
+         throws IllegalArgumentException
+   {
+      // Precondition checks
+      if (handler == null)
+      {
+         throw new IllegalArgumentException("handler is required");
+      }
+      if (states == null)
+      {
+         throw new IllegalArgumentException("states is required");
+      }
+
+      // Delegate
+      for (final LifecycleState state : states)
+      {
+         this.registerEventHandler(state, handler);
+      }
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.bootstrap.spi.server.Server#registerEventHandler(org.jboss.bootstrap.spi.lifecycle.LifecycleEventHandler, org.jboss.bootstrap.spi.lifecycle.LifecycleState[])
+    */
+   public void registerEventHandler(final LifecycleEventHandler<K, T> handler, final LifecycleState... states)
+         throws IllegalArgumentException
+   {
+      // Precondition checks
+      if (handler == null)
+      {
+         throw new IllegalArgumentException("handler is required");
+      }
+      if (states == null)
+      {
+         throw new IllegalArgumentException("states is required");
+      }
+
+      // Delegate
+      for (final LifecycleState state : states)
+      {
+         this.registerEventHandler(state, handler);
+      }
+   }
+
+   /* (non-Javadoc)
+    * @see org.jboss.bootstrap.spi.server.Server#registerEventHandler(org.jboss.bootstrap.spi.lifecycle.LifecycleState, org.jboss.bootstrap.spi.lifecycle.LifecycleEventHandler)
+    */
+   public void registerEventHandlers(final LifecycleState state, final LifecycleEventHandler<K, T>... handlers)
+         throws IllegalArgumentException
+   {
+      // Precondition checks
+      if (handlers == null)
+      {
+         throw new IllegalArgumentException("At least one handler is required");
+      }
+      if (state == null)
+      {
+         throw new IllegalArgumentException("state is required");
+      }
+
       // Get existing handlers for this state change
-      final List<LifecycleEventHandler<K, T>> handlers = this.getHandlersForEvent(state);
+      final Set<LifecycleEventHandler<K, T>> handlersForEvent = this.getHandlersForEvent(state);
 
       // Add the state change
-      handlers.add(handler);
-      log.debug("Added lifecycle handler " + handler + " to fire upon state change to " + state + " for " + this);
+      for (final LifecycleEventHandler<K, T> handler : handlers)
+      {
+         handlersForEvent.add(handler);
+         log.debug("Added lifecycle handler " + handler + " to fire upon state change to " + state + " for " + this);
+      }
    }
 
    /* (non-Javadoc)
-    * @see org.jboss.bootstrap.spi.server.Server#unregisterEventHandler(org.jboss.bootstrap.spi.lifecycle.LifecycleState, org.jboss.bootstrap.spi.lifecycle.LifecycleEventHandler)
+    * @see org.jboss.bootstrap.spi.server.Server#unregisterEventHandler(org.jboss.bootstrap.spi.lifecycle.LifecycleEventHandler,org.jboss.bootstrap.spi.lifecycle.LifecycleState)
     */
-   public boolean unregisterEventHandler(final LifecycleState state, final LifecycleEventHandler<K, T> handler)
+   public boolean unregisterEventHandler(final LifecycleEventHandler<K, T> handler, final LifecycleState state)
          throws IllegalArgumentException
    {
       // Get all handlers for this state change
-      final List<LifecycleEventHandler<K, T>> handlers = this.getHandlersForEvent(state);
+      final Set<LifecycleEventHandler<K, T>> handlers = this.getHandlersForEvent(state);
 
       // Remove and return 
       final boolean removed = handlers.remove(handler);
@@ -595,17 +667,22 @@
     * @return
     * @throws IllegalArgumentException If the state was not specified
     */
-   private List<LifecycleEventHandler<K, T>> getHandlersForEvent(final LifecycleState state)
+   private Set<LifecycleEventHandler<K, T>> getHandlersForEvent(final LifecycleState state)
          throws IllegalArgumentException
    {
+      // Precondition check
+      if (state == null)
+      {
+         throw new IllegalArgumentException("state is required");
+      }
 
       // Initialize
-      List<LifecycleEventHandler<K, T>> handlers = this.eventHandlers.get(state);
+      Set<LifecycleEventHandler<K, T>> handlers = this.eventHandlers.get(state);
 
       // Adjust to empty List if null
       if (handlers == null)
       {
-         handlers = new ArrayList<LifecycleEventHandler<K, T>>();
+         handlers = new CopyOnWriteArraySet<LifecycleEventHandler<K, T>>();
          // Put this new list into the backing Map to prevent null access
          this.eventHandlers.put(state, handlers);
          if (log.isTraceEnabled())
@@ -715,7 +792,7 @@
       this.state = state;
 
       // Fire handlers registered for event changes to this state
-      final List<LifecycleEventHandler<K, T>> handlers = this.getHandlersForEvent(state);
+      final Set<LifecycleEventHandler<K, T>> handlers = this.getHandlersForEvent(state);
       for (final LifecycleEventHandler<K, T> handler : handlers)
       {
          try
@@ -727,7 +804,7 @@
             }
 
             // Fire
-            handler.handleEvent(this, state);
+            handler.handleEvent(state);
          }
          catch (final LifecycleEventException t)
          {

Modified: projects/bootstrap/trunk/impl-base/src/test/java/org/jboss/bootstrap/impl/base/server/LifecycleStateUpdatingEventHander.java
===================================================================
--- projects/bootstrap/trunk/impl-base/src/test/java/org/jboss/bootstrap/impl/base/server/LifecycleStateUpdatingEventHander.java	2009-04-28 23:11:46 UTC (rev 87966)
+++ projects/bootstrap/trunk/impl-base/src/test/java/org/jboss/bootstrap/impl/base/server/LifecycleStateUpdatingEventHander.java	2009-04-28 23:17:27 UTC (rev 87967)
@@ -26,7 +26,6 @@
 import org.jboss.bootstrap.spi.lifecycle.LifecycleEventException;
 import org.jboss.bootstrap.spi.lifecycle.LifecycleEventHandler;
 import org.jboss.bootstrap.spi.lifecycle.LifecycleState;
-import org.jboss.bootstrap.spi.server.Server;
 import org.jboss.logging.Logger;
 
 /**
@@ -70,8 +69,7 @@
    /* (non-Javadoc)
     * @see org.jboss.bootstrap.spi.lifecycle.LifecycleEventHandler#handleEvent(org.jboss.bootstrap.spi.server.Server, org.jboss.bootstrap.spi.lifecycle.LifecycleState)
     */
-   public void handleEvent(Server<TestNoOpServer, TestServerConfig> server, LifecycleState state)
-         throws LifecycleEventException
+   public void handleEvent(LifecycleState state) throws LifecycleEventException
    {
       // Set state
       log.info("Setting state: " + state);

Modified: projects/bootstrap/trunk/impl-base/src/test/java/org/jboss/bootstrap/impl/base/server/unit/ServerLifecycleEventCallbackTestCase.java
===================================================================
--- projects/bootstrap/trunk/impl-base/src/test/java/org/jboss/bootstrap/impl/base/server/unit/ServerLifecycleEventCallbackTestCase.java	2009-04-28 23:11:46 UTC (rev 87966)
+++ projects/bootstrap/trunk/impl-base/src/test/java/org/jboss/bootstrap/impl/base/server/unit/ServerLifecycleEventCallbackTestCase.java	2009-04-28 23:17:27 UTC (rev 87967)
@@ -126,8 +126,7 @@
       log.info("testIdleCallback");
 
       // Register the handler with events
-      server.registerEventHandler(IDLE, handler);
-      server.registerEventHandler(STARTED, handler);
+      server.registerEventHandler(handler, IDLE, STARTED);
 
       // Test no initial state
       TestCase.assertNull(tracker.getState());
@@ -151,4 +150,39 @@
       TestCase.assertEquals(FAIL_MESSAGE, IDLE, tracker.getState());
    }
 
+   /**
+    * Ensures that callbacks unregistered do not fire
+    */
+   @Test
+   public void testLifecycleUnregistration() throws Throwable
+   {
+      // Log
+      log.info("testLifecycleUnregistration");
+
+      // Register the handler with events
+      server.registerEventHandler(handler, IDLE, STARTED);
+
+      // Test no initial state
+      TestCase.assertNull(tracker.getState());
+
+      // Init the server
+      server.initialize();
+
+      // Test for idle
+      TestCase.assertEquals(FAIL_MESSAGE, IDLE, tracker.getState());
+
+      // Unregister the STARTED callback
+      final boolean wasUnregistered = server.unregisterEventHandler(handler, STARTED);
+      TestCase.assertTrue("The server reports that the callback was not unregistered", wasUnregistered);
+
+      // Start server
+      server.start();
+
+      // Test that state is still "IDLE" (as we've cancelled the callback)
+      TestCase.assertEquals(FAIL_MESSAGE, IDLE, tracker.getState());
+
+      // Shutdown server to clean up
+      server.shutdown();
+   }
+
 }

Modified: projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/lifecycle/LifecycleEventHandler.java
===================================================================
--- projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/lifecycle/LifecycleEventHandler.java	2009-04-28 23:11:46 UTC (rev 87966)
+++ projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/lifecycle/LifecycleEventHandler.java	2009-04-28 23:17:27 UTC (rev 87967)
@@ -48,5 +48,5 @@
     * @param state The new state
     * @throws LifecycleEventException If an error was encounterd in processing
     */
-   void handleEvent(Server<K, T> server, LifecycleState state) throws LifecycleEventException;
+   void handleEvent(LifecycleState state) throws LifecycleEventException;
 }

Modified: projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/Server.java
===================================================================
--- projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/Server.java	2009-04-28 23:11:46 UTC (rev 87966)
+++ projects/bootstrap/trunk/spi/src/main/java/org/jboss/bootstrap/spi/server/Server.java	2009-04-28 23:17:27 UTC (rev 87967)
@@ -22,6 +22,8 @@
 
 package org.jboss.bootstrap.spi.server;
 
+import java.util.EnumSet;
+
 import org.jboss.bootstrap.spi.Bootstrap;
 import org.jboss.bootstrap.spi.config.ConfigurationInitializer;
 import org.jboss.bootstrap.spi.config.ConfigurationValidator;
@@ -129,7 +131,7 @@
 
    /**
     * Registers the specified handler to fire when 
-    * the server's state enters that of the specified state
+    * the server's state enters that of the specified state.
     * 
     * @param state
     * @param handler
@@ -138,7 +140,40 @@
    void registerEventHandler(LifecycleState state, LifecycleEventHandler<K, T> handler) throws IllegalArgumentException;
 
    /**
-    * Unregisters one instance of the specified event handler from firing 
+    * Registers the specified handler to fire when 
+    * the server's state enters one of the the specified states
+    * 
+    * @param handler
+    * @param states
+    * @throws IllegalArgumentException If either the states or the handler are unspecified
+    */
+   void registerEventHandler(LifecycleEventHandler<K, T> handler, EnumSet<LifecycleState> states)
+         throws IllegalArgumentException;
+
+   /**
+    * Registers the specified handler to fire when 
+    * the server's state enters one of the the specified states
+    * 
+    * @param handler
+    * @param states
+    * @throws IllegalArgumentException If either the states or the handler are unspecified
+    */
+   void registerEventHandler(LifecycleEventHandler<K, T> handler, LifecycleState... states)
+         throws IllegalArgumentException;
+
+   /**
+    * Registers the specified handlers to fire when 
+    * the server's state enters that of the specified state.
+    * 
+    * @param state
+    * @param handlers
+    * @throws IllegalArgumentException If either the state or the handlers are unspecified
+    */
+   void registerEventHandlers(LifecycleState state, LifecycleEventHandler<K, T>... handlers)
+         throws IllegalArgumentException;
+
+   /**
+    * Unregisters the specified event handler from firing 
     * when the server state changes to the specified state.
     * 
     * @param state
@@ -146,7 +181,7 @@
     * @return Whether or not the handler was removed (ie. false if not registered)
     * @throws IllegalArgumentException If either the state or handler are unspecified
     */
-   boolean unregisterEventHandler(LifecycleState state, LifecycleEventHandler<K, T> handler)
+   boolean unregisterEventHandler(LifecycleEventHandler<K, T> handler, LifecycleState state)
          throws IllegalArgumentException;
 
    /**




More information about the jboss-cvs-commits mailing list