Refactored payload and header transformers to use abstract base classes.

This commit is contained in:
Mark Fisher
2008-09-03 05:05:04 +00:00
parent b2da8c5f2a
commit 3d27d78084
20 changed files with 149 additions and 398 deletions

View File

@@ -1,30 +0,0 @@
/*
* 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.util.Map;
/**
* A strategy interface for modifying the header values of a message.
*
* @author Mark Fisher
*/
public interface HeaderTransformer {
void transform(Map<String, Object> headers);
}

View File

@@ -1,56 +0,0 @@
/*
* 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.util.HashMap;
import java.util.Map;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.MessagingException;
import org.springframework.util.Assert;
/**
* A {@link MessageHandler} implementation that delegates to an instance
* of the {@link HeaderTransformer} strategy interface to modify the
* header values of a {@link Message}.
*
* @author Mark Fisher
*/
public class HeaderTransformingMessageHandler implements MessageHandler {
private final HeaderTransformer transformer;
public HeaderTransformingMessageHandler(HeaderTransformer transformer) {
Assert.notNull(transformer, "transformer must not be null");
this.transformer = transformer;
}
public Message<?> handle(Message<?> message) {
try {
Map<String, Object> headerMap = new HashMap<String, Object>(message.getHeaders());
this.transformer.transform(headerMap);
return MessageBuilder.fromPayload(message.getPayload()).copyHeaders(headerMap).build();
} catch (Exception e) {
throw new MessagingException(message, "failed to transform message headers", e);
}
}
}

View File

@@ -1,68 +0,0 @@
/*
* 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 org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.util.MethodInvoker;
import org.springframework.integration.util.NameResolvingMethodInvoker;
/**
* @author Mark Fisher
*/
public class MethodInvokingPayloadTransformer implements PayloadTransformer<Object, Object>, InitializingBean {
private volatile Object object;
private volatile String methodName;
private volatile MethodInvoker invoker;
private volatile boolean initialized;
private final Object initializationMonitor = new Object();
public void setObject(Object object) {
this.object = object;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public void afterPropertiesSet() {
synchronized (this.initializationMonitor) {
if (this.initialized) {
return;
}
if (this.object == null || this.methodName == null) {
throw new ConfigurationException("the 'object' and 'methodName' are required");
}
this.invoker = new NameResolvingMethodInvoker(this.object, this.methodName);
this.initialized = true;
}
}
public Object transform(Object payload) throws Exception {
if (!this.initialized) {
this.afterPropertiesSet();
}
return this.invoker.invokeMethod(payload);
}
}

View File

@@ -1,26 +0,0 @@
/*
* 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;
/**
* @author Mark Fisher
*/
public interface PayloadTransformer<T, U> {
U transform(T payload) throws Exception;
}

View File

@@ -1,49 +0,0 @@
/*
* 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 org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.MessagingException;
import org.springframework.util.Assert;
/**
* @author Mark Fisher
*/
@SuppressWarnings("unchecked")
public class PayloadTransformingMessageHandler implements MessageHandler {
private final PayloadTransformer transformer;
public PayloadTransformingMessageHandler(PayloadTransformer transformer) {
Assert.notNull(transformer, "transformer must not be null");
this.transformer = transformer;
}
public Message<?> handle(Message<?> message) {
try {
Object result = this.transformer.transform(message.getPayload());
return MessageBuilder.fromPayload(result).copyHeaders(message.getHeaders()).build();
} catch (Exception e) {
throw new MessagingException(message, "failed to transform message payload", e);
}
}
}

View File

@@ -19,11 +19,9 @@ package org.springframework.integration.transformer.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.transformer.PayloadTransformer;
import org.springframework.integration.transformer.PayloadTransformingMessageHandler;
import org.springframework.integration.transformer.AbstractPayloadTransformer;
/**
* @author Mark Fisher
@@ -42,19 +40,15 @@ public abstract class AbstractPayloadTransformerParser extends AbstractSingleBea
@Override
protected Class<?> getBeanClass(Element element) {
return PayloadTransformingMessageHandler.class;
return this.getTransformerClass();
}
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
BeanDefinitionBuilder transformerBuilder = BeanDefinitionBuilder.genericBeanDefinition(this.getTransformerClass());
this.parsePayloadTransformer(element, parserContext, transformerBuilder);
String transformerBeanName = BeanDefinitionReaderUtils.registerWithGeneratedName(
transformerBuilder.getBeanDefinition(), parserContext.getRegistry());
builder.addConstructorArgReference(transformerBeanName);
this.parsePayloadTransformer(element, parserContext, builder);
}
protected abstract Class<? extends PayloadTransformer<?, ?>> getTransformerClass();
protected abstract Class<? extends AbstractPayloadTransformer<?, ?>> getTransformerClass();
protected abstract void parsePayloadTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder);