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 protected 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 IoProcessor
58 */
59 protected NioSession(IoProcessor<NioSession> processor, IoService service, Channel channel) {
60 super(service);
61 this.channel = channel;
62 this.processor = processor;
63 filterChain = new DefaultIoFilterChain(this);
64 }
65
66 /**
67 * @return The ByteChannel associated with this {@link IoSession}
68 */
69 abstract ByteChannel getChannel();
70
71 public IoFilterChain getFilterChain() {
72 return filterChain;
73 }
74
75 /**
76 * @return The {@link SelectionKey} associated with this {@link IoSession}
77 */
78 /* No qualifier*/SelectionKey getSelectionKey() {
79 return key;
80 }
81
82 /**
83 * Sets the {@link SelectionKey} for this {@link IoSession}
84 *
85 * @param key The new {@link SelectionKey}
86 */
87 /* No qualifier*/void setSelectionKey(SelectionKey key) {
88 this.key = key;
89 }
90
91 /**
92 * {@inheritDoc}
93 */
94 public IoProcessor<NioSession> getProcessor() {
95 return processor;
96 }
97 }