Author: chris.laprun(a)jboss.com
Date: 2010-12-06 09:05:01 -0500 (Mon, 06 Dec 2010)
New Revision: 5492
Modified:
components/wsrp/trunk/api/src/main/java/org/gatein/wsrp/api/session/SessionEventBroadcaster.java
components/wsrp/trunk/consumer/pom.xml
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/registry/AbstractConsumerRegistry.java
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/registry/ConsumerRegistryTestCase.java
Log:
- GTNWSRP-180: make sure that we don't start consumers when stopping the
ConsumerRegistry. Added associated test case.
- Added SessionEventBroadcaster.NO_OP_BROADCASTER to use by default so that we don't
always need to provide an implementation in case we don't care about SessionEvents.
Modified:
components/wsrp/trunk/api/src/main/java/org/gatein/wsrp/api/session/SessionEventBroadcaster.java
===================================================================
---
components/wsrp/trunk/api/src/main/java/org/gatein/wsrp/api/session/SessionEventBroadcaster.java 2010-12-06
10:09:01 UTC (rev 5491)
+++
components/wsrp/trunk/api/src/main/java/org/gatein/wsrp/api/session/SessionEventBroadcaster.java 2010-12-06
14:05:01 UTC (rev 5492)
@@ -29,9 +29,33 @@
*/
public interface SessionEventBroadcaster
{
- public void registerListener(String listenerId, SessionEventListener listener);
+ void registerListener(String listenerId, SessionEventListener listener);
- public void unregisterListener(String listenerId);
+ void unregisterListener(String listenerId);
- public void notifyListenersOf(SessionEvent event);
+ void notifyListenersOf(SessionEvent event);
+
+ /**
+ * A default implementation of SessionEventBroadcaster that does nothing, in case
we're not interested in
+ * SessionEvents.
+ */
+ final static class NullSessionEventBroadcaster implements SessionEventBroadcaster
+ {
+ public void registerListener(String listenerId, SessionEventListener listener)
+ {
+ // do nothing
+ }
+
+ public void unregisterListener(String listenerId)
+ {
+ // do nothing
+ }
+
+ public void notifyListenersOf(SessionEvent event)
+ {
+ // do nothing
+ }
+ }
+
+ static final SessionEventBroadcaster NO_OP_BROADCASTER = new
NullSessionEventBroadcaster();
}
Modified: components/wsrp/trunk/consumer/pom.xml
===================================================================
--- components/wsrp/trunk/consumer/pom.xml 2010-12-06 10:09:01 UTC (rev 5491)
+++ components/wsrp/trunk/consumer/pom.xml 2010-12-06 14:05:01 UTC (rev 5492)
@@ -21,7 +21,8 @@
~ 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
-->
-<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>org.gatein.wsrp</groupId>
<artifactId>wsrp-parent</artifactId>
@@ -100,6 +101,12 @@
<version>4.6</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>1.8.5</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<!-- Ignore all classes in the org.gatein.wsrp.test packages as they are not tests
-->
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/registry/AbstractConsumerRegistry.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/registry/AbstractConsumerRegistry.java 2010-12-06
10:09:01 UTC (rev 5491)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/registry/AbstractConsumerRegistry.java 2010-12-06
14:05:01 UTC (rev 5492)
@@ -60,7 +60,7 @@
private SortedMap<String, WSRPConsumer> consumers;
private Map<String, String> keysToIds;
- private SessionEventBroadcaster sessionEventBroadcaster;
+ private SessionEventBroadcaster sessionEventBroadcaster =
SessionEventBroadcaster.NO_OP_BROADCASTER;
private MigrationService migrationService;
private static final String CONSUMER_WITH_ID = "Consumer with id '";
@@ -183,12 +183,23 @@
private WSRPConsumer createConsumerFrom(ProducerInfo producerInfo)
{
- WSRPConsumer consumer = new WSRPConsumerImpl(producerInfo, migrationService);
+ WSRPConsumer consumer = newConsumer(producerInfo);
add(consumer);
return consumer;
}
+ /**
+ * Extracted for testing purposes...
+ *
+ * @param producerInfo
+ * @return
+ */
+ protected WSRPConsumer newConsumer(ProducerInfo producerInfo)
+ {
+ return new WSRPConsumerImpl(producerInfo, migrationService);
+ }
+
public void activateConsumerWith(String id) throws ConsumerException
{
ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(id, "Consumer
identifier", "Activating a Consumer");
@@ -291,7 +302,7 @@
public void stop() throws Exception
{
- for (WSRPConsumer consumer : getConsumers())
+ for (WSRPConsumer consumer : getConsumers(false))
{
// if producer is not active, it shouldn't be registered with the federating
portlet invoker, hence do not
// unregister it. We have changed how consumers are registered (active consumers
are not automatically
@@ -423,7 +434,7 @@
// internal management methods
- private void add(WSRPConsumer consumer)
+ protected void add(WSRPConsumer consumer)
{
String id = consumer.getProducerId();
consumers.put(id, consumer);
@@ -439,20 +450,29 @@
protected Collection<WSRPConsumer> getConsumers()
{
+ return getConsumers(true);
+ }
+
+ protected Collection<WSRPConsumer> getConsumers(boolean startConsumers)
+ {
Collection<WSRPConsumer> consumerz = consumers.values();
- for (WSRPConsumer consumer : consumerz)
+
+ if (startConsumers)
{
- if (consumer.getProducerInfo().isActive() && !consumer.isActive())
+ for (WSRPConsumer consumer : consumerz)
{
- try
+ if (consumer.getProducerInfo().isActive() && !consumer.isActive())
{
- consumer.refresh(false);
+ try
+ {
+ consumer.refresh(false);
+ }
+ catch (Exception e)
+ {
+ log.info("Couldn't activate consumer " +
consumer.getProducerId());
+ consumer.getProducerInfo().setActiveAndSave(false);
+ }
}
- catch (Exception e)
- {
- log.info("Couldn't activate consumer " +
consumer.getProducerId());
- consumer.getProducerInfo().setActiveAndSave(false);
- }
}
}
return consumerz;
Modified:
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/registry/ConsumerRegistryTestCase.java
===================================================================
---
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/registry/ConsumerRegistryTestCase.java 2010-12-06
10:09:01 UTC (rev 5491)
+++
components/wsrp/trunk/consumer/src/test/java/org/gatein/wsrp/consumer/registry/ConsumerRegistryTestCase.java 2010-12-06
14:05:01 UTC (rev 5492)
@@ -1,37 +1,38 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2009, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * This is free software; you can redistribute it and/or modify it *
- * under the terms of the GNU Lesser General Public License as *
- * published by the Free Software Foundation; either version 2.1 of *
- * the License, or (at your option) any later version. *
- * *
- * This software is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
- * Lesser General Public License for more details. *
- * *
- * You should have received a copy of the GNU Lesser General Public *
- * License along with this software; if not, write to the Free *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
- ******************************************************************************/
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
package org.gatein.wsrp.consumer.registry;
import junit.framework.TestCase;
-import org.gatein.pc.federation.impl.FederatingPortletInvokerService;
import org.gatein.wsrp.WSRPConsumer;
import org.gatein.wsrp.consumer.ConsumerException;
import org.gatein.wsrp.consumer.EndpointConfigurationInfo;
import org.gatein.wsrp.consumer.ProducerInfo;
import org.gatein.wsrp.consumer.RegistrationInfo;
+import org.mockito.Mockito;
import java.util.Collection;
+import java.util.Collections;
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
@@ -46,7 +47,6 @@
protected void setUp() throws Exception
{
registry = new InMemoryConsumerRegistry();
- registry.setFederatingPortletInvoker(new FederatingPortletInvokerService());
}
public void testCreateAndGet()
@@ -146,4 +146,41 @@
assertEquals(info, registry.getProducerInfoByKey(key));
assertEquals(consumer, registry.getConsumer("bar"));
}
+
+ public void testStoppingShouldntStartConsumers() throws Exception
+ {
+ // fake marking consumer as active in persistence
+ ProducerInfo info = Mockito.mock(ProducerInfo.class);
+ Mockito.stub(info.isActive()).toReturn(true);
+ Mockito.stub(info.getId()).toReturn("foo");
+ Mockito.stub(info.getKey()).toReturn("fooKey");
+ EndpointConfigurationInfo endpoint =
Mockito.mock(EndpointConfigurationInfo.class);
+ Mockito.stub(info.getEndpointConfigurationInfo()).toReturn(endpoint);
+
+ // create a consumer to spy from
+ WSRPConsumer original = ((AbstractConsumerRegistry)registry).newConsumer(info);
+ WSRPConsumer consumer = Mockito.spy(original);
+
+ // force re-init of registry from "persistence" to ensure that the spy
registry actually uses our spy consumer
+ ConsumerRegistry registrySpy = Mockito.spy(registry);
+
Mockito.doReturn(Collections.singletonList(info).iterator()).when((AbstractConsumerRegistry)registrySpy).getProducerInfosFromStorage();
+
Mockito.doReturn(consumer).when((AbstractConsumerRegistry)registrySpy).newConsumer(info);
+ registrySpy.reloadConsumers();
+
+ WSRPConsumer foo = registrySpy.getConsumer("foo");
+ assertTrue(foo.getProducerInfo().isActive());
+ assertEquals(consumer, foo);
+
+ // start consumer and check that it's properly added to the
FederatingPortletInvoker
+ ((AbstractConsumerRegistry)registrySpy).activateConsumer(foo);
+ assertEquals(consumer,
registrySpy.getFederatingPortletInvoker().getFederatedInvoker("foo").getPortletInvoker());
+
+ // stop the consumer and then the registry and check that consumer.start has only
been called once
+ consumer.stop();
+ registrySpy.stop();
+ Mockito.verify(consumer, Mockito.times(1)).start();
+
+ // check that consumer is not known by the FederatingPortletInvoker anymore
+ assertEquals(null,
registrySpy.getFederatingPortletInvoker().getFederatedInvoker("foo"));
+ }
}