Source for java.nio.channels.spi.AbstractSelector

   1: /* AbstractSelector.java --
   2:    Copyright (C) 2002, 2003, 2004, 2005  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., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 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.ClosedSelectorException;
  42: import java.nio.channels.SelectionKey;
  43: import java.nio.channels.Selector;
  44: import java.util.HashSet;
  45: import java.util.Set;
  46: 
  47: 
  48: public abstract class AbstractSelector extends Selector
  49: {
  50:   private boolean closed;
  51:   private SelectorProvider provider;
  52:   private HashSet<SelectionKey> cancelledKeys;
  53: 
  54:   /**
  55:    * Initializes the slector.
  56:    *
  57:    * @param provider the provider that created this selector
  58:    */
  59:   protected AbstractSelector(SelectorProvider provider)
  60:   {
  61:     this.provider = provider;
  62:     this.cancelledKeys = new HashSet<SelectionKey>();
  63:   }
  64: 
  65:   /**
  66:    * Closes the channel.
  67:    *
  68:    * @exception IOException If an error occurs
  69:    */
  70:   public final synchronized void close() throws IOException
  71:   {
  72:     if (closed)
  73:       return;
  74: 
  75:     implCloseSelector();
  76:     closed = true;
  77:   }
  78: 
  79:   /**
  80:    * Tells whether this channel is open or not.
  81:    *
  82:    * @return true if channel is open, false otherwise.
  83:    */
  84:   public final boolean isOpen()
  85:   {
  86:     return ! closed;
  87:   }
  88: 
  89:   /**
  90:    * Marks the beginning of an I/O operation that might block indefinitely.
  91:    */
  92:   protected final void begin()
  93:   {
  94:   }
  95: 
  96:   /**
  97:    * Marks the end of an I/O operation that might block indefinitely.
  98:    */
  99:   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: }