View Javadoc

1   /*
2    * $Id: SimpleFilenameParser.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
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  {
39      public static final String DEFAULT_DATE_FORMAT = "dd-MM-yy_HH-mm-ss.SSS";
40  
41      private final TemplateParser antParser = TemplateParser.createAntStyleParser();
42      private final TemplateParser squareParser = TemplateParser.createSquareBracesStyleParser();
43  
44      private final AtomicLong count = new AtomicLong(0);
45  
46      public String getFilename(MessageAdapter adapter, String pattern)
47      {
48          if (pattern == null)
49          {
50              return UUID.getUUID() + ".dat";
51          }
52          else
53          {
54              if (pattern.indexOf('{') > -1)
55              {
56                  return getFilename(adapter, pattern, antParser);
57              }
58              else
59              {
60                  return getFilename(adapter, pattern, squareParser);
61              }
62          }
63      }
64  
65      protected String getFilename(final MessageAdapter adapter, String pattern, TemplateParser parser)
66      {
67          return parser.parse(new TemplateParser.TemplateCallback()
68          {
69              public Object match(String token)
70              {
71                  if (token.equals("DATE"))
72                  {
73                      return DateUtils.getTimeStamp(DEFAULT_DATE_FORMAT);
74                  }
75                  else if (token.startsWith("DATE:"))
76                  {
77                      token = token.substring(5);
78                      return DateUtils.getTimeStamp(token);
79                  }
80                  else if (token.startsWith("UUID"))
81                  {
82                      return UUID.getUUID();
83                  }
84                  else if (token.startsWith("SYSTIME"))
85                  {
86                      return String.valueOf(System.currentTimeMillis());
87                  }
88                  else if (token.startsWith("COUNT"))
89                  {
90                      return String.valueOf(count.getAndIncrement());
91                  }
92                  else if (adapter != null)
93                  {
94                      if (token.startsWith("ORIGINALNAME"))
95                      {
96                          return adapter.getStringProperty(FileConnector.PROPERTY_ORIGINAL_FILENAME, null);
97                      }
98                      else
99                      {
100                         return adapter.getStringProperty(token, null);
101                     }
102                 }
103                 return null;
104 
105             }
106         }, pattern);
107     }
108 }