mina-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ber...@apache.org
Subject svn commit: r1185067 - in /mina/vysper/trunk/server/core/src: main/java/org/apache/vysper/spring/ main/java/org/apache/vysper/xmpp/delivery/ main/java/org/apache/vysper/xmpp/delivery/inbound/ main/java/org/apache/vysper/xmpp/server/ test/java/org/apach...
Date Mon, 17 Oct 2011 09:47:43 GMT
Author: berndf
Date: Mon Oct 17 09:47:42 2011
New Revision: 1185067

URL: http://svn.apache.org/viewvc?rev=1185067&view=rev
Log:
VYSPER-299: introduce stop() method to StanzaRelay to be able to properly shutdown resources
(like executor services). Patch based on code by Mike Mahoney. Thanks for opening the issue
and contributing!

Modified:
    mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/spring/SpringCompatibleDefaultServerRuntimeContext.java
    mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/RecordingStanzaRelay.java
    mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaReceiverRelay.java
    mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaRelay.java
    mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaRelayBroker.java
    mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringExternalInboundStanzaRelay.java
    mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringInternalInboundStanzaRelay.java
    mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/server/XMPPServer.java
    mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/StanzaRelayBrokerTestCase.java
    mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringExternalInboundStanzaRelayTestCase.java
    mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringInteralInboundStanzaRelayTestCase.java

Modified: mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/spring/SpringCompatibleDefaultServerRuntimeContext.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/spring/SpringCompatibleDefaultServerRuntimeContext.java?rev=1185067&r1=1185066&r2=1185067&view=diff
==============================================================================
--- mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/spring/SpringCompatibleDefaultServerRuntimeContext.java
(original)
+++ mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/spring/SpringCompatibleDefaultServerRuntimeContext.java
Mon Oct 17 09:47:42 2011
@@ -25,6 +25,7 @@ import org.apache.vysper.xmpp.addressing
 import org.apache.vysper.xmpp.delivery.StanzaRelay;
 import org.apache.vysper.xmpp.delivery.failure.DeliveryException;
 import org.apache.vysper.xmpp.delivery.failure.DeliveryFailureStrategy;
+import org.apache.vysper.xmpp.delivery.failure.ServiceNotAvailableException;
 import org.apache.vysper.xmpp.modules.Module;
 import org.apache.vysper.xmpp.protocol.HandlerDictionary;
 import org.apache.vysper.xmpp.server.DefaultServerRuntimeContext;
@@ -46,8 +47,19 @@ public class SpringCompatibleDefaultServ
 
         public void relay(Entity receiver, Stanza stanza, DeliveryFailureStrategy deliveryFailureStrategy)
                 throws DeliveryException {
+            if (!isRelaying()) {
+                throw new ServiceNotAvailableException("relay is not relaying");
+            }
             stanzaRelay.relay(receiver, stanza, deliveryFailureStrategy);
         }
