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

Classes in this File | Line Coverage | Branch Coverage | Complexity
DefaultCallbackHandler |
43 % | 13/30
50 % | 3/6
| 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 |  |
     package com.sun.grizzly;
39 |  |
40 |  |
     import com.sun.grizzly.Controller.Protocol;
41 |  |
     import java.io.IOException;
42 |  |
     import java.nio.channels.SelectionKey;
43 |  |
     import java.nio.channels.SocketChannel;
44 |  |
     import java.util.logging.Level;
45 |  |
     import java.util.logging.Logger;
46 |  |
47 |  |
     /**
48 |  |
      * Default {@link CallbackHandler} implementation that implements the connect
49 |  |
      * operations, and delegate the read and write operations to its associated
50 |  |
      * {@link SelectorHandler} {@link ProtocolChain}, like the default
51 |  |
      * {@link SelectorHandler} is doing server side component. The code below
52 |  |
      * can be used for by {@link ConnectorHandler} to manipulater the read
53 |  |
      * and write operation of a non blocking client implementation.
54 |  |
      * <p>
55 |  |
      * A {@link ConnectorHandler} can use this {@link CallbackHandler} and delegate
56 |  |
      * the processing of the bytes to a {@link ProtocolChain}
57 |  |
      * </p>
58 |  |
      * <p><pre><code>
59 |  |
      *       Controller sel = new Controller();
60 |  |
      *       sel.setProtocolChainInstanceHandler(new DefaultProtocolChainInstanceHandler(){
61 |  |
      *           public ProtocolChain poll() {
62 |  |
      *               ProtocolChain protocolChain = protocolChains.poll();
63 |  |
      *               if (protocolChain == null){
64 |  |
      *                   protocolChain = new DefaultProtocolChain();
65 |  |
      *                   protocolChain.addFilter(new ReadWriteFilter());
66 |  |
      *                   protocolChain.addFilter(new LogFilter());
67 |  |
      *               }
68 |  |
      *               return protocolChain;
69 |  |
      *           }
70 |  |
      *       });
71 |  |
      *       TCPConnectorHandler tcph = Controller.acquireConnectorHandler(Protocol.TCP);
72 |  |
      *       tcph.connect(....);
73 |  |
      *
74 |  |
      * </code></pre></p><p> With the above example, all read and write operations
75 |  |
      * will be handled by the {@link ProtocolChain} instead of having to be
76 |  |
      * implemented inside the {@link CallbackHandler#onRead} and
77 |  |
      * {@link CallbackHandler#onWrite}
78 |  |
      * </p>
79 |  |
      * @author Jeanfrancois Arcand
80 |  |
      */
