From 6c5610693ae864c7e36ddb9480867c00a599dfc6 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Tue, 31 Aug 2010 16:41:39 +0000 Subject: [PATCH] INT-1129 adding MessageConverter strategy and SimpleMessageConverter implementation --- .../converter/MessageConversionException.java | 37 ++++++++ .../support/converter/MessageConverter.java | 31 ++++++ .../converter/SimpleMessageConverter.java | 94 +++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/support/converter/MessageConversionException.java create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/support/converter/MessageConverter.java create mode 100644 spring-integration-core/src/main/java/org/springframework/integration/support/converter/SimpleMessageConverter.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/support/converter/MessageConversionException.java b/spring-integration-core/src/main/java/org/springframework/integration/support/converter/MessageConversionException.java new file mode 100644 index 0000000000..a0fc5eb54a --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/support/converter/MessageConversionException.java @@ -0,0 +1,37 @@ +/* + * Copyright 2002-2010 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.support.converter; + +import org.springframework.integration.Message; +import org.springframework.integration.MessagingException; + +/** + * @author Mark Fisher + * @since 2.0 + */ +@SuppressWarnings("serial") +public class MessageConversionException extends MessagingException { + + public MessageConversionException(String description, Throwable cause) { + super(description, cause); + } + + public MessageConversionException(Message failedMessage, String description, Throwable cause) { + super(failedMessage, description, cause); + } + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/support/converter/MessageConverter.java b/spring-integration-core/src/main/java/org/springframework/integration/support/converter/MessageConverter.java new file mode 100644 index 0000000000..26be1d55f3 --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/support/converter/MessageConverter.java @@ -0,0 +1,31 @@ +/* + * Copyright 2002-2010 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.support.converter; + +import org.springframework.integration.Message; + +/** + * @author Mark Fisher + * @since 2.0 + */ +public interface MessageConverter { + +

Message

toMessage(Object object); + +

Object fromMessage(Message

message); + +} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/support/converter/SimpleMessageConverter.java b/spring-integration-core/src/main/java/org/springframework/integration/support/converter/SimpleMessageConverter.java new file mode 100644 index 0000000000..5b90380282 --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/support/converter/SimpleMessageConverter.java @@ -0,0 +1,94 @@ +/* + * Copyright 2002-2010 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.support.converter; + +import org.springframework.integration.Message; +import org.springframework.integration.mapping.InboundMessageMapper; +import org.springframework.integration.mapping.OutboundMessageMapper; +import org.springframework.integration.support.MessageBuilder; + +/** + * @author Mark Fisher + * @since 2.0 + */ +@SuppressWarnings({"unchecked", "rawtypes"}) +public class SimpleMessageConverter implements MessageConverter { + + private final InboundMessageMapper inboundMessageMapper; + + private final OutboundMessageMapper outboundMessageMapper; + + + public SimpleMessageConverter() { + this(null, null); + } + + public SimpleMessageConverter(InboundMessageMapper inboundMessageMapper) { + this(inboundMessageMapper, null); + } + + public SimpleMessageConverter(OutboundMessageMapper outboundMessageMapper) { + this(null, outboundMessageMapper); + } + + public SimpleMessageConverter(InboundMessageMapper inboundMessageMapper, OutboundMessageMapper outboundMessageMapper) { + this.inboundMessageMapper = (inboundMessageMapper != null) ? inboundMessageMapper : new DefaultInboundMessageMapper(); + this.outboundMessageMapper = (outboundMessageMapper != null) ? outboundMessageMapper : new DefaultOutboundMessageMapper(); + } + + + public

Message

toMessage(Object object) { + try { + return this.inboundMessageMapper.toMessage(object); + } + catch (Exception e) { + throw new MessageConversionException("failed to convert object to Message", e); + } + } + + public

Object fromMessage(Message

message) { + try { + return this.outboundMessageMapper.fromMessage(message); + } + catch (Exception e) { + throw new MessageConversionException(message, "failed to convert Message to object", e); + } + } + + + private static class DefaultInboundMessageMapper implements InboundMessageMapper { + + public Message toMessage(Object object) throws Exception { + if (object == null) { + return null; + } + if (object instanceof Message) { + return (Message) object; + } + return MessageBuilder.withPayload(object).build(); + } + } + + + private static class DefaultOutboundMessageMapper implements OutboundMessageMapper { + + public Object fromMessage(Message message) throws Exception { + return (message != null) ? message.getPayload() : null; + } + } + +}