+
+        public boolean isRelaying() {
+            return stanzaRelay.isRelaying();
+        }
+
+        public void stop() {
+            stanzaRelay.stop();
+        }
     }
 
     public SpringCompatibleDefaultServerRuntimeContext(Entity serverEntity, ServerFeatures
serverFeatures,

Modified: mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/RecordingStanzaRelay.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/RecordingStanzaRelay.java?rev=1185067&r1=1185066&r2=1185067&view=diff
==============================================================================
--- mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/RecordingStanzaRelay.java
(original)
+++ mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/RecordingStanzaRelay.java
Mon Oct 17 09:47:42 2011
@@ -21,10 +21,12 @@ package org.apache.vysper.xmpp.delivery;
 
 import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.vysper.xmpp.addressing.Entity;
 import org.apache.vysper.xmpp.delivery.failure.DeliveryException;
 import org.apache.vysper.xmpp.delivery.failure.DeliveryFailureStrategy;
+import org.apache.vysper.xmpp.delivery.failure.ServiceNotAvailableException;
 import org.apache.vysper.xmpp.stanza.Stanza;
 
 /**
@@ -38,15 +40,22 @@ public class RecordingStanzaRelay implem
 
     private final ArrayList<Triple> entityStanzaPairs = new ArrayList<Triple>();
 
-    private boolean acceptingMode = true;
+    protected final AtomicBoolean isRelaying = new AtomicBoolean(true);
 
     public void relay(Entity receiver, Stanza stanza, DeliveryFailureStrategy deliveryFailureStrategy)
             throws DeliveryException {
-        if (!acceptingMode)
-            return;
+        if (!isRelaying()) throw new ServiceNotAvailableException("recording stanza relay
is not relaying");
         entityStanzaPairs.add(new Triple(receiver, stanza, deliveryFailureStrategy));
     }
 
+    public boolean isRelaying() {
+        return isRelaying.get();
+    }
+
+    public void stop() {
+        this.isRelaying.set(false);
+    }
+
     public Iterator<Triple> iterator() {
         return entityStanzaPairs.iterator();
     }
@@ -59,8 +68,8 @@ public class RecordingStanzaRelay implem
      * to easily set mode to accept all receivers (default) or to decline all
      * this is useful for setting the behavior when testing
      */
-    public void setAcceptingMode(boolean accepting) {
-        this.acceptingMode = accepting;
+    public void setRelaying(boolean accepting) {
+        this.isRelaying.set(accepting);
     }
 
     public static class Triple {

Modified: mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaReceiverRelay.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaReceiverRelay.java?rev=1185067&r1=1185066&r2=1185067&view=diff
==============================================================================
--- mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaReceiverRelay.java
(original)
+++ mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaReceiverRelay.java
Mon Oct 17 09:47:42 2011
@@ -21,11 +21,13 @@ package org.apache.vysper.xmpp.delivery;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.vysper.xmpp.addressing.Entity;
 import org.apache.vysper.xmpp.delivery.failure.DeliveryException;
 import org.apache.vysper.xmpp.delivery.failure.DeliveryFailureStrategy;
 import org.apache.vysper.xmpp.delivery.failure.LocalRecipientOfflineException;
+import org.apache.vysper.xmpp.delivery.failure.ServiceNotAvailableException;
 import org.apache.vysper.xmpp.server.ServerRuntimeContext;
 import org.apache.vysper.xmpp.stanza.Stanza;
 
@@ -46,7 +48,9 @@ public class StanzaReceiverRelay impleme
     private int countFailed = 0;
 
     private int countDelivered = 0;
-
+    
+    private final AtomicBoolean acceptingMode = new AtomicBoolean(true);
+    
     public void setServerRuntimeContext(ServerRuntimeContext serverRuntimeContext) {
         this.serverRuntimeContext = serverRuntimeContext;
     }
@@ -60,6 +64,9 @@ public class StanzaReceiverRelay impleme
 
     public void relay(Entity receiver, Stanza stanza, DeliveryFailureStrategy deliveryFailureStrategy)
             throws DeliveryException {
+        if (!isRelaying()) {
+            throw new ServiceNotAvailableException("relay is not relaying");
+        }
         countRelayed++;
         if (receiver == null)
             throw new DeliveryException("receiver cannot be NULL");
@@ -78,6 +85,14 @@ public class StanzaReceiverRelay impleme
         receiverMap.get(receiver).deliver(stanza);
     }
 
+    public boolean isRelaying() {
+        return acceptingMode.get();
+    }
+
+    public void stop() {
+        acceptingMode.set(false);
+    }
+
     public int getCountRelayed() {
         return countRelayed;
     }

Modified: mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaRelay.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaRelay.java?rev=1185067&r1=1185066&r2=1185067&view=diff
==============================================================================
--- mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaRelay.java
(original)
+++ mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaRelay.java
Mon Oct 17 09:47:42 2011
@@ -37,9 +37,19 @@ public interface StanzaRelay {
      * @param receiver the stanza receiver
      * @param stanza the payload
      * @param deliveryFailureStrategy what to do in case of errors
-     * @throws DeliveryException relaying failed
+     * @throws ServiceNotAvailableException relaying is stopped
+     * @throws DeliveryException error while relaying
      */
     public void relay(Entity receiver, Stanza stanza, DeliveryFailureStrategy deliveryFailureStrategy)
             throws DeliveryException;
 
+    /**
+     * @return TRUE iff the relay is live (started and not stopped)
+     */
+    public boolean isRelaying();
+    
+    /**
+     * Shutdown this relay and prevent it from accepting any further stanzas.
+     */
+    public void stop();
 }

Modified: mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaRelayBroker.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaRelayBroker.java?rev=1185067&r1=1185066&r2=1185067&view=diff
==============================================================================
--- mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaRelayBroker.java
(original)
+++ mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/StanzaRelayBroker.java
Mon Oct 17 09:47:42 2011
@@ -23,9 +23,12 @@ import org.apache.vysper.xmpp.addressing
 import org.apache.vysper.xmpp.addressing.EntityUtils;
 import org.apache.vysper.xmpp.delivery.failure.DeliveryException;
 import org.apache.vysper.xmpp.delivery.failure.DeliveryFailureStrategy;
+import org.apache.vysper.xmpp.delivery.failure.ServiceNotAvailableException;
 import org.apache.vysper.xmpp.server.ServerRuntimeContext;
 import org.apache.vysper.xmpp.stanza.Stanza;
 
+import java.util.concurrent.atomic.AtomicBoolean;
+
 /**
  * central component for delivering stanzas.
  * handles message by routing to another server/domain or delivers locally.
@@ -40,6 +43,8 @@ public class StanzaRelayBroker implement
     protected StanzaRelay externalRelay;
 
     protected ServerRuntimeContext serverRuntimeContext;
+    
+    protected final AtomicBoolean isRelaying = new AtomicBoolean(true);
 
     /**
      * a StanzaRelay receiving stanzas which are targeted to server-local JIDs
@@ -61,6 +66,10 @@ public class StanzaRelayBroker implement
 
     public void relay(Entity receiver, Stanza stanza, DeliveryFailureStrategy deliveryFailureStrategy)
             throws DeliveryException {
+        
+        if (!isRelaying()) {
+            throw new ServiceNotAvailableException("relay is not relaying");
+        }
 
         boolean toServerTLD = receiver == null
                 || (!receiver.isNodeSet() && EntityUtils.isAddressingServer(receiver,
serverRuntimeContext.getServerEnitity()));
@@ -88,4 +97,14 @@ public class StanzaRelayBroker implement
         }
     }
 
+    public boolean isRelaying() {
+        return isRelaying.get();
+    }
+
+    public void stop() {
+        if (isRelaying.compareAndSet(true, false)) {
+            this.internalRelay.stop();
+            this.externalRelay.stop();
+        }
+    }
 }

Modified: mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringExternalInboundStanzaRelay.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringExternalInboundStanzaRelay.java?rev=1185067&r1=1185066&r2=1185067&view=diff
==============================================================================
--- mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringExternalInboundStanzaRelay.java
(original)
+++ mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringExternalInboundStanzaRelay.java
Mon Oct 17 09:47:42 2011
@@ -33,6 +33,7 @@ import org.apache.vysper.xmpp.delivery.O
 import org.apache.vysper.xmpp.delivery.StanzaRelay;
 import org.apache.vysper.xmpp.delivery.failure.DeliveryException;
 import org.apache.vysper.xmpp.delivery.failure.DeliveryFailureStrategy;
+import org.apache.vysper.xmpp.delivery.failure.ServiceNotAvailableException;
 import org.apache.vysper.xmpp.protocol.NamespaceURIs;
 import org.apache.vysper.xmpp.server.ServerRuntimeContext;
 import org.apache.vysper.xmpp.server.s2s.XMPPServerConnector;
@@ -68,7 +69,7 @@ public class DeliveringExternalInboundSt
                 new LinkedBlockingQueue<Runnable>());
     }
 
-    public DeliveringExternalInboundStanzaRelay(ExecutorService executor) {
+    /*package*/ DeliveringExternalInboundStanzaRelay(ExecutorService executor) {
         this.executor = executor;
     }
 
@@ -79,6 +80,10 @@ public class DeliveringExternalInboundSt
     public void relay(Entity receiver, Stanza stanza, DeliveryFailureStrategy deliveryFailureStrategy)
             throws DeliveryException {
         
+        if (!isRelaying()) {
+            throw new ServiceNotAvailableException("external inbound relay is not relaying");
+        }
+        
         // rewrite the namespace into the jabber:server namespace
         stanza = StanzaBuilder.rewriteNamespace(stanza, NamespaceURIs.JABBER_CLIENT, NamespaceURIs.JABBER_SERVER);
         XMPPCoreStanza coreStanza = XMPPCoreStanza.getWrapper(stanza);
@@ -89,6 +94,14 @@ public class DeliveringExternalInboundSt
             // ignore non-core stanzas
         }
     }
+
+    public boolean isRelaying() {
+        return !executor.isShutdown();
+    }
+
+    public void stop() {
+        this.executor.shutdown();
+    }
     
     private class OutboundRelayCallable implements Callable<RelayResult> {
         private XMPPCoreStanza stanza;

Modified: mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringInternalInboundStanzaRelay.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringInternalInboundStanzaRelay.java?rev=1185067&r1=1185066&r2=1185067&view=diff
==============================================================================
--- mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringInternalInboundStanzaRelay.java
(original)
+++ mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringInternalInboundStanzaRelay.java
Mon Oct 17 09:47:42 2011
@@ -105,15 +105,31 @@ public class DeliveringInternalInboundSt
                 new LinkedBlockingQueue<Runnable>());
     }
 
+    /*package*/ DeliveringInternalInboundStanzaRelay(ExecutorService executor) {
+        this.executor = executor;
+    }
+
     public void setServerRuntimeContext(ServerRuntimeContext serverRuntimeContext) {
         this.serverRuntimeContext = serverRuntimeContext;
     }
 
     public void relay(Entity receiver, Stanza stanza, DeliveryFailureStrategy deliveryFailureStrategy)
             throws DeliveryException {
+        if (!isRelaying()) {
+            throw new ServiceNotAvailableException("internal inbound relay is not relaying");
+        }
+        
         Future<RelayResult> resultFuture = executor.submit(new Relay(receiver, stanza,
deliveryFailureStrategy));
     }
 
+    public boolean isRelaying() {
+        return !executor.isShutdown();
+    }
+
+    public void stop() {
+        this.executor.shutdown();
+    }
+
     private class Relay implements Callable<RelayResult> {
         private Entity receiver;
 

Modified: mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/server/XMPPServer.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/server/XMPPServer.java?rev=1185067&r1=1185066&r2=1185067&view=diff
==============================================================================
--- mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/server/XMPPServer.java
(original)
+++ mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/server/XMPPServer.java
Mon Oct 17 09:47:42 2011
@@ -67,6 +67,8 @@ public class XMPPServer {
     private DefaultServerRuntimeContext serverRuntimeContext;
 
     private StorageProviderRegistry storageProviderRegistry;
+    
+    private StanzaRelayBroker stanzaRelayBroker;
 
     private InputStream tlsCertificate;
 
@@ -141,7 +143,7 @@ public class XMPPServer {
                 resourceRegistry, accountManagement,offlineReceiver);
         DeliveringExternalInboundStanzaRelay externalStanzaRelay = new DeliveringExternalInboundStanzaRelay();
 
-        StanzaRelayBroker stanzaRelayBroker = new StanzaRelayBroker();
+        stanzaRelayBroker = new StanzaRelayBroker();
         stanzaRelayBroker.setInternalRelay(internalStanzaRelay);
         stanzaRelayBroker.setExternalRelay(externalStanzaRelay);
 
@@ -182,6 +184,7 @@ public class XMPPServer {
             }
         }
         
+        stanzaRelayBroker.stop();
         serverRuntimeContext.getServerConnectorRegistry().close();
     }
 

Modified: mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/StanzaRelayBrokerTestCase.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/StanzaRelayBrokerTestCase.java?rev=1185067&r1=1185066&r2=1185067&view=diff
==============================================================================
--- mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/StanzaRelayBrokerTestCase.java
(original)
+++ mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/StanzaRelayBrokerTestCase.java
Mon Oct 17 09:47:42 2011
@@ -19,11 +19,13 @@
  */
 package org.apache.vysper.xmpp.delivery;
 
+import junit.framework.Assert;
 import org.apache.vysper.xmpp.addressing.Entity;
 import org.apache.vysper.xmpp.addressing.EntityImpl;
 import org.apache.vysper.xmpp.delivery.failure.DeliveryException;
 import org.apache.vysper.xmpp.delivery.failure.DeliveryFailureStrategy;
 import org.apache.vysper.xmpp.delivery.failure.IgnoreFailureStrategy;
+import org.apache.vysper.xmpp.delivery.failure.ServiceNotAvailableException;
 import org.apache.vysper.xmpp.server.ServerFeatures;
 import org.apache.vysper.xmpp.server.ServerRuntimeContext;
 import org.apache.vysper.xmpp.stanza.Stanza;
@@ -122,5 +124,20 @@ public class StanzaRelayBrokerTestCase e
     public void toNullReceiver() throws DeliveryException {
         broker.relay(null, stanza, failureStrategy);
     }
-    
+
+    @Test
+    public void shutdown() {
+        Assert.assertTrue(broker.isRelaying());
+        broker.stop();
+        Assert.assertFalse(broker.isRelaying());
+        
+        try {
+            broker.relay(INTERNAL, stanza, null);
+            Assert.fail("ServiceNotAvailableException expected");
+        } catch (ServiceNotAvailableException e) {
+            // test succeeds
+        } catch (DeliveryException e) {
+            Assert.fail("unexpected delivery exception");
+        }
+    }   
 }

