Coverage Report - com.sun.grizzly.BaseSelectionKeyHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseSelectionKeyHandler
52 %
44/85
42 %
11/26
0
 
 1  
 /*
 2  
  * 
 3  
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 4  
  * 
 5  
  * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved.
 6  
  * 
 7  
  * The contents of this file are subject to the terms of either the GNU
 8  
  * General Public License Version 2 only ("GPL") or the Common Development
 9  
  * and Distribution License("CDDL") (collectively, the "License").  You
 10  
  * may not use this file except in compliance with the License. You can obtain
 11  
  * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
 12  
  * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
 13  
  * language governing permissions and limitations under the License.
 14  
  * 
 15  
  * When distributing the software, include this License Header Notice in each
 16  
  * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
 17  
  * Sun designates this particular file as subject to the "Classpath" exception
 18  
  * as provided by Sun in the GPL Version 2 section of the License file that
 19  
  * accompanied this code.  If applicable, add the following below the License
 20  
  * Header, with the fields enclosed by brackets [] replaced by your own
 21  
  * identifying information: "Portions Copyrighted [year]
 22  
  * [name of copyright owner]"
 23  
  * 
 24  
  * Contributor(s):
 25  
  * 
 26  
  * If you wish your version of this file to be governed by only the CDDL or
 27  
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
 28  
  * elects to include this software in this distribution under the [CDDL or GPL
 29  
  * Version 2] license."  If you don't indicate a single choice of license, a
 30  
  * recipient has the option to distribute your version of this file under
 31  
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
 32  
  * its licensees as provided above.  However, if you add GPL Version 2 code
 33  
  * and therefore, elected the GPL Version 2 license, then the option applies
 34  
  * only if the new code is made subject to such option by the copyright
 35  
  * holder.
 36  
  *
 37  
  */
 38  
 
 39  
 package com.sun.grizzly;
 40  
 
 41  
 import com.sun.grizzly.util.Copyable;
 42  
 import com.sun.grizzly.util.SelectionKeyActionAttachment;
 43  
 import java.io.IOException;
 44  
 import java.net.Socket;
 45  
 import java.nio.channels.ClosedChannelException;
 46  
 import java.nio.channels.SelectableChannel;
 47  
 import java.nio.channels.SelectionKey;
 48  
 import java.nio.channels.Selector;
 49  
 import java.nio.channels.SocketChannel;
 50  
 import java.util.Iterator;
 51  
 import java.util.logging.Level;
 52  
 import java.util.logging.Logger;
 53  
 
 54  
 /**
 55  
  * This class is an implementation of a SelectionKeyHandler which does 
 56  
  * not use the SelectionKey attachment, does not expire keys or utilize 
 57  
  * a keep-alive mechanism. However, this is currently not the 
 58  
  * SelectionKeyHandler provisioned by default with Grizzly's Controller. 
 59  
  * Hence for an application to use this SelectionKeyHandler, Grizzly's
 60  
  * Controller must be explicitly configured to use this SelectionKeyHandler
 61  
  * implementation.
 62  
  * 
 63  
  * @author Charlie Hunt
 64  
  */
 65  
 public class BaseSelectionKeyHandler implements SelectionKeyHandler {
 66  
 
 67  135
     protected Logger logger = Controller.logger();
 68  
 
 69  
     
 70  
     /**
 71  
      * Associated {@link SelectorHandler}
 72  
      */
 73  
     protected SelectorHandler selectorHandler;
 74  
 
 75  
     
 76  132
     public BaseSelectionKeyHandler() {
 77  132
     }
 78  
    
 79  
     
 80  3
     public BaseSelectionKeyHandler(SelectorHandler selectorHandler) {
 81  3
         this.selectorHandler = selectorHandler;
 82  3
     }
 83  
 
 84  
     
 85  
     /**
 86  
      * {@inheritDoc}
 87  
      */
 88  
     public SelectorHandler getSelectorHandler() {
 89  0
         return selectorHandler;
 90  
     } 
 91  
 
 92  
 
 93  
     /**
 94  
      * {@inheritDoc}
 95  
      */
 96  
     public void setSelectorHandler(SelectorHandler selectorHandler) {
 97  260
         this.selectorHandler = selectorHandler;
 98  260
     }
 99  
 
 100  
 
 101  
     /**
 102  
      * {@inheritDoc}
 103  
      */
 104  
     public void process(SelectionKey key) {
 105  144142
         Object attachment = key.attachment();
 106  
         
 107  
         // NOTE: If attachment == null, instanceof will evaluate to false.
 108  
         //       If attachment == null does not generate NullPointerException
 109  144142
         if (attachment instanceof SelectionKeyActionAttachment) {
 110  115399
             ((SelectionKeyActionAttachment) attachment).process(key);
 111  
         }
 112  144142
     }
 113  
 
 114  
 
 115  
     /**
 116  
      * {@inheritDoc}
 117  
      */
 118  
     public void postProcess(SelectionKey key) {
 119  143140
         Object attachment = key.attachment();
 120  
         
 121  
         // NOTE: If attachment == null, instanceof will evaluate to false.
 122  
         //       If attachment == null does not generate NullPointerException
 123  143140
         if (attachment instanceof SelectionKeyActionAttachment) {
 124  115403
             ((SelectionKeyActionAttachment) attachment).postProcess(key);
 125  
         }
 126  143139
     }
 127  
 
 128  
 
 129  
     /**
 130  
      * @deprecated
 131  
      */
 132  
     public void register(SelectionKey key, long currentTime) {
 133  0
         throw new UnsupportedOperationException(getClass().getName() + 
 134  
                         " use of this API not allowed.");
 135  
     }
 136  
 
 137  
 
 138  
     /**
 139  
      * {@inheritDoc}
 140  
      */
 141  
     public void register(SelectionKey key, int selectionKeyOps) {
 142  49458
         doRegisterKey(key, selectionKeyOps);
 143  49458
     }
 144  
 
 145  
     
 146  
     /**
 147  
      * Registers {@link SelectionKey} to handle certain operations
 148  
      */
 149  
     protected void doRegisterKey(SelectionKey key, int selectionKeyOps) {
 150  49458
         if (!key.isValid()) {
 151  0
             return;
 152  
         }
 153  49458
         key.interestOps(key.interestOps() | selectionKeyOps);
 154  49458
     }
 155  
 
 156  
 
 157  
     /**
 158  
      * {@inheritDoc}
 159  
      */
 160  
     public void register(SelectableChannel channel, int selectionKeyOps) throws ClosedChannelException {
 161  0
         if (!channel.isOpen()) {
 162  0
             return;
 163  
         }
 164  0
         Selector selector = selectorHandler.getSelector();
 165  0
         SelectionKey key = channel.keyFor(selector);
 166  0
         if (key == null) {
 167  0
             channel.register(selector, selectionKeyOps);
 168  
         } else {
 169  0
             doRegisterKey(key, selectionKeyOps);
 170  
         }
 171  0
     }
 172  
 
 173  
 
 174  
     /**
 175  
      * {@inheritDoc}
 176  
      */
 177  
     public void register(Iterator<SelectionKey> keyIterator, int selectionKeyOps) {
 178  
         SelectionKey key;
 179  0
         while (keyIterator.hasNext()) {
 180  0
             key = keyIterator.next();
 181  0
             keyIterator.remove();
 182  0
             doRegisterKey(key, selectionKeyOps);
 183  
         }
 184  0
     }
 185  
 
 186  
 
 187  
     /**
 188  
      * @deprecated
 189  
      */
 190  
     public void expire(SelectionKey key, long currentTime) {
 191  
         // Do nothing.
 192  0
     }
 193  
 
 194  
 
 195  
     /**
 196  
      * {@inheritDoc}
 197  
      */
 198  
     public void expire(Iterator<SelectionKey> keyIterator) {
 199  
         // Do nothing.
 200  48940
     }
 201  
 
 202  
 
 203  
     /**
 204  
      * Cancel a SelectionKey and close its associated Channel.
 205  
      * @param key {@link SelectionKey} to cancel
 206  
      */
 207  
     public void cancel(SelectionKey key) {
 208  433
         if (!keyIsValid(key)) {
 209  10
             return;
 210  
         }
 211  
 
 212  423
         closeChannel(key);
 213  
 
 214  423
         clearKeyAttachment(key);
 215  
         
 216  423
         cancelKey(key);
 217  423
     }
 218  
 
 219  
 
 220  
     /**
 221  
      * {@inheritDoc}
 222  
      */
 223  
     public void close(SelectionKey key) {
 224  191
         cancel(key);
 225  191
     }
 226  
 
 227  
     /**
 228  
      * {@inheritDoc}
 229  
      */
 230  
     public void copyTo(Copyable copy) {
 231  4
         BaseSelectionKeyHandler copyHandler = (BaseSelectionKeyHandler) copy;
 232  4
         copyHandler.selectorHandler = selectorHandler;
 233  4
     }
 234  
 
 235  
 
 236  
     public Logger getLogger() {
 237  2
         return logger;
 238  
     }
 239  
     
 240  
     
 241  
     public void setLogger(Logger logger) {
 242  0
         this.logger = logger;
 243  0
     }
 244  
     
 245  
     
 246  
     protected void cancelKey(SelectionKey key) {
 247  423
         key.cancel();
 248  423
         key = null;
 249  423
     }
 250  
 
 251  
 
 252  
     protected boolean keyIsValid(SelectionKey key) {
 253  433
         if (key != null && key.isValid()) {
 254  423
             return true;
 255  
         } else {
 256  10
             return false;
 257  
         }
 258  
     }
 259  
 
 260  
     
 261  
     protected void closeChannel(SelectionKey key) {
 262  423
         if (selectorHandler != null) {
 263  423
             selectorHandler.closeChannel(key.channel());
 264  
         } else {
 265  0
             closeChannel(key.channel());
 266  
         }
 267  423
     }
 268  
 
 269  
 
 270  
     protected Object clearKeyAttachment(SelectionKey key) {
 271  423
         return key.attach(null);
 272  
     }
 273  
 
 274  
 
 275  
     protected void closeChannel(SelectableChannel channel) {
 276  
         try {
 277  0
             if (channel instanceof SocketChannel) {
 278  0
                 Socket socket = ((SocketChannel) channel).socket();
 279  
                 try {
 280  0
                     if (!socket.isInputShutdown()) {
 281  0
                         socket.shutdownInput();
 282  
                     }
 283  0
                 } catch (IOException ex) {
 284  0
                     Logger.getLogger(BaseSelectionKeyHandler.class.getName()).
 285  
                                log(Level.FINE, null, ex);
 286  0
                 }
 287  
                 try {
 288  0
                     if (!socket.isOutputShutdown()) {
 289  0
                         socket.shutdownOutput();
 290  
                     }
 291  0
                 } catch (IOException ex) {
 292  0
                     Logger.getLogger(BaseSelectionKeyHandler.class.getName()).
 293  
                                log(Level.FINE, null, ex);
 294  0
                 }
 295  
                 try {
 296  0
                     socket.close();
 297  0
                 } catch (IOException ex) {
 298  0
                     Logger.getLogger(BaseSelectionKeyHandler.class.getName()).
 299  
                                log(Level.FINE, null, ex);
 300  0
                 }
 301  
             }
 302  0
             channel.close();
 303  0
         } catch (IOException ex) {
 304  0
             Logger.getLogger(BaseSelectionKeyHandler.class.getName()).
 305  
                        log(Level.FINE, null, ex);
 306  0
         }
 307  0
     }
 308  
 }