81 |  |
     public class DefaultCallbackHandler implements SSLCallbackHandler<Context> {
82 |  |
83 |  |
         /**
84 |  |
          * Associated {@link ConnectorHandler}
85 |  |
          */
86 |  |
         private ConnectorHandler connectorHandler;
87 |  |
88 |  |
89 |  |
         /**
90 |  |
          * <tt>true</tt> if delegation is enabled.
91 |  |
          */
92 | 30 |
         private boolean delegateToProtocolChain = true;
93 |  |
94 |  |
95 |  |
         /**
96 |  |
          * Create a {@link CallbackHandler} that delegate the read and write
97 |  |
          * operation to the {@link ProtocolChain} associated with the
98 |  |
          * {@link ConnectorHandler}
99 |  |
          * @param connectorHandler An instance of {@link ConnectorHandler}
100 |  |
          */
101 |  |
         public DefaultCallbackHandler(ConnectorHandler connectorHandler) {
102 | 0 |
             this(connectorHandler,true);
103 | 0 |
         }
104 |  |
105 |  |
106 |  |
         /**
107 |  |
          * Create a {@link CallbackHandler} that delegate the read and write
108 |  |
          * operation to the {@link ProtocolChain} associated with the
109 |  |
          * {@link ConnectorHandler}. Delegation is disabled when
110 |  |
          * @param connectorHandler An instance of {@link ConnectorHandler}
111 |  |
          * @param delegateToProtocolChain true to delegate the read/write operation
112 |  |
          *        to a {@link ProtocolChain}
113 |  |
          */
114 | 30 |
         public DefaultCallbackHandler(ConnectorHandler connectorHandler,boolean delegateToProtocolChain) {
115 | 30 |
             this.connectorHandler = connectorHandler;
116 | 30 |
             this.delegateToProtocolChain = delegateToProtocolChain;
117 | 30 |
         }
118 |  |
119 |  |
120 |  |
         /**
121 |  |
          * Execute the non blocking connect operation.
122 |  |
          * @param ioEvent an {@link IOEvent} representing the current state
123 |  |
          * of the OP_CONNECT operations.
124 |  |
          */
125 |  |
         public void onConnect(IOEvent<Context> ioEvent) {
126 | 30 |
             SelectionKey key = ioEvent.attachment().getSelectionKey();
127 | 30 |
             if (ioEvent.attachment().getProtocol() == Controller.Protocol.TCP){
128 | 20 |
                 ((TCPConnectorHandler)connectorHandler).setUnderlyingChannel(
129 |  |
                         (SocketChannel)key.channel());
130 | 10 |
             } else if (ioEvent.attachment().getProtocol() == Controller.Protocol.TLS){
131 | 10 |
                 ((SSLConnectorHandler)connectorHandler).setUnderlyingChannel(
132 |  |
                         (SocketChannel)key.channel());
133 |  |
             }
134 |  |
             try {
135 | 30 |
                 connectorHandler.finishConnect(key);
136 | 0 |
             } catch (IOException ex) {
137 | 0 |
                 Controller.logger().severe(ex.getMessage());
138 | 30 |
             }
139 | 30 |
         }
140 |  |
141 |  |
142 |  |
         /**
143 |  |
          * Delegate the processing of the read operation to the {@link IOEvent{
144 |  |
          * associated {@link ProtocolChain}
145 |  |
          * @param ioEvent an {@link IOEvent} representing the current state of the
146 |  |
          * OP_CONNECT operations.
147 |  |
          */
148 |  |
         public void onRead(IOEvent<Context> ioEvent) {
149 | 0 |
             if (!delegateToProtocolChain) return;
150 | 0 |
             Context context = ioEvent.attachment();
151 |  |
             try {
152 | 0 |
                 context.getProtocolChain().execute(context);
153 | 0 |
             } catch (Exception ex) {
154 | 0 |
                 Controller.logger().log(Level.SEVERE, "Read/Write operation failed.", ex);
155 | 0 |
             }
156 | 0 |
         }
157 |  |
158 |  |
159 |  |
         /**
160 |  |
          * Delegate the processing of the write operation to the {@link IOEvent{
161 |  |
          * associated {@link ProtocolChain}
162 |  |
          * @param ioEvent an {@link IOEvent} representing the current state of the
163 |  |
          * OP_CONNECT operations.
164 |  |
          */
165 |  |
         public void onWrite(IOEvent<Context> ioEvent) {
166 | 0 |
             onRead(ioEvent);
167 | 0 |
         }
168 |  |
169 |  |
170 |  |
         /**
171 |  |
          * By default, do nothing.
172 |  |
          * @param ioEvent an {@link IOEvent} representing the current state of the
173 |  |
          * handshake operations.
174 |  |
          */
175 |  |
         public void onHandshake(IOEvent<Context> ioEvent) {
176 | 0 |
         }
177 |  |
178 |  |
179 |  |
         /**
180 |  |
          * Return <tt>true></tt> if delegation to the {@link ProtocolChain} is enabled.
181 |  |
          * @return <tt>true></tt> if delegation to the {@link ProtocolChain} is enabled.
182 |  |
          */
183 |  |
         public boolean isDelegateToProtocolChain() {
184 | 0 |
             return delegateToProtocolChain;
185 |  |
         }
186 |  |
187 |  |
188 |  |
         /**
189 |  |
          * Set to <tt>true></tt> to enable delagation of the read and write operations
190 |  |
          * to a {@link ProtocolChain} <tt>true></tt> to enable delagation of the read and write operations
191 |  |
          * to a {@link ProtocolChain}
192 |  |
          * @param delegateToProtocolChain
193 |  |
          */
194 |  |
         public void setDelegateToProtocolChain(boolean delegateToProtocolChain) {
195 | 0 |
             this.delegateToProtocolChain = delegateToProtocolChain;
196 | 0 |
         }
197 |  |
     }