##### Coverage Report - com.sun.grizzly.BaseSelectionKeyHandler

Classes in this File | Line Coverage | Branch Coverage | Complexity
BaseSelectionKeyHandler |
53 % | 47/88
46 % | 13/28
| 0.0;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 com.sun.grizzly.util.SelectionKeyAttachment;
44 |  |
     import java.io.IOException;
45 |  |
     import java.net.Socket;
46 |  |
     import java.nio.channels.ClosedChannelException;
47 |  |
     import java.nio.channels.SelectableChannel;
48 |  |
     import java.nio.channels.SelectionKey;
49 |  |
     import java.nio.channels.Selector;
50 |  |
     import java.nio.channels.SocketChannel;
51 |  |
     import java.util.Iterator;
52 |  |
     import java.util.logging.Level;
53 |  |
     import java.util.logging.Logger;
54 |  |
55 |  |
     /**
56 |  |
      * This class is an implementation of a SelectionKeyHandler which does
57 |  |
      * not use the SelectionKey attachment, does not expire keys or utilize
58 |  |
      * a keep-alive mechanism. However, this is currently not the
59 |  |
      * SelectionKeyHandler provisioned by default with Grizzly's Controller.
60 |  |
      * Hence for an application to use this SelectionKeyHandler, Grizzly's
61 |  |
      * Controller must be explicitly configured to use this SelectionKeyHandler
62 |  |
      * implementation.
63 |  |
      *
64 |  |
      * @author Charlie Hunt
65 |  |
      */