Modified: mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringExternalInboundStanzaRelayTestCase.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringExternalInboundStanzaRelayTestCase.java?rev=1185067&r1=1185066&r2=1185067&view=diff
==============================================================================
--- mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringExternalInboundStanzaRelayTestCase.java
(original)
+++ mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringExternalInboundStanzaRelayTestCase.java
Mon Oct 17 09:47:42 2011
@@ -25,7 +25,9 @@ import junit.framework.TestCase;
 
 import org.apache.vysper.xmpp.addressing.Entity;
 import org.apache.vysper.xmpp.addressing.EntityImpl;
+import org.apache.vysper.xmpp.delivery.failure.DeliveryException;
 import org.apache.vysper.xmpp.delivery.failure.RemoteServerNotFoundException;
+import org.apache.vysper.xmpp.delivery.failure.ServiceNotAvailableException;
 import org.apache.vysper.xmpp.protocol.NamespaceURIs;
 import org.apache.vysper.xmpp.server.ServerRuntimeContext;
 import org.apache.vysper.xmpp.server.s2s.XMPPServerConnector;
@@ -36,6 +38,9 @@ import org.apache.vysper.xmpp.stanza.XMP
 import org.mockito.ArgumentCaptor;
 import org.mockito.Mockito;
 
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
 /**
  */
 public class DeliveringExternalInboundStanzaRelayTestCase extends TestCase {
@@ -103,4 +108,21 @@ public class DeliveringExternalInboundSt
         Assert.assertEquals(FROM, writtenStanza.getFrom());
         Assert.assertEquals(TO, writtenStanza.getTo());
     }
