1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 package org.apache.mina.transport.socket.nio; 21 22 import java.nio.channels.ByteChannel; 23 import java.nio.channels.Channel; 24 import java.nio.channels.SelectionKey; 25 26 import org.apache.mina.core.filterchain.DefaultIoFilterChain; 27 import org.apache.mina.core.filterchain.IoFilterChain; 28 import org.apache.mina.core.service.IoProcessor; 29 import org.apache.mina.core.service.IoService; 30 import org.apache.mina.core.session.AbstractIoSession; 31 import org.apache.mina.core.session.IoSession; 32 33 /** 34 * An {@link IoSession} which is managed by the NIO transport. 35 * 36 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 37 */ 38 public abstract class NioSession extends AbstractIoSession { 39 /** The NioSession processor */ 40 protected final IoProcessor<NioSession> processor; 41 42 /** The communication channel */ 43 protected final Channel channel; 44 45 /** The SelectionKey used for this session */ 46 private SelectionKey key; 47 48 /** The FilterChain created for this session */ 49 private final IoFilterChain filterChain; 50 51 /** 52 * 53 * Creates a new instance of NioSession, with its associated IoProcessor. 54 * <br> 55 * This method is only called by the inherited class. 56 * 57 * @param processor The associated {@link IoProcessor} 58 * @param service The associated {@link IoService} 59 * @param channel The associated {@link Channel} 60 */ 61 protected NioSession(IoProcessor<NioSession> processor, IoService service, Channel channel) { 62 super(service); 63 this.channel = channel; 64 this.processor = processor; 65 filterChain = new DefaultIoFilterChain(this); 66 } 67 68 /** 69 * @return The ByteChannel associated with this {@link IoSession} 70 */ 71 abstract ByteChannel getChannel(); 72 73 /** 74 * {@inheritDoc} 75 */ 76 @Override 77 public IoFilterChain getFilterChain() { 78 return filterChain; 79 } 80 81 /** 82 * @return The {@link SelectionKey} associated with this {@link IoSession} 83 */ 84 /* No qualifier*/SelectionKey getSelectionKey() { 85 return key; 86 } 87 88 /** 89 * Sets the {@link SelectionKey} for this {@link IoSession} 90 * 91 * @param key The new {@link SelectionKey} 92 */ 93 /* No qualifier*/void setSelectionKey(SelectionKey key) { 94 this.key = key; 95 } 96 97 /** 98 * {@inheritDoc} 99 */ 100 @Override 101 public IoProcessor<NioSession> getProcessor() { 102 return processor; 103 } 104 105 /** 106 * {@inheritDoc} 107 */ 108 @Override 109 public final boolean isActive() { 110 return key.isValid(); 111 } 112 }