You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by co...@apache.org on 2020/05/05 12:11:58 UTC

[cxf] 01/05: CXF-5216 - WSDL definition cache issue for web service client when WSDL in service war changed

This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch 3.3.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit cc1f624a675808ba5ba12663a0fcdbee39afc706
Author: Colm O hEigeartaigh <co...@apache.org>
AuthorDate: Tue May 5 10:07:12 2020 +0100

    CXF-5216 - WSDL definition cache issue for web service client when WSDL in service war changed
    
    (cherry picked from commit 37a8716b49c7a0b9f0f759a152740ecebf8c44f8)
---
 .../main/java/org/apache/cxf/wsdl/WSDLManager.java |   6 ++
 .../org/apache/cxf/wsdl11/WSDLManagerImpl.java     |  19 ++++
 .../org/apache/cxf/wsdl11/WSDLManagerImplTest.java | 100 ++++++++++++++++++++-
 3 files changed, 122 insertions(+), 3 deletions(-)

diff --git a/rt/wsdl/src/main/java/org/apache/cxf/wsdl/WSDLManager.java b/rt/wsdl/src/main/java/org/apache/cxf/wsdl/WSDLManager.java
index 38326fc..e731a75 100644
--- a/rt/wsdl/src/main/java/org/apache/cxf/wsdl/WSDLManager.java
+++ b/rt/wsdl/src/main/java/org/apache/cxf/wsdl/WSDLManager.java
@@ -104,4 +104,10 @@ public interface WSDLManager {
      */
     void removeDefinition(Definition wsdl);
 
+    /**
+     * If the definition is cached, remove it from the cache
+     * @param url
+     */
+    void removeDefinition(String url);
+
 }
diff --git a/rt/wsdl/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java b/rt/wsdl/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java
index c111756..1a911fc 100644
--- a/rt/wsdl/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java
+++ b/rt/wsdl/src/main/java/org/apache/cxf/wsdl11/WSDLManagerImpl.java
@@ -338,6 +338,7 @@ public class WSDLManagerImpl implements WSDLManager {
         this.disableSchemaCache = disableSchemaCache;
     }
 
+    @Override
     public void removeDefinition(Definition wsdl) {
         synchronized (definitionsMap) {
             List<Object> keys = new ArrayList<>();
@@ -353,5 +354,23 @@ public class WSDLManagerImpl implements WSDLManager {
         }
     }
 
+    @Override
+    public void removeDefinition(String url) {
+        synchronized (definitionsMap) {
+            Definition wsdl = definitionsMap.get(url);
+            if (wsdl != null) {
+                List<Object> keys = new ArrayList<>();
+                for (Map.Entry<Object, Definition> e : definitionsMap.entrySet()) {
+                    if (e.getValue() == wsdl) {
+                        keys.add(e.getKey());
+                    }
+                }
+                for (Object o : keys) {
+                    definitionsMap.remove(o);
+                    schemaCacheMap.remove(o);
+                }
+            }
+        }
+    }
 
 }
diff --git a/rt/wsdl/src/test/java/org/apache/cxf/wsdl11/WSDLManagerImplTest.java b/rt/wsdl/src/test/java/org/apache/cxf/wsdl11/WSDLManagerImplTest.java
index d9a5746..66bb335 100644
--- a/rt/wsdl/src/test/java/org/apache/cxf/wsdl11/WSDLManagerImplTest.java
+++ b/rt/wsdl/src/test/java/org/apache/cxf/wsdl11/WSDLManagerImplTest.java
@@ -19,6 +19,10 @@
 
 package org.apache.cxf.wsdl11;
 
+import java.io.File;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -34,12 +38,14 @@ import javax.xml.stream.XMLStreamReader;
 
 import org.apache.cxf.staxutils.PropertiesExpandingStreamReader;
 import org.apache.cxf.staxutils.XMLStreamReaderWrapper;
