1 // 2 // ======================================================================== 3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. 4 // ------------------------------------------------------------------------ 5 // All rights reserved. This program and the accompanying materials 6 // are made available under the terms of the Eclipse Public License v1.0 7 // and Apache License v2.0 which accompanies this distribution. 8 // 9 // The Eclipse Public License is available at 10 // http://www.eclipse.org/legal/epl-v10.html 11 // 12 // The Apache License v2.0 is available at 13 // http://www.opensource.org/licenses/apache2.0.php 14 // 15 // You may elect to redistribute this code under either of these licenses. 16 // ======================================================================== 17 // 18 19 package org.eclipse.jetty.server; 20 21 import org.eclipse.jetty.util.BlockingArrayQueue; 22 import org.eclipse.jetty.util.log.Log; 23 import org.eclipse.jetty.util.log.Logger; 24 25 import java.io.IOException; 26 import java.util.concurrent.BlockingQueue; 27 import java.util.concurrent.LinkedBlockingQueue; 28 import java.util.concurrent.TimeUnit; 29 30 31 /* ------------------------------------------------------------ */ 32 /** 33 * An asynchronously writing NCSA Request Log 34 */ 35 public class AsyncNCSARequestLog extends NCSARequestLog 36 { 37 private static final Logger LOG = Log.getLogger(AsyncNCSARequestLog.class); 38 private final BlockingQueue<String> _queue; 39 private transient WriterThread _thread; 40 private boolean _warnedFull; 41 AsyncNCSARequestLog()42 public AsyncNCSARequestLog() 43 { 44 this(null,null); 45 } 46 AsyncNCSARequestLog(BlockingQueue<String> queue)47 public AsyncNCSARequestLog(BlockingQueue<String> queue) 48 { 49 this(null,queue); 50 } 51 AsyncNCSARequestLog(String filename)52 public AsyncNCSARequestLog(String filename) 53 { 54 this(filename,null); 55 } 56 AsyncNCSARequestLog(String filename,BlockingQueue<String> queue)57 public AsyncNCSARequestLog(String filename,BlockingQueue<String> queue) 58 { 59 super(filename); 60 if (queue==null) 61 queue=new BlockingArrayQueue<String>(1024); 62 _queue=queue; 63 } 64 65 private class WriterThread extends Thread 66 { WriterThread()67 WriterThread() 68 { 69 setName("AsyncNCSARequestLog@"+Integer.toString(AsyncNCSARequestLog.this.hashCode(),16)); 70 } 71 72 @Override run()73 public void run() 74 { 75 while (isRunning()) 76 { 77 try 78 { 79 String log = _queue.poll(10,TimeUnit.SECONDS); 80 if (log!=null) 81 AsyncNCSARequestLog.super.write(log); 82 83 while(!_queue.isEmpty()) 84 { 85 log=_queue.poll(); 86 if (log!=null) 87 AsyncNCSARequestLog.super.write(log); 88 } 89 } 90 catch (IOException e) 91 { 92 LOG.warn(e); 93 } 94 catch (InterruptedException e) 95 { 96 LOG.ignore(e); 97 } 98 } 99 } 100 } 101 102 @Override doStart()103 protected synchronized void doStart() throws Exception 104 { 105 super.doStart(); 106 _thread = new WriterThread(); 107 _thread.start(); 108 } 109 110 @Override doStop()111 protected void doStop() throws Exception 112 { 113 _thread.interrupt(); 114 _thread.join(); 115 super.doStop(); 116 _thread=null; 117 } 118 119 @Override write(String log)120 protected void write(String log) throws IOException 121 { 122 if (!_queue.offer(log)) 123 { 124 if (_warnedFull) 125 LOG.warn("Log Queue overflow"); 126 _warnedFull=true; 127 } 128 } 129 130 } 131