View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.server.handler; 
15  
16  import java.io.IOException;
17  
18  import javax.servlet.ServletException;
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.eclipse.jetty.server.DispatcherType;
23  import org.eclipse.jetty.server.HttpConnection;
24  import org.eclipse.jetty.server.Request;
25  import org.eclipse.jetty.server.RequestLog;
26  import org.eclipse.jetty.server.Response;
27  import org.eclipse.jetty.server.Server;
28  import org.eclipse.jetty.util.log.Log;
29  
30  
31  
32  /** 
33   * RequestLogHandler.
34   * This handler can be used to wrap an individual context for context logging.
35   * 
36   * 
37   * @org.apache.xbean.XBean
38   */
39  public class RequestLogHandler extends HandlerWrapper
40  {
41      private RequestLog _requestLog;
42      
43      /* ------------------------------------------------------------ */
44      /* 
45       * @see org.eclipse.jetty.server.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
46       */
47      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
48              throws IOException, ServletException
49      {
50          super.handle(target, baseRequest, request, response);
51          if (DispatcherType.REQUEST.equals(baseRequest.getDispatcherType()) && _requestLog!=null)
52              _requestLog.log((Request)request, (Response)response);
53      }
54  
55      /* ------------------------------------------------------------ */
56      public void setRequestLog(RequestLog requestLog)
57      {
58          //are we changing the request log impl?
59          try
60          {
61              if (_requestLog != null)
62                  _requestLog.stop();
63          }
64          catch (Exception e)
65          {
66              Log.warn (e);
67          }
68          
69          if (getServer()!=null)
70              getServer().getContainer().update(this, _requestLog, requestLog, "logimpl",true);
71          
72          _requestLog = requestLog;
73          
74          //if we're already started, then start our request log
75          try
76          {
77              if (isStarted() && (_requestLog != null))
78                  _requestLog.start();
79          }
80          catch (Exception e)
81          {
82              throw new RuntimeException (e);
83          }
84      }
85  
86      /* ------------------------------------------------------------ */
87      /* 
88       * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#setServer(org.eclipse.jetty.server.server.Server)
89       */
90      public void setServer(Server server)
91      {
92          if (_requestLog!=null)
93          {
94              if (getServer()!=null && getServer()!=server)
95                  getServer().getContainer().update(this, _requestLog, null, "logimpl",true);
96              super.setServer(server);
97              if (server!=null && server!=getServer())
98                  server.getContainer().update(this, null,_requestLog, "logimpl",true);
99          }
100         else
101             super.setServer(server);
102     }
103 
104     /* ------------------------------------------------------------ */
105     public RequestLog getRequestLog() 
106     {
107         return _requestLog;
108     }
109 
110     /* ------------------------------------------------------------ */
111     /* 
112      * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStart()
113      */
114     protected void doStart() throws Exception
115     {
116         super.doStart();
117         if (_requestLog!=null)
118             _requestLog.start();
119     }
120 
121     /* ------------------------------------------------------------ */
122     /* 
123      * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStop()
124      */
125     protected void doStop() throws Exception
126     {
127         super.doStop();
128         if (_requestLog!=null)
129             _requestLog.stop();
130     }
131 
132     
133 }