1 //========================================================================
2 //Copyright 2006 Mort Bay Consulting Pty. Ltd.
3 //------------------------------------------------------------------------
4 //Licensed under the Apache License, Version 2.0 (the "License");
5 //you may not use this file except in compliance with the License.
6 //You may obtain a copy of the License at
7 //http://www.apache.org/licenses/LICENSE-2.0
8 //Unless required by applicable law or agreed to in writing, software
9 //distributed under the License is distributed on an "AS IS" BASIS,
10 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 //See the License for the specific language governing permissions and
12 //limitations under the License.
13 //========================================================================
14
15 package org.mortbay.jetty.handler;
16
17 import java.io.IOException;
18
19 import javax.servlet.ServletException;
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
23 import org.mortbay.jetty.Request;
24 import org.mortbay.jetty.RequestLog;
25 import org.mortbay.jetty.Response;
26 import org.mortbay.jetty.Server;
27 import org.mortbay.log.Log;
28
29
30
31 /**
32 * RequestLogHandler.
33 * This handler can be used to wrap an individual context for context logging.
34 *
35 * @author Nigel Canonizado
36 * @org.apache.xbean.XBean
37 */
38 public class RequestLogHandler extends HandlerWrapper
39 {
40 private RequestLog _requestLog;
41
42 /* ------------------------------------------------------------ */
43 /*
44 * @see org.mortbay.jetty.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
45 */
46 public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
47 throws IOException, ServletException
48 {
49 super.handle(target, request, response, dispatch);
50 if (dispatch==REQUEST && _requestLog!=null)
51 _requestLog.log((Request)request, (Response)response);
52 }
53
54 /* ------------------------------------------------------------ */
55 public void setRequestLog(RequestLog requestLog)
56 {
57 //are we changing the request log impl?
58 try
59 {
60 if (_requestLog != null)
61 _requestLog.stop();
62 }
63 catch (Exception e)
64 {
65 Log.warn (e);
66 }
67
68 if (getServer()!=null)
69 getServer().getContainer().update(this, _requestLog, requestLog, "logimpl",true);
70
71 _requestLog = requestLog;
72
73 //if we're already started, then start our request log
74 try
75 {
76 if (isStarted() && (_requestLog != null))
77 _requestLog.start();
78 }
79 catch (Exception e)
80 {
81 throw new RuntimeException (e);
82 }
83 }
84
85 /* ------------------------------------------------------------ */
86 /*
87 * @see org.mortbay.jetty.handler.HandlerWrapper#setServer(org.mortbay.jetty.Server)
88 */
89 public void setServer(Server server)
90 {
91 if (_requestLog!=null)
92 {
93 if (getServer()!=null && getServer()!=server)
94 getServer().getContainer().update(this, _requestLog, null, "logimpl",true);
95 super.setServer(server);
96 if (server!=null && server!=getServer())
97 server.getContainer().update(this, null,_requestLog, "logimpl",true);
98 }
99 else
100 super.setServer(server);
101 }
102
103 /* ------------------------------------------------------------ */
104 public RequestLog getRequestLog()
105 {
106 return _requestLog;
107 }
108
109 /* ------------------------------------------------------------ */
110 /*
111 * @see org.mortbay.jetty.handler.HandlerWrapper#doStart()
112 */
113 protected void doStart() throws Exception
114 {
115 super.doStart();
116 if (_requestLog!=null)
117 _requestLog.start();
118 }
119
120 /* ------------------------------------------------------------ */
121 /*
122 * @see org.mortbay.jetty.handler.HandlerWrapper#doStop()
123 */
124 protected void doStop() throws Exception
125 {
126 super.doStop();
127 if (_requestLog!=null)
128 _requestLog.stop();
129 }
130
131
132 }