View Javadoc

1   /*
2    *   @(#) $Id: ProtocolLoggingFilter.java 165594 2005-05-02 07:21:22Z trustin $
3    *
4    *   Copyright 2004 The Apache Software Foundation
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   *
18   */
19  package org.apache.mina.protocol.filter;
20  
21  import java.util.logging.Level;
22  import java.util.logging.Logger;
23  
24  import org.apache.mina.common.IdleStatus;
25  import org.apache.mina.protocol.ProtocolFilter;
26  import org.apache.mina.protocol.ProtocolSession;
27  import org.apache.mina.util.SessionLog;
28  
29  /***
30   * Logs all MINA protocol events to {@link Logger}.
31   * 
32   * @author The Apache Directory Project (dev@directory.apache.org)
33   * @author Trustin Lee (trustin@apache.org)
34   * @version $Rev: 165594 $, $Date: 2005-05-02 16:21:22 +0900 (?, 02  5? 2005) $
35   * 
36   * @see SessionLog
37   */
38  public class ProtocolLoggingFilter implements ProtocolFilter
39  {
40      /***
41       * Session attribute key: prefix string
42       */
43      public static final String PREFIX = SessionLog.PREFIX;
44  
45      /***
46       * Session attribute key: {@link Logger}
47       */
48      public static final String LOGGER = SessionLog.LOGGER;
49      
50      private Level defaultLevel = Level.INFO;
51      
52      /***
53       * Creates a new instance.
54       */
55      public ProtocolLoggingFilter()
56      {
57      }
58      
59      
60      /***
61       * Returns the default level of log entry this filter logs. 
62       */
63      public Level getDefaultLevel() {
64          return defaultLevel;
65      }
66      
67      /***
68       * Sets the default level of log entry this filter logs. 
69       */
70      public void setDefaultLevel(Level defaultLevel) {
71          if( defaultLevel == null )
72          {
73              defaultLevel = Level.INFO;
74          }
75          this.defaultLevel = defaultLevel;
76      }
77      
78      public void sessionOpened( NextFilter nextFilter, ProtocolSession session )
79      {
80          SessionLog.log( defaultLevel, session, "OPENED" );
81          nextFilter.sessionOpened( session );
82      }
83  
84      public void sessionClosed( NextFilter nextFilter, ProtocolSession session )
85      {
86          SessionLog.log( defaultLevel, session, "CLOSED" );
87          nextFilter.sessionClosed( session );
88      }
89  
90      public void sessionIdle( NextFilter nextFilter, ProtocolSession session, IdleStatus status )
91      {
92          SessionLog.log( defaultLevel, session, "IDLE: " + status );
93          nextFilter.sessionIdle( session, status );
94      }
95  
96      public void exceptionCaught( NextFilter nextFilter, ProtocolSession session, Throwable cause )
97      {
98          SessionLog.log( defaultLevel, session, "EXCEPTION:", cause );
99          nextFilter.exceptionCaught( session, cause );
100     }
101 
102     public void messageReceived( NextFilter nextFilter, ProtocolSession session, Object message )
103     {
104         SessionLog.log( defaultLevel, session, "RECEIVED: " + message );
105         nextFilter.messageReceived( session, message );
106     }
107 
108     public void messageSent( NextFilter nextFilter, ProtocolSession session, Object message )
109     {
110         SessionLog.log( defaultLevel, session, "SENT: " + message );
111         nextFilter.messageSent( session, message );
112     }
113 
114     public void filterWrite( NextFilter nextFilter, ProtocolSession session, Object message)
115     {
116         SessionLog.log( defaultLevel, session, "WRITE: " + message );
117         nextFilter.filterWrite( session, message );
118     }
119 }