From d9226230851e69c44ae228ef83771c2fe630d937 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Fri, 22 Nov 2024 06:52:14 -0500 Subject: [PATCH 1/4] Modernize I/O --- .../plugins/jira/ClassicJiraDownloader.java | 48 ++++++------------- 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/jira/ClassicJiraDownloader.java b/src/main/java/org/apache/maven/plugins/jira/ClassicJiraDownloader.java index a291ae3e..763f05cd 100644 --- a/src/main/java/org/apache/maven/plugins/jira/ClassicJiraDownloader.java +++ b/src/main/java/org/apache/maven/plugins/jira/ClassicJiraDownloader.java @@ -31,7 +31,6 @@ import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpState; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.StatusLine; @@ -57,7 +56,6 @@ public ClassicJiraDownloader() {} /** * Execute the query on the JIRA server. - * */ public void doExecute() { try { @@ -306,22 +304,17 @@ private void determineProxy(String jiraUrl, HttpClient client) { * @param link the URL to JIRA */ private void download(final HttpClient cl, final String link) { - InputStream in = null; - OutputStream out = null; - try { - GetMethod gm = new GetMethod(link); - - getLog().info("Downloading from JIRA at: " + link); - - gm.setFollowRedirects(true); + GetMethod gm = new GetMethod(link); + getLog().info("Downloading from JIRA at: " + link); + gm.setFollowRedirects(true); + try { cl.executeMethod(gm); StatusLine sl = gm.getStatusLine(); if (sl == null) { getLog().error("Unknown error validating link: " + link); - return; } @@ -333,47 +326,34 @@ private void download(final HttpClient cl, final String link) { getLog().warn("Site sent redirect, but did not set Location header"); } else { String newLink = locationHeader.getValue(); - getLog().debug("Following redirect to " + newLink); - download(cl, newLink); } } if (gm.getStatusCode() == HttpStatus.SC_OK) { - in = gm.getResponseBodyAsStream(); - if (!output.getParentFile().exists()) { - output.getParentFile().mkdirs(); + if (!output.getParentFile().mkdirs()) { + getLog().error("Downloading issues from JIRA failed. Could not create " + + output.getParentFile()); + return; + } } - // write the response to file - out = new FileOutputStream(output); - IOUtil.copy(in, out); - out.close(); - out = null; - in.close(); - in = null; - - getLog().debug("Downloading from JIRA was successful"); + try (InputStream in = gm.getResponseBodyAsStream(); + OutputStream out = new FileOutputStream(output)) { + IOUtil.copy(in, out); + getLog().debug("Downloading from JIRA was successful"); + } } else { getLog().warn("Downloading from JIRA failed. Received: [" + gm.getStatusCode() + "]"); } - } catch (HttpException e) { - if (getLog().isDebugEnabled()) { - getLog().error("Error downloading issues from JIRA:", e); - } else { - getLog().error("Error downloading issues from JIRA url: " + e.getLocalizedMessage()); - } } catch (IOException e) { if (getLog().isDebugEnabled()) { getLog().error("Error downloading issues from JIRA:", e); } else { getLog().error("Error downloading issues from JIRA. Cause is " + e.getLocalizedMessage()); } - } finally { - IOUtil.close(out); - IOUtil.close(in); } } From c1d79b4afad1c51bc5e3df0e1ba51bccecb74352 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Fri, 22 Nov 2024 06:55:58 -0500 Subject: [PATCH 2/4] Modernize I/O --- .../apache/maven/plugins/jira/JiraXML.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/jira/JiraXML.java b/src/main/java/org/apache/maven/plugins/jira/JiraXML.java index cea101f5..5cf6ef63 100644 --- a/src/main/java/org/apache/maven/plugins/jira/JiraXML.java +++ b/src/main/java/org/apache/maven/plugins/jira/JiraXML.java @@ -24,6 +24,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -35,7 +36,6 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.issues.Issue; -import org.codehaus.plexus.util.IOUtil; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.helpers.DefaultHandler; @@ -64,8 +64,8 @@ public class JiraXML extends DefaultHandler { private SimpleDateFormat sdf; /** - * @param log not null. - * @param datePattern may be null. + * @param log not null + * @param datePattern may be null * @since 2.4 */ public JiraXML(Log log, String datePattern) { @@ -83,22 +83,20 @@ public JiraXML(Log log, String datePattern) { } /** - * Parse the given xml file. The list of issues can then be retrieved with {@link #getIssueList()}. + * Parse the given XML file. The list of issues can then be retrieved with {@link #getIssueList()}. * - * @param xmlPath the file to pares. - * @throws MojoExecutionException in case of errors. + * @param xmlPath the file to parse + * @throws MojoExecutionException in case of errors * @since 2.4 */ public void parseXML(File xmlPath) throws MojoExecutionException { - InputStream xmlStream = null; - try { - xmlStream = new FileInputStream(xmlPath); + try (InputStream xmlStream = new FileInputStream(xmlPath)) { InputSource inputSource = new InputSource(xmlStream); parse(inputSource); } catch (FileNotFoundException e) { throw new MojoExecutionException("Failed to open JIRA XML file " + xmlPath, e); - } finally { - IOUtil.close(xmlStream); + } catch ( IOException e) { + throw new MojoExecutionException("Failed to read JIRA XML file " + xmlPath, e); } } From f3c8c7bccd369918286dea2002cbe5aa62e5432f Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Fri, 22 Nov 2024 06:58:30 -0500 Subject: [PATCH 3/4] Modernize I/O --- .../schema/DefaultChangesSchemaValidator.java | 14 +------------- .../org/apache/maven/plugins/jira/JiraXML.java | 2 +- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/changes/schema/DefaultChangesSchemaValidator.java b/src/main/java/org/apache/maven/plugins/changes/schema/DefaultChangesSchemaValidator.java index ead9eb29..9b4a6877 100644 --- a/src/main/java/org/apache/maven/plugins/changes/schema/DefaultChangesSchemaValidator.java +++ b/src/main/java/org/apache/maven/plugins/changes/schema/DefaultChangesSchemaValidator.java @@ -32,7 +32,6 @@ import org.apache.commons.io.input.XmlStreamReader; import org.codehaus.plexus.component.annotations.Component; -import org.codehaus.plexus.util.IOUtil; import org.xml.sax.SAXException; /** @@ -88,26 +87,15 @@ public Schema getSchema(String schemaPath) throws SAXException, IOException { return schema; } - /** - * @param uriSchema - * @return Schema - * @throws Exception - */ private Schema compileJAXPSchema(String uriSchema) throws IOException, SAXException, NullPointerException { - InputStream in = null; - try { - in = Thread.currentThread().getContextClassLoader().getResourceAsStream(uriSchema); + try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(uriSchema)) { if (in == null) { throw new NullPointerException(" impossible to load schema with path " + uriSchema); } // newInstance de SchemaFactory not ThreadSafe final Schema schema = SchemaFactory.newInstance(W3C_XML_SCHEMA).newSchema(new StreamSource(in)); - in.close(); - in = null; return schema; - } finally { - IOUtil.close(in); } } diff --git a/src/main/java/org/apache/maven/plugins/jira/JiraXML.java b/src/main/java/org/apache/maven/plugins/jira/JiraXML.java index 5cf6ef63..2967c192 100644 --- a/src/main/java/org/apache/maven/plugins/jira/JiraXML.java +++ b/src/main/java/org/apache/maven/plugins/jira/JiraXML.java @@ -95,7 +95,7 @@ public void parseXML(File xmlPath) throws MojoExecutionException { parse(inputSource); } catch (FileNotFoundException e) { throw new MojoExecutionException("Failed to open JIRA XML file " + xmlPath, e); - } catch ( IOException e) { + } catch (IOException e) { throw new MojoExecutionException("Failed to read JIRA XML file " + xmlPath, e); } } From bd28905f84e77a429364eb72d12f88602fe76def Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Fri, 22 Nov 2024 07:03:00 -0500 Subject: [PATCH 4/4] Modernize I/O --- .../maven/plugins/announcement/AnnouncementMailMojo.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMailMojo.java b/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMailMojo.java index 9a9ede96..639ef2a0 100644 --- a/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMailMojo.java +++ b/src/main/java/org/apache/maven/plugins/announcement/AnnouncementMailMojo.java @@ -41,7 +41,6 @@ import org.codehaus.plexus.mailsender.MailMessage; import org.codehaus.plexus.mailsender.MailSenderException; import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.ReaderFactory; /** * Goal which sends an announcement through email. @@ -339,7 +338,7 @@ protected void sendMessage() throws MojoExecutionException { protected String readAnnouncement(File file) throws MojoExecutionException { try { if (templateEncoding == null || templateEncoding.isEmpty()) { - templateEncoding = ReaderFactory.FILE_ENCODING; + templateEncoding = System.getProperty("file.encoding"); getLog().warn("File encoding has not been set, using platform encoding '" + templateEncoding + "', i.e. build is platform dependent!"); }