Mmuzaf commented on code in PR #2334:
URL: https://github.com/apache/cassandra/pull/2334#discussion_r1196620026


##########
src/java/org/apache/cassandra/config/YamlConfigurationLoader.java:
##########
@@ -424,5 +428,140 @@ public void check() throws ConfigurationException
                 logger.warn("{} parameters have been deprecated. They have new 
names and/or value format; For more information, please refer to NEWS.txt", 
deprecationWarnings);
         }
     }
+
+    /**
+     * Creates a YAML instance based on Cassandra's custom configuration 
classes and types. Yaml factory here is used
+     * to {@link org.yaml.snakeyaml.Yaml#dumpAs} and {@link 
org.yaml.snakeyaml.Yaml#load(String)} given object or
+     * string respectively, that in turn is used to serialize and deserialize 
configuration properties.
+     */
+    public static class YamlFactory
+    {
+        private static final List<TypeDescription> scalarCassandraTypes = new 
ArrayList<>();
+        private static final List<TypeDescription> javaBeanCassandraTypes = 
new ArrayList<>();
+        private static volatile YamlFactory instance;
+
+        private YamlFactory()
+        {
+            loadScalarTypeDescriptions(scalarCassandraTypes);
+            loadJavaBeanTypeDescriptions(javaBeanCassandraTypes);
+        }
+
+        public static YamlFactory getInstance()
+        {
+            YamlFactory instance0 = instance;
+            if (instance0 == null)
+            {
+                synchronized (YamlFactory.class)
+                {
+                    instance0 = instance;
+                    if (instance0 == null)
+                        instance = instance0 = new YamlFactory();
+                }
+            }
+            return instance0;
+        }
+
+        /**
+         * Creates a new Yaml instance with the given root class.
+         * @param root The class (usually JavaBean) to be constructed.
+         * @return A new Yaml instance with the given root class.
+         */
+        public Yaml newYamlInstance(Class<?> root)
+        {
+            PropertyUtils propertyUtils = new PropertyUtils();
+            propertyUtils.setBeanAccess(BeanAccess.FIELD);
+            propertyUtils.setAllowReadOnlyProperties(true);
+            return newYamlInstance(new CustomConstructor(root, 
Config.class.getClassLoader()), propertyUtils);
+        }
+
+        public Yaml newYamlInstance(SafeConstructor constructor, PropertyUtils 
propertyUtils)
+        {
+            return create(constructor, propertyUtils);
+        }
+
+        private static Yaml create(SafeConstructor constructor, PropertyUtils 
propertyUtils)
+        {
+            DumperOptions options = new DumperOptions();
+            options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
+            options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
+            options.setSplitLines(true);
+            options.setPrettyFlow(true); // to remove brackets around
+            Representer representer = new 
CassandraRepresenter(scalarCassandraTypes, options);
+            representer.setPropertyUtils(propertyUtils);
+            constructor.setPropertyUtils(propertyUtils);
+            Yaml yaml = new Yaml(constructor, representer);
+
+            scalarCassandraTypes.forEach(yaml::addTypeDescription);
+            javaBeanCassandraTypes.forEach(yaml::addTypeDescription);
+            return yaml;
+        }
+
+        private static void loadScalarTypeDescriptions(List<TypeDescription> 
types)

Review Comment:
   Fixed.



##########
src/java/org/apache/cassandra/config/YamlConfigurationLoader.java:
##########
@@ -424,5 +428,140 @@ public void check() throws ConfigurationException
                 logger.warn("{} parameters have been deprecated. They have new 
names and/or value format; For more information, please refer to NEWS.txt", 
deprecationWarnings);
         }
     }
