INT-1129 adding MessageConverter strategy and SimpleMessageConverter implementation

This commit is contained in:
Mark Fisher
2010-08-31 16:41:39 +00:00
parent 93bc11fca7
commit 6c5610693a
3 changed files with 162 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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 {
<P> Message<P> toMessage(Object object);
<P> Object fromMessage(Message<P> message);
}

View File

@@ -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 <P> Message<P> toMessage(Object object) {
try {
return this.inboundMessageMapper.toMessage(object);
}
catch (Exception e) {
throw new MessageConversionException("failed to convert object to Message", e);
}
}
public <P> Object fromMessage(Message<P> 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<Object> {
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<Object> {
public Object fromMessage(Message<?> message) throws Exception {
return (message != null) ? message.getPayload() : null;
}
}
}