Created
February 9, 2012 20:52
-
-
Save aheusingfeld/1782994 to your computer and use it in GitHub Desktop.
Class to be used as a Transformer in a Spring Integration workflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.fittransporte.fitdb.util.integration; | |
import de.fittransporte.fitdb.util.ExpressionUtil; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.BeanWrapper; | |
import org.springframework.beans.PropertyAccessorFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.core.convert.ConversionService; | |
import org.springframework.expression.EvaluationException; | |
import org.springframework.integration.Message; | |
import org.springframework.integration.transformer.Transformer; | |
import org.springframework.util.Assert; | |
import java.util.Map; | |
/** | |
* Sets properties of the <em>targetObject</em> using the specified <em>propertyMapping</em> with the incoming message. | |
* If targetObject is not specified the <em>message.getPayload()</em> is set as <em>targetObject</em> | |
* | |
* Configuration sample: | |
* <pre> | |
<int:transformer method="transform"> | |
<bean id="mapToSalesOrderPalletsTransformer" class="de.fittransporte.fitdb.util.integration.BeanPropertySettingTransformer"> | |
<property name="propertyMapping"> | |
<util:map> | |
<entry key="salesOrder" value="%{headers['salesorder']}"/> | |
<entry key="palletCount" value="%{headers['pallet']['ANZAHL_PALETTEN']}"/> | |
<entry key="palletType" value="%{headers['pallet']['PALETTENTYP']}"/> | |
<entry key="palletTypeString" value="%{headers['pallet']['PALETTENTYP']}"/> | |
</util:map> | |
</property> | |
</bean> | |
</int:transformer> | |
* </pre> | |
*/ | |
public class BeanPropertySettingTransformer implements Transformer | |
{ | |
private static final Logger LOG = LoggerFactory.getLogger(BeanPropertySettingTransformer.class); | |
private ConversionService conversionService; | |
private Map<String, Object> propertyMapping; | |
private Object targetObject; | |
@Override | |
public Message<?> transform(final Message<?> message) | |
{ | |
Assert.notNull(propertyMapping, "'propertyMapping' must be set beforehand!"); | |
targetObject = message.getPayload(); | |
for (String property : propertyMapping.keySet()) | |
{ | |
Object propValue = propertyMapping.get(property); | |
final BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(targetObject); | |
final Class targetType = wrapper.getPropertyType(property); | |
if (propValue instanceof String) | |
{ | |
try { | |
propValue = ExpressionUtil.parse((String) propValue, message, conversionService, targetType); | |
} catch (EvaluationException evex) | |
{ | |
LOG.error("Error evaluating the specified expression", evex); | |
propValue = null; | |
} | |
} | |
wrapper.setPropertyValue(property, propValue); | |
} | |
return message; | |
} | |
public void setPropertyMapping(Map<String, Object> propertyMapping) | |
{ | |
this.propertyMapping = propertyMapping; | |
} | |
public Map<String, Object> getPropertyMapping() | |
{ | |
return propertyMapping; | |
} | |
public Object getTargetObject() | |
{ | |
return targetObject; | |
} | |
public void setTargetObject(final Object targetObject) | |
{ | |
this.targetObject = targetObject; | |
} | |
public ConversionService getConversionService() | |
{ | |
return conversionService; | |
} | |
@Autowired | |
public void setConversionService(final ConversionService conversionService) | |
{ | |
this.conversionService = conversionService; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment