Skip to content

Commit

Permalink
CAMEL-6748: Optimize File Producers by Skipping Header Evaluation. Th…
Browse files Browse the repository at this point in the history
…ansk to James Carman for the patch.
  • Loading branch information
davsclaus committed Sep 15, 2013
1 parent 1657378 commit 5ba8f63
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 83 deletions.
Expand Up @@ -286,30 +286,41 @@ public String createFileName(Exchange exchange) {
String answer;

// overrule takes precedence
String overrule = exchange.getIn().getHeader(Exchange.OVERRULE_FILE_NAME, String.class);
String consumed = exchange.getIn().getHeader(Exchange.FILE_NAME_CONSUMED, String.class);
String name = overrule == null ? exchange.getIn().getHeader(Exchange.FILE_NAME, String.class) : overrule;
Object value;

Object overrule = exchange.getIn().getHeader(Exchange.OVERRULE_FILE_NAME);
if (overrule != null) {
if (overrule instanceof Expression) {
value = overrule;
} else {
value = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, overrule);
}
} else {
value = exchange.getIn().getHeader(Exchange.FILE_NAME);
}

// if we have an overrule then override the existing header to use the overrule computed name from this point forward
if (overrule != null) {
exchange.getIn().setHeader(Exchange.FILE_NAME, name);
exchange.getIn().setHeader(Exchange.FILE_NAME, value);
}

if (value != null && value instanceof String && StringHelper.hasStartToken((String) value, "simple")) {
log.warn("Simple expression: {} detected in header: {} of type String. This feature has been removed (see CAMEL-6748).", value, Exchange.FILE_NAME);
}

// expression support
Expression expression = endpoint.getFileName();

if (name != null && !name.equals(consumed)) {
// the header name can be an expression too, that should override
// whatever configured on the endpoint
if (StringHelper.hasStartToken(name, "simple")) {
log.trace("{} contains a Simple expression: {}", Exchange.FILE_NAME, name);
Language language = getEndpoint().getCamelContext().resolveLanguage("file");
expression = language.createExpression(name);
}
if (value != null && value instanceof Expression) {
expression = (Expression) value;
}

// evaluate the name as a String from the value
String name;
if (expression != null) {
log.trace("Filename evaluated as expression: {}", expression);
name = expression.evaluate(exchange, String.class);
} else {
name = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, value);
}

// flatten name
Expand Down
Expand Up @@ -22,6 +22,8 @@
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.JndiRegistry;

import static org.apache.camel.language.simple.SimpleLanguage.simple;

/**
* Unit test for expression option for file consumer.
*/
Expand Down Expand Up @@ -70,7 +72,7 @@ public void configure() throws Exception {
public void testConsumeFileBasedOnDatePattern() throws Exception {
template.sendBodyAndHeader("file://target/filelanguage/date", "Bye World", Exchange.FILE_NAME, "myfile-20081128.txt");
template.sendBodyAndHeader("file://target/filelanguage/date", "Hello World", Exchange.FILE_NAME, "myfile-20081129.txt");
template.sendBodyAndHeader("file://target/filelanguage/date", "Goodday World", Exchange.FILE_NAME, "myfile-${date:now:yyyyMMdd}.txt");
template.sendBodyAndHeader("file://target/filelanguage/date", "Goodday World", Exchange.FILE_NAME, simple("myfile-${date:now:yyyyMMdd}.txt"));

context.addRoutes(new RouteBuilder() {
@Override
Expand Down

This file was deleted.

Expand Up @@ -24,6 +24,8 @@
import org.apache.camel.Exchange;
import org.apache.camel.impl.JndiRegistry;

import static org.apache.camel.language.simple.SimpleLanguage.simple;

/**
* Unit test for expression option for file producer.
*/
Expand All @@ -42,11 +44,9 @@ protected JndiRegistry createRegistry() throws Exception {
return jndi;
}

public void testProduceBeanByHeader() throws Exception {
template.sendBodyAndHeader("file://target/filelanguage", "Hello World",
Exchange.FILE_NAME, "${bean:myguidgenerator}.bak");

assertFileExists("target/filelanguage/123.bak");
public void testProducerFileNameHeaderNotEvaluated() {
template.sendBodyAndHeader("file://target/filelanguage", "Hello World", Exchange.FILE_NAME, "$simple{myfile-${date:now:yyyyMMdd}}.txt");
assertFileExists("target/filelanguage/$simple{myfile-${date:now:yyyyMMdd}}.txt");
}

public void testProduceBeanByExpression() throws Exception {
Expand All @@ -57,7 +57,7 @@ public void testProduceBeanByExpression() throws Exception {

public void testProducerDateByHeader() throws Exception {
template.sendBodyAndHeader("file://target/filelanguage", "Hello World",
Exchange.FILE_NAME, "myfile-${date:now:yyyyMMdd}.txt");
Exchange.FILE_NAME, simple("myfile-${date:now:yyyyMMdd}.txt"));

String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
assertFileExists("target/filelanguage/myfile-" + date + ".txt");
Expand Down
Expand Up @@ -32,6 +32,8 @@
import org.junit.After;
import org.junit.Before;

import static org.apache.camel.language.simple.SimpleLanguage.simple;

/**
* Base class for unit testing using a FTPServer
*/
Expand Down Expand Up @@ -125,7 +127,7 @@ protected FtpServerFactory createFtpServerFactory() throws Exception {
}

public void sendFile(String url, Object body, String fileName) {
template.sendBodyAndHeader(url, body, Exchange.FILE_NAME, fileName);
template.sendBodyAndHeader(url, body, Exchange.FILE_NAME, simple(fileName));
}

}

0 comments on commit 5ba8f63

Please sign in to comment.