felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1775128 - in /felix/trunk/tools/org.apache.felix.scr.generator/src: main/java/org/apache/felix/scrplugin/helper/ main/java/org/apache/felix/scrplugin/xml/ test/java/org/apache/felix/scrplugin/
Date Mon, 19 Dec 2016 15:44:39 GMT
Author: cziegeler
Date: Mon Dec 19 15:44:39 2016
New Revision: 1775128

URL: http://svn.apache.org/viewvc?rev=1775128&view=rev
Log:
SLING-5451 : Input/Output streams not properly closed in org.apache.felix.scr.generator. Apply
patch from Stefan Seifert

Modified:
    felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/helper/ClassModifier.java
    felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java
    felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/IOUtils.java
    felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/MetaTypeIO.java
    felix/trunk/tools/org.apache.felix.scr.generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java

Modified: felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/helper/ClassModifier.java
URL: http://svn.apache.org/viewvc/felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/helper/ClassModifier.java?rev=1775128&r1=1775127&r2=1775128&view=diff
==============================================================================
--- felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/helper/ClassModifier.java
(original)
+++ felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/helper/ClassModifier.java
Mon Dec 19 15:44:39 2016
@@ -62,8 +62,14 @@ public abstract class ClassModifier {
         final String fileName = outputDirectory + File.separatorChar +  className.replace('.',
File.separatorChar) + ".class";
         final ClassNode cn = new ClassNode();
         try {
-            final ClassReader reader = new ClassReader(new FileInputStream(fileName));
-            reader.accept(cn, 0);
+            final FileInputStream fis = new FileInputStream(fileName);
+            try {
+                final ClassReader reader = new ClassReader(fis);
+                reader.accept(cn, 0);
+            }
+            finally {
+                fis.close();
+            }
 
             // For target Java7 and above use: ClassWriter.COMPUTE_MAXS  | ClassWriter.COMPUTE_FRAMES
             final int mask = (cn.version > 50 ? ClassWriter.COMPUTE_MAXS  | ClassWriter.COMPUTE_FRAMES
: 0);
@@ -108,8 +114,12 @@ public abstract class ClassModifier {
             }
 
             final FileOutputStream fos = new FileOutputStream(fileName);
-            fos.write(writer.toByteArray());
-            fos.close();
+            try {
+                fos.write(writer.toByteArray());
+            }
+            finally {
+                fos.close();
+            }
         } catch (final Exception e) {
             throw new SCRDescriptorException("Unable to add methods to " + className, typeName,
e);
         }

Modified: felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java
URL: http://svn.apache.org/viewvc/felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java?rev=1775128&r1=1775127&r2=1775128&view=diff
==============================================================================
--- felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java
(original)
+++ felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/ComponentDescriptorIO.java
Mon Dec 19 15:44:39 2016
@@ -19,6 +19,7 @@
 package org.apache.felix.scrplugin.xml;
 
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -186,32 +187,39 @@ public class ComponentDescriptorIO {
             final Log logger) throws SAXException, IOException, TransformerException {
         logger.info("Writing " + components.size() + " Service Component Descriptors to "
                 + descriptorFile);
-        final ContentHandler contentHandler = IOUtils.getSerializer(descriptorFile);
-        // detect namespace to use
-        final String namespace = module.getOptions().getSpecVersion().getNamespaceUrl();
-
-        contentHandler.startDocument();
-        contentHandler.startPrefixMapping(PREFIX, namespace);
-
-        IOUtils.newline(contentHandler);
-        // wrapper element to generate well formed xml if 0 or more than 1 component
-        int startIndent = 0;
-        if ( components.size() != 1 ) {
-            contentHandler.startElement("", ComponentDescriptorIO.COMPONENTS, ComponentDescriptorIO.COMPONENTS,
new AttributesImpl());
+        
+        FileOutputStream fos = new FileOutputStream(descriptorFile);
+        try {
+            final ContentHandler contentHandler = IOUtils.getSerializer(fos);
+            // detect namespace to use
+            final String namespace = module.getOptions().getSpecVersion().getNamespaceUrl();
+    
+            contentHandler.startDocument();
+            contentHandler.startPrefixMapping(PREFIX, namespace);
+    
             IOUtils.newline(contentHandler);
-            startIndent = 1;
-        }
-        for (final ComponentContainer component : components) {
-            generateXML(namespace, module, component, contentHandler, startIndent);
+            // wrapper element to generate well formed xml if 0 or more than 1 component
+            int startIndent = 0;
+            if ( components.size() != 1 ) {
+                contentHandler.startElement("", ComponentDescriptorIO.COMPONENTS, ComponentDescriptorIO.COMPONENTS,
new AttributesImpl());
+                IOUtils.newline(contentHandler);
+                startIndent = 1;
+            }
+            for (final ComponentContainer component : components) {
+                generateXML(namespace, module, component, contentHandler, startIndent);
+            }
+    
+            // end wrapper element
+            if ( components.size() != 1 ) {
+                contentHandler.endElement("", ComponentDescriptorIO.COMPONENTS, ComponentDescriptorIO.COMPONENTS);
+                IOUtils.newline(contentHandler);
+            }
+            contentHandler.endPrefixMapping(PREFIX);
+            contentHandler.endDocument();
         }
-
-        // end wrapper element
-        if ( components.size() != 1 ) {
-            contentHandler.endElement("", ComponentDescriptorIO.COMPONENTS, ComponentDescriptorIO.COMPONENTS);
-            IOUtils.newline(contentHandler);
+        finally {
+            fos.close();
         }
-        contentHandler.endPrefixMapping(PREFIX);
-        contentHandler.endDocument();
     }
 
     /**

Modified: felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/IOUtils.java
URL: http://svn.apache.org/viewvc/felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/IOUtils.java?rev=1775128&r1=1775127&r2=1775128&view=diff
==============================================================================
--- felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/IOUtils.java
(original)
+++ felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/IOUtils.java
Mon Dec 19 15:44:39 2016
@@ -18,10 +18,9 @@
  */
 package org.apache.felix.scrplugin.xml;
 
-import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Writer;
 import java.util.Properties;
@@ -63,9 +62,9 @@ public class IOUtils {
     /**
      * Get a serializer to write XML to a file.
      */
-    public static ContentHandler getSerializer(final File file)
+    public static ContentHandler getSerializer(final OutputStream outputStream)
     throws TransformerException, IOException {
-        final Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
+        final Writer writer = new OutputStreamWriter(outputStream, "UTF-8");
 
         final TransformerHandler transformerHandler = FACTORY.newTransformerHandler();
         final Transformer transformer = transformerHandler.getTransformer();

Modified: felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/MetaTypeIO.java
URL: http://svn.apache.org/viewvc/felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/MetaTypeIO.java?rev=1775128&r1=1775127&r2=1775128&view=diff
==============================================================================
--- felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/MetaTypeIO.java
(original)
+++ felix/trunk/tools/org.apache.felix.scr.generator/src/main/java/org/apache/felix/scrplugin/xml/MetaTypeIO.java
Mon Dec 19 15:44:39 2016
@@ -192,8 +192,15 @@ public class MetaTypeIO {
                         final String baseName = useFile.getName().substring(0, lastDot);
                         final File propsFile = new File(useFile.getParentFile(), baseName
+ ".properties");
                         try {
-                            metatypeProps.store(new FileOutputStream(propsFile), null);
-                        } catch (IOException e) {
+                            final FileOutputStream fos = new FileOutputStream(propsFile);
+                            try {
+                                metatypeProps.store(fos, null);
+                            }
+                            finally {
+                                fos.close();
+                            }
+                        }
+                        catch (IOException e) {
                             throw new SCRDescriptorException("Unable to get metatype.properties",
propsFile.getAbsolutePath());
                         }
                         fileNames.add(parentDir.getName() + '/' + mtDir.getName() + '/' +
propsFile.getName());
@@ -253,29 +260,35 @@ public class MetaTypeIO {
         final String namespace = detectMetatypeVersion(metaData);
 
         try {
-            final ContentHandler contentHandler = IOUtils.getSerializer(file);
-
-            contentHandler.startDocument();
-            contentHandler.startPrefixMapping(PREFIX, namespace);
-
-            final AttributesImpl ai = new AttributesImpl();
-            IOUtils.addAttribute(ai, "localization", localization);
-
-            contentHandler.startElement(namespace, METADATA_ELEMENT, METADATA_ELEMENT_QNAME,
ai);
-            IOUtils.newline(contentHandler);
-
-            for(final ComponentContainer comp : components) {
-                if ( comp.getMetatypeContainer() != null ) {
-                    generateOCDXML(comp.getMetatypeContainer(), contentHandler);
-                    generateDesignateXML(comp.getMetatypeContainer(), contentHandler);
+            FileOutputStream fos = new FileOutputStream(file);
+            try {
+                final ContentHandler contentHandler = IOUtils.getSerializer(fos);
+    
+                contentHandler.startDocument();
+                contentHandler.startPrefixMapping(PREFIX, namespace);
+    
+                final AttributesImpl ai = new AttributesImpl();
+                IOUtils.addAttribute(ai, "localization", localization);
+    
+                contentHandler.startElement(namespace, METADATA_ELEMENT, METADATA_ELEMENT_QNAME,
ai);
+                IOUtils.newline(contentHandler);
+    
+                for(final ComponentContainer comp : components) {
+                    if ( comp.getMetatypeContainer() != null ) {
+                        generateOCDXML(comp.getMetatypeContainer(), contentHandler);
+                        generateDesignateXML(comp.getMetatypeContainer(), contentHandler);
+                    }
                 }
+    
+                // end wrapper element
+                contentHandler.endElement(namespace, METADATA_ELEMENT, METADATA_ELEMENT_QNAME);
+                IOUtils.newline(contentHandler);
+                contentHandler.endPrefixMapping(PREFIX);
+                contentHandler.endDocument();
+            }
+            finally {
+                fos.close();
             }
-
-            // end wrapper element
-            contentHandler.endElement(namespace, METADATA_ELEMENT, METADATA_ELEMENT_QNAME);
-            IOUtils.newline(contentHandler);
-            contentHandler.endPrefixMapping(PREFIX);
-            contentHandler.endDocument();
         } catch (final IOException e) {
             throw new SCRDescriptorException("Unable to generate xml", file.toString(), e);
         } catch (final TransformerException e) {

Modified: felix/trunk/tools/org.apache.felix.scr.generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/tools/org.apache.felix.scr.generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java?rev=1775128&r1=1775127&r2=1775128&view=diff
==============================================================================
--- felix/trunk/tools/org.apache.felix.scr.generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java
(original)
+++ felix/trunk/tools/org.apache.felix.scr.generator/src/test/java/org/apache/felix/scrplugin/SCRDescriptorGeneratorTest.java
Mon Dec 19 15:44:39 2016
@@ -21,6 +21,7 @@ package org.apache.felix.scrplugin;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Collections;
 import java.util.UUID;
 
@@ -72,7 +73,15 @@ public class SCRDescriptorGeneratorTest
     }
 
     private void unpackSource(String resource, File dest) throws IOException {
-        IOUtils.copy(getClass().getResourceAsStream(resource), new FileOutputStream(dest));
+        InputStream is = getClass().getResourceAsStream(resource);
+        FileOutputStream fos = new FileOutputStream(dest);
+        try {
+            IOUtils.copy(is, fos);
+        }
+        finally {
+            fos.close();
+            is.close();
+        }
     }
 
     /**



Mime
View raw message