+import org.apache.cxf.wsdl.WSDLManager;
 
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 public class WSDLManagerImplTest {
 
@@ -48,7 +54,7 @@ public class WSDLManagerImplTest {
         String qname = "http://apache.org/hello_world_soap_http";
         String wsdlUrl = getClass().getResource("hello_world.wsdl").toString();
 
-        WSDLManagerImpl builder = new WSDLManagerImpl();
+        WSDLManager builder = new WSDLManagerImpl();
         Definition def = builder.getDefinition(wsdlUrl);
         assertNotNull(def);
 
@@ -69,7 +75,7 @@ public class WSDLManagerImplTest {
     public void testBuildImportedWSDL() throws Exception {
         String wsdlUrl = getClass().getResource("hello_world_services.wsdl").toString();
 
-        WSDLManagerImpl builder = new WSDLManagerImpl();
+        WSDLManager builder = new WSDLManagerImpl();
         Definition def = builder.getDefinition(wsdlUrl);
 
         assertNotNull(def);
@@ -109,7 +115,7 @@ public class WSDLManagerImplTest {
     public void testLocalNamespacedWSDL() throws Exception {
         String wsdlUrl = getClass().getResource("hello_world_local_nsdecl.wsdl").toString();
 
-        WSDLManagerImpl builder = new WSDLManagerImpl();
+        WSDLManager builder = new WSDLManagerImpl();
         Definition def = builder.getDefinition(wsdlUrl);
         java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
         builder.getWSDLFactory().newWSDLWriter().writeWSDL(def, bos);
@@ -132,4 +138,92 @@ public class WSDLManagerImplTest {
         builder.getWSDLFactory().newWSDLWriter().writeWSDL(def, bos);
         assertTrue(bos.toString().contains("http://localhost:99999/SoapContext/SoapPort"));
     }
+
+    @Test
+    public void testRemoveDefinition() throws Exception {
+        String basedir = System.getProperty("basedir");
+        if (basedir == null) {
+            basedir = new File(".").getCanonicalPath();
+        }
+
+        // Copy hello_world.wsdl so that we can delete it
+        Path path1 = FileSystems.getDefault().getPath(basedir,
+                "/src/test/resources/org/apache/cxf/wsdl11/hello_world.wsdl");
+        Path path2 = FileSystems.getDefault().getPath(basedir, "/target/test-classes/hello_world2.wsdl");
+        Files.copy(path1, path2);
+
+        // Load the resource
+        WSDLManager builder = new WSDLManagerImpl();
+        Definition def = builder.getDefinition(path2.toString());
+        assertNotNull(def);
+
+        // Delete the resource
+        Files.delete(path2);
+
+        // Now load it again to test caching
+        def = builder.getDefinition(path2.toString());
+        assertNotNull(def);
+
+        Map<?, ?> services = def.getServices();
+        assertNotNull(services);
+        assertEquals(1, services.size());
+        String qname = "http://apache.org/hello_world_soap_http";
+        Service service = (Service) services.get(new QName(qname, "SOAPService"));
+        assertNotNull(service);
+
+        // Now remove it
+        builder.removeDefinition(def);
+
+        // This time loading should fail as the original resource is removed
+        try {
+            builder.getDefinition(path2.toString());
+            fail("Failure expected");
+        } catch (NullPointerException ex) {
+            // expected
+        }
+    }
+
+    @Test
+    public void testRemoveDefinitionByURL() throws Exception {
+        String basedir = System.getProperty("basedir");
+        if (basedir == null) {
+            basedir = new File(".").getCanonicalPath();
+        }
+
+        // Copy hello_world.wsdl so that we can delete it
+        Path path1 = FileSystems.getDefault().getPath(basedir,
+                "/src/test/resources/org/apache/cxf/wsdl11/hello_world.wsdl");
+        Path path2 = FileSystems.getDefault().getPath(basedir, "/target/test-classes/hello_world2.wsdl");
+        Files.copy(path1, path2);
+
+        // Load the resource
+        WSDLManager builder = new WSDLManagerImpl();
+        Definition def = builder.getDefinition(path2.toString());
+        assertNotNull(def);
+
+        // Delete the resource
+        Files.delete(path2);
+
+        // Now load it again to test caching
+        def = builder.getDefinition(path2.toString());
+        assertNotNull(def);
+
+        Map<?, ?> services = def.getServices();
+        assertNotNull(services);
+        assertEquals(1, services.size());
+        String qname = "http://apache.org/hello_world_soap_http";
+        Service service = (Service) services.get(new QName(qname, "SOAPService"));
+        assertNotNull(service);
+
+        // Now remove it
+        builder.removeDefinition(path2.toString());
+
+        // This time loading should fail as the original resource is removed
+        try {
+            builder.getDefinition(path2.toString());
+            fail("Failure expected");
+        } catch (NullPointerException ex) {
+            // expected
+        }
+    }
 }
\ No newline at end of file