查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  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,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.mina.filter;
21  
22  import org.apache.mina.common.IdleStatus;
23  import org.apache.mina.common.IoFilter;
24  import org.apache.mina.common.IoFilterChain;
25  import org.apache.mina.common.IoSession;
26  
27  /**
28   * An {@link IoFilter}s wrapper that keeps track of the number of usages of this filter and will call init/destroy
29   * when the filter is not in use.
30   *
31   * @author The Apache Directory Project (mina-dev@directory.apache.org)
32   * @version $Rev: 587373 $, $Date: 2007-10-23 11:54:05 +0900 (Tue, 23 Oct 2007) $
33   */
34  public class ReferenceCountingIoFilter implements IoFilter {
35      private final IoFilter filter;
36  
37      private int count = 0;
38  
39      public ReferenceCountingIoFilter(IoFilter filter) {
40          this.filter = filter;
41      }
42  
43      public void init() throws Exception {
44          // no-op, will init on-demand in pre-add if count == 0
45      }
46  
47      public void destroy() throws Exception {
48          //no-op, will destroy on-demand in post-remove if count == 0
49      }
50  
51      public synchronized void onPreAdd(IoFilterChain parent, String name,
52              NextFilter nextFilter) throws Exception {
53          if (0 == count) {
54              filter.init();
55  
56              ++count;
57          }
58  
59          filter.onPreAdd(parent, name, nextFilter);
60      }
61  
62      public synchronized void onPostRemove(IoFilterChain parent, String name,
63              NextFilter nextFilter) throws Exception {
64          filter.onPostRemove(parent, name, nextFilter);
65  
66          --count;
67  
68          if (0 == count) {
69              filter.destroy();
70          }
71      }
72  
73      public void exceptionCaught(NextFilter nextFilter, IoSession session,
74              Throwable cause) throws Exception {
75          filter.exceptionCaught(nextFilter, session, cause);
76      }
77  
78      public void filterClose(NextFilter nextFilter, IoSession session)
79              throws Exception {
80          filter.filterClose(nextFilter, session);
81      }
82  
83      public void filterWrite(NextFilter nextFilter, IoSession session,
84              WriteRequest writeRequest) throws Exception {
85          filter.filterWrite(nextFilter, session, writeRequest);
86      }
87  
88      public void messageReceived(NextFilter nextFilter, IoSession session,
89              Object message) throws Exception {
90          filter.messageReceived(nextFilter, session, message);
91      }
92  
93      public void messageSent(NextFilter nextFilter, IoSession session,
94              Object message) throws Exception {
95          filter.messageSent(nextFilter, session, message);
96      }
97  
98      public void onPostAdd(IoFilterChain parent, String name,
99              NextFilter nextFilter) throws Exception {
100         filter.onPostAdd(parent, name, nextFilter);
101     }
102 
103     public void onPreRemove(IoFilterChain parent, String name,
104             NextFilter nextFilter) throws Exception {
105         filter.onPreRemove(parent, name, nextFilter);
106     }
107 
108     public void sessionClosed(NextFilter nextFilter, IoSession session)
109             throws Exception {
110         filter.sessionClosed(nextFilter, session);
111     }
112 
113     public void sessionCreated(NextFilter nextFilter, IoSession session)
114             throws Exception {
115         filter.sessionCreated(nextFilter, session);
116     }
117 
118     public void sessionIdle(NextFilter nextFilter, IoSession session,
119             IdleStatus status) throws Exception {
120         filter.sessionIdle(nextFilter, session, status);
121     }
122 
123     public void sessionOpened(NextFilter nextFilter, IoSession session)
124             throws Exception {
125         filter.sessionOpened(nextFilter, session);
126     }
127 }