+
+    public void testShutdown() throws DeliveryException {
+        final ExecutorService testExecutorService = Executors.newFixedThreadPool(1);
+        DeliveringExternalInboundStanzaRelay relay = new DeliveringExternalInboundStanzaRelay(testExecutorService);
+
+        Assert.assertTrue(relay.isRelaying());
+        relay.stop();
+        Assert.assertFalse(relay.isRelaying());
+        Assert.assertTrue(testExecutorService.isShutdown());
+
+        try {
+            relay.relay(TO, STANZA, null);
+            Assert.fail("ServiceNotAvailableException expected");
+        } catch (ServiceNotAvailableException e) {
+            // test succeeds
+        }
+    }
 }

Modified: mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringInteralInboundStanzaRelayTestCase.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringInteralInboundStanzaRelayTestCase.java?rev=1185067&r1=1185066&r2=1185067&view=diff
==============================================================================
--- mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringInteralInboundStanzaRelayTestCase.java
(original)
+++ mina/vysper/trunk/server/core/src/test/java/org/apache/vysper/xmpp/delivery/inbound/DeliveringInteralInboundStanzaRelayTestCase.java
Mon Oct 17 09:47:42 2011
@@ -19,6 +19,7 @@
  */
 package org.apache.vysper.xmpp.delivery.inbound;
 
