001 package org.apache.camel.component.file.strategy;
002
003 import java.io.File;
004
005 import org.apache.camel.Expression;
006 import org.apache.camel.component.file.FileExchange;
007
008 /**
009 * File renamed using {@link Expression} to dynamically compute the file name.
010 * <p/>
011 * If most cases the {@link org.apache.camel.language.simple.FileLanguage FileLanguage} is used to
012 * create the expressions.
013 */
014 public class FileExpressionRenamer implements FileRenamer {
015
016 private static final boolean ON_WINDOWS = System.getProperty("os.name").startsWith("Windows");
017
018 private Expression expression;
019
020 public File renameFile(FileExchange exchange, File file) {
021 if (expression == null) {
022 throw new IllegalArgumentException("Expression is not set");
023 }
024 File parent = file.getParentFile();
025
026 Object result = expression.evaluate(exchange);
027 String name = exchange.getContext().getTypeConverter().convertTo(String.class, result);
028
029 if (ON_WINDOWS && (name.indexOf(":") >= 0 || name.startsWith("//"))) {
030 return new File(name);
031 }
032 return new File(parent, name);
033 }
034
035 public Expression getExpression() {
036 return expression;
037 }
038
039 public void setExpression(Expression expression) {
040 this.expression = expression;
041 }
042 }