View Javadoc

1   /*
2    *   @(#) $Id: ProtocolThreadPoolFilter.java 210062 2005-07-11 03:52:38Z 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 org.apache.mina.common.IdleStatus;
22  import org.apache.mina.common.Session;
23  import org.apache.mina.protocol.ProtocolHandler;
24  import org.apache.mina.protocol.ProtocolFilter;
25  import org.apache.mina.protocol.ProtocolSession;
26  import org.apache.mina.util.BaseThreadPool;
27  import org.apache.mina.util.EventType;
28  import org.apache.mina.util.ThreadPool;
29  
30  /***
31   * A Thread-pooling filter.  This filter forwards {@link ProtocolHandler} events
32   * to its thread pool.
33   * 
34   * @author Trustin Lee (trustin@apache.org)
35   * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $
36   * 
37   * @see ThreadPool
38   * @see BaseThreadPool
39   */
40  public class ProtocolThreadPoolFilter extends BaseThreadPool implements ThreadPool, ProtocolFilter
41  {
42  
43      /***
44       * Creates a new instanceof this filter with default thread pool settings.
45       * You'll have to invoke {@link #start()} method to start threads actually.
46       */
47      public ProtocolThreadPoolFilter()
48      {
49      }
50  
51      public void sessionOpened( NextFilter nextFilter,
52                                ProtocolSession session )
53      {
54          fireEvent( nextFilter, session, EventType.OPENED, null );
55      }
56  
57      public void sessionClosed( NextFilter nextFilter,
58                                ProtocolSession session )
59      {
60          fireEvent( nextFilter, session, EventType.CLOSED, null );
61      }
62  
63      public void sessionIdle( NextFilter nextFilter,
64                              ProtocolSession session, IdleStatus status )
65      {
66          fireEvent( nextFilter, session, EventType.IDLE, status );
67      }
68  
69      public void exceptionCaught( NextFilter nextFilter,
70                                  ProtocolSession session, Throwable cause )
71      {
72          fireEvent( nextFilter, session, EventType.EXCEPTION, cause );
73      }
74  
75      public void messageReceived( NextFilter nextFilter,
76                                  ProtocolSession session, Object message )
77      {
78          fireEvent( nextFilter, session, EventType.RECEIVED, message );
79      }
80  
81      public void messageSent( NextFilter nextFilter,
82                              ProtocolSession session, Object message )
83      {
84          fireEvent( nextFilter, session, EventType.SENT, message );
85      }
86  
87      protected void processEvent( Object nextFilter0,
88                                 Session session0, EventType type,
89                                 Object data )
90      {
91          NextFilter nextFilter = ( NextFilter ) nextFilter0;
92          ProtocolSession session = ( ProtocolSession ) session0;
93  
94          if( type == EventType.RECEIVED )
95          {
96              nextFilter.messageReceived( session, data );
97          }
98          else if( type == EventType.SENT )
99          {
100             nextFilter.messageSent( session, data );
101         }
102         else if( type == EventType.EXCEPTION )
103         {
104             nextFilter.exceptionCaught( session, ( Throwable ) data );
105         }
106         else if( type == EventType.IDLE )
107         {
108             nextFilter.sessionIdle( session, ( IdleStatus ) data );
109         }
110         else if( type == EventType.OPENED )
111         {
112             nextFilter.sessionOpened( session );
113         }
114         else if( type == EventType.CLOSED )
115         {
116             nextFilter.sessionClosed( session );
117         }
118     }
119 
120     public void filterWrite( NextFilter nextFilter, ProtocolSession session, Object message )
121     {
122         nextFilter.filterWrite( session, message );
123     }
124 }