From 7cc24bba50008abc7da70aa10d62995dc5bb51bf Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Sun, 2 Nov 2008 17:11:41 +0000 Subject: [PATCH] Moved MessageStore, SimpleMessageStore, and MessageStoringInterceptor to the sandbox. --- .../MessageStoringInterceptor.java | 95 --------------- .../integration/message/MessageStore.java | 40 ------ .../message/SimpleMessageStore.java | 64 ---------- .../message/SimpleMessageStoreTests.java | 115 ------------------ 4 files changed, 314 deletions(-) delete mode 100644 org.springframework.integration/src/main/java/org/springframework/integration/channel/interceptor/MessageStoringInterceptor.java delete mode 100644 org.springframework.integration/src/main/java/org/springframework/integration/message/MessageStore.java delete mode 100644 org.springframework.integration/src/main/java/org/springframework/integration/message/SimpleMessageStore.java delete mode 100644 org.springframework.integration/src/test/java/org/springframework/integration/message/SimpleMessageStoreTests.java diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/channel/interceptor/MessageStoringInterceptor.java b/org.springframework.integration/src/main/java/org/springframework/integration/channel/interceptor/MessageStoringInterceptor.java deleted file mode 100644 index 30b5235317..0000000000 --- a/org.springframework.integration/src/main/java/org/springframework/integration/channel/interceptor/MessageStoringInterceptor.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 2002-2008 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.channel.interceptor; - -import java.util.List; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import org.springframework.integration.channel.ChannelInterceptor; -import org.springframework.integration.core.Message; -import org.springframework.integration.core.MessageChannel; -import org.springframework.integration.core.MessagingException; -import org.springframework.integration.message.MessageStore; -import org.springframework.util.Assert; - -/** - * A {@link ChannelInterceptor} that delegates to a {@link MessageStore}. - * Upon sending to the channel, the message will be added to the store, - * and upon receiving from the channel, the message will be removed from - * the store. - * - * @author Mark Fisher - */ -public class MessageStoringInterceptor extends ChannelInterceptorAdapter { - - private final Log logger = LogFactory.getLog(this.getClass()); - - private final MessageStore messageStore; - - private volatile boolean initialized; - - private final Object initializationMonitor = new Object(); - - - public MessageStoringInterceptor(MessageStore messageStore) { - Assert.notNull(messageStore, "MessageStore must not be null"); - this.messageStore = messageStore; - } - - - @Override - public Message preSend(Message message, MessageChannel channel) { - if (message != null) { - this.messageStore.put(message.getHeaders().getId(), message); - } - return message; - } - - @Override - public boolean preReceive(MessageChannel channel) { - synchronized (this.initializationMonitor) { - if (!this.initialized) { - if (logger.isDebugEnabled()) { - logger.debug("pre-loading Messages from MessageStore for channel '" + channel.getName() + "'"); - } - List> storedMessages = this.messageStore.list(); - for (Message message : storedMessages) { - boolean sent = channel.send(message, 0); - if (!sent) { - throw new MessagingException("failed to initialize channel from MessageStore"); - } - } - if (logger.isDebugEnabled()) { - logger.debug("pre-loaded " + storedMessages.size() + " Messages for channel '" + channel.getName() + "'"); - } - } - this.initialized = true; - } - return true; - } - - @Override - public Message postReceive(Message message, MessageChannel channel) { - if (message != null) { - this.messageStore.remove(message.getHeaders().getId()); - } - return message; - } - -} diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/message/MessageStore.java b/org.springframework.integration/src/main/java/org/springframework/integration/message/MessageStore.java deleted file mode 100644 index ca00c0a017..0000000000 --- a/org.springframework.integration/src/main/java/org/springframework/integration/message/MessageStore.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2002-2008 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.message; - -import java.util.List; - -import org.springframework.integration.core.Message; - -/** - * Strategy interface for storing and retrieving messages. - * - * @author Mark Fisher - */ -public interface MessageStore { - - Message put(Object key, Message message); - - Message get(Object key); - - Message remove(Object key); - - List> list(); - - int size(); - -} diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/message/SimpleMessageStore.java b/org.springframework.integration/src/main/java/org/springframework/integration/message/SimpleMessageStore.java deleted file mode 100644 index abb664fa81..0000000000 --- a/org.springframework.integration/src/main/java/org/springframework/integration/message/SimpleMessageStore.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2002-2008 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.message; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.springframework.integration.core.Message; -import org.springframework.integration.util.BoundedHashMap; -import org.springframework.util.Assert; - -/** - * Map-based implementation of {@link MessageStore} that enforces capacity. - * - * @author Mark Fisher - */ -public class SimpleMessageStore implements MessageStore { - - private final Map> map; - - - public SimpleMessageStore(int capacity) { - this.map = new BoundedHashMap>(capacity); - } - - - public Message put(Object key, Message message) { - Assert.notNull(key, "'key' must not be null"); - Assert.notNull(message, "'message' must not be null"); - return this.map.put(key, message); - } - - public Message get(Object key) { - return (key != null) ? this.map.get(key) : null; - } - - public List> list() { - return new ArrayList>(this.map.values()); - } - - public Message remove(Object key) { - return (key != null) ? this.map.remove(key) : null; - } - - public int size() { - return this.map.size(); - } - -} diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/message/SimpleMessageStoreTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/message/SimpleMessageStoreTests.java deleted file mode 100644 index 222760ea66..0000000000 --- a/org.springframework.integration/src/test/java/org/springframework/integration/message/SimpleMessageStoreTests.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2002-2008 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.message; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -import java.util.List; - -import org.junit.Test; - -import org.springframework.integration.core.Message; - -/** - * @author Mark Fisher - */ -public class SimpleMessageStoreTests { - - @Test - public void testPut() { - SimpleMessageStore store = new SimpleMessageStore(5); - Message message1 = new StringMessage("message-1"); - Message previous = store.put(1, message1); - assertNull(previous); - assertNotNull(store.get(1)); - assertEquals(message1, store.get(1)); - } - - @Test - public void testReplace() { - SimpleMessageStore store = new SimpleMessageStore(5); - Message messageA = new StringMessage("message-a"); - Message messageB = new StringMessage("message-b"); - store.put(1, messageA); - Message previous = store.put(1, messageB); - assertEquals(messageA, previous); - assertEquals(messageB, store.get(1)); - } - - @Test - public void testRemove() { - SimpleMessageStore store = new SimpleMessageStore(5); - Message message = new StringMessage("message"); - assertNull(store.remove(1)); - store.put(1, message); - assertEquals(message, store.remove(1)); - } - - @Test - public void testCapacityEnforced() { - SimpleMessageStore store = new SimpleMessageStore(3); - Message message1 = new StringMessage("message-1"); - Message message2 = new StringMessage("message-2"); - Message message3 = new StringMessage("message-3"); - Message message4 = new StringMessage("message-4"); - store.put(1, message1); - store.put(2, message2); - store.put(3, message3); - assertEquals(3, store.size()); - assertEquals(message1, store.get(1)); - assertEquals(message2, store.get(2)); - assertEquals(message3, store.get(3)); - store.put(4, message4); - assertEquals(3, store.size()); - assertNull(store.get(1)); - assertEquals(message2, store.get(2)); - assertEquals(message3, store.get(3)); - assertEquals(message4, store.get(4)); - } - - @Test - public void testListWhenEmpty() { - SimpleMessageStore store = new SimpleMessageStore(3); - List> list = store.list(); - assertNotNull(list); - assertEquals(0, list.size()); - } - - @Test - public void testListWhenUnderCapacity() { - SimpleMessageStore store = new SimpleMessageStore(3); - store.put(1, new StringMessage("foo")); - store.put(2, new StringMessage("bar")); - List> list = store.list(); - assertEquals(2, list.size()); - } - - @Test - public void testListAfterExceedingCapacity() { - SimpleMessageStore store = new SimpleMessageStore(2); - store.put(1, new StringMessage("foo")); - store.put(2, new StringMessage("bar")); - store.put(3, new StringMessage("baz")); - List> list = store.list(); - assertEquals(2, list.size()); - assertEquals("bar", list.get(0).getPayload()); - assertEquals("baz", list.get(1).getPayload()); - } - -}