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

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

mercurial