01: /*
02: * $Id: SimpleFilenameParser.java 10489 2008-01-23 17:53:38Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.transport.file;
12:
13: import org.mule.api.transport.MessageAdapter;
14: import org.mule.util.DateUtils;
15: import org.mule.util.TemplateParser;
16: import org.mule.util.UUID;
17:
18: import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong;
19:
20: /**
21: * <code>SimpleFilenameParser</code> understands a limited set of tokens, namely
22: * <ul>
23: * <li>${DATE} : the currrent date in the format dd-MM-yy_HH-mm-ss.SS</li>
24: * <li>${DATE:yy-MM-dd} : the current date using the specified format</li>
25: * <li>${SYSTIME} : The current system time milliseconds</li>
26: * <li>${UUID} : A generated Universally unique id</li>
27: * <li>${ORIGINALNAME} : The origial file name if the file being written was read
28: * from another location</li>
29: * <li>${COUNT} : An incremental counter</li>
30: * <li>${<Message Property Name>} : A name of a property on the message</li>
31: * </ul>
32: * Note that square brackets can be used instead of curl brackets, this is useful
33: * when defining the file output pattern in a Mule Url endpointUri where the curl
34: * bracket is an invalid character.
35: */
36:
37: public class SimpleFilenameParser implements FilenameParser {
38: public static final String DEFAULT_DATE_FORMAT = "dd-MM-yy_HH-mm-ss.SSS";
39:
40: private final TemplateParser antParser = TemplateParser
41: .createAntStyleParser();
42: private final TemplateParser squareParser = TemplateParser
43: .createSquareBracesStyleParser();
44:
45: private final AtomicLong count = new AtomicLong(0);
46:
47: public String getFilename(MessageAdapter adapter, String pattern) {
48: if (pattern == null) {
49: return UUID.getUUID() + ".dat";
50: } else {
51: if (pattern.indexOf('{') > -1) {
52: return getFilename(adapter, pattern, antParser);
53: } else {
54: return getFilename(adapter, pattern, squareParser);
55: }
56: }
57: }
58:
59: protected String getFilename(final MessageAdapter adapter,
60: String pattern, TemplateParser parser) {
61: return parser.parse(new TemplateParser.TemplateCallback() {
62: public Object match(String token) {
63: if (token.equals("DATE")) {
64: return DateUtils.getTimeStamp(DEFAULT_DATE_FORMAT);
65: } else if (token.startsWith("DATE:")) {
66: token = token.substring(5);
67: return DateUtils.getTimeStamp(token);
68: } else if (token.startsWith("UUID")) {
69: return UUID.getUUID();
70: } else if (token.startsWith("SYSTIME")) {
71: return String.valueOf(System.currentTimeMillis());
72: } else if (token.startsWith("COUNT")) {
73: return String.valueOf(count.getAndIncrement());
74: } else if (adapter != null) {
75: if (token.startsWith("ORIGINALNAME")) {
76: return adapter
77: .getStringProperty(
78: FileConnector.PROPERTY_ORIGINAL_FILENAME,
79: null);
80: } else {
81: return adapter.getStringProperty(token, null);
82: }
83: }
84: return null;
85:
86: }
87: }, pattern);
88: }
89: }
|