Coverage Report - com.sun.grizzly.util.SelectionKeyOP
 
Classes in this File Line Coverage Branch Coverage Complexity
SelectionKeyOP
94 %
32/34
75 %
12/16
0
SelectionKeyOP$1
100 %
2/2
N/A
0
SelectionKeyOP$2
100 %
2/2
N/A
0
SelectionKeyOP$ConnectSelectionKeyOP
100 %
14/14
N/A
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.util;
 40  
 
 41  
 import com.sun.grizzly.CallbackHandler;
 42  
 import java.net.SocketAddress;
 43  
 import java.nio.channels.SelectableChannel;
 44  
 import java.nio.channels.SelectionKey;
 45  
 
 46  
 /**
 47  
  *
 48  
  * @author Alexey Stashok
 49  
  */
 50  1011
 public class SelectionKeyOP {
 51  
     private int op;
 52  
     private SelectionKey key;
 53  
     private SelectableChannel channel;
 54  
     
 55  1
     private static ConcurrentLinkedQueuePool<SelectionKeyOP> readWritePool =
 56  1012
             new ConcurrentLinkedQueuePool<SelectionKeyOP>() {
 57  
                 @Override
 58  
                 public SelectionKeyOP newInstance() {
 59  1011
                     return new SelectionKeyOP();
 60  
                 }
 61  
             };
 62  
     
 63  1
     private static ConcurrentLinkedQueuePool<SelectionKeyOP> connectPool =
 64  2
             new ConcurrentLinkedQueuePool<SelectionKeyOP>() {
 65  
                 @Override
 66  
                 public SelectionKeyOP newInstance() {
 67  1
                     return new ConnectSelectionKeyOP();
 68  
                 }
 69  
             };
 70  
     
 71  
     public static SelectionKeyOP aquireSelectionKeyOP(int op) {
 72  539978
         if (op == SelectionKey.OP_READ || op == SelectionKey.OP_WRITE ||
 73  
                 op == (SelectionKey.OP_WRITE | SelectionKey.OP_READ)) {
 74  539772
             SelectionKeyOP operation = readWritePool.poll();
 75  539764
             return operation;
 76  205
         } else if (op == SelectionKey.OP_CONNECT) {
 77  205
             SelectionKeyOP operation = connectPool.poll();
 78  205
             return operation;
 79  
         }
 80  
         
 81  0
         throw new IllegalStateException("Unknown operation or operation is not supported");
 82  
     }
 83  
     
 84  
     public static void releaseSelectionKeyOP(SelectionKeyOP operation) {
 85  539964
         int op = operation.op;
 86  539964
         operation.recycle();
 87  
         
 88  539964
         if (op == SelectionKey.OP_READ || op == SelectionKey.OP_WRITE ||
 89  
                 op == (SelectionKey.OP_WRITE | SelectionKey.OP_READ)) {
 90  539759
             readWritePool.offer(operation);
 91  539759
             return;
 92  205
         } else if (op == SelectionKey.OP_CONNECT) {
 93  205
             connectPool.offer(operation);
 94  205
             return;
 95  
         }
 96  
         
 97  0
         throw new IllegalStateException("Unknown operation or operation is not supported");
 98  
     }
 99  
 
 100  1012
     private SelectionKeyOP() {
 101  1011
     }
 102  
     
 103  
     public int getOp() {
 104  1619482
         return op;
 105  
     }
 106  
 
 107  
     public void setOp(int op) {
 108  539977
         this.op = op;
 109  539977
     }
 110  
 
 111  
     public SelectionKey getSelectionKey() {
 112  539759
         return key;
 113  
     }
 114  
 
 115  
     public void setSelectionKey(SelectionKey key) {
 116  539743
         this.key = key;
 117  539749
     }
 118  
 
 119  
     public SelectableChannel getChannel() {
 120  23
         return channel;
 121  
     }
 122  
 
 123  
     public void setChannel(SelectableChannel channel) {
 124  23
         this.channel = channel;
 125  23
     }
 126  
     
 127  
     protected void recycle() {
 128  539759
         op = 0;
 129  539759
         key = null;
 130  539759
         channel = null;
 131  539759
     }
 132  
     
 133  1
     public static class ConnectSelectionKeyOP extends SelectionKeyOP {
 134  
         private SocketAddress localAddress;
 135  
         private SocketAddress remoteAddress;
 136  
         private CallbackHandler callbackHandler;
 137  
 
 138  
         public SocketAddress getLocalAddress() {
 139  205
             return localAddress;
 140  
         }
 141  
 
 142  
         public void setLocalAddress(SocketAddress localAddress) {
 143  205
             this.localAddress = localAddress;
 144  205
         }
 145  
 
 146  
         public SocketAddress getRemoteAddress() {
 147  205
             return remoteAddress;
 148  
         }
 149  
 
 150  
         public void setRemoteAddress(SocketAddress remoteAddress) {
 151  205
             this.remoteAddress = remoteAddress;
 152  205
         }
 153  
 
 154  
         public CallbackHandler getCallbackHandler() {
 155  205
             return callbackHandler;
 156  
         }
 157  
 
 158  
         public void setCallbackHandler(CallbackHandler callbackHandler) {
 159  205
             this.callbackHandler = callbackHandler;
 160  205
         }
 161  
         
 162  
         @Override
 163  
         protected void recycle() {
 164  205
             localAddress = null;
 165  205
             remoteAddress = null;
 166  205
             callbackHandler = null;
 167  205
         }
 168  
     }
 169  
 }