KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > ServletOutputLogTarget


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.log.output;
18
19 import javax.servlet.ServletContext JavaDoc;
20 import org.apache.log.format.Formatter;
21
22 /**
23  * Generic logging interface. Implementations are based on the strategy
24  * pattern.
25  *
26  * @author <a HREF="mailto:Tommy.Santoso@osa.de">Tommy Santoso</a>
27  */

28 public class ServletOutputLogTarget
29     extends AbstractOutputTarget
30 {
31     ///The servlet context written to (may be null in which case it won't log at all)
32
private ServletContext JavaDoc m_context;
33
34     /**
35      * Constructor.
36      *
37      * @param context ServletContext to use for logging.
38      */

39     public ServletOutputLogTarget( final ServletContext JavaDoc context, final Formatter formatter )
40     {
41         super( formatter );
42         m_context = context;
43         open();
44     }
45
46     /**
47      * Constructor.
48      *
49      * @param context ServletContext to use for logging.
50      */

51     public ServletOutputLogTarget( final ServletContext JavaDoc context )
52     {
53         m_context = context;
54         open();
55     }
56
57     /**
58      * Logs message to servlet context log file
59      *
60      * @param message message to log to servlet context log file.
61      */

62     protected void write( final String JavaDoc message )
63     {
64         final int len = message.length();
65         final char last = len > 0 ? message.charAt( len - 1 ) : 0;
66         final char prev = len > 1 ? message.charAt( len - 2 ) : 0;
67         final String JavaDoc trimmedMessage;
68         if( prev == '\r' && last == '\n' )
69         {
70             trimmedMessage = message.substring( 0, len - 2 );
71         }
72         else if( last == '\n' )
73         {
74             trimmedMessage = message.substring( 0, len - 1 );
75         }
76         else
77         {
78             trimmedMessage = message;
79         }
80
81         final ServletContext JavaDoc context = m_context;
82         if( null != context )
83         {
84             synchronized( context )
85             {
86                 context.log( trimmedMessage );
87             }
88         }
89     }
90
91     /**
92      * Shutdown target.
93      * Attempting to write to target after close() will cause errors to be logged.
94      */

95     public synchronized void close()
96     {
97         super.close();
98         m_context = null;
99     }
100 }
101
Popular Tags