You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@johnzon.apache.org by sa...@apache.org on 2015/01/05 13:56:06 UTC

incubator-johnzon git commit: JOHNZON-33 Define appropriate buffer size and max text size (also simplified factories)

Repository: incubator-johnzon
Updated Branches:
  refs/heads/master d9e51a841 -> 94b896dfa


JOHNZON-33 Define appropriate buffer size and max text size (also simplified factories)


Project: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/commit/94b896df
Tree: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/tree/94b896df
Diff: http://git-wip-us.apache.org/repos/asf/incubator-johnzon/diff/94b896df

Branch: refs/heads/master
Commit: 94b896dfa3e12cb92bf131c3bd9c63900be24936
Parents: d9e51a8
Author: Hendrik Saly <he...@gmail.com>
Authored: Mon Jan 5 13:49:14 2015 +0100
Committer: Hendrik Saly <he...@gmail.com>
Committed: Mon Jan 5 13:54:20 2015 +0100

----------------------------------------------------------------------
 .../johnzon/core/AbstractJsonFactory.java       | 84 ++++++++++++++++++++
 .../johnzon/core/JsonGeneratorFactoryImpl.java  | 56 +++----------
 .../johnzon/core/JsonParserFactoryImpl.java     | 70 ++++------------
 .../johnzon/core/JsonReaderFactoryImpl.java     | 34 ++------
 .../johnzon/core/JsonWriterFactoryImpl.java     | 34 ++------
 5 files changed, 122 insertions(+), 156 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/94b896df/johnzon-core/src/main/java/org/apache/johnzon/core/AbstractJsonFactory.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/AbstractJsonFactory.java b/johnzon-core/src/main/java/org/apache/johnzon/core/AbstractJsonFactory.java
new file mode 100644
index 0000000..507bf65
--- /dev/null
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/AbstractJsonFactory.java
@@ -0,0 +1,84 @@
+/*
+ * 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.johnzon.core;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.logging.Logger;
+
+public abstract class AbstractJsonFactory implements Serializable{
+
+    protected final Logger logger = Logger.getLogger(this.getClass().getName());
+    
+    public static final String BUFFER_STRATEGY = "org.apache.johnzon.buffer-strategy";
+    public static final BufferStrategy DEFAULT_BUFFER_STRATEGY = BufferStrategy.QUEUE;
+    
+    protected final Map<String, Object> internalConfig = new HashMap<String, Object>();
+    
+    protected AbstractJsonFactory(final Map<String, ?> config, Collection<String> supportedConfigKeys, Collection<String> defaultSupportedConfigKeys) {
+        if(config != null) {
+            
+            if(defaultSupportedConfigKeys != null) {
+                supportedConfigKeys = new ArrayList<String>(supportedConfigKeys);
+                supportedConfigKeys.addAll(defaultSupportedConfigKeys);
+            }
+            
+            for (String configKey : config.keySet()) {
+                if(supportedConfigKeys.contains(configKey)) {
+                    internalConfig.put(configKey, config.get(configKey));
+                } else {
+                    logger.warning(configKey + " is not supported by " + getClass().getName());
+                }
+            }
+        }
+    }
+
+    protected BufferStrategy getBufferProvider() {
+        final Object name = internalConfig.get(BUFFER_STRATEGY);
+        if (name != null) {
+            return BufferStrategy.valueOf(name.toString().toUpperCase(Locale.ENGLISH));
+        }
+        return DEFAULT_BUFFER_STRATEGY;
+    }
+
+    protected int getInt(final String key, final int defaultValue) {
+        final Object intValue = internalConfig.get(key);
+        if (intValue == null) {
+            return defaultValue;
+        } else if (Number.class.isInstance(intValue)) {
+            return Number.class.cast(intValue).intValue();
+        }
+        return Integer.parseInt(intValue.toString());
+    }
+
+    protected boolean getBool(final String key, final boolean defaultValue) {
+        final Object boolValue = internalConfig.get(key);
+        if (boolValue == null) {
+            return defaultValue;
+        } else if (Boolean.class.isInstance(boolValue)) {
+            return Boolean.class.cast(boolValue);
+        }
+        return Boolean.parseBoolean(boolValue.toString());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/94b896df/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorFactoryImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorFactoryImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorFactoryImpl.java
index 332fa33..678f4a9 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorFactoryImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonGeneratorFactoryImpl.java
@@ -18,28 +18,25 @@
  */
 package org.apache.johnzon.core;
 
+import static java.util.Arrays.asList;
+
 import java.io.OutputStream;
-import java.io.Serializable;
 import java.io.Writer;
 import java.nio.charset.Charset;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
-import java.util.Locale;
 import java.util.Map;
 import java.util.concurrent.ConcurrentMap;
 
 import javax.json.stream.JsonGenerator;
 import javax.json.stream.JsonGeneratorFactory;
 
