001: //========================================================================
002: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
003: //------------------------------------------------------------------------
004: //Licensed under the Apache License, Version 2.0 (the "License");
005: //you may not use this file except in compliance with the License.
006: //You may obtain a copy of the License at
007: //http://www.apache.org/licenses/LICENSE-2.0
008: //Unless required by applicable law or agreed to in writing, software
009: //distributed under the License is distributed on an "AS IS" BASIS,
010: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: //See the License for the specific language governing permissions and
012: //limitations under the License.
013: //========================================================================
014:
015: package org.mortbay.jetty.handler;
016:
017: import java.io.IOException;
018:
019: import javax.servlet.ServletException;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022:
023: import org.mortbay.jetty.Request;
024: import org.mortbay.jetty.RequestLog;
025: import org.mortbay.jetty.Response;
026: import org.mortbay.jetty.Server;
027: import org.mortbay.log.Log;
028:
029: /**
030: * RequestLogHandler.
031: * This handler can be used to wrap an individual context for context logging.
032: *
033: * @author Nigel Canonizado
034: * @org.apache.xbean.XBean
035: */
036: public class RequestLogHandler extends HandlerWrapper {
037: private RequestLog _requestLog;
038:
039: /* ------------------------------------------------------------ */
040: /*
041: * @see org.mortbay.jetty.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
042: */
043: public void handle(String target, HttpServletRequest request,
044: HttpServletResponse response, int dispatch)
045: throws IOException, ServletException {
046: super .handle(target, request, response, dispatch);
047: if (dispatch == REQUEST && _requestLog != null)
048: _requestLog.log((Request) request, (Response) response);
049: }
050:
051: /* ------------------------------------------------------------ */
052: public void setRequestLog(RequestLog requestLog) {
053: //are we changing the request log impl?
054: try {
055: if (_requestLog != null)
056: _requestLog.stop();
057: } catch (Exception e) {
058: Log.warn(e);
059: }
060:
061: if (getServer() != null)
062: getServer().getContainer().update(this , _requestLog,
063: requestLog, "logimpl", true);
064:
065: _requestLog = requestLog;
066:
067: //if we're already started, then start our request log
068: try {
069: if (isStarted() && (_requestLog != null))
070: _requestLog.start();
071: } catch (Exception e) {
072: throw new RuntimeException(e);
073: }
074: }
075:
076: /* ------------------------------------------------------------ */
077: /*
078: * @see org.mortbay.jetty.handler.HandlerWrapper#setServer(org.mortbay.jetty.Server)
079: */
080: public void setServer(Server server) {
081: if (_requestLog != null) {
082: if (getServer() != null && getServer() != server)
083: getServer().getContainer().update(this , _requestLog,
084: null, "logimpl", true);
085: super .setServer(server);
086: if (server != null && server != getServer())
087: server.getContainer().update(this , null, _requestLog,
088: "logimpl", true);
089: } else
090: super .setServer(server);
091: }
092:
093: /* ------------------------------------------------------------ */
094: public RequestLog getRequestLog() {
095: return _requestLog;
096: }
097:
098: /* ------------------------------------------------------------ */
099: /*
100: * @see org.mortbay.jetty.handler.HandlerWrapper#doStart()
101: */
102: protected void doStart() throws Exception {
103: if (_requestLog != null)
104: _requestLog.start();
105: super .doStart();
106: }
107:
108: /* ------------------------------------------------------------ */
109: /*
110: * @see org.mortbay.jetty.handler.HandlerWrapper#doStop()
111: */
112: protected void doStop() throws Exception {
113: super.doStop();
114: if (_requestLog != null)
115: _requestLog.stop();
116: }
117:
118: }
|