camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zregv...@apache.org
Subject camel git commit: CAMEL-11112 Make sure streams are closed in cam...
Date Wed, 05 Apr 2017 13:08:58 GMT
Repository: camel
Updated Branches:
  refs/heads/master 037da91ea -> b1e7639f2


CAMEL-11112 Make sure streams are closed in cam...

...el-catalog

Using try-with-resources to make sure that `InputStream`s are closed,
also cleaned up the implementation a bit by refactoring common code,
replacing `while(true)` loops and removing unused imports.


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

Branch: refs/heads/master
Commit: b1e7639f228f3fd6877e43a6e6ec95a81e2fa4a1
Parents: 037da91
Author: Zoran Regvart <zregvart@apache.org>
Authored: Wed Apr 5 13:29:40 2017 +0200
Committer: Zoran Regvart <zregvart@apache.org>
Committed: Wed Apr 5 15:08:13 2017 +0200

----------------------------------------------------------------------
 .../camel/catalog/AbstractCamelCatalog.java     |   2 -
 .../org/apache/camel/catalog/CatalogHelper.java |  38 ++---
 .../camel/catalog/AbstractCamelCatalog.java     |   2 -
 .../catalog/CamelCatalogJSonSchemaResolver.java | 150 +++++++------------
 .../org/apache/camel/catalog/CatalogHelper.java |  38 ++---
 5 files changed, 75 insertions(+), 155 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b1e7639f/camel-core/src/main/java/org/apache/camel/catalog/AbstractCamelCatalog.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/catalog/AbstractCamelCatalog.java b/camel-core/src/main/java/org/apache/camel/catalog/AbstractCamelCatalog.java
index 50c5fa3..6c1e3d4 100644
--- a/camel-core/src/main/java/org/apache/camel/catalog/AbstractCamelCatalog.java
+++ b/camel-core/src/main/java/org/apache/camel/catalog/AbstractCamelCatalog.java
@@ -26,7 +26,6 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -34,7 +33,6 @@ import java.util.Set;
 import java.util.TreeMap;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import java.util.stream.Collectors;
 
 import static org.apache.camel.catalog.CatalogHelper.after;
 import static org.apache.camel.catalog.JSonSchemaHelper.getNames;

