001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.log;
031:
032: import com.caucho.config.ConfigException;
033: import com.caucho.config.types.Period;
034: import com.caucho.util.Alarm;
035: import com.caucho.util.AlarmListener;
036: import com.caucho.util.QDate;
037: import com.caucho.util.WeakAlarm;
038: import com.caucho.vfs.Path;
039: import com.caucho.vfs.StreamImpl;
040: import com.caucho.vfs.WriteStream;
041:
042: import java.io.IOException;
043: import java.lang.ref.SoftReference;
044: import java.util.HashMap;
045: import java.util.logging.Level;
046: import java.util.logging.Logger;
047:
048: /**
049: * Automatically-rotating streams. Normally, clients will call
050: * getStream instead of using the StreamImpl interface.
051: */
052: public class RotateStream extends StreamImpl implements AlarmListener {
053: private static final Logger log = Log.open(RotateStream.class);
054:
055: private static HashMap<Path, SoftReference<RotateStream>> _streams = new HashMap<Path, SoftReference<RotateStream>>();
056:
057: private static HashMap<String, SoftReference<RotateStream>> _formatStreams = new HashMap<String, SoftReference<RotateStream>>();
058:
059: private final AbstractRolloverLog _rolloverLog = new AbstractRolloverLog();
060:
061: private final Alarm _alarm = new WeakAlarm(this );
062:
063: // calendar using the local timezone
064: private QDate _calendar = new QDate(true);
065:
066: private volatile boolean _isInit;
067: private volatile boolean _isClosed;
068:
069: /**
070: * Create rotate stream.
071: *
072: * @param path underlying log path
073: */
074: private RotateStream(Path path) {
075: _rolloverLog.setPath(path);
076: }
077:
078: /**
079: * Create rotate stream.
080: *
081: * @param path underlying log path
082: */
083: private RotateStream(String formatPath) throws ConfigException {
084: _rolloverLog.setPathFormat(formatPath);
085: }
086:
087: /**
088: * Returns the rotate stream corresponding to this path
089: */
090: public static RotateStream create(Path path) {
091: synchronized (_streams) {
092: SoftReference<RotateStream> ref = _streams.get(path);
093: RotateStream stream = ref != null ? ref.get() : null;
094:
095: if (stream == null) {
096: stream = new RotateStream(path);
097:
098: _streams.put(path, new SoftReference<RotateStream>(
099: stream));
100: }
101:
102: return stream;
103: }
104: }
105:
106: /**
107: * Returns the rotate stream corresponding to this path
108: */
109: public static RotateStream create(String path)
110: throws ConfigException {
111: synchronized (_formatStreams) {
112: SoftReference<RotateStream> ref = _formatStreams.get(path);
113: RotateStream stream = ref != null ? ref.get() : null;
114:
115: if (stream == null) {
116: stream = new RotateStream(path);
117:
118: _formatStreams.put(path,
119: new SoftReference<RotateStream>(stream));
120: }
121:
122: return stream;
123: }
124: }
125:
126: /**
127: * Clears the streams.
128: */
129: public static void clear() {
130: synchronized (_streams) {
131: for (SoftReference<RotateStream> streamRef : _streams
132: .values()) {
133: try {
134: RotateStream stream = streamRef.get();
135:
136: if (stream != null)
137: stream.closeImpl();
138: } catch (Throwable e) {
139: }
140: }
141:
142: _streams.clear();
143: }
144:
145: synchronized (_formatStreams) {
146: for (SoftReference<RotateStream> streamRef : _formatStreams
147: .values()) {
148: try {
149: RotateStream stream = streamRef.get();
150:
151: if (stream != null)
152: stream.closeImpl();
153: } catch (Throwable e) {
154: }
155: }
156:
157: _formatStreams.clear();
158: }
159: }
160:
161: /**
162: * Returns the rollover log.
163: */
164: public AbstractRolloverLog getRolloverLog() {
165: return _rolloverLog;
166: }
167:
168: /**
169: * Sets the maximum number of rolled logs.
170: */
171: public void setMaxRolloverCount(int count) {
172: _rolloverLog.setRolloverCount(count);
173: }
174:
175: /**
176: * Sets the log rollover period, rounded up to the nearest hour.
177: *
178: * @param period the new rollover period in milliseconds.
179: */
180: public void setRolloverPeriod(long period) {
181: _rolloverLog.setRolloverPeriod(new Period(period));
182: }
183:
184: /**
185: * Sets the archive format.
186: *
187: * @param format the archive format.
188: */
189: public void setArchiveFormat(String format) {
190: _rolloverLog.setArchiveFormat(format);
191: }
192:
193: /**
194: * Initialize the stream, setting any logStream, System.out and System.err
195: * as necessary.
196: */
197: public void init() throws IOException {
198: synchronized (this ) {
199: if (_isInit)
200: return;
201: _isInit = true;
202: }
203:
204: _rolloverLog.init();
205:
206: long now = Alarm.getCurrentTime();
207:
208: _alarm.queue(_rolloverLog.getNextRolloverCheckTime() - now);
209: }
210:
211: /**
212: * Returns the Path associated with the stream.
213: */
214: public Path getPath() {
215: return _rolloverLog.getPath();
216: }
217:
218: /**
219: * True if the stream can write
220: */
221: public boolean canWrite() {
222: return true;
223: }
224:
225: /**
226: * Writes to the stream
227: */
228: public void write(byte[] buffer, int offset, int length,
229: boolean isEnd) throws IOException {
230: _rolloverLog.rollover();
231: _rolloverLog.write(buffer, offset, length);
232:
233: // _alarm.queue(1000);
234: _rolloverLog.rollover();
235: }
236:
237: /**
238: * Gets the current write stream
239: */
240: public WriteStream getStream() {
241: return new WriteStream(this );
242: }
243:
244: /**
245: * Flushes the underlying stream.
246: */
247: public void flush() throws IOException {
248: _rolloverLog.flush();
249: _rolloverLog.rollover();
250:
251: long now = Alarm.getCurrentTime();
252:
253: _alarm.queue(_rolloverLog.getNextRolloverCheckTime() - now);
254: }
255:
256: /**
257: * The close call does nothing since the rotate stream is shared for
258: * many logs.
259: */
260: public void close() {
261: }
262:
263: public void handleAlarm(Alarm alarm) {
264: try {
265: _rolloverLog.flush();
266: _rolloverLog.rollover();
267: } catch (Throwable e) {
268: log.log(Level.FINE, e.toString(), e);
269: } finally {
270: if (!_isClosed) {
271: long now = Alarm.getCurrentTime();
272:
273: _alarm.queue(_rolloverLog.getNextRolloverCheckTime()
274: - now);
275: }
276: }
277: }
278:
279: /**
280: * Closes the underlying stream.
281: */
282: private void closeImpl() {
283: try {
284: _isClosed = true;
285: _rolloverLog.close();
286: } catch (Throwable e) {
287: log.log(Level.FINE, e.toString(), e);
288: }
289: }
290:
291: /**
292: * finalize.
293: */
294: public void finalize() {
295: closeImpl();
296: }
297: }
|