src/share/jaxws_classes/com/sun/xml/internal/ws/api/server/ThreadLocalContainerResolver.java

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

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
child 397
b99d7e355d4b
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

alanb@368 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
alanb@368 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
alanb@368 4 *
alanb@368 5 * This code is free software; you can redistribute it and/or modify it
alanb@368 6 * under the terms of the GNU General Public License version 2 only, as
alanb@368 7 * published by the Free Software Foundation. Oracle designates this
alanb@368 8 * particular file as subject to the "Classpath" exception as provided
alanb@368 9 * by Oracle in the LICENSE file that accompanied this code.
alanb@368 10 *
alanb@368 11 * This code is distributed in the hope that it will be useful, but WITHOUT
alanb@368 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
alanb@368 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
alanb@368 14 * version 2 for more details (a copy is included in the LICENSE file that
alanb@368 15 * accompanied this code).
alanb@368 16 *
alanb@368 17 * You should have received a copy of the GNU General Public License version
alanb@368 18 * 2 along with this work; if not, write to the Free Software Foundation,
alanb@368 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
alanb@368 20 *
alanb@368 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
alanb@368 22 * or visit www.oracle.com if you need additional information or have any
alanb@368 23 * questions.
alanb@368 24 */
alanb@368 25
alanb@368 26 package com.sun.xml.internal.ws.api.server;
alanb@368 27
alanb@368 28 import java.util.concurrent.Executor;
alanb@368 29
alanb@368 30 /**
alanb@368 31 * ContainerResolver based on {@link ThreadLocal}.
alanb@368 32 * <p>
alanb@368 33 * The ThreadLocalContainerResolver is the default implementation available
alanb@368 34 * from the ContainerResolver using {@link ContainerResolver#getDefault()}. Code
alanb@368 35 * sections that run with a Container must use the following pattern:
alanb@368 36 * <pre>
alanb@368 37 * public void m() {
alanb@368 38 * Container old = ContainerResolver.getDefault().enterContainer(myContainer);
alanb@368 39 * try {
alanb@368 40 * // ... method body
alanb@368 41 * } finally {
alanb@368 42 * ContainerResolver.getDefault().exitContainer(old);
alanb@368 43 * }
alanb@368 44 * }
alanb@368 45 * </pre>
alanb@368 46 * @since 2.2.7
alanb@368 47 */
alanb@368 48 public class ThreadLocalContainerResolver extends ContainerResolver {
alanb@368 49 private ThreadLocal<Container> containers = new ThreadLocal<Container>() {
alanb@368 50 @Override
alanb@368 51 protected Container initialValue() {
alanb@368 52 return Container.NONE;
alanb@368 53 }
alanb@368 54 };
alanb@368 55
alanb@368 56 public Container getContainer() {
alanb@368 57 return containers.get();
alanb@368 58 }
alanb@368 59
alanb@368 60 /**
alanb@368 61 * Enters container
alanb@368 62 * @param container Container to set
alanb@368 63 * @return Previous container; must be remembered and passed to exitContainer
alanb@368 64 */
alanb@368 65 public Container enterContainer(Container container) {
alanb@368 66 Container old = containers.get();
alanb@368 67 containers.set(container);
alanb@368 68 return old;
alanb@368 69 }
alanb@368 70
alanb@368 71 /**
alanb@368 72 * Exits container
alanb@368 73 * @param old Container returned from enterContainer
alanb@368 74 */
alanb@368 75 public void exitContainer(Container old) {
alanb@368 76 containers.set(old);
alanb@368 77 }
alanb@368 78
alanb@368 79 /**
alanb@368 80 * Used by {@link com.sun.xml.internal.ws.api.pipe.Engine} to wrap asynchronous {@link com.sun.xml.internal.ws.api.pipe.Fiber} executions
alanb@368 81 * @param container Container
alanb@368 82 * @param ex Executor to wrap
alanb@368 83 * @return an Executor that will set the container during executions of Runnables
alanb@368 84 */
alanb@368 85 public Executor wrapExecutor(final Container container, final Executor ex) {
alanb@368 86 if (ex == null)
alanb@368 87 return null;
alanb@368 88
alanb@368 89 return new Executor() {
alanb@368 90 @Override
alanb@368 91 public void execute(final Runnable command) {
alanb@368 92 ex.execute(new Runnable() {
alanb@368 93 @Override
alanb@368 94 public void run() {
alanb@368 95 Container old = enterContainer(container);
alanb@368 96 try {
alanb@368 97 command.run();
alanb@368 98 } finally {
alanb@368 99 exitContainer(old);
alanb@368 100 }
alanb@368 101 }
alanb@368 102 });
alanb@368 103 }
alanb@368 104 };
alanb@368 105 }
alanb@368 106 }

mercurial