Skip to content

Commit

Permalink
Apply try-with-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
rfscholte committed Apr 26, 2020
1 parent 0a1aaad commit cfe0355
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.jar.Manifest;
import org.codehaus.plexus.archiver.jar.ManifestException;
import org.codehaus.plexus.util.IOUtil;

import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -82,14 +81,10 @@ public void finalizeArchiveCreation( final Archiver archiver )

if ( manifestFile != null )
{
Reader manifestFileReader = null;
try
try ( Reader manifestFileReader =
new InputStreamReader( new FileInputStream( manifestFile ), StandardCharsets.UTF_8 ) )
{
manifestFileReader = new InputStreamReader( new FileInputStream( manifestFile ),
StandardCharsets.UTF_8 );
manifest = new Manifest( manifestFileReader );
manifestFileReader.close();
manifestFileReader = null;
}
catch ( final FileNotFoundException e )
{
Expand All @@ -99,10 +94,6 @@ public void finalizeArchiveCreation( final Archiver archiver )
{
throw new ArchiverException( "Error processing manifest: " + e.getMessage(), e );
}
finally
{
IOUtil.close( manifestFileReader );
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.codehaus.plexus.archiver.ResourceIterator;
import org.codehaus.plexus.archiver.UnArchiver;
import org.codehaus.plexus.components.io.fileselectors.FileInfo;
import org.codehaus.plexus.util.IOUtil;

import javax.annotation.Nonnull;
import java.io.BufferedReader;
Expand Down Expand Up @@ -80,32 +79,27 @@ void addToArchive( final Archiver archiver )
final String name = entry.getKey();
final String fname = new File( name ).getName();

PrintWriter writer = null;
File f;
try
{
f = File.createTempFile( "assembly-" + fname, ".tmp" );
f.deleteOnExit();

writer = new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), getEncoding() ) );
for ( final String line : entry.getValue() )
try ( PrintWriter writer =
new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), getEncoding() ) ) )
{
writer.println( line );
for ( final String line : entry.getValue() )
{
writer.println( line );
}
}

writer.close();
writer = null;
}
catch ( final IOException e )
{
throw new ArchiverException(
"Error adding aggregated content for: " + fname + " to finalize archive creation. Reason: "
+ e.getMessage(), e );
}
finally
{
IOUtil.close( writer );
}

excludeOverride = true;
archiver.addFile( f, name );
Expand Down Expand Up @@ -157,25 +151,16 @@ public boolean isSelected( @Nonnull final FileInfo fileInfo )
void readLines( final FileInfo fileInfo, final List<String> lines )
throws IOException
{
BufferedReader reader = null;
try
try ( BufferedReader reader =
new BufferedReader( new InputStreamReader( fileInfo.getContents(), getEncoding() ) ) )
{
reader = new BufferedReader( new InputStreamReader( fileInfo.getContents(), getEncoding() ) );

for ( String line = reader.readLine(); line != null; line = reader.readLine() )
{
if ( !lines.contains( line ) )
{
lines.add( line );
}
}

reader.close();
reader = null;
}
finally
{
IOUtil.close( reader );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.codehaus.plexus.archiver.UnArchiver;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.components.io.fileselectors.FileInfo;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.Xpp3DomWriter;
Expand Down Expand Up @@ -107,10 +106,9 @@ private void addToArchive( final Archiver archiver )
final File f = File.createTempFile( "maven-assembly-plugin", "tmp" );
f.deleteOnExit();

Writer fileWriter = null;
try

try ( Writer fileWriter = WriterFactory.newXmlWriter( new FileOutputStream( f ) ) )
{
fileWriter = WriterFactory.newXmlWriter( new FileOutputStream( f ) );
final Xpp3Dom dom = new Xpp3Dom( "component-set" );
final Xpp3Dom componentDom = new Xpp3Dom( "components" );
dom.addChild( componentDom );
Expand All @@ -121,13 +119,6 @@ private void addToArchive( final Archiver archiver )
}

Xpp3DomWriter.write( fileWriter, dom );

fileWriter.close();
fileWriter = null;
}
finally
{
IOUtil.close( fileWriter );
}

excludeOverride = true;
Expand Down Expand Up @@ -192,13 +183,9 @@ public boolean isSelected( @Nonnull final FileInfo fileInfo )

if ( ComponentsXmlArchiverFileFilter.COMPONENTS_XML_PATH.equals( entry ) )
{
Reader reader = null;
try
try ( Reader reader = new BufferedReader( ReaderFactory.newXmlReader( fileInfo.getContents() ) ) )
{
reader = new BufferedReader( ReaderFactory.newXmlReader( fileInfo.getContents() ) );
addComponentsXml( reader );
reader.close();
reader = null;
}
catch ( final XmlPullParserException e )
{
Expand All @@ -207,11 +194,6 @@ public boolean isSelected( @Nonnull final FileInfo fileInfo )

throw error;
}
finally
{
IOUtil.close( reader );
}

return false;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import javax.annotation.Nonnull;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -103,44 +104,43 @@ public void finalizeArchiveCreation( final Archiver archiver )
private File writePropertiesFile()
{
File f;

Writer writer = null;
try
{
f = File.createTempFile( "maven-assembly-plugin", "tmp" );
f.deleteOnExit();

writer = AssemblyFileUtils.isPropertyFile( f )
? new OutputStreamWriter( new FileOutputStream( f ), StandardCharsets.ISO_8859_1 )
: new OutputStreamWriter( new FileOutputStream( f ) ); // Still platform encoding

writer.write( commentChars + " Aggregated on " + new Date() + " from: " );

for ( final String filename : filenames )
try ( Writer writer = getWriter( f ) )
{
writer.write( "\n" + commentChars + " " + filename );
}

writer.write( "\n\n" );
writer.write( commentChars + " Aggregated on " + new Date() + " from: " );

writer.write( aggregateWriter.toString() );
for ( final String filename : filenames )
{
writer.write( "\n" + commentChars + " " + filename );
}

writer.close();
writer = null;
writer.write( "\n\n" );
writer.write( aggregateWriter.toString() );
}
}
catch ( final IOException e )
{
throw new ArchiverException(
"Error adding aggregated properties to finalize archive creation. Reason: " + e.getMessage(), e );
}
finally
{
IOUtil.close( writer );
}

return f;
}

private Writer getWriter( File f )
throws FileNotFoundException
{
Writer writer;
writer = AssemblyFileUtils.isPropertyFile( f )
? new OutputStreamWriter( new FileOutputStream( f ), StandardCharsets.ISO_8859_1 )
: new OutputStreamWriter( new FileOutputStream( f ) ); // Still platform encoding
return writer;
}

@Override
public void finalizeArchiveExtraction( final UnArchiver unarchiver )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* under the License.
*/

import org.apache.commons.io.IOUtils;
import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
import org.apache.maven.plugins.assembly.interpolation.AssemblyExpressionEvaluator;
Expand Down Expand Up @@ -210,24 +209,16 @@ private Assembly addAssemblyForDescriptorReference( final String ref,
}
}

