1 20 package org.apache.mina.filter; 21 22 import org.apache.mina.common.IdleStatus; 23 import org.apache.mina.common.IoFilter; 24 import org.apache.mina.common.IoFilterChain; 25 import org.apache.mina.common.IoSession; 26 27 34 public class ReferenceCountingIoFilter implements IoFilter { 35 private final IoFilter filter; 36 37 private int count = 0; 38 39 public ReferenceCountingIoFilter(IoFilter filter) { 40 this.filter = filter; 41 } 42 43 public void init() throws Exception { 44 } 46 47 public void destroy() throws Exception { 48 } 50 51 public synchronized void onPreAdd(IoFilterChain parent, String name, 52 NextFilter nextFilter) throws Exception { 53 if (0 == count) { 54 filter.init(); 55 56 ++count; 57 } 58 59 filter.onPreAdd(parent, name, nextFilter); 60 } 61 62 public synchronized void onPostRemove(IoFilterChain parent, String name, 63 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, 74 Throwable cause) throws Exception { 75 filter.exceptionCaught(nextFilter, session, cause); 76 } 77 78 public void filterClose(NextFilter nextFilter, IoSession session) 79 throws Exception { 80 filter.filterClose(nextFilter, session); 81 } 82 83 public void filterWrite(NextFilter nextFilter, IoSession session, 84 WriteRequest writeRequest) throws Exception { 85 filter.filterWrite(nextFilter, session, writeRequest); 86 } 87 88 public void messageReceived(NextFilter nextFilter, IoSession session, 89 Object message) throws Exception { 90 filter.messageReceived(nextFilter, session, message); 91 } 92 93 public void messageSent(NextFilter nextFilter, IoSession session, 94 Object message) throws Exception { 95 filter.messageSent(nextFilter, session, message); 96 } 97 98 public void onPostAdd(IoFilterChain parent, String name, 99 NextFilter nextFilter) throws Exception { 100 filter.onPostAdd(parent, name, nextFilter); 101 } 102 103 public void onPreRemove(IoFilterChain parent, String name, 104 NextFilter nextFilter) throws Exception { 105 filter.onPreRemove(parent, name, nextFilter); 106 } 107 108 public void sessionClosed(NextFilter nextFilter, IoSession session) 109 throws Exception { 110 filter.sessionClosed(nextFilter, session); 111 } 112 113 public void sessionCreated(NextFilter nextFilter, IoSession session) 114 throws Exception { 115 filter.sessionCreated(nextFilter, session); 116 } 117 118 public void sessionIdle(NextFilter nextFilter, IoSession session, 119 IdleStatus status) throws Exception { 120 filter.sessionIdle(nextFilter, session, status); 121 } 122 123 public void sessionOpened(NextFilter nextFilter, IoSession session) 124 throws Exception { 125 filter.sessionOpened(nextFilter, session); 126 } 127 } 128 | Popular Tags |