001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.mina.filter.util;
021
022import org.apache.mina.core.filterchain.IoFilter;
023import org.apache.mina.core.filterchain.IoFilterAdapter;
024import org.apache.mina.core.filterchain.IoFilterChain;
025import org.apache.mina.core.session.IdleStatus;
026import org.apache.mina.core.session.IoSession;
027import org.apache.mina.core.write.WriteRequest;
028
029/**
030 * An {@link IoFilter}s wrapper that keeps track of the number of usages of this filter and will call init/destroy
031 * when the filter is not in use.
032 *
033 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
034 * @org.apache.xbean.XBean
035 */
036public class ReferenceCountingFilter extends IoFilterAdapter {
037    private final IoFilter filter;
038
039    private int count = 0;
040
041    public ReferenceCountingFilter(IoFilter filter) {
042        this.filter = filter;
043    }
044
045    public void init() throws Exception {
046        // no-op, will init on-demand in pre-add if count == 0
047    }
048
049    public void destroy() throws Exception {
050        //no-op, will destroy on-demand in post-remove if count == 0
051    }
052
053    public synchronized void onPreAdd(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception {
054        if (0 == count) {
055            filter.init();
056        }
057
058        ++count;
059
060        filter.onPreAdd(parent, name, nextFilter);
061    }
062
063    public synchronized void onPostRemove(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception {
064        filter.onPostRemove(parent, name, nextFilter);
065
066        --count;
067
068        if (0 == count) {
069            filter.destroy();
070        }
071    }
072
073    public void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception {
074        filter.exceptionCaught(nextFilter, session, cause);
075    }
076
077    public void filterClose(NextFilter nextFilter, IoSession session) throws Exception {
078        filter.filterClose(nextFilter, session);
079    }
080
081    public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
082        filter.filterWrite(nextFilter, session, writeRequest);
083    }
084
085    public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
086        filter.messageReceived(nextFilter, session, message);
087    }
088
089    public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
090        filter.messageSent(nextFilter, session, writeRequest);
091    }
092
093    public void onPostAdd(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception {
094        filter.onPostAdd(parent, name, nextFilter);
095    }
096
097    public void onPreRemove(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception {
098        filter.onPreRemove(parent, name, nextFilter);
099    }
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}