+import junit.framework.Assert;
 import junit.framework.TestCase;
 
 import org.apache.vysper.xml.fragment.XMLSemanticError;
@@ -29,6 +30,7 @@ import org.apache.vysper.xmpp.authentica
 import org.apache.vysper.xmpp.authentication.AccountManagement;
 import org.apache.vysper.xmpp.delivery.failure.DeliveryException;
 import org.apache.vysper.xmpp.delivery.failure.IgnoreFailureStrategy;
+import org.apache.vysper.xmpp.delivery.failure.ServiceNotAvailableException;
 import org.apache.vysper.xmpp.server.DefaultServerRuntimeContext;
 import org.apache.vysper.xmpp.server.SessionState;
 import org.apache.vysper.xmpp.server.TestSessionContext;
@@ -38,10 +40,16 @@ import org.apache.vysper.xmpp.state.reso
 import org.apache.vysper.xmpp.state.resourcebinding.DefaultResourceRegistry;
 import org.apache.vysper.xmpp.state.resourcebinding.ResourceRegistry;
 
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
 /**
  */
 public class DeliveringInteralInboundStanzaRelayTestCase extends TestCase {
 
+    protected static final EntityImpl TO_ENTITY = EntityImpl.parseUnchecked("userTo@vysper.org");
+    protected static final EntityImpl FROM_ENTITY = EntityImpl.parseUnchecked("userFrom@vysper.org");
+
     protected ResourceRegistry resourceRegistry = new DefaultResourceRegistry();
 
     protected AccountManagement accountVerification;
@@ -75,16 +83,14 @@ public class DeliveringInteralInboundSta
         DefaultServerRuntimeContext serverRuntimeContext = new DefaultServerRuntimeContext(null,
null);
         stanzaRelay.setServerRuntimeContext(serverRuntimeContext);
 
-        EntityImpl fromEntity = EntityImpl.parse("userFrom@vysper.org");
-        EntityImpl toEntity = EntityImpl.parse("userTo@vysper.org");
-        TestSessionContext sessionContext = TestSessionContext.createSessionContext(toEntity);
+        TestSessionContext sessionContext = TestSessionContext.createSessionContext(TO_ENTITY);
         sessionContext.setSessionState(SessionState.AUTHENTICATED);
         resourceRegistry.bindSession(sessionContext);
 
-        Stanza stanza = StanzaBuilder.createMessageStanza(fromEntity, toEntity, "en", "Hello").build();
+        Stanza stanza = StanzaBuilder.createMessageStanza(FROM_ENTITY, TO_ENTITY, "en", "Hello").build();
 
         try {
-            stanzaRelay.relay(toEntity, stanza, new IgnoreFailureStrategy());
+            stanzaRelay.relay(TO_ENTITY, stanza, new IgnoreFailureStrategy());
             Stanza recordedStanza = sessionContext.getNextRecordedResponse(1000);
             assertNotNull("stanza delivered", recordedStanza);
             assertEquals("Hello", recordedStanza.getSingleInnerElementsNamed("body").getSingleInnerText().getText());
@@ -94,17 +100,15 @@ public class DeliveringInteralInboundSta
     }
 
     public void testSimpleRelayToUnboundSession() throws EntityFormatException, XMLSemanticError,
DeliveryException {
-        EntityImpl fromEntity = EntityImpl.parse("userFrom@vysper.org");
-        EntityImpl toEntity = EntityImpl.parse("userTo@vysper.org");
-        TestSessionContext sessionContext = TestSessionContext.createSessionContext(toEntity);
+        TestSessionContext sessionContext = TestSessionContext.createSessionContext(TO_ENTITY);
         String resource = resourceRegistry.bindSession(sessionContext);
         boolean noResourceRemains = resourceRegistry.unbindResource(resource);
         assertTrue(noResourceRemains);
 
-        Stanza stanza = StanzaBuilder.createMessageStanza(fromEntity, toEntity, "en", "Hello").build();
+        Stanza stanza = StanzaBuilder.createMessageStanza(FROM_ENTITY, TO_ENTITY, "en", "Hello").build();
 
         try {
-            stanzaRelay.relay(toEntity, stanza, new IgnoreFailureStrategy());
+            stanzaRelay.relay(TO_ENTITY, stanza, new IgnoreFailureStrategy());
             Stanza recordedStanza = sessionContext.getNextRecordedResponse(1000);
             assertNull("stanza not delivered to unbound", recordedStanza);
         } catch (DeliveryException e) {
@@ -121,26 +125,22 @@ public class DeliveringInteralInboundSta
 
         stanzaRelay.setServerRuntimeContext(serverRuntimeContext);
 
-        EntityImpl fromEntity = EntityImpl.parse("userFrom@vysper.org");
-
-        EntityImpl toEntity = EntityImpl.parse("userTo@vysper.org");
+        TestSessionContext sessionContextTO_ENTITY_1_prio3 = createSessionForTo(TO_ENTITY,
3); // NON-NEGATIVE
+        TestSessionContext sessionContextTO_ENTITY_2_prio0 = createSessionForTo(TO_ENTITY,
0); // NON-NEGATIVE
+        TestSessionContext sessionContextTO_ENTITY_3_prio3 = createSessionForTo(TO_ENTITY,
3); // NON-NEGATIVE
+        TestSessionContext sessionContextTO_ENTITY_4_prioMinus = createSessionForTo(TO_ENTITY,
-1); // not receiving, negative
 
-        TestSessionContext sessionContextToEntity_1_prio3 = createSessionForTo(toEntity,
3); // NON-NEGATIVE
-        TestSessionContext sessionContextToEntity_2_prio0 = createSessionForTo(toEntity,
0); // NON-NEGATIVE
-        TestSessionContext sessionContextToEntity_3_prio3 = createSessionForTo(toEntity,
3); // NON-NEGATIVE
-        TestSessionContext sessionContextToEntity_4_prioMinus = createSessionForTo(toEntity,
-1); // not receiving, negative
-
-        Stanza stanza = StanzaBuilder.createMessageStanza(fromEntity, toEntity, "en", "Hello").build();
+        Stanza stanza = StanzaBuilder.createMessageStanza(FROM_ENTITY, TO_ENTITY, "en", "Hello").build();
 
         try {
-            stanzaRelay.relay(toEntity, stanza, new IgnoreFailureStrategy());
-            Stanza recordedStanza_1 = sessionContextToEntity_1_prio3.getNextRecordedResponse(100);
+            stanzaRelay.relay(TO_ENTITY, stanza, new IgnoreFailureStrategy());
+            Stanza recordedStanza_1 = sessionContextTO_ENTITY_1_prio3.getNextRecordedResponse(100);
             assertNotNull("stanza 1 delivered", recordedStanza_1);
-            Stanza recordedStanza_2 = sessionContextToEntity_2_prio0.getNextRecordedResponse(100);
+            Stanza recordedStanza_2 = sessionContextTO_ENTITY_2_prio0.getNextRecordedResponse(100);
             assertNotNull("stanza 2 delivered", recordedStanza_2);
-            Stanza recordedStanza_3 = sessionContextToEntity_3_prio3.getNextRecordedResponse(100);
+            Stanza recordedStanza_3 = sessionContextTO_ENTITY_3_prio3.getNextRecordedResponse(100);
             assertNotNull("stanza 3 delivered", recordedStanza_3);
-            Stanza recordedStanza_4 = sessionContextToEntity_4_prioMinus.getNextRecordedResponse(100);
+            Stanza recordedStanza_4 = sessionContextTO_ENTITY_4_prioMinus.getNextRecordedResponse(100);
             assertNull("stanza 4 delivered", recordedStanza_4);
         } catch (DeliveryException e) {
             throw e;
@@ -157,26 +157,22 @@ public class DeliveringInteralInboundSta
 
         stanzaRelay.setServerRuntimeContext(serverRuntimeContext);
 
-        EntityImpl fromEntity = EntityImpl.parse("userFrom@vysper.org");
-
-        EntityImpl toEntity = EntityImpl.parse("userTo@vysper.org");
+        TestSessionContext sessionContextTO_ENTITY_1_prio3 = createSessionForTo(TO_ENTITY,
3); // HIGHEST PRIO
+        TestSessionContext sessionContextTO_ENTITY_2_prio0 = createSessionForTo(TO_ENTITY,
1); // not receiving
+        TestSessionContext sessionContextTO_ENTITY_3_prio3 = createSessionForTo(TO_ENTITY,
3); // HIGHEST PRIO
+        TestSessionContext sessionContextTO_ENTITY_4_prioMinus = createSessionForTo(TO_ENTITY,
-1); // not receiving
 
-        TestSessionContext sessionContextToEntity_1_prio3 = createSessionForTo(toEntity,
3); // HIGHEST PRIO
-        TestSessionContext sessionContextToEntity_2_prio0 = createSessionForTo(toEntity,
1); // not receiving
-        TestSessionContext sessionContextToEntity_3_prio3 = createSessionForTo(toEntity,
3); // HIGHEST PRIO
-        TestSessionContext sessionContextToEntity_4_prioMinus = createSessionForTo(toEntity,
-1); // not receiving
-
-        Stanza stanza = StanzaBuilder.createMessageStanza(fromEntity, toEntity, "en", "Hello").build();
+        Stanza stanza = StanzaBuilder.createMessageStanza(FROM_ENTITY, TO_ENTITY, "en", "Hello").build();
 
         try {
-            stanzaRelay.relay(toEntity, stanza, new IgnoreFailureStrategy());
-            Stanza recordedStanza_1 = sessionContextToEntity_1_prio3.getNextRecordedResponse(100);
+            stanzaRelay.relay(TO_ENTITY, stanza, new IgnoreFailureStrategy());
+            Stanza recordedStanza_1 = sessionContextTO_ENTITY_1_prio3.getNextRecordedResponse(100);
             assertNotNull("stanza 1 delivered", recordedStanza_1);
-            Stanza recordedStanza_2 = sessionContextToEntity_2_prio0.getNextRecordedResponse(100);
+            Stanza recordedStanza_2 = sessionContextTO_ENTITY_2_prio0.getNextRecordedResponse(100);
             assertNull("stanza 2 not delivered", recordedStanza_2);
-            Stanza recordedStanza_3 = sessionContextToEntity_3_prio3.getNextRecordedResponse(100);
+            Stanza recordedStanza_3 = sessionContextTO_ENTITY_3_prio3.getNextRecordedResponse(100);
             assertNotNull("stanza 3 delivered", recordedStanza_3);
-            Stanza recordedStanza_4 = sessionContextToEntity_4_prioMinus.getNextRecordedResponse(100);
+            Stanza recordedStanza_4 = sessionContextTO_ENTITY_4_prioMinus.getNextRecordedResponse(100);
             assertNull("stanza 4 not delivered", recordedStanza_4);
         } catch (DeliveryException e) {
             throw e;
@@ -184,12 +180,30 @@ public class DeliveringInteralInboundSta
 
     }
 
-    private TestSessionContext createSessionForTo(EntityImpl toEntity, final int priority)
{
-        TestSessionContext sessionContextToEntity = TestSessionContext.createSessionContext(toEntity);
-        sessionContextToEntity.setSessionState(SessionState.AUTHENTICATED);
-        String toEntityRes = resourceRegistry.bindSession(sessionContextToEntity);
-        resourceRegistry.setResourcePriority(toEntityRes, priority);
-        return sessionContextToEntity;
+    private TestSessionContext createSessionForTo(EntityImpl TO_ENTITY, final int priority)
{
+        TestSessionContext sessionContextTO_ENTITY = TestSessionContext.createSessionContext(TO_ENTITY);
+        sessionContextTO_ENTITY.setSessionState(SessionState.AUTHENTICATED);
+        String TO_ENTITYRes = resourceRegistry.bindSession(sessionContextTO_ENTITY);
+        resourceRegistry.setResourcePriority(TO_ENTITYRes, priority);
+        return sessionContextTO_ENTITY;
+    }
+
+    public void testShutdown() throws DeliveryException {
+        final ExecutorService testExecutorService = Executors.newFixedThreadPool(1);
+        DeliveringInternalInboundStanzaRelay relay = new DeliveringInternalInboundStanzaRelay(testExecutorService);
+
+        Assert.assertTrue(relay.isRelaying());
+        relay.stop();
+        Assert.assertFalse(relay.isRelaying());
+        Assert.assertTrue(testExecutorService.isShutdown());
+        
+        Stanza stanza = StanzaBuilder.createMessageStanza(FROM_ENTITY, TO_ENTITY, "en", "Hello").build();
+        try {
+            relay.relay(TO_ENTITY, stanza, null);
+            Assert.fail("ServiceNotAvailableException expected");
+        } catch (ServiceNotAvailableException e) {
+            // test succeeds
+        }
     }
-
+    
 }



Mime
View raw message