]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/server/AsyncNCSARequestLog.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / server / AsyncNCSARequestLog.java
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 java.io.IOException;
22 import java.util.concurrent.BlockingQueue;
23 import java.util.concurrent.TimeUnit;
24
25 import org.eclipse.jetty.util.BlockingArrayQueue;
26 import org.eclipse.jetty.util.log.Log;
27 import org.eclipse.jetty.util.log.Logger;
28
29
30 /* ------------------------------------------------------------ */
31 /**
32  * An asynchronously writing NCSA Request Log
33  */
34 public class AsyncNCSARequestLog extends NCSARequestLog
35 {
36     private static final Logger LOG = Log.getLogger(AsyncNCSARequestLog.class);
37     private final BlockingQueue<String> _queue;
38     private transient WriterThread _thread;
39     private boolean _warnedFull;
40
41     public AsyncNCSARequestLog()
42     {
43         this(null,null);
44     }
45
46     public AsyncNCSARequestLog(BlockingQueue<String> queue)
47     {
48         this(null,queue);
49     }
50
51     public AsyncNCSARequestLog(String filename)
52     {
53         this(filename,null);
54     }
55
56     public AsyncNCSARequestLog(String filename,BlockingQueue<String> queue)
57     {
58         super(filename);
59         if (queue==null)
60             queue=new BlockingArrayQueue<>(1024);
61         _queue=queue;
62     }
63
64     private class WriterThread extends Thread
65     {
66         WriterThread()
67         {
68             setName("AsyncNCSARequestLog@"+Integer.toString(AsyncNCSARequestLog.this.hashCode(),16));
69         }
70
71         @Override
72         public void run()
73         {
74             while (isRunning())
75             {
76                 try
77                 {
78                     String log = _queue.poll(10,TimeUnit.SECONDS);
79                     if (log!=null)
80                         AsyncNCSARequestLog.super.write(log);
81
82                     while(!_queue.isEmpty())
83                     {
84                         log=_queue.poll();
85                         if (log!=null)
86                             AsyncNCSARequestLog.super.write(log);
87                     }
88                 }
89                 catch (IOException e)
90                 {
91                     LOG.warn(e);
92                 }
93                 catch (InterruptedException e)
94                 {
95                     LOG.ignore(e);
96                 }
97             }
98         }
99     }
100
101     @Override
102     protected synchronized void doStart() throws Exception
103     {
104         super.doStart();
105         _thread = new WriterThread();
106         _thread.start();
107     }
108
109     @Override
110     protected void doStop() throws Exception
111     {
112         _thread.interrupt();
113         _thread.join();
114         super.doStop();
115         _thread=null;
116     }
117
118     @Override
119     public void write(String log) throws IOException
120     {
121         if (!_queue.offer(log))
122         {
123             if (_warnedFull)
124                 LOG.warn("Log Queue overflow");
125             _warnedFull=true;
126         }
127     }
128
129 }