66 |  |
     public class BaseSelectionKeyHandler implements SelectionKeyHandler {
67 |  |
68 | 135 |
         protected Logger logger = Controller.logger();
69 |  |
70 |  |
71 |  |
         /**
72 |  |
          * Associated {@link SelectorHandler}
73 |  |
          */
74 |  |
         protected SelectorHandler selectorHandler;
75 |  |
76 |  |
77 | 132 |
         public BaseSelectionKeyHandler() {
78 | 132 |
         }
79 |  |
80 |  |
81 | 3 |
         public BaseSelectionKeyHandler(SelectorHandler selectorHandler) {
82 | 3 |
             this.selectorHandler = selectorHandler;
83 | 3 |
         }
84 |  |
85 |  |
86 |  |
         /**
87 |  |
          * {@inheritDoc}
88 |  |
          */
89 |  |
         public SelectorHandler getSelectorHandler() {
90 | 0 |
             return selectorHandler;
91 |  |
         }
92 |  |
93 |  |
94 |  |
         /**
95 |  |
          * {@inheritDoc}
96 |  |
          */
97 |  |
         public void setSelectorHandler(SelectorHandler selectorHandler) {
98 | 260 |
             this.selectorHandler = selectorHandler;
99 | 260 |
         }
100 |  |
101 |  |
102 |  |
         /**
103 |  |
          * {@inheritDoc}
104 |  |
          */
105 |  |
         public void process(SelectionKey key) {
106 | 149430 |
             Object attachment = key.attachment();
107 |  |
108 |  |
             // NOTE: If attachment == null, instanceof will evaluate to false.
109 |  |
             //       If attachment == null does not generate NullPointerException
110 | 149430 |
             if (attachment instanceof SelectionKeyActionAttachment) {
111 | 111706 |
                 ((SelectionKeyActionAttachment) attachment).process(key);
112 |  |
             }
113 | 149430 |
         }
114 |  |
115 |  |
116 |  |
         /**
117 |  |
          * {@inheritDoc}
118 |  |
          */
119 |  |
         public void postProcess(SelectionKey key) {
120 | 148428 |
             Object attachment = key.attachment();
121 |  |
122 |  |
             // NOTE: If attachment == null, instanceof will evaluate to false.
123 |  |
             //       If attachment == null does not generate NullPointerException
124 | 148428 |
             if (attachment instanceof SelectionKeyActionAttachment) {
125 | 111695 |
                 ((SelectionKeyActionAttachment) attachment).postProcess(key);
126 |  |
             }
127 | 148428 |
         }
128 |  |
129 |  |
130 |  |
         /**
131 |  |
          * @deprecated
132 |  |
          */
133 |  |
         public void register(SelectionKey key, long currentTime) {
134 | 0 |
             throw new UnsupportedOperationException(getClass().getName() +
135 |  |
                             " use of this API not allowed.");
136 |  |
         }
137 |  |
138 |  |
139 |  |
         /**
140 |  |
          * {@inheritDoc}
141 |  |
          */
142 |  |
         public void register(SelectionKey key, int selectionKeyOps) {
143 | 50545 |
             doRegisterKey(key, selectionKeyOps);
144 | 50545 |
         }
145 |  |
146 |  |
147 |  |
         /**
148 |  |
          * Registers {@link SelectionKey} to handle certain operations
149 |  |
          */
150 |  |
         protected void doRegisterKey(SelectionKey key, int selectionKeyOps) {
151 | 50545 |
             if (!key.isValid()) {
152 | 0 |
                 return;
153 |  |
             }
154 | 50545 |
             key.interestOps(key.interestOps() | selectionKeyOps);
155 | 50545 |
         }
156 |  |
157 |  |
158 |  |
         /**
159 |  |
          * {@inheritDoc}
160 |  |
          */
161 |  |
         public void register(SelectableChannel channel, int selectionKeyOps) throws ClosedChannelException {
162 | 0 |
             if (!channel.isOpen()) {
163 | 0 |
                 return;
164 |  |
             }
165 | 0 |
             Selector selector = selectorHandler.getSelector();
166 | 0 |
             SelectionKey key = channel.keyFor(selector);
167 | 0 |
             if (key == null) {
168 | 0 |
                 channel.register(selector, selectionKeyOps);
169 |  |
             } else {
170 | 0 |
                 doRegisterKey(key, selectionKeyOps);
171 |  |
             }
172 | 0 |
         }
173 |  |
174 |  |
175 |  |
         /**
176 |  |
          * {@inheritDoc}
177 |  |
          */
178 |  |
         public void register(Iterator<SelectionKey> keyIterator, int selectionKeyOps) {
179 |  |
             SelectionKey key;
180 | 0 |
             while (keyIterator.hasNext()) {
181 | 0 |
                 key = keyIterator.next();
182 | 0 |
                 keyIterator.remove();
183 | 0 |
                 doRegisterKey(key, selectionKeyOps);
184 |  |
             }
185 | 0 |
         }
186 |  |
187 |  |
188 |  |
         /**
189 |  |
          * @deprecated
190 |  |
          */
191 |  |
         public void expire(SelectionKey key, long currentTime) {
192 |  |
             // Do nothing.
193 | 0 |
         }
194 |  |
195 |  |
196 |  |
         /**
197 |  |
          * {@inheritDoc}
198 |  |
          */
199 |  |
         public void expire(Iterator<SelectionKey> keyIterator) {
200 |  |
             // Do nothing.
201 | 47705 |
         }
202 |  |
203 |  |
204 |  |
         /**
205 |  |
          * Cancel a SelectionKey and close its associated Channel.
206 |  |
          * @param key {@link SelectionKey} to cancel
207 |  |
          */
208 |  |
         public void cancel(SelectionKey key) {
209 | 443 |
             if (!keyIsValid(key)) {
210 | 4 |
                 return;
211 |  |
             }
212 |  |
213 | 439 |
             closeChannel(key);
214 |  |
215 | 439 |
             clearKeyAttachment(key);
216 |  |
217 | 439 |
             cancelKey(key);
218 | 439 |
         }
219 |  |
220 |  |
221 |  |
         /**
222 |  |
          * {@inheritDoc}
223 |  |
          */
224 |  |
         public void close(SelectionKey key) {
225 | 198 |
             cancel(key);
226 | 198 |
         }
227 |  |
228 |  |
         /**
229 |  |
          * {@inheritDoc}
230 |  |
          */
231 |  |
         public void copyTo(Copyable copy) {
232 | 4 |
             BaseSelectionKeyHandler copyHandler = (BaseSelectionKeyHandler) copy;
233 | 4 |
             copyHandler.selectorHandler = selectorHandler;
234 | 4 |
         }
235 |  |
236 |  |
237 |  |
         public Logger getLogger() {
238 | 2 |
             return logger;
239 |  |
         }
240 |  |
241 |  |
242 |  |
         public void setLogger(Logger logger) {
243 | 0 |
             this.logger = logger;
244 | 0 |
         }
245 |  |
246 |  |
247 |  |
         protected void cancelKey(SelectionKey key) {
248 | 439 |
             key.cancel();
249 | 439 |
             key = null;
250 | 439 |
         }
251 |  |
252 |  |
253 |  |
         protected boolean keyIsValid(SelectionKey key) {
254 | 443 |
             if (key != null && key.isValid()) {
255 | 439 |
                 return true;
256 |  |
             } else {
257 | 4 |
                 return false;
258 |  |
             }
259 |  |
         }
260 |  |
261 |  |
262 |  |
         protected void closeChannel(SelectionKey key) {
263 | 439 |
             if (selectorHandler != null) {
264 | 439 |
                 selectorHandler.closeChannel(key.channel());
265 |  |
             } else {
266 | 0 |
                 closeChannel(key.channel());
267 |  |
             }
268 | 439 |
         }
269 |  |
270 |  |
271 |  |
         protected Object clearKeyAttachment(SelectionKey key) {
272 | 439 |
             Object attachment = key.attach(null);
273 | 439 |
             if (attachment instanceof SelectionKeyAttachment) {
274 | 328 |
                 ((SelectionKeyAttachment) attachment).release(key);
275 |  |
             }
276 |  |
277 | 439 |
             return attachment;
278 |  |
         }
279 |  |
280 |  |
281 |  |
         protected void closeChannel(SelectableChannel channel) {
282 |  |
             try {
283 | 0 |
                 if (channel instanceof SocketChannel) {
284 | 0 |
                     Socket socket = ((SocketChannel) channel).socket();
285 |  |
                     try {
286 | 0 |
                         if (!socket.isInputShutdown()) {
287 | 0 |
                             socket.shutdownInput();
288 |  |
                         }
289 | 0 |
                     } catch (IOException ex) {
290 | 0 |
                         Logger.getLogger(BaseSelectionKeyHandler.class.getName()).
291 |  |
                                    log(Level.FINE, null, ex);
292 | 0 |
                     }
293 |  |
                     try {
294 | 0 |
                         if (!socket.isOutputShutdown()) {
295 | 0 |
                             socket.shutdownOutput();
296 |  |
                         }
297 | 0 |
                     } catch (IOException ex) {
298 | 0 |
                         Logger.getLogger(BaseSelectionKeyHandler.class.getName()).
299 |  |
                                    log(Level.FINE, null, ex);
300 | 0 |
                     }
301 |  |
                     try {
302 | 0 |
                         socket.close();
303 | 0 |
                     } catch (IOException ex) {
304 | 0 |
                         Logger.getLogger(BaseSelectionKeyHandler.class.getName()).
305 |  |
                                    log(Level.FINE, null, ex);
306 | 0 |
                     }
307 |  |
                 }
308 | 0 |
                 channel.close();
309 | 0 |
             } catch (IOException ex) {
310 | 0 |
                 Logger.getLogger(BaseSelectionKeyHandler.class.getName()).
311 |  |
                            log(Level.FINE, null, ex);
312 | 0 |
             }
313 | 0 |
         }
314 |  |
     }