geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jinmeil...@apache.org
Subject [geode] branch develop updated: GEODE-5971: JaxbService should be able to unmarshall older namespace xml (#2951)
Date Wed, 05 Dec 2018 23:15:50 GMT
This is an automated email from the ASF dual-hosted git repository.

jinmeiliao pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new ad3a082  GEODE-5971: JaxbService should be able to unmarshall older namespace xml
(#2951)
ad3a082 is described below

commit ad3a08295cca7c15c1350f52af4c3436e2da9e4c
Author: jinmeiliao <jiliao@pivotal.io>
AuthorDate: Wed Dec 5 15:15:40 2018 -0800

    GEODE-5971: JaxbService should be able to unmarshall older namespace xml (#2951)
---
 .../geode/cache/configuration/CacheConfig.java     |  5 ++-
 .../apache/geode/internal/config/JAXBService.java  | 16 +++++++-
 .../geode/internal/config/NameSpaceFilter.java     | 43 ++++++++++++++++++++++
 .../geode/internal/config/VersionAdapter.java      | 30 +++++++++++++++
 .../geode/internal/config/JAXBServiceTest.java     | 31 ++++++++++++++++
 5 files changed, 122 insertions(+), 3 deletions(-)

diff --git a/geode-core/src/main/java/org/apache/geode/cache/configuration/CacheConfig.java
b/geode-core/src/main/java/org/apache/geode/cache/configuration/CacheConfig.java
index c1c59f8..ec23dac 100644
--- a/geode-core/src/main/java/org/apache/geode/cache/configuration/CacheConfig.java
+++ b/geode-core/src/main/java/org/apache/geode/cache/configuration/CacheConfig.java
@@ -31,13 +31,13 @@ import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlType;
-import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 
 import org.w3c.dom.Element;
 
 import org.apache.geode.annotations.Experimental;
 import org.apache.geode.cache.Region;
+import org.apache.geode.internal.config.VersionAdapter;
 
 
 /**
@@ -328,12 +328,13 @@ public class CacheConfig {
   @XmlAttribute(name = "search-timeout")
   protected String searchTimeout;
   @XmlAttribute(name = "version", required = true)
-  @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
+  @XmlJavaTypeAdapter(VersionAdapter.class)
   protected String version;
 
   public CacheConfig() {}
 
   public CacheConfig(String version) {
+
     this.version = version;
   }
 
diff --git a/geode-core/src/main/java/org/apache/geode/internal/config/JAXBService.java b/geode-core/src/main/java/org/apache/geode/internal/config/JAXBService.java
index 9f3af69..6927d2f 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/config/JAXBService.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/config/JAXBService.java
@@ -28,11 +28,15 @@ import javax.xml.XMLConstants;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
+import javax.xml.transform.sax.SAXSource;
 import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
 
 import org.apache.commons.lang3.StringUtils;
+import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
 
 import org.apache.geode.cache.configuration.XSDRootElement;
 import org.apache.geode.internal.ClassPathLoader;
@@ -93,9 +97,19 @@ public class JAXBService {
 
   public <T> T unMarshall(String xml) {
     try {
-      return (T) unmarshaller.unmarshal(new StringReader(xml));
+      InputSource is = new InputSource(new StringReader(xml));
+      XMLReader reader = XMLReaderFactory.createXMLReader();
+
+      // use a custom Filter so that we can unmarshall older namespace or no namespace xml
+      NameSpaceFilter filter = new NameSpaceFilter();
+      filter.setParent(reader);
+      SAXSource source = new SAXSource(filter, is);
+
+      return (T) unmarshaller.unmarshal(source);
     } catch (Exception e) {
       throw new RuntimeException(e.getMessage(), e);
     }
   }
+
+
 }
diff --git a/geode-core/src/main/java/org/apache/geode/internal/config/NameSpaceFilter.java
b/geode-core/src/main/java/org/apache/geode/internal/config/NameSpaceFilter.java
new file mode 100644
index 0000000..a31546b
--- /dev/null
+++ b/geode-core/src/main/java/org/apache/geode/internal/config/NameSpaceFilter.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information
regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version
2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain
a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under
the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express
+ * or implied. See the License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+
+package org.apache.geode.internal.config;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.XMLFilterImpl;
+
+import org.apache.geode.internal.cache.xmlcache.CacheXml;
+
+public class NameSpaceFilter extends XMLFilterImpl {
+
+  @Override
+  public void startDocument() throws SAXException {
+    super.startDocument();
+  }
+
+  /**
+   * be able to handle no namespace or older namespace
+   */
+  @Override
+  public void startElement(String uri, String localName, String qName,
+      Attributes atts) throws SAXException {
+    if ("".equals(uri) || CacheXml.GEMFIRE_NAMESPACE.equals(uri)) {
+      uri = CacheXml.GEODE_NAMESPACE;
+    }
+    super.startElement(uri, localName, qName, atts);
+  }
+
+}
diff --git a/geode-core/src/main/java/org/apache/geode/internal/config/VersionAdapter.java
b/geode-core/src/main/java/org/apache/geode/internal/config/VersionAdapter.java
new file mode 100644
index 0000000..15fd53c
--- /dev/null
+++ b/geode-core/src/main/java/org/apache/geode/internal/config/VersionAdapter.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information
regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version
2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain
a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under
the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express
+ * or implied. See the License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+
+package org.apache.geode.internal.config;
+
+import javax.xml.bind.annotation.adapters.XmlAdapter;
+
+public class VersionAdapter extends XmlAdapter<String, String> {
+  @Override
+  public String unmarshal(String v) throws Exception {
+    return "1.0";
+  }
+
+  @Override
+  public String marshal(String v) throws Exception {
+    return v;
+  }
+}
diff --git a/geode-core/src/test/java/org/apache/geode/internal/config/JAXBServiceTest.java
b/geode-core/src/test/java/org/apache/geode/internal/config/JAXBServiceTest.java
index 311022a..67f0807 100644
--- a/geode-core/src/test/java/org/apache/geode/internal/config/JAXBServiceTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/config/JAXBServiceTest.java
@@ -153,6 +153,37 @@ public class JAXBServiceTest {
     assertThat(elements.get(0)).isInstanceOf(ElementOne.class);
   }
 
+  @Test
+  public void marshalOlderNameSpace() {
+    String xml =
+        "<cache xsi:schemaLocation=\"http://schema.pivotal.io/gemfire/cache http://schema.pivotal.io/gemfire/cache/cache-8.1.xsd\"\n"
+            +
+            "       version=\"8.1\"\n" +
+            "       xmlns=\"http://schema.pivotal.io/gemfire/cache\"\n" +
+            "       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
+            "    <region name=\"one\">\n" +
+            "        <region-attributes scope=\"distributed-ack\" data-policy=\"replicate\"/>\n"
+
+            "    </region>\n" +
+            "</cache>";
+
+    CacheConfig cacheConfig = service2.unMarshall(xml);
+    assertThat(cacheConfig.getRegions()).hasSize(1);
+    assertThat(cacheConfig.getRegions().get(0).getName()).isEqualTo("one");
+  }
+
+  @Test
+  public void marshallNoNameSpace() {
+    String xml = "<cache version=\"1.0\">\n" +
+        "    <region name=\"one\">\n" +
+        "        <region-attributes scope=\"distributed-ack\" data-policy=\"replicate\"/>\n"
+
+        "    </region>\n" +
+        "</cache>";
+
+    CacheConfig cacheConfig = service2.unMarshall(xml);
+    assertThat(cacheConfig.getRegions()).hasSize(1);
+    assertThat(cacheConfig.getRegions().get(0).getName()).isEqualTo("one");
+  }
+
   public static void setBasicValues(CacheConfig cache) {
     cache.setCopyOnRead(true);
     CacheConfig.GatewayReceiver receiver = new CacheConfig.GatewayReceiver();


Mime
View raw message