| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ParserProtocolFilter |
|
| 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.filter; | |
| 40 | ||
| 41 | import com.sun.grizzly.Context; | |
| 42 | import com.sun.grizzly.Context.AttributeScope; | |
| 43 | import com.sun.grizzly.ProtocolFilter; | |
| 44 | import com.sun.grizzly.ProtocolParser; | |
| 45 | import com.sun.grizzly.util.AttributeHolder; | |
| 46 | import com.sun.grizzly.util.WorkerThread; | |
| 47 | import java.io.IOException; | |
| 48 | import java.nio.ByteBuffer; | |
| 49 | import java.nio.channels.SelectionKey; | |
| 50 | ||
| 51 | import com.sun.grizzly.util.ThreadAttachment; | |
| 52 | import com.sun.grizzly.util.ThreadAttachment.Mode; | |
| 53 | ||
| 54 | /** | |
| 55 | * Simple ProtocolFilter implementation which read the available bytes | |
| 56 | * and delegate the decision of reading more bytes or not to a ProtocolParser. | |
| 57 | * The ProtocolParser will decide if more bytes are required before continuing | |
| 58 | * the invokation of the ProtocolChain. | |
| 59 | * | |
| 60 | * @author Jeanfrancois Arcand | |
| 61 | */ | |
| 62 | public abstract class ParserProtocolFilter extends ReadFilter{ | |
| 63 | ||
| 64 | /** | |
| 65 | * Should ParserProtocolFilter read the data from channel | |
| 66 | */ | |
| 67 | protected boolean isSkipRead; | |
| 68 | ||
| 69 | 2 | public ParserProtocolFilter() { |
| 70 | 2 | } |
| 71 | ||
| 72 | /** | |
| 73 | * Read available bytes and delegate the processing of them to the next | |
| 74 | * ProtocolFilter in the ProtocolChain. | |
| 75 | * @return <tt>true</tt> if the next ProtocolFilter on the ProtocolChain | |
| 76 | * need to be invoked. | |
| 77 | */ | |
| 78 | @Override | |
| 79 | public boolean execute(Context ctx) throws IOException { | |
| 80 | 3 | ProtocolParser parser = null; |
| 81 | ||
| 82 | // Remove Parser from Connection related attributes (if it was there) | |
| 83 | ||
| 84 | 3 | AttributeHolder connectionAttrs = |
| 85 | ctx.getAttributeHolderByScope(AttributeScope.CONNECTION); | |
| 86 | 3 | if (connectionAttrs != null) { |
| 87 | 2 | parser = (ProtocolParser) connectionAttrs.removeAttribute(ProtocolParser.PARSER); |
| 88 | } | |
| 89 | ||
| 90 | 3 | if (parser == null) { |
| 91 | 3 | parser = (ProtocolParser) ctx.getAttribute(ProtocolParser.PARSER); |
| 92 | ||
| 93 | 3 | if (parser == null) { |
| 94 | 2 | parser = newProtocolParser(); |
| 95 | 2 | ctx.setAttribute(ProtocolParser.PARSER, parser); |
| 96 | } | |
| 97 | } else { | |
| 98 | 0 | ctx.setAttribute(ProtocolParser.PARSER, parser); |
| 99 | } | |
| 100 | ||
| 101 | 3 | boolean isExpectingMoreData = parser.isExpectingMoreData(); |
| 102 | 3 | if (isExpectingMoreData || !parser.hasMoreBytesToParse()) { |
| 103 | 3 | if (isExpectingMoreData) { |
| 104 | // Saved ByteBuffer was restored. May be we will not need it next time | |
| 105 | 0 | ((WorkerThread) Thread.currentThread()).updateAttachment(Mode.ATTRIBUTES_ONLY); |
| 106 | } | |
| 107 | ||
| 108 | 3 | boolean continueExecution = isSkipRead || super.execute(ctx); |
| 109 | 3 | WorkerThread workerThread = (WorkerThread)Thread.currentThread(); |
| 110 | 3 | ByteBuffer byteBuffer = workerThread.getByteBuffer(); |
| 111 | 3 | parser.startBuffer(byteBuffer); |
| 112 | 3 | if (!continueExecution){ |
| 113 | 1 | return continueExecution; |
| 114 | } | |
| 115 | } | |
| 116 | 2 | if (!parser.hasNextMessage()) { |
| 117 | 1 | return false; |
| 118 | } | |
| 119 | ||
| 120 | 1 | return invokeProtocolParser(ctx, parser); |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * Invoke the {@link ProtocolParser}. If more bytes are required, | |
| 125 | * register the {@link SelectionKey} back to its associated | |
| 126 | * {@link SelectorHandler} | |
| 127 | * @param ctx the Context object. | |
| 128 | * @return <tt>true</tt> if no more bytes are needed. | |
| 129 | */ | |
| 130 | protected boolean invokeProtocolParser(Context ctx, ProtocolParser parser) { | |
| 131 | ||
| 132 | 1 | if (parser == null){ |
| 133 | 0 | throw new IllegalStateException("ProcotolParser cannot be null"); |
| 134 | } | |
| 135 | ||
| 136 | 1 | Object o = parser.getNextMessage(); |
| 137 | 1 | ctx.setAttribute(ProtocolParser.MESSAGE, o); |
| 138 | 1 | return true; |
| 139 | } | |
| 140 | ||
| 141 | @Override | |
| 142 | public boolean postExecute(Context context) throws IOException { | |
| 143 | 3 | ProtocolParser parser = (ProtocolParser) |
| 144 | context.getAttribute(ProtocolParser.PARSER); | |
| 145 | ||
| 146 | 3 | if (parser == null) return true; |
| 147 | ||
| 148 | 3 | if (parser != null && parser.hasMoreBytesToParse()) { |
| 149 | // Need to say that we read successfully since bytes are left | |
| 150 | 0 | context.setAttribute(ProtocolFilter.SUCCESSFUL_READ, Boolean.TRUE); |
| 151 | 0 | return true; |
| 152 | } | |
| 153 | ||
| 154 | 3 | SelectionKey key = context.getSelectionKey(); |
| 155 | 3 | if (parser.isExpectingMoreData()) { |
| 156 | 1 | if (parser.releaseBuffer()) { |
| 157 | 0 | saveParser(key, parser); |
| 158 | } | |
| 159 | ||
| 160 | 1 | saveByteBuffer(key); |
| 161 | ||
| 162 | // Register to get more bytes. | |
| 163 | 1 | context.getSelectorHandler().register(key,SelectionKey.OP_READ); |
| 164 | 1 | return false; |
| 165 | } | |
| 166 | ||
| 167 | 2 | if (parser.releaseBuffer()) { |
| 168 | 0 | saveParser(key, parser); |
| 169 | } | |
| 170 | ||
| 171 | 2 | return super.postExecute(context); |
| 172 | } | |
| 173 | ||
| 174 | /** | |
| 175 | * Return a new or cached ProtocolParser instance. | |
| 176 | */ | |
| 177 | public abstract ProtocolParser newProtocolParser(); | |
| 178 | ||
| 179 | private void saveByteBuffer(SelectionKey key) { | |
| 180 | 1 | WorkerThread workerThread = (WorkerThread) Thread.currentThread(); |
| 181 | // Detach the current Thread data. | |
| 182 | 1 | ThreadAttachment threadAttachment = |
| 183 | workerThread.updateAttachment(Mode.BYTE_BUFFER); | |
| 184 | ||
| 185 | // Attach it to the SelectionKey so the it can be resumed latter. | |
| 186 | 1 | key.attach(threadAttachment); |
| 187 | 1 | } |
| 188 | ||
| 189 | private void saveParser(SelectionKey key, ProtocolParser parser) { | |
| 190 | 0 | WorkerThread workerThread = (WorkerThread) Thread.currentThread(); |
| 191 | // Detach the current Thread data. | |
| 192 | 0 | ThreadAttachment threadAttachment = workerThread.getAttachment(); |
| 193 | 0 | threadAttachment.setAttribute(ProtocolParser.PARSER, parser); |
| 194 | // Attach it to the SelectionKey so the it can be resumed latter. | |
| 195 | 0 | key.attach(threadAttachment); |
| 196 | 0 | } |
| 197 | ||
| 198 | /** | |
| 199 | * Method returns true, if this Filter perform channel read operation on | |
| 200 | * execute, or false - Filter assumes the data was read by previous Filters in | |
| 201 | * a chain. | |
| 202 | * | |
| 203 | * @return true, if Filter will perform channel read operation on execute. | |
| 204 | * False - Filter assumes the data was read by previous Filters in a chain. | |
| 205 | */ | |
| 206 | protected boolean isSkipRead() { | |
| 207 | 0 | return isSkipRead; |
| 208 | } | |
| 209 | ||
| 210 | /** | |
| 211 | * Method set if this Filter should perform channel read operation on | |
| 212 | * execute, or should assumes the data was read by previous Filters in | |
| 213 | * a chain. | |
| 214 | * | |
| 215 | * @param isSkipRead If isSkipRead is true, this Filter will perform | |
| 216 | * channel read operation on execute. If false - Filter assumes the data | |
| 217 | * was read by previous Filters in a chain. | |
| 218 | */ | |
| 219 | protected void setSkipRead(boolean isSkipRead) { | |
| 220 | 0 | this.isSkipRead = isSkipRead; |
| 221 | 0 | } |
| 222 | } |