001 /* AbstractSelector.java --
002 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038 package java.nio.channels.spi;
039
040 import java.io.IOException;
041 import java.nio.channels.ClosedSelectorException;
042 import java.nio.channels.SelectionKey;
043 import java.nio.channels.Selector;
044 import java.util.HashSet;
045 import java.util.Set;
046
047
048 public abstract class AbstractSelector extends Selector
049 {
050 private boolean closed;
051 private SelectorProvider provider;
052 private HashSet<SelectionKey> cancelledKeys;
053
054 /**
055 * Initializes the slector.
056 *
057 * @param provider the provider that created this selector
058 */
059 protected AbstractSelector(SelectorProvider provider)
060 {
061 this.provider = provider;
062 this.cancelledKeys = new HashSet<SelectionKey>();
063 }
064
065 /**
066 * Closes the channel.
067 *
068 * @exception IOException If an error occurs
069 */
070 public final synchronized void close() throws IOException
071 {
072 if (closed)
073 return;
074
075 implCloseSelector();
076 closed = true;
077 }
078
079 /**
080 * Tells whether this channel is open or not.
081 *
082 * @return true if channel is open, false otherwise.
083 */
084 public final boolean isOpen()
085 {
086 return ! closed;
087 }
088
089 /**
090 * Marks the beginning of an I/O operation that might block indefinitely.
091 */
092 protected final void begin()
093 {
094 }
095
096 /**
097 * Marks the end of an I/O operation that might block indefinitely.
098 */
099 protected final void end()
100 {
101 }
102
103 /**
104 * Returns the provider for this selector object.
105 *
106 * @return the SelectorProvider object that created this seletor
107 */
108 public final SelectorProvider provider()
109 {
110 return provider;
111 }
112
113 /**
114 * Returns the cancelled keys set.
115 *
116 * @return the cancelled keys set
117 */
118 protected final Set<SelectionKey> cancelledKeys()
119 {
120 if (! isOpen())
121 throw new ClosedSelectorException();
122
123 return cancelledKeys;
124 }
125
126 /**
127 * Cancels a selection key.
128 */
129
130 // This method is only called by AbstractSelectionKey.cancel().
131 final void cancelKey(AbstractSelectionKey key)
132 {
133 synchronized (cancelledKeys)
134 {
135 cancelledKeys.add(key);
136 }
137 }
138
139 /**
140 * Closes the channel.
141 *
142 * @exception IOException if an error occurs
143 */
144 protected abstract void implCloseSelector() throws IOException;
145
146 /**
147 * Registers a channel for the selection process.
148 *
149 * @param ch the channel register
150 * @param ops the interested operations
151 * @param att an attachement to the selection key
152 *
153 * @return the registered selection key
154 */
155 protected abstract SelectionKey register(AbstractSelectableChannel ch,
156 int ops, Object att);
157
158 /**
159 * Deregisters the given selection key.
160 *
161 * @param key the key to deregister
162 */
163 protected final void deregister(AbstractSelectionKey key)
164 {
165 ((AbstractSelectableChannel) key.channel()).removeSelectionKey(key);
166 }
167 }