2004-03-18 Michael Koch <konqueror@gmx.de>
[gcc.git] / libjava / java / nio / channels / spi / AbstractSelectableChannel.java
1 /* AbstractSelectableChannel.java
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37
38 package java.nio.channels.spi;
39
40 import java.io.IOException;
41 import java.nio.channels.ClosedChannelException;
42 import java.nio.channels.SelectableChannel;
43 import java.nio.channels.SelectionKey;
44 import java.nio.channels.Selector;
45 import java.util.LinkedList;
46 import java.util.List;
47 import java.util.ListIterator;
48
49 public abstract class AbstractSelectableChannel extends SelectableChannel
50 {
51 private boolean blocking = true;
52 private Object LOCK = new Object();
53 private SelectorProvider provider;
54 private LinkedList keys = new LinkedList();
55
56 /**
57 * Initializes the channel
58 */
59 protected AbstractSelectableChannel (SelectorProvider provider)
60 {
61 this.provider = provider;
62 }
63
64 /**
65 * Retrieves the object upon which the configureBlocking and register
66 * methods synchronize.
67 */
68 public final Object blockingLock ()
69 {
70 return LOCK;
71 }
72
73 /**
74 * Adjusts this channel's blocking mode.
75 */
76 public final SelectableChannel configureBlocking (boolean blocking)
77 throws IOException
78 {
79 synchronized (blockingLock())
80 {
81 if (this.blocking != blocking)
82 {
83 implConfigureBlocking(blocking);
84 this.blocking = blocking;
85 }
86 }
87
88 return this;
89 }
90
91 /**
92 * Closes this channel.
93 *
94 * @exception IOException If an error occurs
95 */
96 protected final void implCloseChannel () throws IOException
97 {
98 implCloseSelectableChannel ();
99 }
100
101 /**
102 * Closes this selectable channel.
103 */
104 protected abstract void implCloseSelectableChannel () throws IOException;
105
106 /**
107 * Adjusts this channel's blocking mode.
108 */
109 protected abstract void implConfigureBlocking (boolean block)
110 throws IOException;
111
112 /**
113 * Tells whether or not every I/O operation on this channel will block
114 * until it completes.
115 */
116 public final boolean isBlocking()
117 {
118 return blocking;
119 }
120
121 /**
122 * Tells whether or not this channel is currently registered with
123 * any selectors.
124 */
125 public final boolean isRegistered()
126 {
127 return !keys.isEmpty();
128 }
129
130 /**
131 * Retrieves the key representing the channel's registration with the
132 * given selector.
133 */
134 public final SelectionKey keyFor(Selector selector)
135 {
136 if (! isOpen())
137 return null;
138
139 try
140 {
141 synchronized(blockingLock())
142 {
143 return locate (selector);
144 }
145 }
146 catch (Exception e)
147 {
148 return null;
149 }
150 }
151
152 /**
153 * Returns the provider that created this channel.
154 */
155 public final SelectorProvider provider ()
156 {
157 return provider;
158 }
159
160 private SelectionKey locate (Selector selector)
161 {
162 ListIterator it = keys.listIterator ();
163
164 while (it.hasNext ())
165 {
166 SelectionKey key = (SelectionKey) it.next();
167
168 if (key.selector() == selector)
169 return key;
170 }
171
172 return null;
173 }
174
175 /**
176 * Registers this channel with the given selector, returning a selection key.
177 *
178 * @exception ClosedChannelException If the channel is already closed.
179 */
180 public final SelectionKey register (Selector selin, int ops, Object att)
181 throws ClosedChannelException
182 {
183 if (!isOpen ())
184 throw new ClosedChannelException();
185
186 SelectionKey key = null;
187 AbstractSelector selector = (AbstractSelector) selin;
188
189 synchronized (blockingLock())
190 {
191 key = locate (selector);
192
193 if (key != null)
194 {
195 if (att != null)
196 key.attach (att);
197 }
198 else
199 {
200 key = selector.register (this, ops, att);
201
202 if (key != null)
203 addSelectionKey (key);
204 }
205 }
206
207 return key;
208 }
209
210 void addSelectionKey(SelectionKey key)
211 {
212 keys.add(key);
213 }
214
215 // This method gets called by AbstractSelector.deregister().
216 void removeSelectionKey(SelectionKey key)
217 {
218 keys.remove(key);
219 }
220 }