http://git-wip-us.apache.org/repos/asf/camel/blob/b1e7639f/camel-core/src/main/java/org/apache/camel/catalog/CatalogHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/catalog/CatalogHelper.java b/camel-core/src/main/java/org/apache/camel/catalog/CatalogHelper.java
index f7c0072..caba4a3 100644
--- a/camel-core/src/main/java/org/apache/camel/catalog/CatalogHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/catalog/CatalogHelper.java
@@ -37,20 +37,12 @@ public final class CatalogHelper {
      * Warning, don't use for crazy big streams :)
      */
     public static void loadLines(InputStream in, List<String> lines) throws IOException
{
-        InputStreamReader isr = new InputStreamReader(in);
-        try {
-            BufferedReader reader = new LineNumberReader(isr);
-            while (true) {
-                String line = reader.readLine();
-                if (line != null) {
-                    lines.add(line);
-                } else {
-                    break;
-                }
+        try (final InputStreamReader isr = new InputStreamReader(in);
+            final BufferedReader reader = new LineNumberReader(isr)) {
+            String line;
+            while ((line = reader.readLine()) != null) {
+                lines.add(line);
             }
-        } finally {
-            isr.close();
-            in.close();
         }
     }
 
@@ -64,22 +56,14 @@ public final class CatalogHelper {
      */
     public static String loadText(InputStream in) throws IOException {
         StringBuilder builder = new StringBuilder();
-        InputStreamReader isr = new InputStreamReader(in);
-        try {
-            BufferedReader reader = new LineNumberReader(isr);
-            while (true) {
-                String line = reader.readLine();
-                if (line != null) {
-                    builder.append(line);
-                    builder.append("\n");
-                } else {
-                    break;
-                }
+        try (final InputStreamReader isr = new InputStreamReader(in);
+            final BufferedReader reader = new LineNumberReader(isr)) {
+            String line;
+            while ((line = reader.readLine()) != null) {
+                builder.append(line);
+                builder.append("\n");
             }
             return builder.toString();
-        } finally {
-            isr.close();
-            in.close();
         }
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/b1e7639f/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/AbstractCamelCatalog.java
----------------------------------------------------------------------
diff --git a/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/AbstractCamelCatalog.java
b/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/AbstractCamelCatalog.java
index 50c5fa3..6c1e3d4 100644
--- a/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/AbstractCamelCatalog.java
+++ b/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/AbstractCamelCatalog.java
@@ -26,7 +26,6 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -34,7 +33,6 @@ import java.util.Set;
 import java.util.TreeMap;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import java.util.stream.Collectors;
 
 import static org.apache.camel.catalog.CatalogHelper.after;
 import static org.apache.camel.catalog.JSonSchemaHelper.getNames;

http://git-wip-us.apache.org/repos/asf/camel/blob/b1e7639f/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/CamelCatalogJSonSchemaResolver.java
----------------------------------------------------------------------
diff --git a/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/CamelCatalogJSonSchemaResolver.java
b/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/CamelCatalogJSonSchemaResolver.java
index 2823fe3..f552d0e 100644
--- a/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/CamelCatalogJSonSchemaResolver.java
+++ b/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/CamelCatalogJSonSchemaResolver.java
@@ -18,7 +18,6 @@ package org.apache.camel.catalog;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.HashMap;
 import java.util.Map;
 
 /**
@@ -48,78 +47,42 @@ public class CamelCatalogJSonSchemaResolver implements JSonSchemaResolver
{
 
     @Override
     public String getComponentJSonSchema(String name) {
-        String file = camelCatalog.getRuntimeProvider().getComponentJSonSchemaDirectory()
+ "/" + name + ".json";
-
-        String answer = null;
-        InputStream is = camelCatalog.getVersionManager().getResourceAsStream(file);
-        if (is != null) {
-            try {
-                answer = CatalogHelper.loadText(is);
-            } catch (IOException e) {
-                // ignore
-            }
+        final String file = camelCatalog.getRuntimeProvider().getComponentJSonSchemaDirectory()
+ "/" + name + ".json";
+
+        final String fromVersionManager = loadResourceFromVersionManager(file);
+        if (fromVersionManager != null) {
+            return fromVersionManager;
         }
-        if (answer == null) {
-            // its maybe a third party so try to see if we have the json schema already
-            answer = extraComponentsJSonSchema.get(name);
-            if (answer == null) {
-                // or if we can load it from the classpath
-                String className = extraComponents.get(name);
-                if (className != null) {
-                    String packageName = className.substring(0, className.lastIndexOf('.'));
-                    packageName = packageName.replace('.', '/');
-                    String path = packageName + "/" + name + ".json";
-                    is = camelCatalog.getVersionManager().getResourceAsStream(path);
-                    if (is != null) {
-                        try {
-                            answer = CatalogHelper.loadText(is);
-                        } catch (IOException e) {
-                            // ignore
-                        }
-                    }
-                }
-            }
+
+        // its maybe a third party so try to see if we have the json schema already
+        final String answer = extraComponentsJSonSchema.get(name);
+        if (answer != null) {
+            return answer;
         }
 
-        return answer;
+        // or if we can load it from the classpath
+        final String className = extraComponents.get(name);
+        return loadFromClasspath(className, name);
     }
 
     @Override
     public String getDataFormatJSonSchema(String name) {
-        String file = camelCatalog.getRuntimeProvider().getDataFormatJSonSchemaDirectory()
+ "/" + name + ".json";
-
-        String answer = null;
-        InputStream is = camelCatalog.getVersionManager().getResourceAsStream(file);
-        if (is != null) {
-            try {
-                answer = CatalogHelper.loadText(is);
-            } catch (IOException e) {
-                // ignore
-            }
+        final String file = camelCatalog.getRuntimeProvider().getDataFormatJSonSchemaDirectory()
+ "/" + name + ".json";
+
+        final String fromVersionManager = loadResourceFromVersionManager(file);
+        if (fromVersionManager != null) {
+            return fromVersionManager;
         }
-        if (answer == null) {
-            // its maybe a third party so try to see if we have the json schema already
-            answer = extraDataFormatsJSonSchema.get(name);
-            if (answer == null) {
-                // or if we can load it from the classpath
-                String className = extraDataFormats.get(name);
-                if (className != null) {
-                    String packageName = className.substring(0, className.lastIndexOf('.'));
-                    packageName = packageName.replace('.', '/');
-                    String path = packageName + "/" + name + ".json";
-                    is = camelCatalog.getVersionManager().getResourceAsStream(path);
-                    if (is != null) {
-                        try {
-                            answer = CatalogHelper.loadText(is);
-                        } catch (IOException e) {
-                            // ignore
-                        }
-                    }
-                }
-            }
+
+        // its maybe a third party so try to see if we have the json schema already
+        final String schema = extraDataFormatsJSonSchema.get(name);
+        if (schema != null) {
+            return schema;
         }
 
-        return answer;
+        // or if we can load it from the classpath
+        final String className = extraDataFormats.get(name);
+        return loadFromClasspath(className, name);
     }
 
     @Override
@@ -129,53 +92,46 @@ public class CamelCatalogJSonSchemaResolver implements JSonSchemaResolver
{
             name = "bean";
         }
 
-        String file = camelCatalog.getRuntimeProvider().getLanguageJSonSchemaDirectory()
+ "/" + name + ".json";
+        final String file = camelCatalog.getRuntimeProvider().getLanguageJSonSchemaDirectory()
+ "/" + name + ".json";
 
-        String answer = null;
-        InputStream is = camelCatalog.getVersionManager().getResourceAsStream(file);
-        if (is != null) {
-            try {
-                answer = CatalogHelper.loadText(is);
-            } catch (IOException e) {
-                // ignore
-            }
-        }
+        return loadResourceFromVersionManager(file);
+    }
 
-        return answer;
+    @Override
+    public String getModelJSonSchema(String name) {
+        final String file = MODEL_DIR + "/" + name + ".json";
+
+        return loadResourceFromVersionManager(file);
     }
 
     @Override
     public String getOtherJSonSchema(String name) {
-        String file = camelCatalog.getRuntimeProvider().getOtherJSonSchemaDirectory() + "/"
+ name + ".json";
-
-        String answer = null;
-        InputStream is = camelCatalog.getVersionManager().getResourceAsStream(file);
-        if (is != null) {
-            try {
-                answer = CatalogHelper.loadText(is);
-            } catch (IOException e) {
-                // ignore
-            }
-        }
+        final String file = camelCatalog.getRuntimeProvider().getOtherJSonSchemaDirectory()
+ "/" + name + ".json";
 
-        return answer;
+        return loadResourceFromVersionManager(file);
     }
 
-    @Override
-    public String getModelJSonSchema(String name) {
-        String file = MODEL_DIR + "/" + name + ".json";
+    String loadFromClasspath(final String className, final String fileName) {
+        if (className != null) {
+            String packageName = className.substring(0, className.lastIndexOf('.'));
+            packageName = packageName.replace('.', '/');
+            final String path = packageName + "/" + fileName + ".json";
 
-        String answer = null;
+            return loadResourceFromVersionManager(path);
+        }
+
+        return null;
+    }
 
-        InputStream is = camelCatalog.getVersionManager().getResourceAsStream(file);
-        if (is != null) {
-            try {
-                answer = CatalogHelper.loadText(is);
-            } catch (IOException e) {
-                // ignore
+    String loadResourceFromVersionManager(final String file) {
+        try (final InputStream is = camelCatalog.getVersionManager().getResourceAsStream(file))
{
+            if (is != null) {
+                return CatalogHelper.loadText(is);
             }
+        } catch (IOException e) {
+            // ignore
         }
 
-        return answer;
+        return null;
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/b1e7639f/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/CatalogHelper.java
----------------------------------------------------------------------
diff --git a/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/CatalogHelper.java
b/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/CatalogHelper.java
index f7c0072..caba4a3 100644
--- a/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/CatalogHelper.java
+++ b/platforms/camel-catalog/src/main/java/org/apache/camel/catalog/CatalogHelper.java
@@ -37,20 +37,12 @@ public final class CatalogHelper {
      * Warning, don't use for crazy big streams :)
      */
     public static void loadLines(InputStream in, List<String> lines) throws IOException
{
-        InputStreamReader isr = new InputStreamReader(in);
-        try {
-            BufferedReader reader = new LineNumberReader(isr);
-            while (true) {
-                String line = reader.readLine();
-                if (line != null) {
-                    lines.add(line);
-                } else {
-                    break;
-                }
+        try (final InputStreamReader isr = new InputStreamReader(in);
+            final BufferedReader reader = new LineNumberReader(isr)) {
+            String line;
+            while ((line = reader.readLine()) != null) {
+                lines.add(line);
             }
-        } finally {
-            isr.close();
-            in.close();
         }
     }
 
@@ -64,22 +56,14 @@ public final class CatalogHelper {
      */
     public static String loadText(InputStream in) throws IOException {
         StringBuilder builder = new StringBuilder();
-        InputStreamReader isr = new InputStreamReader(in);
-        try {
-            BufferedReader reader = new LineNumberReader(isr);
-            while (true) {
-                String line = reader.readLine();
-                if (line != null) {
-                    builder.append(line);
-                    builder.append("\n");
-                } else {
-                    break;
-                }
+        try (final InputStreamReader isr = new InputStreamReader(in);
+            final BufferedReader reader = new LineNumberReader(isr)) {
+            String line;
+            while ((line = reader.readLine()) != null) {
+                builder.append(line);
+                builder.append("\n");
             }
             return builder.toString();
-        } finally {
-            isr.close();
-            in.close();
         }
     }
 


Mime
View raw message