Author: dkulp
Date: Tue Mar 18 08:19:41 2008
New Revision: 638411
URL: http://svn.apache.org/viewvc?rev=638411&view=rev
Log:
Merged revisions 637990 via svnmerge from
https://svn.apache.org/repos/asf/incubator/cxf/trunk
........
r637990 | dkulp | 2008-03-17 13:44:57 -0400 (Mon, 17 Mar 2008) | 2 lines
[CXF-1410] Allow proxies to be configured such that the request context is
thread local like the response context
........
Added:
incubator/cxf/branches/2.0.x-fixes/systests/src/test/java/org/apache/cxf/systest/jaxws/JaxWsClientThreadTest.java
- copied unchanged from r637990,
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxws/JaxWsClientThreadTest.java
Modified:
incubator/cxf/branches/2.0.x-fixes/ (props changed)
incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/JaxWsClientProxy.java
incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/JaxWsProxyFactoryBean.java
incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java
Propchange: incubator/cxf/branches/2.0.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.
Modified:
incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/JaxWsClientProxy.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/JaxWsClientProxy.java?rev=638411&r1=638410&r2=638411&view=diff
==============================================================================
---
incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/JaxWsClientProxy.java
(original)
+++
incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/JaxWsClientProxy.java
Tue Mar 18 08:19:41 2008
@@ -24,6 +24,7 @@
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.FutureTask;
import java.util.logging.Logger;
@@ -58,10 +59,44 @@
public class JaxWsClientProxy extends org.apache.cxf.frontend.ClientProxy
implements
InvocationHandler, BindingProvider {
+
+ /*
+ * modification are echoed back to the shared map
+ */
+ public static class EchoContext extends HashMap<String, Object> {
+ final Map<String, Object> shared;
+ public EchoContext(Map<String, Object> sharedMap) {
+ super(sharedMap);
+ shared = sharedMap;
+ }
+
+ public Object put(String key, Object value) {
+ shared.put(key, value);
+ return super.put(key, value);
+ }
+
+ public void putAll(Map<? extends String, ? extends Object> t) {
+ shared.putAll(t);
+ super.putAll(t);
+ }
+
+ public Object remove(Object key) {
+ shared.remove(key);
+ return super.remove(key);
+ }
+
+ public void reload() {
+ super.clear();
+ super.putAll(shared);
+ }
+ }
+ public static final String THREAD_LOCAL_REQUEST_CONTEXT =
"thread.local.request.context";
private static final Logger LOG =
LogUtils.getL7dLogger(JaxWsClientProxy.class);
- protected Map<String, Object> requestContext = new
ConcurrentHashMap<String, Object>();
+ protected Map<String, Object> currentRequestContext = new
ConcurrentHashMap<String, Object>();
+ protected ThreadLocal <EchoContext> requestContext =
+ new ThreadLocal<EchoContext>();
protected ThreadLocal <Map<String, Object>> responseContext =
new ThreadLocal<Map<String, Object>>();
@@ -77,8 +112,8 @@
private void setupEndpointAddressContext(Endpoint endpoint) {
// NOTE for jms transport the address would be null
if (null != endpoint && null !=
endpoint.getEndpointInfo().getAddress()) {
- getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
- endpoint.getEndpointInfo().getAddress());
+
currentRequestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
+ endpoint.getEndpointInfo().getAddress());
}
}
@@ -225,19 +260,47 @@
}
}
+ public boolean isThreadLocalRequestContext() {
+ if (currentRequestContext.containsKey(THREAD_LOCAL_REQUEST_CONTEXT)) {
+ Object o = currentRequestContext.get(THREAD_LOCAL_REQUEST_CONTEXT);
+ boolean local = false;
+ if (o instanceof Boolean) {
+ local = ((Boolean)o).booleanValue();
+ } else {
+ local = Boolean.parseBoolean(o.toString());
+ }
+ return local;
+ }
+ return false;
+ }
+ public void setThreadLocalRequestContext(boolean b) {
+ currentRequestContext.put(THREAD_LOCAL_REQUEST_CONTEXT, b);
+ }
+
private Map<String, Object> getRequestContextCopy() {
Map<String, Object> realMap = new HashMap<String, Object>();
WrappedMessageContext ctx = new WrappedMessageContext(realMap,
Scope.APPLICATION);
- for (Map.Entry<String, Object> ent : requestContext.entrySet()) {
- ctx.put(ent.getKey(), ent.getValue());
+ // thread local contexts reflect currentRequestContext as of
+ // last call to getRequestContext()
+ if (isThreadLocalRequestContext()
+ && null != requestContext.get()) {
+ ctx.putAll(requestContext.get());
+ } else {
+ ctx.putAll(currentRequestContext);
}
return realMap;
}
-
+
public Map<String, Object> getRequestContext() {
- return requestContext;
+ if (isThreadLocalRequestContext()) {
+ if (null == requestContext.get()) {
+ requestContext.set(new EchoContext(currentRequestContext));
+ }
+ return requestContext.get();
+ }
+ return currentRequestContext;
}
public Map<String, Object> getResponseContext() {
Modified:
incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/JaxWsProxyFactoryBean.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/JaxWsProxyFactoryBean.java?rev=638411&r1=638410&r2=638411&view=diff
==============================================================================
---
incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/JaxWsProxyFactoryBean.java
(original)
+++
incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/JaxWsProxyFactoryBean.java
Tue Mar 18 08:19:41 2008
@@ -57,6 +57,7 @@
protected ClientProxy clientClientProxy(Client c) {
JaxWsClientProxy cp = new JaxWsClientProxy(c,
((JaxWsEndpointImpl)c.getEndpoint()).getJaxwsBinding());
+ cp.getRequestContext().putAll(this.getProperties());
buildHandlerChain(cp);
return cp;
}
Modified:
incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java?rev=638411&r1=638410&r2=638411&view=diff
==============================================================================
---
incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java
(original)
+++
incubator/cxf/branches/2.0.x-fixes/rt/frontend/jaxws/src/test/java/org/apache/cxf/jaxws/JaxWsClientTest.java
Tue Mar 18 08:19:41 2008
@@ -106,6 +106,43 @@
}
@Test
+ public void testRequestContextPutAndRemoveEcho() throws Exception {
+ URL url = getClass().getResource("/wsdl/hello_world.wsdl");
+ javax.xml.ws.Service s = javax.xml.ws.Service
+ .create(url, serviceName);
+ Greeter greeter = s.getPort(portName, Greeter.class);
+ final InvocationHandler handler = Proxy.getInvocationHandler(greeter);
+
+ Map<String, Object> requestContext =
((BindingProvider)handler).getRequestContext();
+ requestContext.put(JaxWsClientProxy.THREAD_LOCAL_REQUEST_CONTEXT,
Boolean.TRUE);
+
+ //re-get the context so it's not a thread safe variant
+ requestContext = ((BindingProvider)handler).getRequestContext();
+
+ final String key = "Hi";
+
+ requestContext.put(key, "ho");
+
+ final Object[] result = new Object[2];
+ Thread t = new Thread() {
+ public void run() {
+ Map<String, Object> requestContext =
((BindingProvider)handler).getRequestContext();
+ result[0] = requestContext.get(key);
+ requestContext.remove(key);
+ result[1] = requestContext.get(key);
+ }
+ };
+ t.start();
+ t.join();
+
+ assertEquals("thread sees the put", "ho", result[0]);
+ assertNull("thread did not remove the put", result[1]);
+
+ assertEquals("main thread does not see removal",
+ "ho", requestContext.get(key));
+ }
+
+ @Test
public void testEndpoint() throws Exception {
ReflectionServiceFactoryBean bean = new JaxWsServiceFactoryBean();
URL resource = getClass().getResource("/wsdl/hello_world.wsdl");