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      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, NextFilter nextFilter) throws Exception {
    54          if (0 == count) {
    55              filter.init();
    56          }
    57
    58          ++count;
    59
    60          filter.onPreAdd(parent, name, nextFilter);
    61      }
    62
    63      public synchronized void onPostRemove(IoFilterChain parent, String name, 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, Throwable cause) throws Exception {
    74          filter.exceptionCaught(nextFilter, session, cause);
    75      }
    76
    77      public void filterClose(NextFilter nextFilter, IoSession session) throws Exception {
    78          filter.filterClose(nextFilter, session);
    79      }
    80
    81      public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
    82          filter.filterWrite(nextFilter, session, writeRequest);
    83      }
    84
    85      public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
    86          filter.messageReceived(nextFilter, session, message);
    87      }
    88
    89      public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
    90          filter.messageSent(nextFilter, session, writeRequest);
    91      }
    92
    93      public void onPostAdd(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception {
    94          filter.onPostAdd(parent, name, nextFilter);
    95      }
    96
    97      public void onPreRemove(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception {
    98          filter.onPreRemove(parent, name, nextFilter);
    99      }
    100
    101     public void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception {
    102         filter.sessionClosed(nextFilter, session);
    103     }
    104
    105     public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception {
    106         filter.sessionCreated(nextFilter, session);
    107     }
    108
    109     public void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception {
    110         filter.sessionIdle(nextFilter, session, status);
    111     }
    112
    113     public void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception {
    114         filter.sessionOpened(nextFilter, session);
    115     }
    116 }