View Javadoc

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.util;
21  
22  import org.apache.mina.core.filterchain.IoFilter;
23  import org.apache.mina.core.filterchain.IoFilterChain;
24  import org.apache.mina.core.session.IdleStatus;
25  import org.apache.mina.core.session.IoSession;
26  import org.apache.mina.core.session.TrafficMask;
27  import org.apache.mina.core.write.WriteRequest;
28  
29  /**
30   * An {@link IoFilter}s wrapper that keeps track of the number of usages of this filter and will call init/destroy
31   * when the filter is not in use.
32   *
33   * @author The Apache MINA Project (dev@mina.apache.org)
34   * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (jeu, 26 jun 2008) $
35   */
36  public class ReferenceCountingFilter implements IoFilter {
37      private final IoFilter filter;
38  
39      private int count = 0;
40  
41      public ReferenceCountingFilter(IoFilter filter) {
42          this.filter = filter;
43      }
44  
45      public void init() throws Exception {
46          // no-op, will init on-demand in pre-add if count == 0
47      }
48  
49      public void destroy() throws Exception {
50          //no-op, will destroy on-demand in post-remove if count == 0
51      }
52  
53      public synchronized void onPreAdd(IoFilterChain parent, String name,
54              NextFilter nextFilter) throws Exception {
55          if (0 == count) {
56              filter.init();
57  
58              ++count;
59          }
60  
61          filter.onPreAdd(parent, name, nextFilter);
62      }
63  
64      public synchronized void onPostRemove(IoFilterChain parent, String name,
65              NextFilter nextFilter) throws Exception {
66          filter.onPostRemove(parent, name, nextFilter);
67  
68          --count;
69  
70          if (0 == count) {
71              filter.destroy();
72          }
73      }
74  
75      public void exceptionCaught(NextFilter nextFilter, IoSession session,
76              Throwable cause) throws Exception {
77          filter.exceptionCaught(nextFilter, session, cause);
78      }
79  
80      public void filterClose(NextFilter nextFilter, IoSession session)
81              throws Exception {
82          filter.filterClose(nextFilter, session);
83      }
84  
85      public void filterWrite(NextFilter nextFilter, IoSession session,
86              WriteRequest writeRequest) throws Exception {
87          filter.filterWrite(nextFilter, session, writeRequest);
88      }
89  
90      public void messageReceived(NextFilter nextFilter, IoSession session,
91              Object message) throws Exception {
92          filter.messageReceived(nextFilter, session, message);
93      }
94  
95      public void messageSent(NextFilter nextFilter, IoSession session,
96              WriteRequest writeRequest) throws Exception {
97          filter.messageSent(nextFilter, session, writeRequest);
98      }
99  
100     public void onPostAdd(IoFilterChain parent, String name,
101             NextFilter nextFilter) throws Exception {
102         filter.onPostAdd(parent, name, nextFilter);
103     }
104 
105     public void onPreRemove(IoFilterChain parent, String name,
106             NextFilter nextFilter) throws Exception {
107         filter.onPreRemove(parent, name, nextFilter);
108     }
109 
110     public void sessionClosed(NextFilter nextFilter, IoSession session)
111             throws Exception {
112         filter.sessionClosed(nextFilter, session);
113     }
114 
115     public void sessionCreated(NextFilter nextFilter, IoSession session)
116             throws Exception {
117         filter.sessionCreated(nextFilter, session);
118     }
119 
120     public void sessionIdle(NextFilter nextFilter, IoSession session,
121             IdleStatus status) throws Exception {
122         filter.sessionIdle(nextFilter, session, status);
123     }
124 
125     public void sessionOpened(NextFilter nextFilter, IoSession session)
126             throws Exception {
127         filter.sessionOpened(nextFilter, session);
128     }
129 
130     public void filterSetTrafficMask(NextFilter nextFilter, IoSession session,
131             TrafficMask trafficMask) throws Exception {
132         filter.filterSetTrafficMask(nextFilter, session, trafficMask);
133     }
134 }