View Javadoc

1   /*
2    * Copyright 1999,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.log4j.rolling;
18  
19  import org.apache.log4j.pattern.FormattingInfo;
20  import org.apache.log4j.pattern.PatternConverter;
21  import org.apache.log4j.pattern.PatternParser;
22  import org.apache.log4j.pattern.IntegerPatternConverter;
23  import org.apache.log4j.pattern.DatePatternConverter;
24  import org.apache.log4j.helpers.LogLog;
25  import org.apache.log4j.spi.OptionHandler;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  
31  /***
32   * Implements methods common to most, it not all, rolling
33   * policies. Currently such methods are limited to a compression mode
34   * getter/setter.
35   *
36   * @author Ceki Gülcü
37   * @author Curt Arnold
38   */
39  public abstract class RollingPolicyBase
40    implements RollingPolicy, OptionHandler {
41      /***
42       * Error message.
43       */
44      private static final String FNP_NOT_SET =
45        "The FileNamePattern option must be set before using RollingPolicy. ";
46  
47      /***
48       *   Reference for error message.
49       */
50      private static final String SEE_FNP_NOT_SET =
51        "See also http://logging.apache.org/log4j/codes.html#tbr_fnp_not_set";
52  
53    /***
54     * File name pattern converters.
55     */
56    private PatternConverter[] patternConverters;
57  
58    /***
59     * File name field specifiers.
60     */
61    private FormattingInfo[] patternFields;
62  
63    /***
64     * File name pattern.
65     */
66    private String fileNamePatternStr;
67  
68    /***
69     * Active file name may be null.
70     * Duplicates FileAppender.file and should be removed.
71     */
72    protected String activeFileName;
73  
74    /***
75     * {@inheritDoc}
76     */
77    public void activateOptions() {
78        // find out period from the filename pattern
79        if (fileNamePatternStr != null) {
80          parseFileNamePattern();
81        } else {
82          LogLog.warn(FNP_NOT_SET);
83          LogLog.warn(SEE_FNP_NOT_SET);
84          throw new IllegalStateException(FNP_NOT_SET + SEE_FNP_NOT_SET);
85        }
86  
87    }
88  
89    /***
90     * Set file name pattern.
91     * @param fnp file name pattern.
92     */
93    public void setFileNamePattern(String fnp) {
94      fileNamePatternStr = fnp;
95    }
96  
97    /***
98     * Get file name pattern.
99     * @return file name pattern.
100    */
101   public String getFileNamePattern() {
102     return fileNamePatternStr;
103   }
104 
105   /***
106    * ActiveFileName can be left unset, i.e. as null.
107    * @param afn active file name.
108    * @deprecated Duplicates FileAppender.file and should be removed
109    */
110   public void setActiveFileName(String afn) {
111     activeFileName = afn;
112   }
113 
114   /***
115    * Return the value of the <b>ActiveFile</b> option.
116    * @deprecated Duplicates FileAppender.file and should be removed
117    * @return active file name.
118   */
119   public String getActiveFileName() {
120     return activeFileName;
121   }
122 
123   /***
124    *   Parse file name pattern.
125    */
126   protected final void parseFileNamePattern() {
127     List converters = new ArrayList();
128     List fields = new ArrayList();
129 
130     PatternParser.parse(
131       fileNamePatternStr, converters, fields, null,
132       PatternParser.getFileNamePatternRules());
133     patternConverters = new PatternConverter[converters.size()];
134     patternConverters =
135       (PatternConverter[]) converters.toArray(patternConverters);
136     patternFields = new FormattingInfo[converters.size()];
137     patternFields = (FormattingInfo[]) fields.toArray(patternFields);
138   }
139 
140   /***
141    * Format file name.
142    *
143    * @param obj object to be evaluted in formatting, may not be null.
144    * @param buf string buffer to which formatted file name is appended, may not be null.
145    */
146   protected final void formatFileName(
147     final Object obj, final StringBuffer buf) {
148     for (int i = 0; i < patternConverters.length; i++) {
149       int fieldStart = buf.length();
150       patternConverters[i].format(obj, buf);
151 
152       if (patternFields[i] != null) {
153         patternFields[i].format(fieldStart, buf);
154       }
155     }
156   }
157 
158   protected final PatternConverter getDatePatternConverter() {
159       for (int i = 0; i < patternConverters.length; i++) {
160         if (patternConverters[i] instanceof DatePatternConverter) {
161           return patternConverters[i];
162         }
163       }
164       return null;
165 
166   }
167 
168   protected final PatternConverter getIntegerPatternConverter() {
169       for (int i = 0; i < patternConverters.length; i++) {
170         if (patternConverters[i] instanceof IntegerPatternConverter) {
171           return patternConverters[i];
172         }
173       }
174       return null;
175   }
176 
177 }