Skip to content

Commit

Permalink
close resources and check for I/O operation success
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Sep 24, 2020
1 parent adea879 commit ce76c3e
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 262 deletions.
35 changes: 4 additions & 31 deletions src/main/java/org/apache/maven/plugins/ear/AbstractXmlWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@ abstract class AbstractXmlWriter

private final String encoding;

protected static final String MODULE_ELEMENT = "module";

protected static final String SERVICE_ELEMENT = "service";

AbstractXmlWriter( String encoding )
{
this.encoding = encoding;
}

protected Writer initializeWriter( final File destinationFile )
Writer initializeWriter( final File destinationFile )
throws EarPluginException
{
try
Expand All @@ -55,36 +51,13 @@ protected Writer initializeWriter( final File destinationFile )
}
catch ( IOException ex )
{
// CHECKSTYLE_OFF: LineLength
throw new EarPluginException( "Exception while opening file[" + destinationFile.getAbsolutePath() + "]", ex );
// CHECKSTYLE_ON: LineLength
throw new EarPluginException(
"Exception while opening file[" + destinationFile.getAbsolutePath() + "]", ex );
}
}

protected XMLWriter initializeXmlWriter( final Writer writer, final String docType )
XMLWriter initializeXmlWriter( final Writer writer, final String docType )
{
return new PrettyPrintXMLWriter( writer, encoding, docType );
}

protected void close( Writer closeable )
{
if ( closeable == null )
{
return;
}

try
{
closeable.close();
}
catch ( Exception e )
{
// TODO: warn
}
}

public String getEncoding()
{
return encoding;
}
}
188 changes: 94 additions & 94 deletions src/main/java/org/apache/maven/plugins/ear/ApplicationXmlWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
* under the License.
*/

import java.io.IOException;
import java.io.Writer;

import org.apache.maven.plugins.ear.util.JavaEEVersion;
import org.codehaus.plexus.util.xml.XMLWriter;

/**
* An <tt>XmlWriter</tt> based implementation used to generate an <tt>application.xml</tt> file
* An <code>XmlWriter</code> based implementation used to generate an <tt>application.xml</tt> file.
*
* @author <a href="snicoll@apache.org">Stephane Nicoll</a>
*/
Expand All @@ -51,103 +52,106 @@ final class ApplicationXmlWriter
this.generateModuleId = generateModuleId;
}