-import static java.util.Arrays.asList;
-
-public class JsonGeneratorFactoryImpl implements JsonGeneratorFactory, Serializable {    
-    public static final String BUFFER_LENGTH = "org.apache.johnzon.default-char-buffer-generator";
-    public static final int DEFAULT_BUFFER_LENGTH = Integer.getInteger(BUFFER_LENGTH, 1024); //TODO check default string length/buffer size
-    private final Map<String, Object> internalConfig = new HashMap<String, Object>();
-    private static final Collection<String> SUPPORTED_CONFIG_KEYS = asList(
-        JsonGenerator.PRETTY_PRINTING, BUFFER_LENGTH, JsonParserFactoryImpl.BUFFER_STRATEGY
+public class JsonGeneratorFactoryImpl extends AbstractJsonFactory implements JsonGeneratorFactory {    
+    public static final String GENERATOR_BUFFER_LENGTH = "org.apache.johnzon.default-char-buffer-generator";
+    public static final int DEFAULT_GENERATOR_BUFFER_LENGTH =  Integer.getInteger(GENERATOR_BUFFER_LENGTH, 64 * 1024); //64k
+   
+    static final Collection<String> SUPPORTED_CONFIG_KEYS = asList(
+        JsonGenerator.PRETTY_PRINTING, GENERATOR_BUFFER_LENGTH, BUFFER_STRATEGY
     );
     //key caching currently disabled
     private final ConcurrentMap<String, String> cache = null;//new ConcurrentHashMap<String, String>();
@@ -48,46 +45,17 @@ public class JsonGeneratorFactoryImpl implements JsonGeneratorFactory, Serializa
 
     public JsonGeneratorFactoryImpl(final Map<String, ?> config) {
         
-          if(config != null) {
+          super(config, SUPPORTED_CONFIG_KEYS, null); 
           
-              for (String configKey : config.keySet()) {
-                  if(SUPPORTED_CONFIG_KEYS.contains(configKey)) {
-                      internalConfig.put(configKey, config.get(configKey));
-                  }
-              }
-          } 
-
-          if(internalConfig.containsKey(JsonGenerator.PRETTY_PRINTING)) {
-              this.pretty = Boolean.TRUE.equals(internalConfig.get(JsonGenerator.PRETTY_PRINTING)) || "true".equals(internalConfig.get(JsonGenerator.PRETTY_PRINTING));
-          } else {
-              this.pretty = false;
-          }
+          this.pretty = getBool(JsonGenerator.PRETTY_PRINTING, false);
           
-          final int bufferSize = getInt(BUFFER_LENGTH);
+          final int bufferSize = getInt(GENERATOR_BUFFER_LENGTH, DEFAULT_GENERATOR_BUFFER_LENGTH);
           if (bufferSize <= 0) {
               throw new IllegalArgumentException("buffer length must be greater than zero");
           }
 
           this.bufferProvider = getBufferProvider().newCharProvider(bufferSize);
     }
-    
-    private BufferStrategy getBufferProvider() {
-        final Object name = internalConfig.get(JsonParserFactoryImpl.BUFFER_STRATEGY);
-        if (name != null) {
-            return BufferStrategy.valueOf(name.toString().toUpperCase(Locale.ENGLISH));
-        }
-        return BufferStrategy.QUEUE;
-    }
-
-    private int getInt(final String key) {
-        final Object maxStringSize = internalConfig.get(key);
-        if (maxStringSize == null) {
-            return DEFAULT_BUFFER_LENGTH;
-        } else if (Number.class.isInstance(maxStringSize)) {
-            return Number.class.cast(maxStringSize).intValue();
-        }
-        return Integer.parseInt(maxStringSize.toString());
-    }
 
     @Override
     public JsonGenerator createGenerator(final Writer writer) {
@@ -97,8 +65,6 @@ public class JsonGeneratorFactoryImpl implements JsonGeneratorFactory, Serializa
         return new JsonGeneratorImpl(writer, bufferProvider, cache);
     }
 
-   
-
     @Override
     public JsonGenerator createGenerator(final OutputStream out) {
         if (pretty) {

http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/94b896df/johnzon-core/src/main/java/org/apache/johnzon/core/JsonParserFactoryImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonParserFactoryImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonParserFactoryImpl.java
index 6ec69ce..6480ac8 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonParserFactoryImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonParserFactoryImpl.java
@@ -18,36 +18,31 @@
  */
 package org.apache.johnzon.core;
 
+import static java.util.Arrays.asList;
+
 import java.io.InputStream;
 import java.io.Reader;
-import java.io.Serializable;
 import java.nio.charset.Charset;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
-import java.util.Locale;
 import java.util.Map;
-import java.util.logging.Logger;
 
 import javax.json.JsonArray;
 import javax.json.JsonObject;
 import javax.json.stream.JsonParser;
 import javax.json.stream.JsonParserFactory;
 
-import static java.util.Arrays.asList;
-
-class JsonParserFactoryImpl implements JsonParserFactory, Serializable {
-    private static final Logger LOGGER = Logger.getLogger(JsonParserFactoryImpl.class.getName());
-
-    public static final String BUFFER_STRATEGY = "org.apache.johnzon.buffer-strategy";
+class JsonParserFactoryImpl extends AbstractJsonFactory implements JsonParserFactory {
     public static final String MAX_STRING_LENGTH = "org.apache.johnzon.max-string-length";
+    public static final int DEFAULT_MAX_STRING_LENGTH = Integer.getInteger(MAX_STRING_LENGTH, 10 * 1024 * 1024); //10m
+    
     public static final String BUFFER_LENGTH = "org.apache.johnzon.default-char-buffer";
+    public static final int DEFAULT_BUFFER_LENGTH = Integer.getInteger(BUFFER_LENGTH, 64 * 1024); //64k
+    
     public static final String SUPPORTS_COMMENTS = "org.apache.johnzon.supports-comments";
-    public static final int DEFAULT_MAX_SIZE = Integer.getInteger(MAX_STRING_LENGTH, 8192*32); //TODO check default string length/buffer size
-    public static final boolean DEFAULT_SUPPORTS_COMMENT = false;
+    public static final boolean DEFAULT_SUPPORTS_COMMENT = Boolean.getBoolean(SUPPORTS_COMMENTS); //default is false;
 
-    private final Map<String, Object> internalConfig = new HashMap<String, Object>();
-    private static final Collection<String> SUPPORTED_CONFIG_KEYS = asList(
+    static final Collection<String> SUPPORTED_CONFIG_KEYS = asList(
         BUFFER_STRATEGY, MAX_STRING_LENGTH, BUFFER_LENGTH, SUPPORTS_COMMENTS
     );
       
@@ -57,54 +52,17 @@ class JsonParserFactoryImpl implements JsonParserFactory, Serializable {
     private final boolean supportsComments;
 
     JsonParserFactoryImpl(final Map<String, ?> config) {
-        if(config != null) {
-            for (String configKey : config.keySet()) {
-                if(SUPPORTED_CONFIG_KEYS.contains(configKey)) {
-                    internalConfig.put(configKey, config.get(configKey));
-                } else {
-                    LOGGER.warning(configKey + " is not supported by " + getClass().getName());
-                }
-            }
-        } 
-        
-
-        final int bufferSize = getInt(BUFFER_LENGTH);
+        super(config, SUPPORTED_CONFIG_KEYS, null);
+
+        final int bufferSize = getInt(BUFFER_LENGTH, DEFAULT_BUFFER_LENGTH);
         if (bufferSize <= 0) {
             throw new IllegalArgumentException("buffer length must be greater than zero");
         }
 
-        this.maxSize = getInt(MAX_STRING_LENGTH);
+        this.maxSize = getInt(MAX_STRING_LENGTH, DEFAULT_MAX_STRING_LENGTH);
         this.bufferProvider = getBufferProvider().newCharProvider(bufferSize);
         this.valueBufferProvider = getBufferProvider().newCharProvider(maxSize);
-        this.supportsComments = getBool(SUPPORTS_COMMENTS);
-    }
-
-    private BufferStrategy getBufferProvider() {
-        final Object name = internalConfig.get(BUFFER_STRATEGY);
-        if (name != null) {
-            return BufferStrategy.valueOf(name.toString().toUpperCase(Locale.ENGLISH));
-        }
-        return BufferStrategy.QUEUE;
-    }
-
-    private int getInt(final String key) {
-        final Object maxStringSize = internalConfig.get(key);
-        if (maxStringSize == null) {
-            return DEFAULT_MAX_SIZE;
-        } else if (Number.class.isInstance(maxStringSize)) {
-            return Number.class.cast(maxStringSize).intValue();
-        }
-        return Integer.parseInt(maxStringSize.toString());
-    }
-
-    private boolean getBool(final String key) {
-        final Object maxStringSize = internalConfig.get(key);
-        if (maxStringSize == null) {
-            return DEFAULT_SUPPORTS_COMMENT;
-        } else if (Boolean.class.isInstance(maxStringSize)) {
-            return Boolean.class.cast(maxStringSize);
-        }
-        return Boolean.parseBoolean(maxStringSize.toString());
+        this.supportsComments = getBool(SUPPORTS_COMMENTS, DEFAULT_SUPPORTS_COMMENT);
     }
 
     private JsonParser getDefaultJsonParserImpl(final InputStream in) {

http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/94b896df/johnzon-core/src/main/java/org/apache/johnzon/core/JsonReaderFactoryImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonReaderFactoryImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonReaderFactoryImpl.java
index f79e23d..98484b1 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonReaderFactoryImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonReaderFactoryImpl.java
@@ -18,49 +18,27 @@
  */
 package org.apache.johnzon.core;
 
+import static java.util.Arrays.asList;
+
 import java.io.InputStream;
 import java.io.Reader;
-import java.io.Serializable;
 import java.nio.charset.Charset;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.Map;
-import java.util.logging.Logger;
 
 import javax.json.JsonReader;
 import javax.json.JsonReaderFactory;
 
-import static java.util.Arrays.asList;
+class JsonReaderFactoryImpl extends AbstractJsonFactory implements JsonReaderFactory {
+    static final Collection<String> SUPPORTED_CONFIG_KEYS = asList(
 
-class JsonReaderFactoryImpl implements JsonReaderFactory, Serializable {
-    private static final Logger LOGGER = Logger.getLogger(JsonReaderFactoryImpl.class.getName());
-
-    private final Map<String, Object> internalConfig = new HashMap<String, Object>();
-    private static final Collection<String> SUPPORTED_CONFIG_KEYS = asList(
-        JsonParserFactoryImpl.BUFFER_STRATEGY, JsonParserFactoryImpl.MAX_STRING_LENGTH,
-        JsonParserFactoryImpl.BUFFER_LENGTH, JsonParserFactoryImpl.SUPPORTS_COMMENTS
     );
     private final JsonParserFactoryImpl parserFactory;
 
     JsonReaderFactoryImpl(final Map<String, ?> config) {
-
-        if(config != null) {
-            
-            for (String configKey : config.keySet()) {
-                if(SUPPORTED_CONFIG_KEYS.contains(configKey)) {
-                    internalConfig.put(configKey, config.get(configKey));
-                } else {
-                    LOGGER.warning(configKey + " not supported by " + getClass().getName());
-                }
-            }
-            
-            this.parserFactory = new JsonParserFactoryImpl(internalConfig);
-            
-        } else {
-            this.parserFactory = new JsonParserFactoryImpl(null);
-        }
-        
+        super(config, SUPPORTED_CONFIG_KEYS, JsonParserFactoryImpl.SUPPORTED_CONFIG_KEYS);
+        this.parserFactory = new JsonParserFactoryImpl(internalConfig);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-johnzon/blob/94b896df/johnzon-core/src/main/java/org/apache/johnzon/core/JsonWriterFactoryImpl.java
----------------------------------------------------------------------
diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonWriterFactoryImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonWriterFactoryImpl.java
index 92c9df8..84f3df2 100644
--- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonWriterFactoryImpl.java
+++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonWriterFactoryImpl.java
@@ -18,50 +18,30 @@
  */
 package org.apache.johnzon.core;
 
+import static java.util.Arrays.asList;
+
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
-import java.io.Serializable;
 import java.io.Writer;
 import java.nio.charset.Charset;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.Map;
-import java.util.logging.Logger;
 
 import javax.json.JsonWriter;
 import javax.json.JsonWriterFactory;
-import javax.json.stream.JsonGenerator;
 import javax.json.stream.JsonGeneratorFactory;
 
-import static java.util.Arrays.asList;
-
-class JsonWriterFactoryImpl implements JsonWriterFactory, Serializable {
-    private static final Logger LOGGER = Logger.getLogger(JsonWriterFactoryImpl.class.getName());
-
+class JsonWriterFactoryImpl extends AbstractJsonFactory implements JsonWriterFactory{
     private static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
-    private final Map<String, Object> internalConfig = new HashMap<String, Object>();
-    private static final Collection<String> SUPPORTED_CONFIG_KEYS = asList(
-        JsonGenerator.PRETTY_PRINTING
+    static final Collection<String> SUPPORTED_CONFIG_KEYS = asList(
+
     );
     private final JsonGeneratorFactory factory;
 
     JsonWriterFactoryImpl(final Map<String, ?> config) {
-        if (config != null) {
-
-            for (final String configKey : config.keySet()) {
-                if (SUPPORTED_CONFIG_KEYS.contains(configKey)) {
-                    internalConfig.put(configKey, config.get(configKey));
-                } else {
-                    LOGGER.warning(configKey + " not supported by " + getClass().getName());
-                }
-            }
-
-            this.factory = new JsonGeneratorFactoryImpl(internalConfig);
-        } else {
-            this.factory = new JsonGeneratorFactoryImpl(null);
-        }
-
+        super(config, SUPPORTED_CONFIG_KEYS, JsonGeneratorFactoryImpl.SUPPORTED_CONFIG_KEYS);
+        this.factory = new JsonGeneratorFactoryImpl(internalConfig);
     }
 
     @Override