com.sun.xml.ws.api.server.ThreadLocalContainerResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webservices-rt Show documentation
Show all versions of webservices-rt Show documentation
This module contains the Metro runtime code.
/*
* Copyright (c) 1997, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.ws.api.server;
import java.util.concurrent.Executor;
/**
* ContainerResolver based on {@link ThreadLocal}.
*
* The ThreadLocalContainerResolver is the default implementation available
* from the ContainerResolver using {@link ContainerResolver#getDefault()}. Code
* sections that run with a Container must use the following pattern:
*
* public void m() {
* Container old = ContainerResolver.getDefault().enterContainer(myContainer);
* try {
* // ... method body
* } finally {
* ContainerResolver.getDefault().exitContainer(old);
* }
* }
*
* @since 2.2.7
*/
public class ThreadLocalContainerResolver extends ContainerResolver {
private ThreadLocal containerThreadLocal = new ThreadLocal() {
@Override
protected Container initialValue() {
return Container.NONE;
}
};
public Container getContainer() {
return containerThreadLocal.get();
}
/**
* Enters container
* @param container Container to set
* @return Previous container; must be remembered and passed to exitContainer
*/
public Container enterContainer(Container container) {
Container old = containerThreadLocal.get();
containerThreadLocal.set(container);
return old;
}
/**
* Exits container
* @param old Container returned from enterContainer
*/
public void exitContainer(Container old) {
containerThreadLocal.set(old);
}
/**
* Used by {@link com.sun.xml.ws.api.pipe.Engine} to wrap asynchronous {@link com.sun.xml.ws.api.pipe.Fiber} executions
* @param container Container
* @param ex Executor to wrap
* @return an Executor that will set the container during executions of Runnables
*/
public Executor wrapExecutor(final Container container, final Executor ex) {
if (ex == null)
return null;
return new Executor() {
@Override
public void execute(final Runnable command) {
ex.execute(new Runnable() {
@Override
public void run() {
Container old = enterContainer(container);
try {
command.run();
} finally {
exitContainer(old);
}
}
});
}
};
}
}