public void write( ApplicationXmlWriterContext context )
void write( ApplicationXmlWriterContext context )
throws EarPluginException
{
Writer w = initializeWriter( context.getDestinationFile() );

XMLWriter writer = null;
if ( JavaEEVersion.ONE_DOT_THREE.eq( version ) )
{
writer = initializeRootElementOneDotThree( w );
}
else if ( JavaEEVersion.ONE_DOT_FOUR.eq( version ) )
{
writer = initializeRootElementOneDotFour( w );
}
else if ( JavaEEVersion.FIVE.eq( version ) )
{
writer = initializeRootElementFive( w );
}
else if ( JavaEEVersion.SIX.eq( version ) )
{
writer = initializeRootElementSix( w );
}
else if ( JavaEEVersion.SEVEN.eq( version ) )
{
writer = initializeRootElementSeven( w );
}
else if ( JavaEEVersion.EIGHT.eq( version ) )
{
writer = initializeRootElementEight( w );
}

// writer is still on root element, so we can still add this attribute
if ( context.getApplicationId() != null )
{
writer.addAttribute( "id", context.getApplicationId() );
}

// As from JavaEE6
if ( version.ge( JavaEEVersion.SIX ) )
{
writeApplicationName( context.getApplicationName(), writer );
}

// IMPORTANT: the order of the description and display-name elements was
// reversed between J2EE 1.3 and J2EE 1.4.
if ( version.eq( JavaEEVersion.ONE_DOT_THREE ) )
{
writeDisplayName( context.getDisplayName(), writer );
writeDescription( context.getDescription(), writer );
}
else
{
writeDescription( context.getDescription(), writer );
writeDisplayName( context.getDisplayName(), writer );
}

// As from JavaEE6
if ( version.ge( JavaEEVersion.SIX ) )
{
writeInitializeInOrder( context.getInitializeInOrder(), writer );
}

// Do not change this unless you really know what you're doing :)
for ( EarModule module : context.getEarModules() )
{
module.appendModule( writer, version.getVersion(), generateModuleId );
}

for ( SecurityRole securityRole : context.getSecurityRoles() )
{
securityRole.appendSecurityRole( writer );
}

if ( version.ge( JavaEEVersion.FIVE ) )
try ( Writer w = initializeWriter( context.getDestinationFile() ) )
{
writeLibraryDirectory( context.getLibraryDirectory(), writer );
}

if ( version.ge( JavaEEVersion.SIX ) )
{
for ( EnvEntry envEntry : context.getEnvEntries() )
XMLWriter writer = null;
if ( JavaEEVersion.ONE_DOT_THREE.eq( version ) )
{
envEntry.appendEnvEntry( writer );
writer = initializeRootElementOneDotThree( w );
}
for ( EjbRef ejbEntry : context.getEjbEntries() )
else if ( JavaEEVersion.ONE_DOT_FOUR.eq( version ) )
{
ejbEntry.appendEjbRefEntry( writer );
writer = initializeRootElementOneDotFour( w );
}
for ( ResourceRef resourceEntry : context.getResourceRefs() )
else if ( JavaEEVersion.FIVE.eq( version ) )
{
resourceEntry.appendResourceRefEntry( writer );
writer = initializeRootElementFive( w );
}
else if ( JavaEEVersion.SIX.eq( version ) )
{
writer = initializeRootElementSix( w );
}
else if ( JavaEEVersion.SEVEN.eq( version ) )
{
writer = initializeRootElementSeven( w );
}
else if ( JavaEEVersion.EIGHT.eq( version ) )
{
writer = initializeRootElementEight( w );
}

// writer is still on root element, so we can still add this attribute
if ( context.getApplicationId() != null )
{
writer.addAttribute( "id", context.getApplicationId() );
}

// As from JavaEE6
if ( version.ge( JavaEEVersion.SIX ) )
{
writeApplicationName( context.getApplicationName(), writer );
}

// IMPORTANT: the order of the description and display-name elements was
// reversed between J2EE 1.3 and J2EE 1.4.
if ( version.eq( JavaEEVersion.ONE_DOT_THREE ) )
{
writeDisplayName( context.getDisplayName(), writer );
writeDescription( context.getDescription(), writer );
}
else
{
writeDescription( context.getDescription(), writer );
writeDisplayName( context.getDisplayName(), writer );
}

// As from JavaEE6
if ( version.ge( JavaEEVersion.SIX ) )
{
writeInitializeInOrder( context.getInitializeInOrder(), writer );
}

// Do not change this unless you really know what you're doing :)
for ( EarModule module : context.getEarModules() )
{
module.appendModule( writer, version.getVersion(), generateModuleId );
}

for ( SecurityRole securityRole : context.getSecurityRoles() )
{
securityRole.appendSecurityRole( writer );
}

if ( version.ge( JavaEEVersion.FIVE ) )
{
writeLibraryDirectory( context.getLibraryDirectory(), writer );
}

if ( version.ge( JavaEEVersion.SIX ) )
{
for ( EnvEntry envEntry : context.getEnvEntries() )
{
envEntry.appendEnvEntry( writer );
}
for ( EjbRef ejbEntry : context.getEjbEntries() )
{
ejbEntry.appendEjbRefEntry( writer );
}
for ( ResourceRef resourceEntry : context.getResourceRefs() )
{
resourceEntry.appendResourceRefEntry( writer );
}
}

writer.endElement();
}
catch ( IOException ex )
{
// ignore
}

writer.endElement();

close( w );
}

