src/share/jaxws_classes/com/sun/xml/internal/ws/api/pipe/Tube.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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.api.pipe;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.NotNull;
ohair@286 29 import com.sun.xml.internal.ws.api.message.Message;
ohair@286 30 import com.sun.xml.internal.ws.api.message.Packet;
ohair@286 31 import com.sun.xml.internal.ws.api.pipe.helper.AbstractFilterTubeImpl;
ohair@286 32 import com.sun.xml.internal.ws.api.pipe.helper.AbstractTubeImpl;
ohair@286 33 import com.sun.xml.internal.ws.api.server.Adapter;
ohair@286 34
ohair@286 35 import javax.annotation.PreDestroy;
ohair@286 36 import javax.xml.ws.Dispatch;
ohair@286 37 import javax.xml.ws.Provider;
ohair@286 38 import javax.xml.ws.WebServiceException;
ohair@286 39 import javax.xml.ws.handler.LogicalHandler;
ohair@286 40 import javax.xml.ws.handler.soap.SOAPHandler;
ohair@286 41 import java.text.SimpleDateFormat;
ohair@286 42
ohair@286 43 /**
ohair@286 44 * Abstraction of the intermediate layers in the processing chain
ohair@286 45 * and transport.
ohair@286 46 *
ohair@286 47 * <h2>What is a {@link Tube}?</h2>
ohair@286 48 * <p>
ohair@286 49 * {@link Tube} is a basic processing unit that represents SOAP-level
ohair@286 50 * protocol handling code. Mutliple tubes are often put together in
ohair@286 51 * a line (it needs not one dimensional &mdash; more later), and act on
ohair@286 52 * {@link Packet}s in a sequential fashion.
ohair@286 53 *
ohair@286 54 * <p>
ohair@286 55 * {@link Tube}s run asynchronously. That is, there is no guarantee that
ohair@286 56 * {@link #processRequest(Packet)} and {@link #processResponse(Packet)} runs
ohair@286 57 * in the same thread, nor is there any guarantee that this tube and next
ohair@286 58 * tube runs in the same thread. Furthermore, one thread may be used to
ohair@286 59 * run multiple pipeline in turn (just like a real CPU runs multiple
ohair@286 60 * threads in turn.)
ohair@286 61 *
ohair@286 62 *
ohair@286 63 * <h2>Tube examples</h2>
ohair@286 64 * <p>
ohair@286 65 * Transport is a kind of tube. It sends the {@link Packet}
ohair@286 66 * through, say, HTTP connection, and receives the data back into another {@link Packet}.
ohair@286 67 *
ohair@286 68 * <p>
ohair@286 69 * More often, a tube works like a filter. It acts on a packet,
ohair@286 70 * and then it tells the JAX-WS that the packet should be passed into another
ohair@286 71 * tube. It can do the same on the way back.
ohair@286 72 *
ohair@286 73 * <p>
ohair@286 74 * For example, XWSS will be a {@link Tube}. It will act on a request
ohair@286 75 * {@link Packet}, then perhaps wrap it into
ohair@286 76 * another {@link Packet} to encrypt the body and add a header, then
ohair@286 77 * the processing will go on to the next tube.
ohair@286 78 *
ohair@286 79 * <p>
ohair@286 80 * Yet another kind of filter tube is those that wraps {@link LogicalHandler}
ohair@286 81 * and {@link SOAPHandler}. These tubes are heavy-weight; they often consume
ohair@286 82 * a message in a packet and create a new one, and then pass it to the next tube.
ohair@286 83 *
ohair@286 84 * <p>
ohair@286 85 * There would be a {@link Tube} implementation that invokes {@link Provider}.
ohair@286 86 * There would be a {@link Tube} implementation that invokes a service method
ohair@286 87 * on the user's code.
ohair@286 88 * There would be a {@link Dispatch} implementation that invokes a {@link Tube}.
ohair@286 89 *
ohair@286 90 * <p>
ohair@286 91 * WS-MEX can be implemented as a {@link Tube} that looks for
ohair@286 92 * {@link Message#getPayloadNamespaceURI()} and serves the request.
ohair@286 93 *
ohair@286 94 *
ohair@286 95 *
ohair@286 96 *
ohair@286 97 * <h2>Tube Lifecycle</h2>
ohair@286 98 * Pipeline is expensive to set up, so once it's created it will be reused.
ohair@286 99 * A pipeline is not reentrant; one pipeline is used to process one request/response
ohair@286 100 * at at time. The same pipeline instance may serve multiple request/response,
ohair@286 101 * if one comes after another and they don't overlap.
ohair@286 102 * <p>
ohair@286 103 * Where a need arises to process multiple requests concurrently, a pipeline
ohair@286 104 * gets cloned through {@link TubeCloner}. Note that this need may happen on
ohair@286 105 * both server (because it quite often serves multiple requests concurrently)
ohair@286 106 * and client (because it needs to support asynchronous method invocations.)
ohair@286 107 * <p>
ohair@286 108 * Created pipelines (including cloned ones and the original) may be discarded and GC-ed
ohair@286 109 * at any time at the discretion of whoever owns pipelines. Tubes can, however, expect
ohair@286 110 * at least one copy (or original) of pipeline to live at any given time while a pipeline
ohair@286 111 * owner is interested in the given pipeline configuration (in more concerete terms,
ohair@286 112 * for example, as long as a dispatch object lives, it's going to keep at least one
ohair@286 113 * copy of a pipeline alive.)
ohair@286 114 * <p>
ohair@286 115 * Before a pipeline owner dies, it may invoke {@link #preDestroy()} on the last
ohair@286 116 * remaining pipeline. It is "may" for pipeline owners that live in the client-side
ohair@286 117 * of JAX-WS (such as dispatches and proxies), but it is a "must" for pipeline owners
ohair@286 118 * that live in the server-side of JAX-WS.
ohair@286 119 * <p>
ohair@286 120 * This last invocation gives a chance for some pipes to clean up any state/resource
ohair@286 121 * acquired (such as WS-RM's sequence, WS-Trust's SecurityToken), although as stated above,
ohair@286 122 * this is not required for clients.
ohair@286 123 *
ohair@286 124 *
ohair@286 125 *
ohair@286 126 * <h2>Tube and state</h2>
ohair@286 127 * <p>
ohair@286 128 * The lifecycle of pipelines is designed to allow a {@link Tube} to store various
ohair@286 129 * state in easily accessible fashion.
ohair@286 130 *
ohair@286 131 *
ohair@286 132 * <h3>Per-packet state</h3>
ohair@286 133 * <p>
ohair@286 134 * Any information that changes from a packet to packet should be
ohair@286 135 * stored in {@link Packet} (if such informaton is specific to your problem domain,
ohair@286 136 * then most likely {@link Packet#invocationProperties}.)
ohair@286 137 * This includes information like transport-specific headers.
ohair@286 138 *
ohair@286 139 * <h3>Per-thread state</h3>
ohair@286 140 * <p>
ohair@286 141 * Any expensive-to-create objects that are non-reentrant can be stored
ohair@286 142 * either in instance variables of a {@link Tube}, or a static {@link ThreadLocal}.
ohair@286 143 *
ohair@286 144 * <p>
ohair@286 145 * The first approach works, because {@link Tube} is
ohair@286 146 * non reentrant. When a tube is copied, new instances should be allocated
ohair@286 147 * so that two {@link Tube} instances don't share thread-unsafe resources.
ohair@286 148 *
ohair@286 149 * Similarly the second approach works, since {@link ThreadLocal} guarantees
ohair@286 150 * that each thread gets its own private copy.
ohair@286 151 *
ohair@286 152 * <p>
ohair@286 153 * The former is faster to access, and you need not worry about clean up.
ohair@286 154 * On the other hand, because there can be many more concurrent requests
ohair@286 155 * than # of threads, you may end up holding onto more resources than necessary.
ohair@286 156 *
ohair@286 157 * <p>
ohair@286 158 * This includes state like canonicalizers, JAXB unmarshallers,
ohair@286 159 * {@link SimpleDateFormat}, etc.
ohair@286 160 *
ohair@286 161 *
ohair@286 162 * <h3>Per-proxy/per-endpoint state</h3>
ohair@286 163 * <p>
ohair@286 164 * Information that is tied to a particular proxy/dispatch can be stored
ohair@286 165 * in a separate object that is referenced from a tube. When
ohair@286 166 * a new tube is copied, you can simply hand out a reference to the newly
ohair@286 167 * created one, so that all copied tubes refer to the same instance.
ohair@286 168 * See the following code as an example:
ohair@286 169 *
ohair@286 170 * <pre>
ohair@286 171 * class TubeImpl {
ohair@286 172 * // this object stores per-proxy state
ohair@286 173 * class DataStore {
ohair@286 174 * int counter;
ohair@286 175 * }
ohair@286 176 *
ohair@286 177 * private DataStore ds;
ohair@286 178 *
ohair@286 179 * // create a fresh new pipe
ohair@286 180 * public TubeImpl(...) {
ohair@286 181 * ....
ohair@286 182 * ds = new DataStore();
ohair@286 183 * }
ohair@286 184 *
ohair@286 185 * // copy constructor
ohair@286 186 * private TubeImpl(TubeImpl that, PipeCloner cloner) {
ohair@286 187 * cloner.add(that,this);
ohair@286 188 * ...
ohair@286 189 * this.ds = that.ds;
ohair@286 190 * }
ohair@286 191 *
ohair@286 192 * public TubeImpl copy(PipeCloner pc) {
ohair@286 193 * return new TubeImpl(this,pc);
ohair@286 194 * }
ohair@286 195 * }
ohair@286 196 * </pre>
ohair@286 197 *
ohair@286 198 * <p>
ohair@286 199 * Note that access to such resource may need to be synchronized,
ohair@286 200 * since multiple copies of pipelines may execute concurrently.
ohair@286 201 *
ohair@286 202 *
ohair@286 203 *
ohair@286 204 * <h3>VM-wide state</h3>
ohair@286 205 * <p>
ohair@286 206 * <tt>static</tt> is always there for you to use.
ohair@286 207 *
ohair@286 208 *
ohair@286 209 *
ohair@286 210 * @see AbstractTubeImpl
ohair@286 211 * @see AbstractFilterTubeImpl
ohair@286 212 *
ohair@286 213 * @author Kohsuke Kawaguchi
ohair@286 214 * @author Jitendra Kotamraju
ohair@286 215 */
ohair@286 216 public interface Tube {
ohair@286 217 /**
ohair@286 218 * Acts on a request and perform some protocol specific operation.
ohair@286 219 *
ohair@286 220 * TODO: exception handling semantics need more discussion
ohair@286 221 *
ohair@286 222 * @throws WebServiceException
ohair@286 223 * On the server side, this signals an error condition where
ohair@286 224 * a fault reply is in order (or the exception gets eaten by
ohair@286 225 * the top-most transport {@link Adapter} if it's one-way.)
ohair@286 226 * This frees each {@link Tube} from try/catching a
ohair@286 227 * {@link WebServiceException} in every layer.
ohair@286 228 *
ohair@286 229 * Note that this method is also allowed to return
ohair@286 230 * {@link NextAction#returnWith(Packet)} with
ohair@286 231 * a {@link Packet} that has a fault as the payload.
ohair@286 232 *
ohair@286 233 * <p>
ohair@286 234 * On the client side, the {@link WebServiceException} thrown
ohair@286 235 * will be propagated all the way back to the calling client
ohair@286 236 * applications. (The consequence of that is that if you are
ohair@286 237 * a filtering {@link Tube}, you must not eat the exception
ohair@286 238 * that was given to {@link #processException(Throwable)} .
ohair@286 239 *
ohair@286 240 * @throws RuntimeException
ohair@286 241 * Other runtime exception thrown by this method must
ohair@286 242 * be treated as a bug in the tube implementation,
ohair@286 243 * and therefore should not be converted into a fault.
ohair@286 244 * (Otherwise it becomes very difficult to debug implementation
ohair@286 245 * problems.)
ohair@286 246 *
ohair@286 247 * <p>
ohair@286 248 * On the server side, this exception should be most likely
ohair@286 249 * just logged. On the client-side it gets propagated to the
ohair@286 250 * client application.
ohair@286 251 *
ohair@286 252 * <p>
ohair@286 253 * The consequence of this is that if a pipe calls
ohair@286 254 * into an user application (such as {@link SOAPHandler}
ohair@286 255 * or {@link LogicalHandler}), where a {@link RuntimeException}
ohair@286 256 * is *not* a bug in the JAX-WS implementation, it must be catched
ohair@286 257 * and wrapped into a {@link WebServiceException}.
ohair@286 258 *
ohair@286 259 * @param request
ohair@286 260 * The packet that represents a request message.
ohair@286 261 * If the packet has a non-null message, it must be a valid
ohair@286 262 * unconsumed {@link Message}. This message represents the
ohair@286 263 * SOAP message to be sent as a request.
ohair@286 264 * <p>
ohair@286 265 * The packet is also allowed to carry no message, which indicates
ohair@286 266 * that this is an output-only request.
ohair@286 267 * (that's called "solicit", right? - KK)
ohair@286 268 *
ohair@286 269 * @return
ohair@286 270 * A {@link NextAction} object that represents the next action
ohair@286 271 * to be taken by the JAX-WS runtime.
ohair@286 272 */
ohair@286 273 @NotNull NextAction processRequest(@NotNull Packet request);
ohair@286 274
ohair@286 275 /**
ohair@286 276 * Acts on a response and performs some protocol specific operation.
ohair@286 277 *
ohair@286 278 * <p>
ohair@286 279 * Once a {@link #processRequest(Packet)} is invoked, this method
ohair@286 280 * will be always invoked with the response, before this {@link Tube}
ohair@286 281 * processes another request.
ohair@286 282 *
ohair@286 283 * @param response
ohair@286 284 * If the packet has a non-null message, it must be
ohair@286 285 * a valid unconsumed {@link Message}. This message represents
ohair@286 286 * a response to the request message passed to
ohair@286 287 * {@link #processRequest(Packet)} earlier.
ohair@286 288 * <p>
ohair@286 289 * The packet is also allowed to carry no message, which indicates
ohair@286 290 * that there was no response. This is used for things like
ohair@286 291 * one-way message and/or one-way transports.
ohair@286 292 *
ohair@286 293 * TODO: exception handling semantics need more discussion
ohair@286 294 *
ohair@286 295 * @return
ohair@286 296 * A {@link NextAction} object that represents the next action
ohair@286 297 * to be taken by the JAX-WS runtime.
ohair@286 298 */
ohair@286 299 @NotNull NextAction processResponse(@NotNull Packet response);
ohair@286 300
ohair@286 301
ohair@286 302 /**
ohair@286 303 * Acts on a exception and performs some clean up operations.
ohair@286 304 *
ohair@286 305 * <p>
ohair@286 306 * If a {@link #processRequest(Packet)}, {@link #processResponse(Packet)},
ohair@286 307 * {@link #processException(Throwable)} throws an exception, this method
ohair@286 308 * will be always invoked on all the {@link Tube}s in the remaining
ohair@286 309 * {@link NextAction}s.
ohair@286 310 *
ohair@286 311 * <p>
ohair@286 312 * On the server side, the {@link Throwable} thrown will be propagated to the
ohair@286 313 * top-most transport. The transport converts the exception to fault reply or
ohair@286 314 * simply logs in case of one-way MEP. If you are a filtering {@link Tube} like
ohair@286 315 * {@link AbstractTubeImpl}, you don't have to override the implementation). On
ohair@286 316 * the other hand, any intermediate {@link Tube} may want to convert the exception
ohair@286 317 * to a fault message.
ohair@286 318 *
ohair@286 319 * <p>
ohair@286 320 * On the client side, the {@link Throwable} thrown
ohair@286 321 * will be propagated all the way back to the calling client
ohair@286 322 * applications. (The consequence of that is that if you are
ohair@286 323 * a filtering {@link Tube} like {@link AbstractTubeImpl}, you don't have to
ohair@286 324 * override the implementation)
ohair@286 325 *
ohair@286 326 * @param t
ohair@286 327 *
ohair@286 328 * @return
ohair@286 329 * A {@link NextAction} object that represents the next action
ohair@286 330 * to be taken by the JAX-WS runtime.
ohair@286 331 */
ohair@286 332 @NotNull NextAction processException(@NotNull Throwable t);
ohair@286 333
ohair@286 334 /**
ohair@286 335 * Invoked before the last copy of the pipeline is about to be discarded,
ohair@286 336 * to give {@link Tube}s a chance to clean up any resources.
ohair@286 337 *
ohair@286 338 * <p>
ohair@286 339 * This can be used to invoke {@link PreDestroy} lifecycle methods
ohair@286 340 * on user handler. The invocation of it is optional on the client side,
ohair@286 341 * but mandatory on the server side.
ohair@286 342 *
ohair@286 343 * <p>
ohair@286 344 * When multiple copies of pipelines are created, this method is called
ohair@286 345 * only on one of them.
ohair@286 346 *
ohair@286 347 * @throws WebServiceException
ohair@286 348 * If the clean up fails, {@link WebServiceException} can be thrown.
ohair@286 349 * This exception will be propagated to users (if this is client),
ohair@286 350 * or recorded (if this is server.)
ohair@286 351 */
ohair@286 352 void preDestroy();
ohair@286 353
ohair@286 354 /**
ohair@286 355 * Creates an identical clone of this {@link Tube}.
ohair@286 356 *
ohair@286 357 * <p>
ohair@286 358 * This method creates an identical pipeline that can be used
ohair@286 359 * concurrently with this pipeline. When the caller of a pipeline
ohair@286 360 * is multi-threaded and need concurrent use of the same pipeline,
ohair@286 361 * it can do so by creating copies through this method.
ohair@286 362 *
ohair@286 363 * <h3>Implementation Note</h3>
ohair@286 364 * <p>
ohair@286 365 * It is the implementation's responsibility to call
ohair@286 366 * {@link TubeCloner#add(Tube,Tube)} to register the copied pipe
ohair@286 367 * with the original. This is required before you start copying
ohair@286 368 * the other {@link Tube} references you have, or else there's a
ohair@286 369 * risk of infinite recursion.
ohair@286 370 * <p>
ohair@286 371 * For most {@link Tube} implementations that delegate to another
ohair@286 372 * {@link Tube}, this method requires that you also copy the {@link Tube}
ohair@286 373 * that you delegate to.
ohair@286 374 * <p>
ohair@286 375 * For limited number of {@link Tube}s that do not maintain any
ohair@286 376 * thread unsafe resource, it is allowed to simply return <tt>this</tt>
ohair@286 377 * from this method (notice that even if you are stateless, if you
ohair@286 378 * got a delegating {@link Tube} and that one isn't stateless, you
ohair@286 379 * still have to copy yourself.)
ohair@286 380 *
ohair@286 381 * <p>
ohair@286 382 * Note that this method might be invoked by one thread while another
ohair@286 383 * thread is executing the other process method. See
ohair@286 384 * the {@link Codec#copy()} for more discussion about this.
ohair@286 385 *
ohair@286 386 * @param cloner
ohair@286 387 * Use this object (in particular its {@link TubeCloner#copy(Tube)} method
ohair@286 388 * to clone other pipe references you have
ohair@286 389 * in your pipe. See {@link TubeCloner} for more discussion
ohair@286 390 * about why.
ohair@286 391 *
ohair@286 392 * @return
ohair@286 393 * always non-null {@link Tube}.
ohair@286 394 */
ohair@286 395 Tube copy(TubeCloner cloner);
ohair@286 396 }

mercurial