diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/message/DefaultMessageHeader.java b/org.springframework.integration/src/main/java/org/springframework/integration/message/DefaultMessageHeader.java new file mode 100644 index 0000000000..8ef60fb33b --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/message/DefaultMessageHeader.java @@ -0,0 +1,162 @@ +/* + * 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.io.Serializable; +import java.util.Date; +import java.util.HashSet; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * The default implementation of the {@link MessageHeader} interface. + * + * @author Mark Fisher + */ +public class DefaultMessageHeader implements MessageHeader, Serializable { + + private final Date timestamp = new Date(); + + private volatile Date expiration; + + private volatile Object correlationId; + + private transient volatile Object returnAddress; + + private volatile int sequenceNumber = 1; + + private volatile int sequenceSize = 1; + + private volatile MessagePriority priority = MessagePriority.NORMAL; + + private final Properties properties = new Properties(); + + private final ConcurrentMap attributes = new ConcurrentHashMap(); + + + /** + * Return the creation time of this message. + */ + public Date getTimestamp() { + return this.timestamp; + } + + /** + * Return the expiration date for this message or null to + * indicate 'never expire'. + */ + public Date getExpiration() { + return this.expiration; + } + + /** + * Set the expiration date for this message or null to + * indicate 'never expire'. The default is null. + */ + public void setExpiration(Date expiration) { + this.expiration = expiration; + } + + public Object getCorrelationId() { + return this.correlationId; + } + + public void setCorrelationId(Object correlationId) { + this.correlationId = correlationId; + } + + public Object getReturnAddress() { + return this.returnAddress; + } + + public void setReturnAddress(Object returnAddress) { + this.returnAddress = returnAddress; + } + + public int getSequenceNumber() { + return this.sequenceNumber; + } + + public void setSequenceNumber(int sequenceNumber) { + this.sequenceNumber = sequenceNumber; + } + + public int getSequenceSize() { + return this.sequenceSize; + } + + public void setSequenceSize(int sequenceSize) { + this.sequenceSize = sequenceSize; + } + + public MessagePriority getPriority() { + return this.priority; + } + + public void setPriority(MessagePriority priority) { + this.priority = priority; + } + + public String getProperty(String key) { + return this.properties.getProperty(key); + } + + public String setProperty(String key, String value) { + return (String) this.properties.setProperty(key, value); + } + + public String removeProperty(String key) { + return (String) this.properties.remove(key); + } + + public Set getPropertyNames() { + Set propertyNames = new HashSet(); + for (Object key : this.properties.keySet()) { + propertyNames.add((String) key); + } + return propertyNames; + } + + public Object getAttribute(String key) { + return this.attributes.get(key); + } + + public Object setAttribute(String key, Object value) { + return this.attributes.put(key, value); + } + + public Object setAttributeIfAbsent(String key, Object value) { + return this.attributes.putIfAbsent(key, value); + } + + public Object removeAttribute(String key) { + return this.attributes.remove(key); + } + + public Set getAttributeNames() { + return this.attributes.keySet(); + } + + public String toString() { + return "[CorrelationID=" + this.correlationId + "][Properties=" + this.properties + "][Attributes=" + this.attributes + + "][Timestamp=" + this.timestamp + "][Expiration=" + this.expiration + "][Priority=" + this.priority + + "][Sequence #" + this.sequenceNumber + " (of " + this.sequenceSize + ")]"; + } + +} diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/message/GenericMessage.java b/org.springframework.integration/src/main/java/org/springframework/integration/message/GenericMessage.java index 43544af848..3fd0967aa2 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/message/GenericMessage.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/message/GenericMessage.java @@ -32,7 +32,7 @@ public class GenericMessage implements Message { private final Object id; - private final MessageHeader header = new MessageHeader(); + private final MessageHeader header = new DefaultMessageHeader(); private volatile T payload; diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/message/MessageHeader.java b/org.springframework.integration/src/main/java/org/springframework/integration/message/MessageHeader.java index 59d3280a20..48651bdeaa 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/message/MessageHeader.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/message/MessageHeader.java @@ -16,13 +16,8 @@ package org.springframework.integration.message; -import java.io.Serializable; import java.util.Date; -import java.util.HashSet; -import java.util.Properties; import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; /** * A holder for Message metadata. This includes information that may be used by @@ -33,134 +28,50 @@ import java.util.concurrent.ConcurrentMap; * * @author Mark Fisher */ -public class MessageHeader implements Serializable { +public interface MessageHeader { - private final Date timestamp = new Date(); + Date getTimestamp(); - private volatile Date expiration; + Date getExpiration(); - private volatile Object correlationId; + void setExpiration(Date expiration); - private transient volatile Object returnAddress; + Object getCorrelationId(); - private volatile int sequenceNumber = 1; + void setCorrelationId(Object correlationId); - private volatile int sequenceSize = 1; + Object getReturnAddress(); - private volatile MessagePriority priority = MessagePriority.NORMAL; + void setReturnAddress(Object returnAddress); - private final Properties properties = new Properties(); + int getSequenceNumber(); - private final ConcurrentMap attributes = new ConcurrentHashMap(); + void setSequenceNumber(int sequenceNumber); + int getSequenceSize(); - /** - * Return the creation time of this message. - */ - public Date getTimestamp() { - return this.timestamp; - } + void setSequenceSize(int sequenceSize); - /** - * Return the expiration date for this message or null to - * indicate 'never expire'. - */ - public Date getExpiration() { - return this.expiration; - } + MessagePriority getPriority(); - /** - * Set the expiration date for this message or null to - * indicate 'never expire'. The default is null. - */ - public void setExpiration(Date expiration) { - this.expiration = expiration; - } + void setPriority(MessagePriority priority); - public Object getCorrelationId() { - return this.correlationId; - } + String getProperty(String key); - public void setCorrelationId(Object correlationId) { - this.correlationId = correlationId; - } + String setProperty(String key, String value); - public Object getReturnAddress() { - return this.returnAddress; - } + String removeProperty(String key); - public void setReturnAddress(Object returnAddress) { - this.returnAddress = returnAddress; - } + Set getPropertyNames(); - public int getSequenceNumber() { - return this.sequenceNumber; - } + Object getAttribute(String key); - public void setSequenceNumber(int sequenceNumber) { - this.sequenceNumber = sequenceNumber; - } + Object setAttribute(String key, Object value); - public int getSequenceSize() { - return this.sequenceSize; - } + Object setAttributeIfAbsent(String key, Object value); - public void setSequenceSize(int sequenceSize) { - this.sequenceSize = sequenceSize; - } + Object removeAttribute(String key); - public MessagePriority getPriority() { - return this.priority; - } - - public void setPriority(MessagePriority priority) { - this.priority = priority; - } - - public String getProperty(String key) { - return this.properties.getProperty(key); - } - - public String setProperty(String key, String value) { - return (String) this.properties.setProperty(key, value); - } - - public String removeProperty(String key) { - return (String) this.properties.remove(key); - } - - public Set getPropertyNames() { - Set propertyNames = new HashSet(); - for (Object key : this.properties.keySet()) { - propertyNames.add((String) key); - } - return propertyNames; - } - - public Object getAttribute(String key) { - return this.attributes.get(key); - } - - public Object setAttribute(String key, Object value) { - return this.attributes.put(key, value); - } - - public Object setAttributeIfAbsent(String key, Object value) { - return this.attributes.putIfAbsent(key, value); - } - - public Object removeAttribute(String key) { - return this.attributes.remove(key); - } - - public Set getAttributeNames() { - return this.attributes.keySet(); - } - - public String toString() { - return "[CorrelationID=" + this.correlationId + "][Properties=" + this.properties + "][Attributes=" + this.attributes + - "][Timestamp=" + this.timestamp + "][Expiration=" + this.expiration + "][Priority=" + this.priority + - "][Sequence #" + this.sequenceNumber + " (of " + this.sequenceSize + ")]"; - } + Set getAttributeNames(); } diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/MessageHeaderTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/message/DefaultMessageHeaderTests.java similarity index 87% rename from org.springframework.integration/src/test/java/org/springframework/integration/MessageHeaderTests.java rename to org.springframework.integration/src/test/java/org/springframework/integration/message/DefaultMessageHeaderTests.java index 2717567295..9fca868c1f 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/MessageHeaderTests.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/message/DefaultMessageHeaderTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.integration; +package org.springframework.integration.message; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -26,22 +26,23 @@ import java.util.Set; import org.junit.Test; +import org.springframework.integration.message.DefaultMessageHeader; import org.springframework.integration.message.MessageHeader; /** * @author Mark Fisher */ -public class MessageHeaderTests { +public class DefaultMessageHeaderTests { @Test public void testTimestamp() { - MessageHeader header = new MessageHeader(); + MessageHeader header = new DefaultMessageHeader(); assertNotNull(header.getTimestamp()); } @Test public void testAttributes() { - MessageHeader header = new MessageHeader(); + MessageHeader header = new DefaultMessageHeader(); Integer value = new Integer(123); Object previousValue = header.setAttribute("test", value); assertNull(previousValue); @@ -58,7 +59,7 @@ public class MessageHeaderTests { @Test public void testProperties() { - MessageHeader header = new MessageHeader(); + MessageHeader header = new DefaultMessageHeader(); String previousValue = header.setProperty("foo", "bar"); assertNull(previousValue); assertEquals("bar", header.getProperty("foo")); @@ -73,7 +74,7 @@ public class MessageHeaderTests { @Test public void testSetAttributeIfAbsent() { - MessageHeader header = new MessageHeader(); + MessageHeader header = new DefaultMessageHeader(); Integer integer = new Integer(123); assertNull(header.getAttribute("test")); assertFalse(header.getAttributeNames().contains("test")); @@ -85,7 +86,7 @@ public class MessageHeaderTests { @Test public void testSetAttributeIfAbsentDoesNotOverride() { - MessageHeader header = new MessageHeader(); + MessageHeader header = new DefaultMessageHeader(); Integer originalValue = new Integer(123); header.setAttributeIfAbsent("test", originalValue); Object existingValue = header.setAttributeIfAbsent("test", new Integer(456)); diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/message/GenericMessageTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/message/GenericMessageTests.java index da019f6e2c..96153b204a 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/message/GenericMessageTests.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/message/GenericMessageTests.java @@ -58,7 +58,7 @@ public class GenericMessageTests { @Test public void testMessageHeaderCopied() { - MessageHeader header = new MessageHeader(); + MessageHeader header = new DefaultMessageHeader(); header.setAttribute("testAttribute", new Integer(123)); header.setProperty("testProperty", "foo"); header.setSequenceSize(42);