View Javadoc

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