이 콘텐츠는 선택한 언어로 제공되지 않습니다.
Chapter 16. The File Language
Abstract
The file language is an extension to the simple language, not an independent language in its own right. But the file language extension can only be used in conjunction with File or FTP endpoints.
16.1. When to Use the File Language
Overview
The file language is an extension to the simple language which is not always available. You can use it under the following circumstances:
The escape character, \
, is not available in the file language.
In a File or FTP consumer endpoint
There are several URI options that you can set on a File or FTP consumer endpoint, which take a file language expression as their value. For example, in a File consumer endpoint URI you can set the fileName
, move
, preMove
, moveFailed
, and sortBy
options using a file expression.
In a File consumer endpoint, the fileName
option acts as a filter, determining which file will actually be read from the starting directory. If a plain text string is specified (for example, fileName=report.txt
), the File consumer reads the same file each time it is updated. You can make this option more dynamic, however, by specifying a simple expression. For example, you could use a counter bean to select a different file each time the File consumer polls the starting directory, as follows:
file://target/filelanguage/bean/?fileName=${bean:counter.next}.txt&delete=true
Where the ${bean:counter.next}
expression invokes the next()
method on the bean registered under the ID, counter
.
The move
option is used to move files to a backup location after then have been read by a File consumer endpoint. For example, the following endpoint moves files to a backup directory, after they have been processed:
file://target/filelanguage/?move=backup/${date:now:yyyyMMdd}/${file:name.noext}.bak&recursive=false
Where the ${file:name.noext}.bak
expression modifies the original file name, replacing the file extension with .bak
.
You can use the sortBy
option to specify the order in which file should be processed. For example, to process files according to the alphabetical order of their file name, you could use the following File consumer endpoint:
file://target/filelanguage/?sortBy=file:name
To process file according to the order in which they were last modified, you could use the following File consumer endpoint:
file://target/filelanguage/?sortBy=file:modified
You can reverse the order by adding the reverse:
prefix — for example:
file://target/filelanguage/?sortBy=reverse:file:modified
On exchanges created by a File or FTP consumer
When an exchange originates from a File or FTP consumer endpoint, it is possible to apply file language expressions to the exchange throughout the route (as long as the original message headers are not erased). For example, you could define a content-based router, which routes messages according to their file extension, as follows:
<from uri="file://input/orders"/> <choice> <when> <simple>${file:ext} == 'txt'</simple> <to uri="bean:orderService?method=handleTextFiles"/> </when> <when> <simple>${file:ext} == 'xml'</simple> <to uri="bean:orderService?method=handleXmlFiles"/> </when> <otherwise> <to uri="bean:orderService?method=handleOtherFiles"/> </otherwise> </choice>
16.2. File Variables
Overview
File variables can be used whenever a route starts with a File or FTP consumer endpoint, which implies that the underlying message body is of java.io.File
type. The file variables enable you to access various parts of the file pathname, almost as if you were invoking the methods of the java.io.File
class (in fact, the file language extracts the information it needs from message headers that have been set by the File or FTP endpoint).
Starting directory
Some of file variables return paths that are defined relative to a starting directory, which is just the directory that is specified in the File or FTP endpoint. For example, the following File consumer endpoint has the starting directory, ./filetransfer
(a relative path):
file:filetransfer
The following FTP consumer endpoint has the starting directory, ./ftptransfer
(a relative path):
ftp://myhost:2100/ftptransfer
Naming convention of file variables
In general, the file variables are named after corresponding methods on the java.io.File
class. For example, the file:absolute
variable gives the value that would be returned by the java.io.File.getAbsolute()
method.
This naming convention is not strictly followed, however. For example, there is no such method as java.io.File.getSize()
.
Table of variables
Table 16.1, “Variables for the File Language” shows all of the variable supported by the file language.
Variable | Type | Description |
---|---|---|
|
| The pathname relative to the starting directory. |
|
|
The file extension (characters following the last |
|
|
The file extension (characters following the last |
|
| The pathname relative to the starting directory, omitting the file extension. |
|
| The pathname relative to the starting directory, omitting the file extension. If the file extension has multiple dots, then this expression strips only the last part, and keep the others. |
|
| The final segment of the pathname. That is, the file name without the parent directory path. |
|
| The final segment of the pathname, omitting the file extension. |
|
| The final segment of the pathname, omitting the file extension. If the file extension has multiple dots, then this expression strips only the last part, and keep the others. |
|
|
The file extension (same as |
|
| The pathname of the parent directory, including the starting directory in the path. |
|
| The file pathname, including the starting directory in the path. |
|
|
|
|
| The absolute pathname of the file. |
|
| The size of the referenced file. |
|
|
Same as |
|
| Date last modified. |
16.3. Examples
Relative pathname
Consider a File consumer endpoint, where the starting directory is specified as a relative pathname. For example, the following File endpoint has the starting directory, ./filelanguage
:
file://filelanguage
Now, while scanning the filelanguage
directory, suppose that the endpoint has just consumed the following file:
./filelanguage/test/hello.txt
And, finally, assume that the filelanguage
directory itself has the following absolute location:
/workspace/camel/camel-core/target/filelanguage
Given the preceding scenario, the file language variables return the following values, when applied to the current exchange:
Expression | Result |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Absolute pathname
Consider a File consumer endpoint, where the starting directory is specified as an absolute pathname. For example, the following File endpoint has the starting directory, /workspace/camel/camel-core/target/filelanguage
:
file:///workspace/camel/camel-core/target/filelanguage
Now, while scanning the filelanguage
directory, suppose that the endpoint has just consumed the following file:
./filelanguage/test/hello.txt
Given the preceding scenario, the file language variables return the following values, when applied to the current exchange:
Expression | Result |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|