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.core.filterchain; 021 022import org.apache.mina.core.filterchain.IoFilter.NextFilter; 023import org.apache.mina.core.session.IdleStatus; 024import org.apache.mina.core.session.IoEvent; 025import org.apache.mina.core.session.IoEventType; 026import org.apache.mina.core.session.IoSession; 027import org.apache.mina.core.write.WriteRequest; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031/** 032 * An I/O event or an I/O request that MINA provides for {@link IoFilter}s. 033 * Most users won't need to use this class. It is usually used by internal 034 * components to store I/O events. 035 * 036 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 037 */ 038public class IoFilterEvent extends IoEvent { 039 /** A logger for this class */ 040 private static final Logger LOGGER = LoggerFactory.getLogger(IoFilterEvent.class); 041 042 /** A speedup for logs */ 043 private static final boolean DEBUG = LOGGER.isDebugEnabled(); 044 045 private final NextFilter nextFilter; 046 047 public IoFilterEvent(NextFilter nextFilter, IoEventType type, IoSession session, Object parameter) { 048 super(type, session, parameter); 049 050 if (nextFilter == null) { 051 throw new IllegalArgumentException("nextFilter must not be null"); 052 } 053 054 this.nextFilter = nextFilter; 055 } 056 057 public NextFilter getNextFilter() { 058 return nextFilter; 059 } 060 061 @Override 062 public void fire() { 063 IoSession session = getSession(); 064 NextFilter nextFilter = getNextFilter(); 065 IoEventType type = getType(); 066 067 if (DEBUG) { 068 LOGGER.debug("Firing a {} event for session {}", type, session.getId()); 069 } 070 071 switch (type) { 072 case MESSAGE_RECEIVED: 073 Object parameter = getParameter(); 074 nextFilter.messageReceived(session, parameter); 075 break; 076 077 case MESSAGE_SENT: 078 WriteRequest writeRequest = (WriteRequest) getParameter(); 079 nextFilter.messageSent(session, writeRequest); 080 break; 081 082 case WRITE: 083 writeRequest = (WriteRequest) getParameter(); 084 nextFilter.filterWrite(session, writeRequest); 085 break; 086 087 case CLOSE: 088 nextFilter.filterClose(session); 089 break; 090 091 case EXCEPTION_CAUGHT: 092 Throwable throwable = (Throwable) getParameter(); 093 nextFilter.exceptionCaught(session, throwable); 094 break; 095 096 case SESSION_IDLE: 097 nextFilter.sessionIdle(session, (IdleStatus) getParameter()); 098 break; 099 100 case SESSION_OPENED: 101 nextFilter.sessionOpened(session); 102 break; 103 104 case SESSION_CREATED: 105 nextFilter.sessionCreated(session); 106 break; 107 108 case SESSION_CLOSED: 109 nextFilter.sessionClosed(session); 110 break; 111 112 default: 113 throw new IllegalArgumentException("Unknown event type: " + type); 114 } 115 116 if (DEBUG) { 117 LOGGER.debug("Event {} has been fired for session {}", type, session.getId()); 118 } 119 } 120}