View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.rolling.helper;
15  
16  import java.util.Date;
17  import java.util.List;
18  import java.util.TimeZone;
19  
20  import ch.qos.logback.core.CoreConstants;
21  import ch.qos.logback.core.pattern.DynamicConverter;
22  import ch.qos.logback.core.util.CachingDateFormatter;
23  import ch.qos.logback.core.util.DatePatternToRegexUtil;
24  
25  /**
26   * Returns a date formatted by SimpleDateFormatter.
27   * 
28   * @author Ceki Gülcü
29   */
30  public class DateTokenConverter<E> extends DynamicConverter<E> implements MonoTypedConverter {
31  
32      /**
33       * The conversion word/character with which this converter is registered.
34       */
35      public final static String CONVERTER_KEY = "d";
36      public final static String AUXILIARY_TOKEN = "AUX";
37      public static final String DEFAULT_DATE_PATTERN = CoreConstants.DAILY_DATE_PATTERN;
38  
39      private String datePattern;
40      private TimeZone timeZone;
41      private CachingDateFormatter cdf;
42      // is this token converter primary or auxiliary? Only the primary converter
43      // determines the rolling period
44      private boolean primary = true;
45  
46      public void start() {
47          this.datePattern = getFirstOption();
48          if (this.datePattern == null) {
49              this.datePattern = DEFAULT_DATE_PATTERN;
50          }
51  
52          final List<String> optionList = getOptionList();
53          if (optionList != null) {
54              for (int optionIndex = 1; optionIndex < optionList.size(); optionIndex++) {
55                  String option = optionList.get(optionIndex);
56                  if (AUXILIARY_TOKEN.equalsIgnoreCase(option)) {
57                      primary = false;
58                  } else {
59                      timeZone = TimeZone.getTimeZone(option);
60                  }
61              }
62          }
63  
64          cdf = new CachingDateFormatter(datePattern);
65          if (timeZone != null) {
66              cdf.setTimeZone(timeZone);
67          }
68      }
69  
70      public String convert(Date date) {
71          return cdf.format(date.getTime());
72      }
73  
74      public String convert(Object o) {
75          if (o == null) {
76              throw new IllegalArgumentException("Null argument forbidden");
77          }
78          if (o instanceof Date) {
79              return convert((Date) o);
80          }
81          throw new IllegalArgumentException("Cannot convert " + o + " of type" + o.getClass().getName());
82      }
83  
84      /**
85       * Return the date pattern.
86       */
87      public String getDatePattern() {
88          return datePattern;
89      }
90  
91      public TimeZone getTimeZone() {
92          return timeZone;
93      }
94  
95      public boolean isApplicable(Object o) {
96          return (o instanceof Date);
97      }
98  
99      public String toRegex() {
100         DatePatternToRegexUtil datePatternToRegexUtil = new DatePatternToRegexUtil(datePattern);
101         return datePatternToRegexUtil.toRegex();
102     }
103 
104     public boolean isPrimary() {
105         return primary;
106     }
107 }