1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 The Apache Directory Project (dev@directory.apache.org)
35 * @version $Rev: 327113 $, $Date: 2005-10-21 15:59:15 +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 this( "ProtocolThreadPool" );
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 ProtocolThreadPoolFilter( String threadNamePrefix )
59 {
60 super( threadNamePrefix );
61 }
62
63 public void sessionOpened( NextFilter nextFilter,
64 ProtocolSession session )
65 {
66 fireEvent( nextFilter, session, EventType.OPENED, null );
67 }
68
69 public void sessionClosed( NextFilter nextFilter,
70 ProtocolSession session )
71 {
72 fireEvent( nextFilter, session, EventType.CLOSED, null );
73 }
74
75 public void sessionIdle( NextFilter nextFilter,
76 ProtocolSession session, IdleStatus status )
77 {
78 fireEvent( nextFilter, session, EventType.IDLE, status );
79 }
80
81 public void exceptionCaught( NextFilter nextFilter,
82 ProtocolSession session, Throwable cause )
83 {
84 fireEvent( nextFilter, session, EventType.EXCEPTION, cause );
85 }
86
87 public void messageReceived( NextFilter nextFilter,
88 ProtocolSession session, Object message )
89 {
90 fireEvent( nextFilter, session, EventType.RECEIVED, message );
91 }
92
93 public void messageSent( NextFilter nextFilter,
94 ProtocolSession session, Object message )
95 {
96 fireEvent( nextFilter, session, EventType.SENT, message );
97 }
98
99 protected void processEvent( Object nextFilter0,
100 Session session0, EventType type,
101 Object data )
102 {
103 NextFilter nextFilter = ( NextFilter ) nextFilter0;
104 ProtocolSession session = ( ProtocolSession ) session0;
105
106 if( type == EventType.RECEIVED )
107 {
108 nextFilter.messageReceived( session, data );
109 }
110 else if( type == EventType.SENT )
111 {
112 nextFilter.messageSent( 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, ProtocolSession session, Object message )
133 {
134 nextFilter.filterWrite( session, message );
135 }
136 }