Added message handler chain and intercepting handler.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A message handler implementation that intercepts calls to another handler.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public abstract class InterceptingMessageHandler implements MessageHandler {
|
||||
|
||||
private MessageHandler target;
|
||||
|
||||
|
||||
public InterceptingMessageHandler(MessageHandler target) {
|
||||
Assert.notNull(target, "target must not be null");
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public Message handle(Message message) {
|
||||
return handle(message, this.target);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The handler method for subclasses to implement.
|
||||
*
|
||||
* @param message the message to handle
|
||||
* @param target the intercepted handler
|
||||
* @return a reply message or null
|
||||
*/
|
||||
public abstract Message handle(Message message, MessageHandler target);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* A message handler implementation that passes incoming messages through a
|
||||
* chain of handlers.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class MessageHandlerChain implements MessageHandler {
|
||||
|
||||
private List<MessageHandler> handlers = new CopyOnWriteArrayList<MessageHandler>();
|
||||
|
||||
|
||||
/**
|
||||
* Add a handler to the end of the chain.
|
||||
*/
|
||||
public void add(MessageHandler handler) {
|
||||
this.handlers.add(handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a handler to the chain at the specified index.
|
||||
*/
|
||||
public void add(int index, MessageHandler handler) {
|
||||
this.handlers.add(index, handler);
|
||||
}
|
||||
|
||||
public Message handle(Message message) {
|
||||
for (MessageHandler next : handlers) {
|
||||
message = next.handle(message);
|
||||
if (message == null) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.message.DocumentMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class MessageHandlerChainTests {
|
||||
|
||||
@Test
|
||||
public void testSimpleChain() {
|
||||
MessageHandlerChain chain = new MessageHandlerChain();
|
||||
chain.add(new TestHandler("a"));
|
||||
chain.add(new TestHandler("b"));
|
||||
chain.add(new TestHandler("c"));
|
||||
chain.add(new TestHandler("d"));
|
||||
Message result = chain.handle(new DocumentMessage(1, "!"));
|
||||
assertEquals("!abcd", result.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChainWithInterceptors() {
|
||||
MessageHandler handler1 = new TestHandler("*");
|
||||
MessageHandler handler2 = new TestInterceptingHandler("2", handler1);
|
||||
MessageHandler handler3 = new TestInterceptingHandler("3", handler2);
|
||||
MessageHandler handler4 = new TestInterceptingHandler("4", handler3);
|
||||
MessageHandlerChain chain = new MessageHandlerChain();
|
||||
chain.add(new TestHandler("a"));
|
||||
chain.add(handler4);
|
||||
chain.add(new TestHandler("b"));
|
||||
Message result = chain.handle(new DocumentMessage(1, "!"));
|
||||
assertEquals("234!a*234b", result.getPayload());
|
||||
}
|
||||
|
||||
|
||||
private static class TestHandler implements MessageHandler {
|
||||
|
||||
private String text;
|
||||
|
||||
TestHandler(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public Message handle(Message message) {
|
||||
return new DocumentMessage(1, message.getPayload() + text);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class TestInterceptingHandler extends InterceptingMessageHandler {
|
||||
|
||||
private String text;
|
||||
|
||||
TestInterceptingHandler(String text, MessageHandler target) {
|
||||
super(target);
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public Message handle(Message message, MessageHandler target) {
|
||||
message = target.handle(new DocumentMessage(1, text + message.getPayload()));
|
||||
return new DocumentMessage(1, message.getPayload() + text);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user