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