karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fla...@apache.org
Subject karaf git commit: [KARAF-4979] fixes resource leaks in UrlLoader & StaxParser
Date Mon, 06 Feb 2017 20:10:53 GMT
Repository: karaf
Updated Branches:
  refs/heads/master 868c68524 -> 264cd4e2a


[KARAF-4979] fixes resource leaks in UrlLoader & StaxParser


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

Branch: refs/heads/master
Commit: 264cd4e2ae9d8cb6b8410a597cc5ee981c374694
Parents: 868c685
Author: Fabian Lange <lange.fabian@gmail.com>
Authored: Mon Feb 6 21:09:44 2017 +0100
Committer: Fabian Lange <lange.fabian@gmail.com>
Committed: Mon Feb 6 21:09:53 2017 +0100

----------------------------------------------------------------------
 .../internal/repository/StaxParser.java         | 160 ++++++++++---------
 .../features/internal/repository/UrlLoader.java |  22 +--
 2 files changed, 97 insertions(+), 85 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/264cd4e2/features/core/src/main/java/org/apache/karaf/features/internal/repository/StaxParser.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/repository/StaxParser.java
b/features/core/src/main/java/org/apache/karaf/features/internal/repository/StaxParser.java
index 5359fb4..effbce3 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/repository/StaxParser.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/repository/StaxParser.java
@@ -89,33 +89,37 @@ public final class StaxParser {
 
     public static void write(XmlRepository repository, Writer os) throws XMLStreamException
{
         XMLStreamWriter writer = getOutputFactory().createXMLStreamWriter(os);
-        writer.writeStartDocument();
-        writer.setDefaultNamespace(REPOSITORY_NAMESPACE);
-        // repository element
-        writer.writeStartElement(REPOSITORY_NAMESPACE, REPOSITORY);
-        writer.writeAttribute("xmlns", REPOSITORY_NAMESPACE);
-        writer.writeAttribute(REPO_NAME, repository.name);
-        writer.writeAttribute(INCREMENT, Long.toString(repository.increment));
-        // referrals
-        for (Referral referral : repository.referrals) {
-            writer.writeStartElement(REPOSITORY_NAMESPACE, REFERRAL);
-            writer.writeAttribute(DEPTH, Integer.toString(referral.depth));
-            writer.writeAttribute(URL, referral.url);
-            writer.writeEndElement();
-        }
-        // resources
-        for (Resource resource : repository.resources) {
-            writer.writeStartElement(REPOSITORY_NAMESPACE, RESOURCE);
-            for (Capability cap : resource.getCapabilities(null)) {
-                writeClause(writer, CAPABILITY, cap.getNamespace(), cap.getDirectives(),
cap.getAttributes());
+        try {
+            writer.writeStartDocument();
+            writer.setDefaultNamespace(REPOSITORY_NAMESPACE);
+            // repository element
+            writer.writeStartElement(REPOSITORY_NAMESPACE, REPOSITORY);
+            writer.writeAttribute("xmlns", REPOSITORY_NAMESPACE);
+            writer.writeAttribute(REPO_NAME, repository.name);
+            writer.writeAttribute(INCREMENT, Long.toString(repository.increment));
+            // referrals
+            for (Referral referral : repository.referrals) {
+                writer.writeStartElement(REPOSITORY_NAMESPACE, REFERRAL);
+                writer.writeAttribute(DEPTH, Integer.toString(referral.depth));
+                writer.writeAttribute(URL, referral.url);
+                writer.writeEndElement();
             }
-            for (Requirement req : resource.getRequirements(null)) {
-                writeClause(writer, REQUIREMENT, req.getNamespace(), req.getDirectives(),
req.getAttributes());
+            // resources
+            for (Resource resource : repository.resources) {
+                writer.writeStartElement(REPOSITORY_NAMESPACE, RESOURCE);
+                for (Capability cap : resource.getCapabilities(null)) {
+                    writeClause(writer, CAPABILITY, cap.getNamespace(), cap.getDirectives(),
cap.getAttributes());
+                }
+                for (Requirement req : resource.getRequirements(null)) {
+                    writeClause(writer, REQUIREMENT, req.getNamespace(), req.getDirectives(),
req.getAttributes());
+                }
+                writer.writeEndElement();
             }
-            writer.writeEndElement();
+            writer.writeEndDocument();
+            writer.flush();
+        } finally {
+            writer.close();
         }
-        writer.writeEndDocument();
-        writer.flush();
     }
 
     private static void writeClause(XMLStreamWriter writer, String element, String namespace,
Map<String, String> directives, Map<String, Object> attributes) throws XMLStreamException
{
@@ -189,63 +193,67 @@ public final class StaxParser {
 
     public static XmlRepository parse(URI repositoryUrl, InputStream is, XmlRepository previous)
throws XMLStreamException {
         XMLStreamReader reader = getInputFactory().createXMLStreamReader(is);
-        int event = reader.nextTag();
-        if (event != START_ELEMENT || !REPOSITORY.equals(reader.getLocalName())) {
-            throw new IllegalStateException("Expected element 'repository' at the root of
the document");
-        }
-        XmlRepository repo = new XmlRepository();
-        for (int i = 0, nb = reader.getAttributeCount(); i < nb; i++) {
-            String attrName = reader.getAttributeLocalName(i);
-            String attrValue = reader.getAttributeValue(i);
-            switch (attrName) {
-            case REPO_NAME:
-                repo.name = attrValue;
-                break;
-            case INCREMENT:
-                repo.increment = Long.parseLong(attrValue);
-                break;
-            default:
-                throw new IllegalStateException("Unexpected attribute '" + attrName + "'");
+        try {
+            int event = reader.nextTag();
+            if (event != START_ELEMENT || !REPOSITORY.equals(reader.getLocalName())) {
+                throw new IllegalStateException("Expected element 'repository' at the root
of the document");
             }
-        }
-        if (previous != null && repo.increment == previous.increment) {
-            return previous;
-        }
-        while ((event = reader.nextTag()) == START_ELEMENT) {
-            String element = reader.getLocalName();
-            switch (element) {
-            case REFERRAL:
-                Referral referral = new Referral();
-                for (int i = 0, nb = reader.getAttributeCount(); i < nb; i++) {
-                    String attrName = reader.getAttributeLocalName(i);
-                    String attrValue = reader.getAttributeValue(i);
-                    switch (attrName) {
-                    case DEPTH:
-                        referral.depth = Integer.parseInt(attrValue);
-                        break;
-                    case URL:
-                        referral.url = attrValue;
-                        break;
-                    default:
-                        throw new IllegalStateException("Unexpected attribute '" + attrName
+ "'");
-                    }
+            XmlRepository repo = new XmlRepository();
+            for (int i = 0, nb = reader.getAttributeCount(); i < nb; i++) {
+                String attrName = reader.getAttributeLocalName(i);
+                String attrValue = reader.getAttributeValue(i);
+                switch (attrName) {
+                case REPO_NAME:
+                    repo.name = attrValue;
+                    break;
+                case INCREMENT:
+                    repo.increment = Long.parseLong(attrValue);
+                    break;
+                default:
+                    throw new IllegalStateException("Unexpected attribute '" + attrName +
"'");
                 }
-                if (referral.url == null) {
-                    throw new IllegalStateException("Expected attribute '" + URL + "'");
+            }
+            if (previous != null && repo.increment == previous.increment) {
+                return previous;
+            }
+            while ((event = reader.nextTag()) == START_ELEMENT) {
+                String element = reader.getLocalName();
+                switch (element) {
+                case REFERRAL:
+                    Referral referral = new Referral();
+                    for (int i = 0, nb = reader.getAttributeCount(); i < nb; i++) {
+                        String attrName = reader.getAttributeLocalName(i);
+                        String attrValue = reader.getAttributeValue(i);
+                        switch (attrName) {
+                        case DEPTH:
+                            referral.depth = Integer.parseInt(attrValue);
+                            break;
+                        case URL:
+                            referral.url = attrValue;
+                            break;
+                        default:
+                            throw new IllegalStateException("Unexpected attribute '" + attrName
+ "'");
+                        }
+                    }
+                    if (referral.url == null) {
+                        throw new IllegalStateException("Expected attribute '" + URL + "'");
+                    }
+                    repo.referrals.add(referral);
+                    sanityCheckEndElement(reader, reader.nextTag(), REFERRAL);
+                    break;
+                case RESOURCE:
+                    repo.resources.add(parseResource(repositoryUrl, reader));
+                    break;
+                default:
+                    throw new IllegalStateException("Unsupported element '" + element + "'.
Expected 'referral' or 'resource'");
                 }
-                repo.referrals.add(referral);
-                sanityCheckEndElement(reader, reader.nextTag(), REFERRAL);
-                break;
-            case RESOURCE:
-                repo.resources.add(parseResource(repositoryUrl, reader));
-                break;
-            default:
-                throw new IllegalStateException("Unsupported element '" + element + "'. Expected
'referral' or 'resource'");
             }
+            // Sanity check
+            sanityCheckEndElement(reader, event, REPOSITORY);
+            return repo;
+        } finally {
+            reader.close();
         }
-        // Sanity check
-        sanityCheckEndElement(reader, event, REPOSITORY);
-        return repo;
     }
 
     private static void sanityCheckEndElement(XMLStreamReader reader, int event, String element)
{

http://git-wip-us.apache.org/repos/asf/karaf/blob/264cd4e2/features/core/src/main/java/org/apache/karaf/features/internal/repository/UrlLoader.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/repository/UrlLoader.java
b/features/core/src/main/java/org/apache/karaf/features/internal/repository/UrlLoader.java
index ec0ab92..48552fb 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/repository/UrlLoader.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/repository/UrlLoader.java
@@ -76,22 +76,26 @@ public abstract class UrlLoader {
                 lastChecked = time;
                 return false;
             }
-            try (
-                    BufferedInputStream bis = new BufferedInputStream(connection.getInputStream())
-            ) {
+            InputStream is = null;
+            try {
+                is = new BufferedInputStream(connection.getInputStream());
                 // Auto-detect gzipped streams
-                InputStream is = bis;
-                bis.mark(512);
-                int b0 = bis.read();
-                int b1 = bis.read();
-                bis.reset();
+                is.mark(512);
+                int b0 = is.read();
+                int b1 = is.read();
+                is.reset();
                 if (b0 == 0x1f && b1 == 0x8b) {
-                    is = new GZIPInputStream(bis);
+                    is = new GZIPInputStream(is);
                 }
                 boolean r = doRead(is);
                 lastModified = lm;
                 lastChecked = time;
                 return r;
+            } finally {
+                // cannot be use try-with-resources, as it would not close GZIPInpuStream
+                if (is != null) {
+                    is.close();
+                }
             }
         } catch (IOException e) {
             throw new RuntimeException(e);


Mime
View raw message