You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:16:34 UTC

[sling-maven-sling-plugin] 21/28: SLING-653 : Properly handle closing of file, json file might be a json array.

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag maven-sling-plugin-2.0.4-incubator
in repository https://gitbox.apache.org/repos/asf/sling-maven-sling-plugin.git

commit 4539ff17ce9719f874d3f4dc6f8926ce0eb52b07
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Sat Mar 28 16:14:47 2009 +0000

    SLING-653 : Properly handle closing of file, json file might be a json array.
    
    git-svn-id: https://svn.apache.org/repos/asf/incubator/sling/trunk/maven/maven-sling-plugin@759494 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/maven/bundlesupport/ValidationMojo.java  | 26 ++++++++++++++++------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java b/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
index 1990ebd..034ead8 100644
--- a/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
+++ b/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
@@ -28,6 +28,7 @@ import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
+import org.apache.sling.commons.json.JSONArray;
 import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.JSONObject;
 import org.codehaus.plexus.util.DirectoryScanner;
@@ -110,16 +111,27 @@ public class ValidationMojo extends AbstractMojo {
         final File file = new File(directory, fileName);
         if ( file.isFile() ) {
             if ( fileName.endsWith(".json") && !this.skipJson ) {
+                getLog().debug("Validation JSON file " + fileName);
+                FileInputStream fis = null;
+                String json = null;
                 try {
-                    final FileInputStream fis = new FileInputStream(file);
-                    getLog().debug("Validation JSON file " + fileName);
-                    final String json = IOUtils.toString(fis);
-                    new JSONObject(json);
-                    fis.close();
-                } catch (JSONException e) {
-                    throw new MojoExecutionException("An Error occured while validating the file '"+fileName+"'", e);
+                    fis = new FileInputStream(file);
+                    json = IOUtils.toString(fis);
                 } catch (IOException e) {
                     throw new MojoExecutionException("An Error occured while validating the file '"+fileName+"'", e);
+                } finally {
+                    IOUtils.closeQuietly(fis);
+                }
+                // first, let's see if this is a json array
+                try {
+                    new JSONArray(json);
+                } catch (JSONException e) {
+                    // it might be a json object
+                    try {
+                        new JSONObject(json);
+                    } catch (JSONException je) {
+                        throw new MojoExecutionException("An Error occured while validating the file '"+fileName+"'", je);
+                    }
                 }
             }
         }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.