KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > log > RotateLog


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.log;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.config.types.Bytes;
34 import com.caucho.config.types.Period;
35 import com.caucho.util.L10N;
36 import com.caucho.vfs.Path;
37
38 import javax.annotation.PostConstruct;
39 import java.io.IOException JavaDoc;
40
41 /**
42  * Configuration for a rotating log
43  */

44 public class RotateLog {
45   private final static L10N L = new L10N(RotateLog.class);
46   
47   private static final long ROLLOVER_SIZE = Long.MAX_VALUE / 2;
48   
49   private Path _path;
50   private String JavaDoc _pathFormat;
51   private String JavaDoc _archiveFormat;
52   
53   private Period _rolloverPeriod;
54   private Bytes _rolloverSize;
55   private int _rolloverCount = -1;
56   
57   private RotateStream _rotateStream;
58   
59   private String JavaDoc _timestamp;
60
61   /**
62    * Gets the output path.
63    */

64   public Path getPath()
65   {
66     return _path;
67   }
68
69   /**
70    * Sets the output path.
71    */

72   public void setPath(Path path)
73   {
74     _path = path;
75   }
76
77   /**
78    * Gets the output path.
79    */

80   public String JavaDoc getPathFormat()
81   {
82     return _pathFormat;
83   }
84
85   /**
86    * Sets the output path.
87    */

88   public void setPathFormat(String JavaDoc path)
89   {
90     _pathFormat = path;
91   }
92
93   /**
94    * Sets the output path (backward compat).
95    */

96   public void setHref(Path path)
97   {
98     setPath(path);
99   }
100
101   /**
102    * Sets the rollover period.
103    */

104   public void setRolloverPeriod(Period period)
105   {
106     _rolloverPeriod = period;
107   }
108
109   /**
110    * Sets the rollover size.
111    */

112   public void setRolloverSize(Bytes size)
113   {
114     _rolloverSize = size;
115   }
116
117   /**
118    * Sets the rollover count
119    */

120   public int getRolloverCount()
121   {
122     return _rolloverCount;
123   }
124
125   /**
126    * Sets the rollover count.
127    */

128   public void setRolloverCount(int count)
129   {
130     _rolloverCount = count;
131   }
132
133   /**
134    * Sets the timestamp
135    */

136   public String JavaDoc getTimestamp()
137   {
138     return _timestamp;
139   }
140
141   /**
142    * Sets the timestamp.
143    */

144   /*
145   public void setTimestamp(String timestamp)
146   {
147     _timestamp = timestamp;
148   }
149   */

150
151   /**
152    * Gets the archive format
153    */

154   public String JavaDoc getArchiveFormat()
155   {
156     return _archiveFormat;
157   }
158
159   /**
160    * Sets the archive format.
161    */

162   public void setArchiveFormat(String JavaDoc format)
163   {
164     _archiveFormat = format;
165   }
166
167   /**
168    * Returns the rotated stream.
169    */

170   public RotateStream getRotateStream()
171   {
172     return _rotateStream;
173   }
174
175   /**
176    * Returns the tag name.
177    */

178   public String JavaDoc getTagName()
179   {
180     return "rotate-log";
181   }
182
183   /**
184    * Initialize the log.
185    */

186   @PostConstruct
187   public void init()
188     throws ConfigException, IOException JavaDoc
189   {
190     if (_path != null)
191       _rotateStream = RotateStream.create(_path);
192     else if (_pathFormat != null)
193       _rotateStream = RotateStream.create(_pathFormat);
194     else
195       throw new ConfigException(L.l("`path' is a required attribute of <{0}>. Each <{0}> must configure the destination stream.", getTagName()));
196
197     if (_path != null && _path.exists() && ! _path.canRead() &&
198     (_rolloverPeriod != null ||
199      _rolloverSize != null ||
200      _archiveFormat != null)) {
201       throw new ConfigException(L.l("log path '{0}' is not readable and therefore cannot be rotated.", _path.getURL()));
202     }
203
204     AbstractRolloverLog rolloverLog = _rotateStream.getRolloverLog();
205
206     if (_rolloverPeriod != null)
207       rolloverLog.setRolloverPeriod(_rolloverPeriod);
208
209     if (_rolloverSize != null)
210       rolloverLog.setRolloverSize(_rolloverSize);
211     _rotateStream.setMaxRolloverCount(_rolloverCount);
212     if (_archiveFormat != null)
213       rolloverLog.setArchiveFormat(_archiveFormat);
214
215     _rotateStream.init();
216   }
217 }
218
Popular Tags