View Javadoc

1   /*
2    *   @(#) $Id: IoThreadPoolFilter.java 327113 2005-10-21 06:59:15Z 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.io.filter;
20  
21  import org.apache.mina.common.ByteBuffer;
22  import org.apache.mina.common.IdleStatus;
23  import org.apache.mina.common.Session;
24  import org.apache.mina.io.IoHandler;
25  import org.apache.mina.io.IoFilter;
26  import org.apache.mina.io.IoSession;
27  import org.apache.mina.util.BaseThreadPool;
28  import org.apache.mina.util.EventType;
29  import org.apache.mina.util.ThreadPool;
30  
31  /***
32   * A Thread-pooling filter.  This filter forwards {@link IoHandler} events 
33   * to its thread pool.
34   * 
35   * @author The Apache Directory Project (dev@directory.apache.org)
36   * @version $Rev: 327113 $, $Date: 2005-10-21 15:59:15 +0900 $
37   * 
38   * @see ThreadPool
39   * @see BaseThreadPool
40   */
41  public class IoThreadPoolFilter extends BaseThreadPool implements ThreadPool, IoFilter
42  {
43      /***
44       * Creates a new instance of this filter with default thread pool settings.
45       * You'll have to invoke {@link #start()} method to start threads actually.
46       */
47      public IoThreadPoolFilter()
48      {
49          this( "IoThreadPool" );
50      }
51      
52      /***
53       * Creates a new instance of this filter with default thread pool settings.
54       * You'll have to invoke {@link #start()} method to start threads actually.
55       * 
56       * @param threadNamePrefix the prefix of the thread names this pool will create.
57       */
58      public IoThreadPoolFilter( String threadNamePrefix )
59      {
60          super( threadNamePrefix );
61      }
62  
63      public void sessionOpened( NextFilter nextFilter, IoSession session )
64      {
65          fireEvent( nextFilter, session, EventType.OPENED, null );
66      }
67  
68      public void sessionClosed( NextFilter nextFilter, IoSession session )
69      {
70          fireEvent( nextFilter, session, EventType.CLOSED, null );
71      }
72  
73      public void sessionIdle( NextFilter nextFilter, IoSession session,
74                              IdleStatus status )
75      {
76          fireEvent( nextFilter, session, EventType.IDLE, status );
77      }
78  
79      public void exceptionCaught( NextFilter nextFilter, IoSession session,
80                                  Throwable cause )
81      {
82          fireEvent( nextFilter, session, EventType.EXCEPTION, cause );
83      }
84  
85      public void dataRead( NextFilter nextFilter, IoSession session,
86                            ByteBuffer buf )
87      {
88          // MINA will release the buffer if this method returns.
89          buf.acquire();
90          fireEvent( nextFilter, session, EventType.READ, buf );
91      }
92  
93      public void dataWritten( NextFilter nextFilter, IoSession session,
94                              Object marker )
95      {
96          fireEvent( nextFilter, session, EventType.WRITTEN, marker );
97      }
98  
99      protected void processEvent( Object nextFilter0, Session session0,
100                                  EventType type, Object data )
101     {
102         NextFilter nextFilter = ( NextFilter ) nextFilter0;
103         IoSession session = ( IoSession ) session0;
104         if( type == EventType.READ )
105         {
106             ByteBuffer buf = ( ByteBuffer ) data;
107             nextFilter.dataRead( session, buf );
108             buf.release();
109         }
110         else if( type == EventType.WRITTEN )
111         {
112             nextFilter.dataWritten( session, data );
113         }
114         else if( type == EventType.EXCEPTION )
115         {
116             nextFilter.exceptionCaught( session, ( Throwable ) data );
117         }
118         else if( type == EventType.IDLE )
119         {
120             nextFilter.sessionIdle( session, ( IdleStatus ) data );
121         }
122         else if( type == EventType.OPENED )
123         {
124             nextFilter.sessionOpened( session );
125         }
126         else if( type == EventType.CLOSED )
127         {
128             nextFilter.sessionClosed( session );
129         }
130     }
131 
132     public void filterWrite( NextFilter nextFilter, IoSession session, ByteBuffer buf, Object marker ) throws Exception
133     {
134         nextFilter.filterWrite( session, buf, marker );
135     }
136 }