Author: chris.laprun(a)jboss.com
Date: 2009-11-13 16:33:30 -0500 (Fri, 13 Nov 2009)
New Revision: 596
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/hibernate/HibernateConsumerRegistry.java
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/registry/xml/XMLConsumerRegistry.java
Log:
- Renamed AbstractConsumerRegistry.getAllProducerInfos to getProducerInfosFromStorage and
make the return type explicit.
- Reworked de-/activate methods so they take a WSRPConsumer as an argument to avoid
roundtrips between id and consumer.
- Fixed XMLConsumerRegistry as it wasn't fitting properly in the registry hierarchy by
fixing getProducerInfosFromStorage
implementation and refitting start method into reloadConsumers method.
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 2009-11-13
16:03:06 UTC (rev 595)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/registry/AbstractConsumerRegistry.java 2009-11-13
21:33:30 UTC (rev 596)
@@ -116,7 +116,7 @@
registerOrDeregisterConsumerWith(id, false);
}
- deactivateConsumerWith(id);
+ deactivateConsumer(consumer);
consumers.remove(id);
delete(info);
@@ -156,20 +156,25 @@
public void activateConsumerWith(String id) throws ConsumerException
{
ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(id, "Consumer
identifier", "Activating a Consumer");
+ activateConsumer(getConsumer(id));
+ }
- // if the consumer associated with the given id is already registered, don't do
anything
+ protected void activateConsumer(WSRPConsumer consumer)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNull(consumer,
"WSRPConsumer");
+ String id = consumer.getProducerId();
+
if (federatingPortletInvoker.getFederatedInvoker(id) == null)
{
- startOrStopConsumer(id, true);
+ startOrStopConsumer(consumer, true);
}
else
{
// todo: fix-me federated portlet invoker gets desynchronized...
- WSRPConsumer consumer = getConsumer(id);
- if (consumer != null && !consumer.isActive())
+ if (!consumer.isActive())
{
federatingPortletInvoker.unregisterInvoker(id);
- startOrStopConsumer(id, true);
+ startOrStopConsumer(consumer, true);
}
}
}
@@ -177,20 +182,26 @@
public void deactivateConsumerWith(String id) throws ConsumerException
{
ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(id, "Consumer
identifier", "Deactivating a Consumer");
+ deactivateConsumer(getConsumer(id));
+ }
+ protected void deactivateConsumer(WSRPConsumer consumer)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNull(consumer,
"Consumer");
+ String id = consumer.getProducerId();
+
// only process if there is a registered Consumer with the specified id
if (federatingPortletInvoker.getFederatedInvoker(id) != null)
{
- startOrStopConsumer(id, false);
+ startOrStopConsumer(consumer, false);
}
else
{
// todo: fix-me federated portlet invoker gets desynchronized...
- WSRPConsumer consumer = getConsumer(id);
- if (consumer != null && consumer.isActive())
+ if (consumer.isActive())
{
federatingPortletInvoker.registerInvoker(id, consumer);
- startOrStopConsumer(id, false);
+ startOrStopConsumer(consumer, false);
}
}
}
@@ -219,13 +230,13 @@
// load the configured consumers
consumers = new TreeMap<String, WSRPConsumer>();
- Iterator producerInfos = getAllProducerInfos();
+ Iterator<ProducerInfo> producerInfos = getProducerInfosFromStorage();
// load the configured producers
ProducerInfo producerInfo;
while (producerInfos.hasNext())
{
- producerInfo = (ProducerInfo)producerInfos.next();
+ producerInfo = producerInfos.next();
// need to set the registry after loading from DB since registry is not
persisted.
producerInfo.setRegistry(this);
@@ -310,28 +321,20 @@
catch (Exception e)
{
// unexpected exception: deactivate the consumer
- deactivateConsumerWith(id);
+ deactivateConsumer(consumer);
Throwable cause = e.getCause();
throw new ConsumerException("Couldn't " + (register ?
"register" : "deregister") + CONSUMER_WITH_ID + id +
"'",
cause != null ? cause : e);
}
}
- private void startOrStopConsumer(String id, boolean start)
+ private void startOrStopConsumer(WSRPConsumer consumer, boolean start)
{
- WSRPConsumer consumer;
-
try
{
+ String id = consumer.getProducerId();
if (start)
{
- consumer = getConsumer(id);
-
- if (consumer == null)
- {
- throw new IllegalArgumentException(CONSUMER_WITH_ID + id + "'
doesn't exist!");
- }
-
consumer.activate();
federatingPortletInvoker.registerInvoker(id, consumer);
sessionEventBroadcaster.registerListener(getListenerIdFrom(id), consumer);
@@ -362,7 +365,7 @@
}
catch (Exception e)
{
- throw new ConsumerException("Couldn't " + (start ?
"start" : "stop") + " Consumer service '" + id +
"'", e);
+ throw new ConsumerException("Couldn't " + (start ?
"start" : "stop") + " Consumer service '" +
consumer.getProducerId() + "'", e);
}
// update ProducerInfo
@@ -380,5 +383,5 @@
protected abstract String update(ProducerInfo producerInfo);
- protected abstract Iterator getAllProducerInfos();
+ protected abstract Iterator<ProducerInfo> getProducerInfosFromStorage();
}
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/registry/hibernate/HibernateConsumerRegistry.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/registry/hibernate/HibernateConsumerRegistry.java 2009-11-13
16:03:06 UTC (rev 595)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/registry/hibernate/HibernateConsumerRegistry.java 2009-11-13
21:33:30 UTC (rev 596)
@@ -104,12 +104,11 @@
return oldId;
}
- protected Iterator getAllProducerInfos()
+ protected Iterator<ProducerInfo> getProducerInfosFromStorage()
{
Session session = sessionFactory.getCurrentSession();
- Iterator producerInfos = session.createQuery("from ProducerInfo pi order by
pi.persistentId").iterate();
- return producerInfos;
+ return session.createQuery("from ProducerInfo pi order by
pi.persistentId").iterate();
}
@Override
Modified:
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/registry/xml/XMLConsumerRegistry.java
===================================================================
---
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/registry/xml/XMLConsumerRegistry.java 2009-11-13
16:03:06 UTC (rev 595)
+++
components/wsrp/trunk/consumer/src/main/java/org/gatein/wsrp/consumer/registry/xml/XMLConsumerRegistry.java 2009-11-13
21:33:30 UTC (rev 596)
@@ -34,6 +34,7 @@
import org.slf4j.LoggerFactory;
import org.xml.sax.EntityResolver;
+import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Iterator;
@@ -68,12 +69,20 @@
this.entityResolver = entityResolver;
}
- public void start() throws Exception
+ public void reloadConsumers()
{
URL defaultWSRPURL =
Thread.currentThread().getContextClassLoader().getResource(defaultWSRPLocation);
if (defaultWSRPURL != null)
{
- InputStream inputStream = defaultWSRPURL.openStream();
+ InputStream inputStream;
+ try
+ {
+ inputStream = defaultWSRPURL.openStream();
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException("Couldn't open default XML WSRP Consumer
configuration file", e);
+ }
Unmarshaller unmarshaller =
UnmarshallerFactory.newInstance().newUnmarshaller();
ObjectModelFactory factory = new XMLWSRPConsumerFactory(this);
@@ -82,19 +91,33 @@
log.debug("Could not obtain entity resolver for
XMLConsumerRegistry");
entityResolver = new NullEntityResolver();
}
- unmarshaller.setEntityResolver(entityResolver);
try
{
+ unmarshaller.setEntityResolver(entityResolver);
consumers = (SortedMap<String,
WSRPConsumer>)unmarshaller.unmarshal(inputStream, factory, null);
}
catch (JBossXBException e)
{
- e.printStackTrace();
+ throw new RuntimeException("Couldn't set unmarshall WSRP Consumers
configuration", e);
}
for (WSRPConsumer consumer : consumers.values())
{
- activateConsumerWith(consumer.getProducerId());
+
+ ProducerInfo producerInfo = consumer.getProducerInfo();
+ try
+ {
+ // if the producer is marked as active, activate it fo' real! :)
+ if (producerInfo.isActive())
+ {
+ activateConsumer(consumer);
+ }
+ }
+ catch (Exception e)
+ {
+ producerInfo.setActive(false);
+ updateProducerInfo(producerInfo);
+ }
}
}
}
@@ -127,13 +150,38 @@
}
@Override
- protected Iterator getAllProducerInfos()
+ protected Iterator<ProducerInfo> getProducerInfosFromStorage()
{
- return consumers.values().iterator();
+ return new ProducerInfoIterator(consumers.values().iterator());
}
SortedMap<String, WSRPConsumer> getConsumers()
{
return consumers;
}
+
+ class ProducerInfoIterator implements Iterator<ProducerInfo>
+ {
+ private Iterator<WSRPConsumer> consumers;
+
+ ProducerInfoIterator(Iterator<WSRPConsumer> consumers)
+ {
+ this.consumers = consumers;
+ }
+
+ public boolean hasNext()
+ {
+ return consumers.hasNext();
+ }
+
+ public ProducerInfo next()
+ {
+ return consumers.next().getProducerInfo();
+ }
+
+ public void remove()
+ {
+ throw new UnsupportedOperationException("remove not supported on this
iterator implementation");
+ }
+ }
}