src/share/jaxws_classes/com/sun/xml/internal/ws/handler/HandlerTube.java

Fri, 14 Feb 2014 11:13:45 +0100

author
mkos
date
Fri, 14 Feb 2014 11:13:45 +0100
changeset 515
6cd506508147
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.ws.handler;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.Nullable;
ohair@286 29 import com.sun.xml.internal.ws.api.WSBinding;
ohair@286 30 import com.sun.xml.internal.ws.api.message.Packet;
ohair@286 31 import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
ohair@286 32 import com.sun.xml.internal.ws.api.pipe.*;
ohair@286 33 import com.sun.xml.internal.ws.api.pipe.helper.AbstractFilterTubeImpl;
ohair@286 34 import com.sun.xml.internal.ws.binding.BindingImpl;
ohair@286 35 import com.sun.xml.internal.ws.client.HandlerConfiguration;
ohair@286 36
ohair@286 37 import javax.xml.ws.handler.MessageContext;
ohair@286 38 import javax.xml.ws.handler.Handler;
ohair@286 39 import java.util.List;
ohair@286 40
ohair@286 41 /**
ohair@286 42 * @author WS Development team
ohair@286 43 */
ohair@286 44
ohair@286 45 public abstract class HandlerTube extends AbstractFilterTubeImpl {
ohair@286 46 /**
ohair@286 47 * handle hold reference to other Tube for inter-tube communication
ohair@286 48 */
ohair@286 49 HandlerTube cousinTube;
ohair@286 50 protected List<Handler> handlers;
ohair@286 51 HandlerProcessor processor;
ohair@286 52 boolean remedyActionTaken = false;
ohair@286 53 protected final @Nullable WSDLPort port;
ohair@286 54 // flag used to decide whether to call close on cousinTube
ohair@286 55 boolean requestProcessingSucessful = false;
ohair@286 56 private WSBinding binding;
ohair@286 57 private HandlerConfiguration hc;
ohair@286 58
ohair@286 59 public HandlerTube(Tube next, WSDLPort port, WSBinding binding) {
ohair@286 60 super(next);
ohair@286 61 this.port = port;
ohair@286 62 this.binding = binding;
ohair@286 63 }
ohair@286 64
ohair@286 65 public HandlerTube(Tube next, HandlerTube cousinTube, WSBinding binding) {
ohair@286 66 super(next);
ohair@286 67 this.cousinTube = cousinTube;
ohair@286 68 this.binding = binding;
ohair@286 69 if(cousinTube != null) {
ohair@286 70 this.port = cousinTube.port;
ohair@286 71 } else {
ohair@286 72 this.port = null;
ohair@286 73 }
ohair@286 74 }
ohair@286 75
ohair@286 76 /**
ohair@286 77 * Copy constructor for {@link Tube#copy(TubeCloner)}.
ohair@286 78 */
ohair@286 79 protected HandlerTube(HandlerTube that, TubeCloner cloner) {
ohair@286 80 super(that,cloner);
ohair@286 81 if(that.cousinTube != null) {
ohair@286 82 this.cousinTube = cloner.copy(that.cousinTube);
ohair@286 83 }
ohair@286 84 this.port = that.port;
ohair@286 85 this.binding = that.binding;
ohair@286 86 }
ohair@286 87
ohair@286 88 protected WSBinding getBinding() {
ohair@286 89 return binding;
ohair@286 90 }
ohair@286 91
ohair@286 92 @Override
ohair@286 93 public NextAction processRequest(Packet request) {
ohair@286 94 setupExchange();
ohair@286 95 // This check is done to cover handler returning false in Oneway request
ohair@286 96 if (isHandleFalse()) {
ohair@286 97 // Cousin HandlerTube returned false during Oneway Request processing.
ohair@286 98 // Don't call handlers and dispatch the message.
ohair@286 99 remedyActionTaken = true;
ohair@286 100 return doInvoke(super.next, request);
ohair@286 101 }
ohair@286 102
ohair@286 103 // This is done here instead of the constructor, since User can change
ohair@286 104 // the roles and handlerchain after a stub/proxy is created.
ohair@286 105 setUpProcessorInternal();
ohair@286 106
ohair@286 107 MessageUpdatableContext context = getContext(request);
ohair@286 108 boolean isOneWay = checkOneWay(request);
ohair@286 109 try {
ohair@286 110 if (!isHandlerChainEmpty()) {
ohair@286 111 // Call handlers on Request
ohair@286 112 boolean handlerResult = callHandlersOnRequest(context, isOneWay);
ohair@286 113 //Update Packet with user modifications
ohair@286 114 context.updatePacket();
ohair@286 115 // two-way case where no message is sent
ohair@286 116 if (!isOneWay && !handlerResult) {
ohair@286 117 return doReturnWith(request);
ohair@286 118 }
ohair@286 119 }
ohair@286 120 requestProcessingSucessful = true;
ohair@286 121 // Call next Tube
ohair@286 122 return doInvoke(super.next, request);
ohair@286 123 } catch (RuntimeException re) {
ohair@286 124 if(isOneWay) {
ohair@286 125 //Eat the exception, its already logged and close the transportBackChannel
ohair@286 126 if(request.transportBackChannel != null ) {
ohair@286 127 request.transportBackChannel.close();
ohair@286 128 }
ohair@286 129 request.setMessage(null);
ohair@286 130 return doReturnWith(request);
ohair@286 131 } else
ohair@286 132 throw re;
ohair@286 133 } finally {
ohair@286 134 if(!requestProcessingSucessful) {
ohair@286 135 initiateClosing(context.getMessageContext());
ohair@286 136 }
ohair@286 137 }
ohair@286 138
ohair@286 139 }
ohair@286 140
ohair@286 141 @Override
ohair@286 142 public NextAction processResponse(Packet response) {
ohair@286 143 setupExchange();
ohair@286 144 MessageUpdatableContext context = getContext(response);
ohair@286 145 try {
ohair@286 146 if (isHandleFalse() || (response.getMessage() == null)) {
ohair@286 147 // Cousin HandlerTube returned false during Response processing.
ohair@286 148 // or it is oneway request
ohair@286 149 // or handler chain is empty
ohair@286 150 // Don't call handlers.
ohair@286 151 return doReturnWith(response);
ohair@286 152 }
ohair@286 153
ohair@286 154 setUpProcessorInternal();
ohair@286 155
ohair@286 156 boolean isFault = isHandleFault(response);
ohair@286 157 if (!isHandlerChainEmpty()) {
ohair@286 158 // Call handlers on Response
ohair@286 159 callHandlersOnResponse(context, isFault);
ohair@286 160 }
ohair@286 161 } finally {
ohair@286 162 initiateClosing(context.getMessageContext());
ohair@286 163 }
ohair@286 164 //Update Packet with user modifications
ohair@286 165 context.updatePacket();
ohair@286 166
ohair@286 167 return doReturnWith(response);
ohair@286 168
ohair@286 169 }
ohair@286 170
ohair@286 171 @Override
ohair@286 172 public NextAction processException(Throwable t) {
ohair@286 173 try {
ohair@286 174 return doThrow(t);
ohair@286 175 } finally {
ohair@286 176 Packet packet = Fiber.current().getPacket();
ohair@286 177 MessageUpdatableContext context = getContext(packet);
ohair@286 178 initiateClosing(context.getMessageContext());
ohair@286 179 /* TODO revisit: commented this out as the modified packet is no longer used
ohair@286 180 In future if the message is propagated even when an exception
ohair@286 181 occurs, then uncomment context.updatePacket();
ohair@286 182 */
ohair@286 183 //Update Packet with user modifications
ohair@286 184 //context.updatePacket();
ohair@286 185
ohair@286 186
ohair@286 187 }
ohair@286 188 }
ohair@286 189
ohair@286 190 /**
ohair@286 191 * Must be overridden by HandlerTube that drives other handler tubes for processing a message.
ohair@286 192 * On Client-side: ClientLogicalHandlerTube drives the Handler Processing.
ohair@286 193 * On Server-side: In case SOAP Binding, ServerMessageHandlerTube drives the Handler Processing.
ohair@286 194 * In case XML/HTTP Binding, ServerLogicalHandlerTube drives the Handler Processing.
ohair@286 195 *
ohair@286 196 *
ohair@286 197 * If its a top HandlerTube, should override by calling #close(MessaggeContext);
ohair@286 198 *
ohair@286 199 */
ohair@286 200
ohair@286 201 protected void initiateClosing(MessageContext mc) {
ohair@286 202 // Do nothing
ohair@286 203
ohair@286 204 }
ohair@286 205
ohair@286 206 /**
ohair@286 207 * Calls close on previously invoked handlers.
ohair@286 208 * Also, Cleans up any state left over in the Tube instance from the current
ohair@286 209 * invocation, as Tube instances can be reused after the completion of MEP.
ohair@286 210 *
ohair@286 211 * On Client, SOAPHandlers are closed first and then LogicalHandlers
ohair@286 212 * On Server, LogicalHandlers are closed first and then SOAPHandlers
ohair@286 213 */
ohair@286 214 final public void close(MessageContext msgContext) {
ohair@286 215 //assuming cousinTube is called if requestProcessingSucessful is true
ohair@286 216 if (requestProcessingSucessful) {
ohair@286 217 if (cousinTube != null) {
ohair@286 218 cousinTube.close(msgContext);
ohair@286 219 }
ohair@286 220
ohair@286 221 }
ohair@286 222 if (processor != null)
ohair@286 223 closeHandlers(msgContext);
ohair@286 224
ohair@286 225 // Clean up the exchange for next invocation.
ohair@286 226 exchange = null;
ohair@286 227 requestProcessingSucessful = false;
ohair@286 228
ohair@286 229 }
ohair@286 230
ohair@286 231 /**
ohair@286 232 * On Client, Override by calling #closeClientHandlers(MessageContext mc)
ohair@286 233 * On Server, Override by calling #closeServerHandlers(MessageContext mc)
ohair@286 234 * The difference is the order in which they are closed.
ohair@286 235 * @param mc
ohair@286 236 */
ohair@286 237 abstract void closeHandlers(MessageContext mc);
ohair@286 238
ohair@286 239 /**
ohair@286 240 * Called by close(MessageContext mc) in a Client Handlertube
ohair@286 241 */
ohair@286 242 protected void closeClientsideHandlers(MessageContext msgContext) {
ohair@286 243 if (processor == null)
ohair@286 244 return;
ohair@286 245 if (remedyActionTaken) {
ohair@286 246 //Close only invoked handlers in the chain
ohair@286 247
ohair@286 248 //CLIENT-SIDE
ohair@286 249 processor.closeHandlers(msgContext, processor.getIndex(), 0);
ohair@286 250 processor.setIndex(-1);
ohair@286 251 //reset remedyActionTaken
ohair@286 252 remedyActionTaken = false;
ohair@286 253 } else {
ohair@286 254 //Close all handlers in the chain
ohair@286 255
ohair@286 256 //CLIENT-SIDE
ohair@286 257 processor.closeHandlers(msgContext, handlers.size() - 1, 0);
ohair@286 258
ohair@286 259 }
ohair@286 260 }
ohair@286 261
ohair@286 262 /**
ohair@286 263 * Called by close(MessageContext mc) in a Server Handlertube
ohair@286 264 */
ohair@286 265 protected void closeServersideHandlers(MessageContext msgContext) {
ohair@286 266 if (processor == null)
ohair@286 267 return;
ohair@286 268 if (remedyActionTaken) {
ohair@286 269 //Close only invoked handlers in the chain
ohair@286 270
ohair@286 271 //SERVER-SIDE
ohair@286 272 processor.closeHandlers(msgContext, processor.getIndex(), handlers.size() - 1);
ohair@286 273 processor.setIndex(-1);
ohair@286 274 //reset remedyActionTaken
ohair@286 275 remedyActionTaken = false;
ohair@286 276 } else {
ohair@286 277 //Close all handlers in the chain
ohair@286 278
ohair@286 279 //SERVER-SIDE
ohair@286 280 processor.closeHandlers(msgContext, 0, handlers.size() - 1);
ohair@286 281
ohair@286 282 }
ohair@286 283 }
ohair@286 284
ohair@286 285 abstract void callHandlersOnResponse(MessageUpdatableContext context, boolean handleFault);
ohair@286 286
ohair@286 287 abstract boolean callHandlersOnRequest(MessageUpdatableContext context, boolean oneWay);
ohair@286 288
ohair@286 289 private boolean checkOneWay(Packet packet) {
ohair@286 290 if (port != null) {
ohair@286 291 /* we can determine this value from WSDL */
ohair@286 292 return packet.getMessage().isOneWay(port);
ohair@286 293 } else {
ohair@286 294 /*
ohair@286 295 otherwise use this value as an approximation, since this carries
alanb@368 296 the application's intention --- whether it was invokeOneway vs invoke,etc.
ohair@286 297 */
ohair@286 298 return !(packet.expectReply != null && packet.expectReply);
ohair@286 299 }
ohair@286 300 }
ohair@286 301
ohair@286 302 private void setUpProcessorInternal() {
ohair@286 303 HandlerConfiguration hc = ((BindingImpl) binding).getHandlerConfig();
ohair@286 304 if (hc != this.hc)
ohair@286 305 resetProcessor();
ohair@286 306 this.hc = hc;
ohair@286 307
ohair@286 308 setUpProcessor();
ohair@286 309 }
ohair@286 310
ohair@286 311 abstract void setUpProcessor();
ohair@286 312
ohair@286 313 protected void resetProcessor() {
ohair@286 314 handlers = null;
ohair@286 315 }
ohair@286 316
ohair@286 317 final public boolean isHandlerChainEmpty() {
ohair@286 318 return handlers.isEmpty();
ohair@286 319 }
ohair@286 320 abstract MessageUpdatableContext getContext(Packet p);
ohair@286 321
ohair@286 322 private boolean isHandleFault(Packet packet) {
ohair@286 323 if (cousinTube != null) {
ohair@286 324 return exchange.isHandleFault();
ohair@286 325 } else {
ohair@286 326 boolean isFault = packet.getMessage().isFault();
ohair@286 327 exchange.setHandleFault(isFault);
ohair@286 328 return isFault;
ohair@286 329 }
ohair@286 330 }
ohair@286 331
ohair@286 332 final void setHandleFault() {
ohair@286 333 exchange.setHandleFault(true);
ohair@286 334 }
ohair@286 335
ohair@286 336 private boolean isHandleFalse() {
ohair@286 337 return exchange.isHandleFalse();
ohair@286 338 }
ohair@286 339
ohair@286 340 final void setHandleFalse() {
ohair@286 341 exchange.setHandleFalse();
ohair@286 342 }
ohair@286 343
ohair@286 344 private void setupExchange() {
ohair@286 345 if(exchange == null) {
ohair@286 346 exchange = new HandlerTubeExchange();
ohair@286 347 if(cousinTube != null) {
ohair@286 348 cousinTube.exchange = exchange;
ohair@286 349 }
ohair@286 350 } else {
ohair@286 351 if(cousinTube != null) {
ohair@286 352 cousinTube.exchange = exchange;
ohair@286 353 }
ohair@286 354
ohair@286 355 }
ohair@286 356 }
ohair@286 357 private HandlerTubeExchange exchange;
ohair@286 358
ohair@286 359 /**
ohair@286 360 * This class is used primarily to exchange information or status between
ohair@286 361 * LogicalHandlerTube and SOAPHandlerTube
ohair@286 362 */
ohair@286 363 static final class HandlerTubeExchange {
ohair@286 364 private boolean handleFalse;
ohair@286 365 private boolean handleFault;
ohair@286 366
ohair@286 367 boolean isHandleFault() {
ohair@286 368 return handleFault;
ohair@286 369 }
ohair@286 370
ohair@286 371 void setHandleFault(boolean isFault) {
ohair@286 372 this.handleFault = isFault;
ohair@286 373 }
ohair@286 374
ohair@286 375 public boolean isHandleFalse() {
ohair@286 376 return handleFalse;
ohair@286 377 }
ohair@286 378
ohair@286 379 void setHandleFalse() {
ohair@286 380 this.handleFalse = true;
ohair@286 381 }
ohair@286 382 }
ohair@286 383
ohair@286 384 }

mercurial