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 The Apache MINA Project (dev@mina.apache.org)
34   * @version $Rev: 713957 $, $Date: 2008-11-14 10:27:16 +0100 (Fri, 14 Nov 2008) $
35   * @org.apache.xbean.XBean
36   */
37  public class ReferenceCountingFilter extends IoFilterAdapter {
38      private final IoFilter filter;
39  
40      private int count = 0;
41  
42      public ReferenceCountingFilter(IoFilter filter) {
43          this.filter = filter;
44      }
45  
46      public void init() throws Exception {
47          // no-op, will init on-demand in pre-add if count == 0
48      }
49  
50      public void destroy() throws Exception {
51          //no-op, will destroy on-demand in post-remove if count == 0
52      }
53  
54      public synchronized void onPreAdd(IoFilterChain parent, String name,
55              NextFilter nextFilter) throws Exception {
56          if (0 == count) {
57              filter.init();
58  
59              ++count;
60          }
61  
62          filter.onPreAdd(parent, name, nextFilter);
63      }
64  
65      public synchronized void onPostRemove(IoFilterChain parent, String name,
66              NextFilter nextFilter) throws Exception {
67          filter.onPostRemove(parent, name, nextFilter);
68  
69          --count;
70  
71          if (0 == count) {
72              filter.destroy();
73          }
74      }
75  
76      public void exceptionCaught(NextFilter nextFilter, IoSession session,
77              Throwable cause) throws Exception {
78          filter.exceptionCaught(nextFilter, session, cause);
79      }
80  
81      public void filterClose(NextFilter nextFilter, IoSession session)
82              throws Exception {
83          filter.filterClose(nextFilter, session);
84      }
85  
86      public void filterWrite(NextFilter nextFilter, IoSession session,
87              WriteRequest writeRequest) throws Exception {
88          filter.filterWrite(nextFilter, session, writeRequest);
89      }
90  
91      public void messageReceived(NextFilter nextFilter, IoSession session,
92              Object message) throws Exception {
93          filter.messageReceived(nextFilter, session, message);
94      }
95  
96      public void messageSent(NextFilter nextFilter, IoSession session,
97              WriteRequest writeRequest) throws Exception {
98          filter.messageSent(nextFilter, session, writeRequest);
99      }
100 
101     public void onPostAdd(IoFilterChain parent, String name,
102             NextFilter nextFilter) throws Exception {
103         filter.onPostAdd(parent, name, nextFilter);
104     }
105 
106     public void onPreRemove(IoFilterChain parent, String name,
107             NextFilter nextFilter) throws Exception {
108         filter.onPreRemove(parent, name, nextFilter);
109     }
110 
111     public void sessionClosed(NextFilter nextFilter, IoSession session)
112             throws Exception {
113         filter.sessionClosed(nextFilter, session);
114     }
115 
116     public void sessionCreated(NextFilter nextFilter, IoSession session)
117             throws Exception {
118         filter.sessionCreated(nextFilter, session);
119     }
120 
121     public void sessionIdle(NextFilter nextFilter, IoSession session,
122             IdleStatus status) throws Exception {
123         filter.sessionIdle(nextFilter, session, status);
124     }
125 
126     public void sessionOpened(NextFilter nextFilter, IoSession session)
127             throws Exception {
128         filter.sessionOpened(nextFilter, session);
129     }
130 }