+
+    /**
+     * Creates a YAML instance based on Cassandra's custom configuration 
classes and types. Yaml factory here is used
+     * to {@link org.yaml.snakeyaml.Yaml#dumpAs} and {@link 
org.yaml.snakeyaml.Yaml#load(String)} given object or
+     * string respectively, that in turn is used to serialize and deserialize 
configuration properties.
+     */
+    public static class YamlFactory
+    {
+        private static final List<TypeDescription> scalarCassandraTypes = new 
ArrayList<>();
+        private static final List<TypeDescription> javaBeanCassandraTypes = 
new ArrayList<>();
+        private static volatile YamlFactory instance;
+
+        private YamlFactory()
+        {
+            loadScalarTypeDescriptions(scalarCassandraTypes);
+            loadJavaBeanTypeDescriptions(javaBeanCassandraTypes);
+        }
+
+        public static YamlFactory getInstance()
+        {
+            YamlFactory instance0 = instance;
+            if (instance0 == null)
+            {
+                synchronized (YamlFactory.class)
+                {
+                    instance0 = instance;
+                    if (instance0 == null)
+                        instance = instance0 = new YamlFactory();
+                }
+            }
+            return instance0;
+        }
+
+        /**
+         * Creates a new Yaml instance with the given root class.
+         * @param root The class (usually JavaBean) to be constructed.
+         * @return A new Yaml instance with the given root class.
+         */
+        public Yaml newYamlInstance(Class<?> root)
+        {
+            PropertyUtils propertyUtils = new PropertyUtils();
+            propertyUtils.setBeanAccess(BeanAccess.FIELD);
+            propertyUtils.setAllowReadOnlyProperties(true);
+            return newYamlInstance(new CustomConstructor(root, 
Config.class.getClassLoader()), propertyUtils);
+        }
+
+        public Yaml newYamlInstance(SafeConstructor constructor, PropertyUtils 
propertyUtils)
+        {
+            return create(constructor, propertyUtils);
+        }
+
+        private static Yaml create(SafeConstructor constructor, PropertyUtils 
propertyUtils)
+        {
+            DumperOptions options = new DumperOptions();
+            options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
+            options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
+            options.setSplitLines(true);
+            options.setPrettyFlow(true); // to remove brackets around
+            Representer representer = new 
CassandraRepresenter(scalarCassandraTypes, options);
+            representer.setPropertyUtils(propertyUtils);
+            constructor.setPropertyUtils(propertyUtils);
+            Yaml yaml = new Yaml(constructor, representer);
+
+            scalarCassandraTypes.forEach(yaml::addTypeDescription);
+            javaBeanCassandraTypes.forEach(yaml::addTypeDescription);
+            return yaml;
+        }
+
+        private static void loadScalarTypeDescriptions(List<TypeDescription> 
types)
+        {
+            // Enum types resolved with a custom overridden handler 
DefaultRepresentEnum() as a smiple string.
+            
types.add(createTypeDescription(DataRateSpec.LongBytesPerSecondBound.class));
+            
types.add(createTypeDescription(DataStorageSpec.IntBytesBound.class));
+            
types.add(createTypeDescription(DataStorageSpec.IntKibibytesBound.class));
+            
types.add(createTypeDescription(DataStorageSpec.IntMebibytesBound.class));
+            
types.add(createTypeDescription(DataStorageSpec.LongBytesBound.class));
+            
types.add(createTypeDescription(DataStorageSpec.LongMebibytesBound.class));
+            
types.add(createTypeDescription(DurationSpec.IntMillisecondsBound.class));
+            
types.add(createTypeDescription(DurationSpec.IntMinutesBound.class));
+            
types.add(createTypeDescription(DurationSpec.IntSecondsBound.class));
+            
types.add(createTypeDescription(DurationSpec.LongMillisecondsBound.class));
+            
types.add(createTypeDescription(DurationSpec.LongNanosecondsBound.class));
+            
types.add(createTypeDescription(DurationSpec.LongSecondsBound.class));
+        }
+
+        private static void loadJavaBeanTypeDescriptions(List<TypeDescription> 
types)

Review Comment:
   Fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to