private void writeApplicationName( String applicationName, XMLWriter writer )
Expand Down Expand Up @@ -249,10 +253,8 @@ private XMLWriter initializeRootElementSeven( Writer w )
writer.startElement( APPLICATION_ELEMENT );
writer.addAttribute( "xmlns", "http://xmlns.jcp.org/xml/ns/javaee" );
writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
// CHECKSTYLE_OFF: LineLength
writer.addAttribute( "xsi:schemaLocation",
"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd" );
// CHECKSTYLE_ON: LineLength
"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd" );
writer.addAttribute( "version", "7" );
return writer;
}
Expand All @@ -263,10 +265,8 @@ private XMLWriter initializeRootElementEight( Writer w )
writer.startElement( APPLICATION_ELEMENT );
writer.addAttribute( "xmlns", "http://xmlns.jcp.org/xml/ns/javaee" );
writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
// CHECKSTYLE_OFF: LineLength
writer.addAttribute( "xsi:schemaLocation",
"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_8.xsd" );
// CHECKSTYLE_ON: LineLength
"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_8.xsd" );
writer.addAttribute( "version", "8" );
return writer;
}
Expand Down
52 changes: 32 additions & 20 deletions src/main/java/org/apache/maven/plugins/ear/EarMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -754,13 +754,19 @@ private void changeManifestClasspath( EarModule module, File original, JavaEEVer
// Create a temporary work directory
// MEAR-167 use uri as directory to prevent merging of artifacts with the same artifactId
workDirectory = new File( new File( getTempFolder(), "temp" ), module.getUri() );
workDirectory.mkdirs();
getLog().debug( "Created a temporary work directory: " + workDirectory.getAbsolutePath() );

// Unpack the archive to a temporary work directory
zipUnArchiver.setSourceFile( original );
zipUnArchiver.setDestDirectory( workDirectory );
zipUnArchiver.extract();
if ( workDirectory.mkdirs() )

This comment has been minimized.

Copy link
@towtow

towtow Dec 9, 2020

this breaks builds - mkdirs() also returns false if there was nothing that had to be created (f.e. the directory still exists from a previous run).

This comment has been minimized.

Copy link
@elharo

elharo Dec 9, 2020

Author Contributor

Thanks. Can you file an issue in Jira and assign it to me? I suspect we probably need to add

workDirectory.exists() ||

This comment has been minimized.

Copy link
@towtow

towtow Dec 9, 2020

Yes that looks like it should work. Issue is filed as https://issues.apache.org/jira/browse/MEAR-291. I can't change the assignee though.

{
getLog().debug( "Created a temporary work directory: " + workDirectory.getAbsolutePath() );

// Unpack the archive to a temporary work directory
zipUnArchiver.setSourceFile( original );
zipUnArchiver.setDestDirectory( workDirectory );
zipUnArchiver.extract();
}
else
{
throw new MojoFailureException( "Failed to create directory " + workDirectory );
}
}
else
{
Expand All @@ -772,21 +778,18 @@ private void changeManifestClasspath( EarModule module, File original, JavaEEVer
boolean newMetaInfCreated = metaInfDirectory.mkdirs();
if ( newMetaInfCreated )
{
// CHECKSTYLE_OFF: LineLength
getLog().debug( "This project did not have a META-INF directory before, so a new directory was created." );
// CHECKSTYLE_ON: LineLength
getLog().debug(
"This project did not have a META-INF directory before, so a new directory was created." );
}
File newCreatedManifestFile = new File( metaInfDirectory, "MANIFEST.MF" );
boolean newManifestCreated = newCreatedManifestFile.createNewFile();
File manifestFile = new File( metaInfDirectory, "MANIFEST.MF" );
boolean newManifestCreated = manifestFile.createNewFile();
if ( newManifestCreated )
{
// CHECKSTYLE_OFF: LineLength
getLog().debug( "This project did not have a META-INF/MANIFEST.MF file before, so a new file was created." );
// CHECKSTYLE_ON: LineLength
getLog().debug(
"This project did not have a META-INF/MANIFEST.MF file before, so a new file was created." );
}

// Read the manifest from disk
Manifest mf = new Manifest( new FileInputStream( newCreatedManifestFile ) );
Manifest mf = readManifest( manifestFile );
Attribute classPath = mf.getMainSection().getAttribute( "Class-Path" );
List<String> classPathElements = new ArrayList<String>();

Expand All @@ -808,10 +811,8 @@ private void changeManifestClasspath( EarModule module, File original, JavaEEVer
// We use the original name, cause in case of outputFileNameMapping
// we could not not delete it and it will end up in the resulting EAR and the WAR
// will not be cleaned up.
// CHECKSTYLE_OFF: LineLength
File artifact = new File( new File( workDirectory, module.getLibDir() ),
module.getArtifact().getFile().getName() );
// CHECKSTYLE_ON: LineLength

// MEAR-217
// If WAR contains files with timestamps, but EAR strips them away (useBaseVersion=true)
Expand Down Expand Up @@ -873,7 +874,7 @@ private void changeManifestClasspath( EarModule module, File original, JavaEEVer
mf.getMainSection().addConfiguredAttribute( classPath );

// Write the manifest to disk
try ( FileOutputStream out = new FileOutputStream( newCreatedManifestFile );
try ( FileOutputStream out = new FileOutputStream( manifestFile );
OutputStreamWriter writer = new OutputStreamWriter( out, StandardCharsets.UTF_8 ) )
{
mf.write( writer );
Expand All @@ -898,4 +899,15 @@ private void changeManifestClasspath( EarModule module, File original, JavaEEVer
throw new MojoFailureException( e.getMessage(), e );
}
}

private static Manifest readManifest( File manifestFile )
throws IOException
{
// Read the manifest from disk
try ( FileInputStream in = new FileInputStream( manifestFile ) )
{
Manifest manifest = new Manifest( in );
return manifest;
}
}
}
Loading

0 comments on commit ce76c3e

Please sign in to comment.