CAMEL-10838: camel-cache - Create a better body replacer processor
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0d3f461f Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0d3f461f Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0d3f461f Branch: refs/heads/master Commit: 0d3f461f6f576b74aae8c02118689c570c48d4f2 Parents: bdf4aaa Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Feb 15 22:00:33 2017 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Feb 15 22:01:21 2017 +0100 ---------------------------------------------------------------------- .../cache/CacheBasedTokenReplacer.java | 46 ++++++++++---- .../cache/CacheBasedXPathReplacer.java | 65 +++++++++++++------- 2 files changed, 77 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/0d3f461f/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedTokenReplacer.java ---------------------------------------------------------------------- diff --git a/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedTokenReplacer.java b/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedTokenReplacer.java index 0fadb72..82d72d1 100755 --- a/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedTokenReplacer.java +++ b/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedTokenReplacer.java @@ -22,7 +22,10 @@ import net.sf.ehcache.CacheManager; import net.sf.ehcache.Ehcache; import org.apache.camel.Exchange; +import org.apache.camel.Expression; import org.apache.camel.Processor; +import org.apache.camel.Service; +import org.apache.camel.builder.ExpressionBuilder; import org.apache.camel.component.cache.CacheConstants; import org.apache.camel.component.cache.DefaultCacheManagerFactory; import org.apache.camel.converter.IOConverter; @@ -30,15 +33,18 @@ import org.apache.camel.util.IOHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class CacheBasedTokenReplacer extends CacheValidate implements Processor { +public class CacheBasedTokenReplacer extends CacheValidate implements Processor, Service { private static final Logger LOG = LoggerFactory.getLogger(CacheBasedTokenReplacer.class); + private CacheManager cacheManager; private String cacheName; - private String key; + private Expression key; private String replacementToken; - private CacheManager cacheManager; - private Ehcache cache; public CacheBasedTokenReplacer(String cacheName, String key, String replacementToken) { + this(cacheName, ExpressionBuilder.constantExpression(key), replacementToken); + } + + public CacheBasedTokenReplacer(String cacheName, Expression key, String replacementToken) { if (cacheName.contains("cache://")) { this.setCacheName(cacheName.replace("cache://", "")); } else { @@ -49,17 +55,16 @@ public class CacheBasedTokenReplacer extends CacheValidate implements Processor } public void process(Exchange exchange) throws Exception { - // Cache the buffer to the specified Cache against the specified key - cacheManager = new DefaultCacheManagerFactory().getInstance(); + String cacheKey = key.evaluate(exchange, String.class); - if (isValid(cacheManager, cacheName, key)) { - cache = cacheManager.getCache(cacheName); + if (isValid(cacheManager, cacheName, cacheKey)) { + Ehcache cache = cacheManager.getCache(cacheName); if (LOG.isDebugEnabled()) { LOG.debug("Replacing Token {} in Message with value stored against key {} in CacheName {}", - new Object[]{replacementToken, key, cacheName}); + new Object[]{replacementToken, cacheKey, cacheName}); } - exchange.getIn().setHeader(CacheConstants.CACHE_KEY, key); + exchange.getIn().setHeader(CacheConstants.CACHE_KEY, cacheKey); Object body = exchange.getIn().getBody(); InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, body); @@ -72,7 +77,7 @@ public class CacheBasedTokenReplacer extends CacheValidate implements Processor // Note: The value in the cache must be a String String cacheValue = exchange.getContext().getTypeConverter() - .convertTo(String.class, cache.get(key).getObjectValue()); + .convertTo(String.class, cache.get(cacheKey).getObjectValue()); String replacedTokenString = new String(buffer).replaceAll(replacementToken, cacheValue); LOG.trace("replacedTokenString = {}", replacedTokenString); @@ -88,11 +93,15 @@ public class CacheBasedTokenReplacer extends CacheValidate implements Processor this.cacheName = cacheName; } - public String getKey() { + public Expression getKey() { return key; } public void setKey(String key) { + this.key = ExpressionBuilder.constantExpression(key); + } + + public void setKey(Expression key) { this.key = key; } @@ -104,4 +113,17 @@ public class CacheBasedTokenReplacer extends CacheValidate implements Processor this.replacementToken = replacementToken; } + @Override + public void start() throws Exception { + // Cache the buffer to the specified Cache against the specified key + if (cacheManager == null) { + cacheManager = new DefaultCacheManagerFactory().getInstance(); + } + } + + @Override + public void stop() throws Exception { + // noop + } + } http://git-wip-us.apache.org/repos/asf/camel/blob/0d3f461f/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java ---------------------------------------------------------------------- diff --git a/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java b/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java index 3077381..44810ab 100755 --- a/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java +++ b/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java @@ -19,7 +19,6 @@ package org.apache.camel.processor.cache; import java.io.File; import java.io.InputStream; import java.io.StringReader; - import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; @@ -30,8 +29,12 @@ import org.w3c.dom.Document; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Ehcache; + import org.apache.camel.Exchange; +import org.apache.camel.Expression; import org.apache.camel.Processor; +import org.apache.camel.Service; +import org.apache.camel.builder.ExpressionBuilder; import org.apache.camel.component.cache.CacheConstants; import org.apache.camel.component.cache.DefaultCacheManagerFactory; import org.apache.camel.converter.IOConverter; @@ -40,18 +43,20 @@ import org.apache.camel.util.IOHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class CacheBasedXPathReplacer extends CacheValidate implements Processor { +public class CacheBasedXPathReplacer extends CacheValidate implements Processor, Service { private static final Logger LOG = LoggerFactory.getLogger(CacheBasedXPathReplacer.class); + + private CacheManager cacheManager; + private String cacheName; - private String key; + private Expression key; private String xpath; - private CacheManager cacheManager; - private Ehcache cache; - private Document document; - private DOMSource source; - private DOMResult result; public CacheBasedXPathReplacer(String cacheName, String key, String xpath) { + this(cacheName, ExpressionBuilder.constantExpression(key), xpath); + } + + public CacheBasedXPathReplacer(String cacheName, Expression key, String xpath) { if (cacheName.contains("cache://")) { this.setCacheName(cacheName.replace("cache://", "")); } else { @@ -62,19 +67,19 @@ public class CacheBasedXPathReplacer extends CacheValidate implements Processor } public void process(Exchange exchange) throws Exception { - // Cache the buffer to the specified Cache against the specified key - cacheManager = new DefaultCacheManagerFactory().getInstance(); + String cacheKey = key.evaluate(exchange, String.class); - if (isValid(cacheManager, cacheName, key)) { - cache = cacheManager.getCache(cacheName); + if (isValid(cacheManager, cacheName, cacheKey)) { + Ehcache cache = cacheManager.getCache(cacheName); if (LOG.isDebugEnabled()) { LOG.debug("Replacing XPath value {} in Message with value stored against key {} in CacheName {}", - new Object[]{xpath, key, cacheName}); + new Object[]{xpath, cacheKey, cacheName}); } - exchange.getIn().setHeader(CacheConstants.CACHE_KEY, key); + exchange.getIn().setHeader(CacheConstants.CACHE_KEY, cacheKey); Object body = exchange.getIn().getBody(); InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, body); + Document document; try { document = exchange.getContext().getTypeConverter().convertTo(Document.class, exchange, is); } finally { @@ -82,7 +87,7 @@ public class CacheBasedXPathReplacer extends CacheValidate implements Processor } InputStream cis = exchange.getContext().getTypeConverter() - .convertTo(InputStream.class, cache.get(key).getObjectValue()); + .convertTo(InputStream.class, cache.get(cacheKey).getObjectValue()); try { Document cacheValueDocument = exchange.getContext().getTypeConverter() @@ -95,19 +100,19 @@ public class CacheBasedXPathReplacer extends CacheValidate implements Processor Source xslSource = xmlConverter.toStreamSource(new StringReader(xslString)); TransformerFactory transformerFactory = xmlConverter.createTransformerFactory(); Transformer transformer = transformerFactory.newTransformer(xslSource); - source = xmlConverter.toDOMSource(document); - result = new DOMResult(); + DOMSource source = xmlConverter.toDOMSource(document); + DOMResult result = new DOMResult(); transformer.setParameter("cacheValue", cacheValueDocument); transformer.transform(source, result); + + // DOMSource can be converted to byte[] by camel type converter mechanism + DOMSource dom = new DOMSource(result.getNode()); + exchange.getIn().setBody(dom, byte[].class); } finally { IOHelper.close(cis, "cis", LOG); } } - - // DOMSource can be converted to byte[] by camel type converter mechanism - DOMSource dom = new DOMSource(result.getNode()); - exchange.getIn().setBody(dom, byte[].class); } public String getCacheName() { @@ -118,11 +123,15 @@ public class CacheBasedXPathReplacer extends CacheValidate implements Processor this.cacheName = cacheName; } - public String getKey() { + public Expression getKey() { return key; } public void setKey(String key) { + this.key = ExpressionBuilder.constantExpression(key); + } + + public void setKey(Expression key) { this.key = key; } @@ -134,4 +143,16 @@ public class CacheBasedXPathReplacer extends CacheValidate implements Processor this.xpath = xpath; } + @Override + public void start() throws Exception { + // Cache the buffer to the specified Cache against the specified key + if (cacheManager == null) { + cacheManager = new DefaultCacheManagerFactory().getInstance(); + } + } + + @Override + public void stop() throws Exception { + // noop + } }