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: */
020: package org.apache.mina.filter.util;
021:
022: import org.apache.mina.common.IdleStatus;
023: import org.apache.mina.common.IoFilter;
024: import org.apache.mina.common.IoFilterChain;
025: import org.apache.mina.common.IoSession;
026: import org.apache.mina.common.TrafficMask;
027: import org.apache.mina.common.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 The Apache MINA Project (dev@mina.apache.org)
034: * @version $Rev: 591770 $, $Date: 2007-11-04 05:22:44 -0700 (Sun, 04 Nov 2007) $
035: */
036: public class ReferenceCountingFilter implements IoFilter {
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,
054: String name, NextFilter nextFilter) throws Exception {
055: if (0 == count) {
056: filter.init();
057:
058: ++count;
059: }
060:
061: filter.onPreAdd(parent, name, nextFilter);
062: }
063:
064: public synchronized void onPostRemove(IoFilterChain parent,
065: String name, NextFilter nextFilter) throws Exception {
066: filter.onPostRemove(parent, name, nextFilter);
067:
068: --count;
069:
070: if (0 == count) {
071: filter.destroy();
072: }
073: }
074:
075: public void exceptionCaught(NextFilter nextFilter,
076: IoSession session, Throwable cause) throws Exception {
077: filter.exceptionCaught(nextFilter, session, cause);
078: }
079:
080: public void filterClose(NextFilter nextFilter, IoSession session)
081: throws Exception {
082: filter.filterClose(nextFilter, session);
083: }
084:
085: public void filterWrite(NextFilter nextFilter, IoSession session,
086: WriteRequest writeRequest) throws Exception {
087: filter.filterWrite(nextFilter, session, writeRequest);
088: }
089:
090: public void messageReceived(NextFilter nextFilter,
091: IoSession session, Object message) throws Exception {
092: filter.messageReceived(nextFilter, session, message);
093: }
094:
095: public void messageSent(NextFilter nextFilter, IoSession session,
096: WriteRequest writeRequest) throws Exception {
097: filter.messageSent(nextFilter, session, writeRequest);
098: }
099:
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,
131: IoSession session, TrafficMask trafficMask)
132: throws Exception {
133: filter.filterSetTrafficMask(nextFilter, session, trafficMask);
134: }
135: }
|