cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXFR-6066] Closing WebClient on finalize too
Date Mon, 03 Nov 2014 14:03:42 GMT
Repository: cxf
Updated Branches:
  refs/heads/master b7e74f08a -> 61c7b99b6


[CXFR-6066] Closing WebClient on finalize too


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/61c7b99b
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/61c7b99b
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/61c7b99b

Branch: refs/heads/master
Commit: 61c7b99b67fe4a29e61af4a079b75d5154e585f9
Parents: b7e74f0
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Mon Nov 3 14:03:26 2014 +0000
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Mon Nov 3 14:03:26 2014 +0000

----------------------------------------------------------------------
 .../apache/cxf/jaxrs/client/AbstractClient.java | 65 ++++++++++++--------
 1 file changed, 39 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/61c7b99b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
----------------------------------------------------------------------
diff --git a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
index 46acbfb..a745d77 100644
--- a/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
+++ b/rt/rs/client/src/main/java/org/apache/cxf/jaxrs/client/AbstractClient.java
@@ -36,6 +36,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.logging.Logger;
 
 import javax.ws.rs.ProcessingException;
@@ -107,7 +108,7 @@ public abstract class AbstractClient implements Client {
         
     protected ClientConfiguration cfg = new ClientConfiguration();
     private ClientState state;
-    
+    private AtomicBoolean closed = new AtomicBoolean(); 
     protected AbstractClient(ClientState initialState) {
         this.state = initialState;
     }
@@ -286,33 +287,36 @@ public abstract class AbstractClient implements Client {
     }
 
     public void close() {
-        if (cfg.getBus() == null) {
-            return;
-        }
-        for (Closeable c : cfg.getEndpoint().getCleanupHooks()) {
-            try {
-                c.close();
-            } catch (IOException e) {
-                //ignore
+        if (closed.compareAndSet(false, true)) {
+            if (cfg.getBus() == null) {
+                return;
             }
-        }
-        ClientLifeCycleManager mgr = cfg.getBus().getExtension(ClientLifeCycleManager.class);
-        if (null != mgr) {
-            mgr.clientDestroyed(new FrontendClientAdapter(getConfiguration()));
-        }
-
-        if (cfg.getConduitSelector() instanceof Closeable) {
-            try {
-                ((Closeable)cfg.getConduitSelector()).close();
-            } catch (IOException e) {
-                //ignore, we're destroying anyway
+            for (Closeable c : cfg.getEndpoint().getCleanupHooks()) {
+                try {
+                    c.close();
+                } catch (IOException e) {
+                    //ignore
+                }
             }
-        } else {
-            cfg.getConduit().close();
+            ClientLifeCycleManager mgr = cfg.getBus().getExtension(ClientLifeCycleManager.class);
+            if (null != mgr) {
+                mgr.clientDestroyed(new FrontendClientAdapter(getConfiguration()));
+            }
+    
+            if (cfg.getConduitSelector() instanceof Closeable) {
+                try {
+                    ((Closeable)cfg.getConduitSelector()).close();
+                } catch (IOException e) {
+                    //ignore, we're destroying anyway
+                }
+            } else {
+                cfg.getConduit().close();
+            }
+            state.reset();
+            state = null;
+            cfg = null;
         }
-        state.reset();
-        state = null;
-        cfg = null;
+        closed = null;
     }
     
     private void possiblyAddHeader(String name, String value) {
@@ -872,7 +876,15 @@ public abstract class AbstractClient implements Client {
             outMessage.put(Message.PROCESS_ONEWAY_RESPONSE, true);
         }
     }
-    
+    private void checkClosed() {
+        if (closed.get()) {
+            throw new IllegalStateException("Client is closed");
+        }
+    }
+    protected void finalize() throws Throwable {
+        super.finalize();
+        close();
+    }
     protected Message createMessage(Object body,
                                     String httpMethod, 
                                     MultivaluedMap<String, String> headers,
@@ -880,6 +892,7 @@ public abstract class AbstractClient implements Client {
                                     Exchange exchange,
                                     Map<String, Object> invocationContext,
                                     boolean proxy) {
+        checkClosed();
         Message m = cfg.getConduitSelector().getEndpoint().getBinding().createMessage();
         m.put(Message.REQUESTOR_ROLE, Boolean.TRUE);
         m.put(Message.INBOUND_MESSAGE, Boolean.FALSE);


Mime
View raw message