Reader reader = null;
try
try ( Reader reader = ReaderFactory.newXmlReader( resourceAsStream ) )
{
reader = ReaderFactory.newXmlReader( resourceAsStream );
final Assembly assembly = readAssembly( reader, ref, null, configSource );
reader.close();
reader = null;
assemblies.add( assembly );
return assembly;
}
catch ( final IOException e )
{
throw new AssemblyReadException( "Problem with descriptor with ID '" + ref + "'", e );
}
finally
{
IOUtils.closeQuietly( reader );
}
}

private Assembly addAssemblyFromDescriptorFile( final File descriptor,
Expand All @@ -249,17 +240,11 @@ private Assembly addAssemblyFromDescriptorFile( final File descriptor,
}
}

Reader r = null;
try
try ( Reader r = ReaderFactory.newXmlReader( descriptor ) )
{
r = ReaderFactory.newXmlReader( descriptor );

final Assembly assembly =
readAssembly( r, descriptor.getAbsolutePath(), descriptor.getParentFile(), configSource );

r.close();
r = null;

assemblies.add( assembly );

return assembly;
Expand All @@ -268,10 +253,6 @@ private Assembly addAssemblyFromDescriptorFile( final File descriptor,
{
throw new AssemblyReadException( "Error reading assembly descriptor: " + descriptor, e );
}
finally
{
IOUtil.close( r );
}
}

private Assembly addAssemblyFromDescriptor( final String spec, final Locator locator,
Expand All @@ -296,11 +277,9 @@ private Assembly addAssemblyFromDescriptor( final String spec, final Locator loc
}
}

Reader r = null;
try

try ( Reader r = ReaderFactory.newXmlReader( location.getInputStream() ) )
{
r = ReaderFactory.newXmlReader( location.getInputStream() );

File dir = null;
if ( location.getFile() != null )
{
Expand All @@ -309,9 +288,6 @@ private Assembly addAssemblyFromDescriptor( final String spec, final Locator loc

final Assembly assembly = readAssembly( r, spec, dir, configSource );

r.close();
r = null;

assemblies.add( assembly );

return assembly;
Expand All @@ -320,11 +296,6 @@ private Assembly addAssemblyFromDescriptor( final String spec, final Locator loc
{
throw new AssemblyReadException( "Error reading assembly descriptor: " + spec, e );
}
finally
{
IOUtil.close( r );
}

}

public Assembly readAssembly( Reader reader, final String locationDescription, final File assemblyDir,
Expand Down Expand Up @@ -443,21 +414,15 @@ protected void mergeComponentsWithMainAssembly( final Assembly assembly, final F
}

Component component = null;
Reader reader = null;
try
try ( Reader reader = new InputStreamReader( resolvedLocation.getInputStream() ) )
{
reader = new InputStreamReader( resolvedLocation.getInputStream() );
component = new ComponentXpp3Reader( transformer ).read( reader );
}
catch ( final IOException | XmlPullParserException e )
{
throw new AssemblyReadException( "Error reading component descriptor: " + location + " (resolved to: "
+ resolvedLocation.getSpecification() + ")", e );
}
finally
{
IOUtil.close( reader );
}

mergeComponentWithAssembly( component, assembly );
}
Expand Down
Loading

0 comments on commit cfe0355

Please sign in to comment.