You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by jb...@apache.org on 2013/09/03 19:13:06 UTC

[3/5] git commit: Improve error message when yaml contains invalid properties patch by Mikhail Stepura; reviewed by jbellis for CASSANDRA-5958

Improve error message when yaml contains invalid properties
patch by Mikhail Stepura; reviewed by jbellis for CASSANDRA-5958


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/31f6ec1b
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/31f6ec1b
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/31f6ec1b

Branch: refs/heads/trunk
Commit: 31f6ec1bf5ac642ea0e47e40e62add46901135fc
Parents: 760e68f
Author: Jonathan Ellis <jb...@apache.org>
Authored: Tue Sep 3 12:11:40 2013 -0500
Committer: Jonathan Ellis <jb...@apache.org>
Committed: Tue Sep 3 12:12:47 2013 -0500

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../config/YamlConfigurationLoader.java         | 43 ++++++++++++++++++--
 2 files changed, 41 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/31f6ec1b/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 40cd5ae..c09ba86 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 2.0.1
+ * Improve error message when yaml contains invalid properties (CASSANDRA-5958)
  * Improve leveled compaction's ability to find non-overlapping L0 compactions
    to work on concurrently (CASSANDRA-5921)
  * Notify indexer of columns shadowed by range tombstones (CASSANDRA-5614)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/31f6ec1b/src/java/org/apache/cassandra/config/YamlConfigurationLoader.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/config/YamlConfigurationLoader.java b/src/java/org/apache/cassandra/config/YamlConfigurationLoader.java
index 642fe8b..d8a138c 100644
--- a/src/java/org/apache/cassandra/config/YamlConfigurationLoader.java
+++ b/src/java/org/apache/cassandra/config/YamlConfigurationLoader.java
@@ -17,19 +17,23 @@
  */
 package org.apache.cassandra.config;
 
+import java.beans.IntrospectionException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
 import org.apache.cassandra.exceptions.ConfigurationException;
 import org.apache.cassandra.io.util.FileUtils;
-
 import org.yaml.snakeyaml.TypeDescription;
 import org.yaml.snakeyaml.Yaml;
 import org.yaml.snakeyaml.error.YAMLException;
+import org.yaml.snakeyaml.introspector.MissingProperty;
+import org.yaml.snakeyaml.introspector.Property;
+import org.yaml.snakeyaml.introspector.PropertyUtils;
 
 public class YamlConfigurationLoader implements ConfigurationLoader
 {
@@ -83,8 +87,12 @@ public class YamlConfigurationLoader implements ConfigurationLoader
             TypeDescription seedDesc = new TypeDescription(SeedProviderDef.class);
             seedDesc.putMapPropertyType("parameters", String.class, String.class);
             constructor.addTypeDescription(seedDesc);
+            MissingPropertiesChecker propertiesChecker = new MissingPropertiesChecker();
+            constructor.setPropertyUtils(propertiesChecker);
             Yaml yaml = new Yaml(constructor);
-            return (Config)yaml.load(input);
+            Config result = yaml.loadAs(input, Config.class);
+            propertiesChecker.check();
+            return result;
         }
         catch (YAMLException e)
         {
@@ -95,4 +103,33 @@ public class YamlConfigurationLoader implements ConfigurationLoader
             FileUtils.closeQuietly(input);
         }
     }
+    
+    private static class MissingPropertiesChecker extends PropertyUtils 
+    {
+        private final Set<String> missingProperties = new HashSet<>();
+        
+        public MissingPropertiesChecker()
+        {
+            setSkipMissingProperties(true);
+        }
+        
+        @Override
+        public Property getProperty(Class<? extends Object> type, String name) throws IntrospectionException
+        {
+            Property result = super.getProperty(type, name);
+            if (result instanceof MissingProperty)
+            {
+                missingProperties.add(result.getName());
+            }
+            return result;
+        }
+        
+        public void check() throws ConfigurationException
+        {
+            if (!missingProperties.isEmpty()) 
+            {
+                throw new ConfigurationException("Invalid yaml. Please remove properties " + missingProperties + " from your cassandra.yaml");
+            }
+        }
+    }
 }