1 29 30 package com.caucho.log; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.config.types.Period; 34 import com.caucho.util.Alarm; 35 import com.caucho.util.AlarmListener; 36 import com.caucho.util.QDate; 37 import com.caucho.util.WeakAlarm; 38 import com.caucho.vfs.Path; 39 import com.caucho.vfs.StreamImpl; 40 import com.caucho.vfs.WriteStream; 41 42 import java.io.IOException ; 43 import java.lang.ref.SoftReference ; 44 import java.util.HashMap ; 45 import java.util.logging.Level ; 46 import java.util.logging.Logger ; 47 48 52 public class RotateStream extends StreamImpl implements AlarmListener { 53 private static final Logger log = Log.open(RotateStream.class); 54 55 private static HashMap <Path,SoftReference <RotateStream>> _streams 56 = new HashMap <Path,SoftReference <RotateStream>>(); 57 58 private static HashMap <String ,SoftReference <RotateStream>> _formatStreams 59 = new HashMap <String ,SoftReference <RotateStream>>(); 60 61 private final AbstractRolloverLog _rolloverLog = new AbstractRolloverLog(); 62 63 private final Alarm _alarm = new WeakAlarm(this); 64 65 private QDate _calendar = new QDate(true); 67 68 private volatile boolean _isInit; 69 private volatile boolean _isClosed; 70 71 76 private RotateStream(Path path) 77 { 78 _rolloverLog.setPath(path); 79 } 80 81 86 private RotateStream(String formatPath) 87 throws ConfigException 88 { 89 _rolloverLog.setPathFormat(formatPath); 90 } 91 92 95 public static RotateStream create(Path path) 96 { 97 synchronized (_streams) { 98 SoftReference <RotateStream> ref = _streams.get(path); 99 RotateStream stream = ref != null ? ref.get() : null; 100 101 if (stream == null) { 102 stream = new RotateStream(path); 103 104 _streams.put(path, new SoftReference <RotateStream>(stream)); 105 } 106 107 return stream; 108 } 109 } 110 111 114 public static RotateStream create(String path) 115 throws ConfigException 116 { 117 synchronized (_formatStreams) { 118 SoftReference <RotateStream> ref = _formatStreams.get(path); 119 RotateStream stream = ref != null ? ref.get() : null; 120 121 if (stream == null) { 122 stream = new RotateStream(path); 123 124 _formatStreams.put(path, new SoftReference <RotateStream>(stream)); 125 } 126 127 return stream; 128 } 129 } 130 131 134 public static void clear() 135 { 136 synchronized (_streams) { 137 for (SoftReference <RotateStream> streamRef : _streams.values()) { 138 try { 139 RotateStream stream = streamRef.get(); 140 141 if (stream != null) 142 stream.closeImpl(); 143 } catch (Throwable e) { 144 } 145 } 146 147 _streams.clear(); 148 } 149 150 synchronized (_formatStreams) { 151 for (SoftReference <RotateStream> streamRef : _formatStreams.values()) { 152 try { 153 RotateStream stream = streamRef.get(); 154 155 if (stream != null) 156 stream.closeImpl(); 157 } catch (Throwable e) { 158 } 159 } 160 161 _formatStreams.clear(); 162 } 163 } 164 165 168 public AbstractRolloverLog getRolloverLog() 169 { 170 return _rolloverLog; 171 } 172 173 176 public void setMaxRolloverCount(int count) 177 { 178 _rolloverLog.setRolloverCount(count); 179 } 180 181 186 public void setRolloverPeriod(long period) 187 { 188 _rolloverLog.setRolloverPeriod(new Period(period)); 189 } 190 191 196 public void setArchiveFormat(String format) 197 { 198 _rolloverLog.setArchiveFormat(format); 199 } 200 201 205 public void init() 206 throws IOException 207 { 208 synchronized (this) { 209 if (_isInit) 210 return; 211 _isInit = true; 212 } 213 214 _rolloverLog.init(); 215 216 long now = Alarm.getCurrentTime(); 217 218 _alarm.queue(_rolloverLog.getNextRolloverCheckTime() - now); 219 } 220 221 224 public Path getPath() 225 { 226 return _rolloverLog.getPath(); 227 } 228 229 232 public boolean canWrite() 233 { 234 return true; 235 } 236 237 240 public void write(byte []buffer, int offset, int length, boolean isEnd) 241 throws IOException 242 { 243 _rolloverLog.rollover(); 244 _rolloverLog.write(buffer, offset, length); 245 246 _rolloverLog.rollover(); 248 } 249 250 253 public WriteStream getStream() 254 { 255 return new WriteStream(this); 256 } 257 258 261 public void flush() 262 throws IOException 263 { 264 _rolloverLog.flush(); 265 _rolloverLog.rollover(); 266 267 long now = Alarm.getCurrentTime(); 268 269 _alarm.queue(_rolloverLog.getNextRolloverCheckTime() - now); 270 } 271 272 276 public void close() 277 { 278 } 279 280 public void handleAlarm(Alarm alarm) 281 { 282 try { 283 _rolloverLog.flush(); 284 _rolloverLog.rollover(); 285 } catch (Throwable e) { 286 log.log(Level.FINE, e.toString(), e); 287 } finally { 288 if (! _isClosed) { 289 long now = Alarm.getCurrentTime(); 290 291 _alarm.queue(_rolloverLog.getNextRolloverCheckTime() - now); 292 } 293 } 294 } 295 296 299 private void closeImpl() 300 { 301 try { 302 _isClosed = true; 303 _rolloverLog.close(); 304 } catch (Throwable e) { 305 log.log(Level.FINE, e.toString(), e); 306 } 307 } 308 309 312 public void finalize() 313 { 314 closeImpl(); 315 } 316 } 317 | Popular Tags |