Skip to content

Commit

Permalink
fixed compilation and test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ddossot committed Sep 10, 2010
1 parent c4ccf4a commit 7c8fddc
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 214 deletions.
Original file line number Diff line number Diff line change
@@ -1,52 +1,67 @@

package com.clood.interceptor;

import org.mule.api.MuleContext;
import org.mule.api.MuleEvent;
import org.mule.api.MuleException;
import org.mule.api.MuleMessage;
import org.mule.api.context.MuleContextAware;
import org.mule.api.context.notification.MuleContextNotificationListener;
import org.mule.api.context.notification.ServerNotification;
import org.mule.api.interceptor.Interceptor;
import org.mule.api.interceptor.Invocation;
import org.mule.api.processor.MessageProcessor;
import org.mule.context.notification.MuleContextNotification;
import org.mule.context.notification.NotificationException;

/**
* @author David Dossot (david@dossot.net)
*/
// <start id="BrokerNotReadyInterceptor"/>
public final class BrokerNotReadyInterceptor implements MuleContextAware,
MuleContextNotificationListener, Interceptor {

public final class BrokerNotReadyInterceptor
implements MuleContextAware, MuleContextNotificationListener<MuleContextNotification>, Interceptor
{
private volatile boolean brokerReady = false;
private MessageProcessor next;

public void setMuleContext(final MuleContext context) {
try {
public void setMuleContext(final MuleContext context)
{
try
{
context.registerListener(this);
} catch (final NotificationException ne) {
}
catch (final NotificationException ne)
{
throw new RuntimeException(ne);
}
}

public void onNotification(final ServerNotification notification) {
final int action = notification.getAction();

if (action == MuleContextNotification.CONTEXT_STARTED) {
public void setListener(MessageProcessor listener)
{
next = listener;
}

public void onNotification(final MuleContextNotification notification)
{
final int action = notification.getAction();

if (action == MuleContextNotification.CONTEXT_STARTED)
{
brokerReady = true;
}
else if (action == MuleContextNotification.CONTEXT_STOPPED) {
else if (action == MuleContextNotification.CONTEXT_STOPPED)
{
brokerReady = false;
}
}

public MuleMessage intercept(final Invocation invocation)
throws MuleException {

if (!brokerReady) {
throw new IllegalStateException("Invocation of service " + invocation.getService().getName() + " impossible at this time!");
public MuleEvent process(MuleEvent event) throws MuleException
{
if (!brokerReady)
{
throw new IllegalStateException("Invocation of service " + event.getFlowConstruct().getName()
+ " impossible at this time!");
}

return invocation.invoke();
return next.process(event);
}

}
// <end id="BrokerNotReadyInterceptor"/>
// <end id="BrokerNotReadyInterceptor"/>
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@

package com.clood.interceptor;

import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;

import org.mule.DefaultMuleEvent;
import org.mule.DefaultMuleMessage;
import org.mule.api.MuleEvent;
import org.mule.api.MuleException;
import org.mule.api.MuleMessage;
import org.mule.api.interceptor.Interceptor;
import org.mule.api.interceptor.Invocation;
import org.mule.api.processor.MessageProcessor;

/**
* @author David Dossot (david@dossot.net)
*/
// <start id="PayloadCacheInterceptor"/>
public final class PayloadCacheInterceptor implements Interceptor {

public final class PayloadCacheInterceptor implements Interceptor
{
private MessageProcessor next;
private Ehcache cache;

public void setCache(final Ehcache cache) {
this.cache = cache;
public void setListener(MessageProcessor listener)
{
next = listener;
}

public MuleMessage intercept(final Invocation invocation)
throws MuleException {
public void setCache(final Ehcache cache)
{
this.cache = cache;
}

final MuleMessage currentMessage = invocation.getMessage();
public MuleEvent process(MuleEvent event) throws MuleException
{
final MuleMessage currentMessage = event.getMessage();
final Object key = currentMessage.getPayload();
final Element cachedElement = cache.get(key);

if (cachedElement != null) {
return new DefaultMuleMessage(cachedElement.getObjectValue(),
currentMessage, invocation.getEvent().getMuleContext());
if (cachedElement != null)
{
return new DefaultMuleEvent(new DefaultMuleMessage(cachedElement.getObjectValue(),
currentMessage, event.getMuleContext()), event);
}

// we don't synchronize so several threads can compete to fill the cache
// for the same key: this is rare enough to be acceptable
final MuleMessage result = invocation.invoke();
cache.put(new Element(key, result.getPayload()));
final MuleEvent result = next.process(event);
cache.put(new Element(key, result.getMessage().getPayload()));
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<service name="BrokerReadinessService">
<inbound>
<vm:inbound-endpoint path="BrokerReadinessService.In" />
<vm:inbound-endpoint path="BrokerReadinessService.In" exchange-pattern="request-response" />
</inbound>

<component>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<service name="Md5FileHasher">
<inbound>
<vm:inbound-endpoint path="Md5FileHasher.In" />
<vm:inbound-endpoint path="Md5FileHasher.In" exchange-pattern="request-response" />
</inbound>
<pooled-component>
<interceptor-stack ref="PayloadCacheInterceptors" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package com.muleinaction.muleclient;

import java.io.File;
Expand All @@ -11,19 +12,22 @@
/**
* @author David Dossot (david@dossot.net)
*/
public class InVmMuleClientFunctionalTestCase extends FunctionalTestCase {
public class InVmMuleClientFunctionalTestCase extends FunctionalTestCase
{

private String expectedHash;

private String tempFileName;

@Override
protected String getConfigResources() {
protected String getConfigResources()
{
return "conf/in-vm-muleclient-config.xml";
}

@Override
protected void doSetUp() throws Exception {
protected void doSetUp() throws Exception
{
super.doSetUp();

// prepare test file for MD5 File Hasher Service
Expand All @@ -34,34 +38,39 @@ protected void doSetUp() throws Exception {

FileOutputStream fos = null;

try {
try
{
fos = new FileOutputStream(tempFile);
fos.write(fileData.getBytes("US-ASCII"));
fos.flush();
} finally {
}
finally
{
fos.close();
}

expectedHash = DigestUtils.md5Hex(fileData);
tempFileName = tempFile.getName();
}

public void testInMemoryVmCall() throws Exception {
public void testInMemoryVmCall() throws Exception
{
// this is prototypical for integration testing
// <start id="MuleClient-IntegrationTesting"/>
final MuleClient muleClient = new MuleClient(muleContext);

assertEquals(expectedHash, muleClient.send("vm://Md5FileHasher.In",
tempFileName, null).getPayload());
assertEquals(expectedHash, muleClient.send("vm://Md5FileHasher.In", tempFileName, null).getPayload());
// <end id="MuleClient-IntegrationTesting"/>
}

public void testInMemoryVmCallWithContextDetection() throws Exception {
// <start id="MuleClient-CreationNoContext"/>
final MuleClient muleClient = new MuleClient();
// <end id="MuleClient-CreationNoContext"/>

assertEquals(expectedHash, muleClient.send("vm://Md5FileHasher.In",
tempFileName, null).getPayload());
}
// not supported by Mule 3 anymore
// public void testInMemoryVmCallWithContextDetection() throws Exception
// {
// // <start id="MuleClient-CreationNoContext"/>
// final MuleClient muleClient = new MuleClient();
// // <end id="MuleClient-CreationNoContext"/>
//
// assertEquals(expectedHash, muleClient.send("vm://Md5FileHasher.In",
// tempFileName, null).getPayload());
// }
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@

package com.muleinaction.muleclient;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.Ignore;
import org.junit.Test;
import org.mule.MuleServer;
import org.mule.api.MuleMessage;
import org.mule.module.client.MuleClient;

/**
* @author David Dossot (david@dossot.net)
*/
public class RawHttpMuleClientTest {
public class RawHttpMuleClientTest
{
// TODO reactivate when cookie issues will be fixed in Mule 3
@Ignore
@Test
public void tapHttpTransport() throws Exception {
public void tapHttpTransport() throws Exception
{
// <start id="MuleClient-RawHttp-Transport"/>
final MuleClient muleClient = new MuleClient(true);

final MuleMessage response = muleClient
.send(
"http://www.google.com/finance/historical?q=NASDAQ:GOOG&histperiod=weekly&output=csv",
null, null);
final MuleMessage response = muleClient.send(
"http://www.google.com/finance/historical?q=NASDAQ:GOOG&histperiod=weekly&output=csv", null, null);

final String payload = response.getPayloadAsString();

muleClient.dispose();
// <end id="MuleClient-RawHttp-Transport"/>

assertNotNull(response);
assertNotNull(payload);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package com.muleinaction.muleclient;

import javax.jms.Connection;
Expand All @@ -12,20 +13,22 @@
/**
* @author David Dossot (david@dossot.net)
*/
public class RemoteHttpXmlMuleClientFunctionalTestCase extends
FunctionalTestCase {
public class RemoteHttpXmlMuleClientFunctionalTestCase extends FunctionalTestCase
{

private String queueName;

private String expectedPayload;

@Override
protected String getConfigResources() {
protected String getConfigResources()
{
return "conf/remote-http-xml-muleclient-config.xml";
}

@Override
protected void doSetUp() throws Exception {
protected void doSetUp() throws Exception
{
super.doSetUp();

expectedPayload = RandomStringUtils.randomAlphanumeric(10);
Expand All @@ -34,28 +37,26 @@ protected void doSetUp() throws Exception {

// load a message in the test queue so we can fetch it via the remote
// client
final ConnectionFactory connectionFactory =
(ConnectionFactory) muleContext.getRegistry().lookupObject(
"amqConnectionFactory");
final ConnectionFactory connectionFactory = (ConnectionFactory) muleContext.getRegistry()
.lookupObject("amqConnectionFactory");

final Connection connection = connectionFactory.createConnection();
final Session session =
connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
session.createProducer(session.createQueue(queueName)).send(
session.createTextMessage(expectedPayload));
session.createTextMessage(expectedPayload));
connection.close();
}

public void testJmsAsynchronousRequest() throws Exception {
public void testJmsAsynchronousRequest() throws Exception
{
// <start id="MuleClient-RDA-HTTPXML"/>
final MuleClient muleClient = new MuleClient(false);
final MuleClient muleClient = new MuleClient(muleContext);

final RemoteDispatcher remoteDispatcher =
muleClient.getRemoteDispatcher("http://localhost:8181");
final RemoteDispatcher remoteDispatcher = muleClient.getRemoteDispatcher("http://localhost:8181");
// <end id="MuleClient-RDA-HTTPXML"/>

final Object actualPayload =
remoteDispatcher.receiveRemote("jms://" + queueName, 1000).getPayloadAsString();
final Object actualPayload = remoteDispatcher.receiveRemote("jms://" + queueName, 1000)
.getPayloadAsString();

assertEquals(expectedPayload, actualPayload);

Expand Down
Loading

0 comments on commit 7c8fddc

Please sign in to comment.