AbstractSelectableChannel.java 6.9 KB

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