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.IoFilterAdapter;
24  import org.apache.mina.core.filterchain.IoFilterChain;
25  import org.apache.mina.core.session.IdleStatus;
26  import org.apache.mina.core.session.IoSession;
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 <a href="http://mina.apache.org">Apache MINA Project</a>
34   * @org.apache.xbean.XBean
35   */
36  public class ReferenceCountingFilter extends IoFilterAdapter {
37      private final IoFilter filter;
38  
39      private int count = 0;
40  
41      /**
42       * Creates a new ReferenceCountingFilter instance
43       * 
44       * @param filter the filter we are counting references on
45       */
46      public ReferenceCountingFilter(IoFilter filter) {
47          this.filter = filter;
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      public synchronized void onPreAdd(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception {
55          if (0 == count) {
56              filter.init();
57          }
58  
59          ++count;
60  
61          filter.onPreAdd(parent, name, nextFilter);
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public synchronized void onPostRemove(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception {
69          filter.onPostRemove(parent, name, nextFilter);
70  
71          --count;
72  
73          if (0 == count) {
74              filter.destroy();
75          }
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception {
83          filter.exceptionCaught(nextFilter, session, cause);
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      @Override
90      public void filterClose(NextFilter nextFilter, IoSession session) throws Exception {
91          filter.filterClose(nextFilter, session);
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
99          filter.filterWrite(nextFilter, session, writeRequest);
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
107         filter.messageReceived(nextFilter, session, message);
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
115         filter.messageSent(nextFilter, session, writeRequest);
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public void onPostAdd(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception {
123         filter.onPostAdd(parent, name, nextFilter);
124     }
125 
126     /**
127      * {@inheritDoc}
128      */
129     @Override
130     public void onPreRemove(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception {
131         filter.onPreRemove(parent, name, nextFilter);
132     }
133 
134     /**
135      * {@inheritDoc}
136      */
137     @Override
138     public void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception {
139         filter.sessionClosed(nextFilter, session);
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     @Override
146     public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception {
147         filter.sessionCreated(nextFilter, session);
148     }
149 
150     /**
151      * {@inheritDoc}
152      */
153     @Override
154     public void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception {
155         filter.sessionIdle(nextFilter, session, status);
156     }
157 
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     public void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception {
163         filter.sessionOpened(nextFilter, session);
164     }
165 }