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.transport.socket.nio; 021 022import java.nio.channels.ByteChannel; 023import java.nio.channels.Channel; 024import java.nio.channels.SelectionKey; 025 026import org.apache.mina.core.filterchain.DefaultIoFilterChain; 027import org.apache.mina.core.filterchain.IoFilterChain; 028import org.apache.mina.core.service.IoProcessor; 029import org.apache.mina.core.service.IoService; 030import org.apache.mina.core.session.AbstractIoSession; 031import org.apache.mina.core.session.IoSession; 032 033/** 034 * An {@link IoSession} which is managed by the NIO transport. 035 * 036 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 037 */ 038public abstract class NioSession extends AbstractIoSession { 039 /** The NioSession processor */ 040 protected final IoProcessor<NioSession> processor; 041 042 /** The communication channel */ 043 protected final Channel channel; 044 045 /** The SelectionKey used for this session */ 046 private SelectionKey key; 047 048 /** The FilterChain created for this session */ 049 private final IoFilterChain filterChain; 050 051 /** 052 * 053 * Creates a new instance of NioSession, with its associated IoProcessor. 054 * <br> 055 * This method is only called by the inherited class. 056 * 057 * @param processor The associated {@link IoProcessor} 058 * @param service The associated {@link IoService} 059 * @param channel The associated {@link Channel} 060 */ 061 protected NioSession(IoProcessor<NioSession> processor, IoService service, Channel channel) { 062 super(service); 063 this.channel = channel; 064 this.processor = processor; 065 filterChain = new DefaultIoFilterChain(this); 066 } 067 068 /** 069 * @return The ByteChannel associated with this {@link IoSession} 070 */ 071 abstract ByteChannel getChannel(); 072 073 public IoFilterChain getFilterChain() { 074 return filterChain; 075 } 076 077 /** 078 * @return The {@link SelectionKey} associated with this {@link IoSession} 079 */ 080 /* No qualifier*/SelectionKey getSelectionKey() { 081 return key; 082 } 083 084 /** 085 * Sets the {@link SelectionKey} for this {@link IoSession} 086 * 087 * @param key The new {@link SelectionKey} 088 */ 089 /* No qualifier*/void setSelectionKey(SelectionKey key) { 090 this.key = key; 091 } 092 093 /** 094 * {@inheritDoc} 095 */ 096 public IoProcessor<NioSession> getProcessor() { 097 return processor; 098 } 099 100 /** 101 * {@inheritDoc} 102 */ 103 public final boolean isActive() { 104 return key.isValid(); 105 } 106}