Added MethodInvokingTransformer.

This commit is contained in:
Mark Fisher
2008-09-03 04:41:46 +00:00
parent cde6f5e23e
commit 2c7981ce04

View File

@@ -0,0 +1,73 @@
/*
* 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.transformer;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Properties;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.MessageHandlingException;
import org.springframework.integration.message.MessageMappingMethodInvoker;
/**
* @author Mark Fisher
*/
public class MethodInvokingTransformer implements Transformer {
private final MessageMappingMethodInvoker invoker;
public MethodInvokingTransformer(Object object, Method method) {
this.invoker = new MessageMappingMethodInvoker(object, method);
}
public MethodInvokingTransformer(Object object, String methodName) {
this.invoker = new MessageMappingMethodInvoker(object, methodName);
}
public Message<?> transform(Message<?> message) {
Object result = this.invoker.invokeMethod(message);
if (result instanceof Properties && !(message.getPayload() instanceof Properties)) {
Properties propertiesToSet = (Properties) result;
MessageBuilder<?> builder = MessageBuilder.fromMessage(message);
for (Object keyObject : propertiesToSet.keySet()) {
String key = (String) keyObject;
builder.setHeader(key, propertiesToSet.getProperty(key));
}
return builder.build();
}
else if (result instanceof Map && !(message.getPayload() instanceof Map)) {
Map<?, ?> attributesToSet = (Map <?, ?>) result;
MessageBuilder<?> builder = MessageBuilder.fromMessage(message);
for (Object key : attributesToSet.keySet()) {
if (!(key instanceof String)) {
throw new MessageHandlingException(message,
"Map returned from a Transformer method must have String-typed keys");
}
builder.setHeader((String) key, attributesToSet.get(key));
}
return builder.build();
}
else {
return MessageBuilder.fromPayload(result).copyHeaders(message.getHeaders()).build();
}
}
}