This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-net.git
commit 5689c0d671d0d30fef71e851848ad041a90aa015 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Jun 16 08:56:01 2024 -0400 Refactor example duplicate code - Add example class PrintCommandListeners - Reduce compiler warnings - Reduce SpotBugs warnings --- .../net/examples/PrintCommandListeners.java | 33 ++++++++++++++++++++++ .../commons/net/examples/ftp/FTPClientExample.java | 8 ++---- .../net/examples/ftp/ServerToServerFTP.java | 8 ++---- .../apache/commons/net/examples/mail/POP3Mail.java | 5 ++-- .../apache/commons/net/examples/mail/SMTPMail.java | 4 +-- .../commons/net/examples/nntp/ArticleReader.java | 8 ++---- .../commons/net/examples/nntp/ExtendedNNTPOps.java | 5 ++-- .../net/examples/nntp/MessageThreading.java | 5 ++-- .../commons/net/examples/nntp/PostMessage.java | 4 +-- 9 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/apache/commons/net/examples/PrintCommandListeners.java b/src/main/java/org/apache/commons/net/examples/PrintCommandListeners.java new file mode 100644 index 00000000..417a787d --- /dev/null +++ b/src/main/java/org/apache/commons/net/examples/PrintCommandListeners.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.net.examples; + +import org.apache.commons.net.PrintCommandListener; +import org.apache.commons.net.io.Util; + +/** + * Factory of {@link PrintCommandListener} for examples. + */ +public class PrintCommandListeners { + + @SuppressWarnings("resource") // No need to close writer on System.out. + public static PrintCommandListener sysOutPrintCommandListener() { + return new PrintCommandListener(Util.newPrintWriter(System.out), true); + } + +} diff --git a/src/main/java/org/apache/commons/net/examples/ftp/FTPClientExample.java b/src/main/java/org/apache/commons/net/examples/ftp/FTPClientExample.java index 580312d6..5aa8189e 100644 --- a/src/main/java/org/apache/commons/net/examples/ftp/FTPClientExample.java +++ b/src/main/java/org/apache/commons/net/examples/ftp/FTPClientExample.java @@ -22,13 +22,12 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.PrintWriter; import java.net.InetAddress; import java.net.UnknownHostException; import java.time.Duration; import java.util.Arrays; -import org.apache.commons.net.PrintCommandListener; +import org.apache.commons.net.examples.PrintCommandListeners; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClientConfig; @@ -39,7 +38,6 @@ import org.apache.commons.net.ftp.FTPReply; import org.apache.commons.net.ftp.FTPSClient; import org.apache.commons.net.io.CopyStreamEvent; import org.apache.commons.net.io.CopyStreamListener; -import org.apache.commons.net.io.Util; import org.apache.commons.net.util.TrustManagerUtils; /** @@ -281,9 +279,7 @@ public final class FTPClientExample { ftp.setListHiddenFiles(hidden); // suppress login details - @SuppressWarnings("resource") - final PrintWriter printWriter = Util.newPrintWriter(System.out); - ftp.addProtocolCommandListener(new PrintCommandListener(printWriter, true)); + ftp.addProtocolCommandListener(PrintCommandListeners.sysOutPrintCommandListener()); final FTPClientConfig config; if (serverType != null) { diff --git a/src/main/java/org/apache/commons/net/examples/ftp/ServerToServerFTP.java b/src/main/java/org/apache/commons/net/examples/ftp/ServerToServerFTP.java index d8d8b46b..f49c9a38 100644 --- a/src/main/java/org/apache/commons/net/examples/ftp/ServerToServerFTP.java +++ b/src/main/java/org/apache/commons/net/examples/ftp/ServerToServerFTP.java @@ -18,14 +18,12 @@ package org.apache.commons.net.examples.ftp; import java.io.IOException; -import java.io.PrintWriter; import java.net.InetAddress; -import org.apache.commons.net.PrintCommandListener; import org.apache.commons.net.ProtocolCommandListener; +import org.apache.commons.net.examples.PrintCommandListeners; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; -import org.apache.commons.net.io.Util; /** * This is an example program demonstrating how to use the FTPClient class. This program arranges a server to server file transfer that transfers a file from @@ -75,9 +73,7 @@ public final class ServerToServerFTP { password2 = args[6]; file2 = args[7]; - @SuppressWarnings("resource") // Don't close System.out - final PrintWriter printWriter = Util.newPrintWriter(System.out); - listener = new PrintCommandListener(printWriter, true); + listener = PrintCommandListeners.sysOutPrintCommandListener(); ftp1 = new FTPClient(); ftp1.addProtocolCommandListener(listener); ftp2 = new FTPClient(); diff --git a/src/main/java/org/apache/commons/net/examples/mail/POP3Mail.java b/src/main/java/org/apache/commons/net/examples/mail/POP3Mail.java index 25075228..d3c68b7f 100644 --- a/src/main/java/org/apache/commons/net/examples/mail/POP3Mail.java +++ b/src/main/java/org/apache/commons/net/examples/mail/POP3Mail.java @@ -21,8 +21,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.util.Locale; -import org.apache.commons.net.PrintCommandListener; -import org.apache.commons.net.io.Util; +import org.apache.commons.net.examples.PrintCommandListeners; import org.apache.commons.net.pop3.POP3Client; import org.apache.commons.net.pop3.POP3MessageInfo; import org.apache.commons.net.pop3.POP3SClient; @@ -77,7 +76,7 @@ public final class POP3Mail { pop3.setDefaultTimeout(60000); // suppress login details - pop3.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true)); + pop3.addProtocolCommandListener(PrintCommandListeners.sysOutPrintCommandListener()); try { pop3.connect(server); diff --git a/src/main/java/org/apache/commons/net/examples/mail/SMTPMail.java b/src/main/java/org/apache/commons/net/examples/mail/SMTPMail.java index 3c699a99..d8c61e38 100644 --- a/src/main/java/org/apache/commons/net/examples/mail/SMTPMail.java +++ b/src/main/java/org/apache/commons/net/examples/mail/SMTPMail.java @@ -28,7 +28,7 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; -import org.apache.commons.net.PrintCommandListener; +import org.apache.commons.net.examples.PrintCommandListeners; import org.apache.commons.net.io.Util; import org.apache.commons.net.smtp.SMTPClient; import org.apache.commons.net.smtp.SMTPReply; @@ -108,7 +108,7 @@ public final class SMTPMail { } client = new SMTPClient(); - client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true)); + client.addProtocolCommandListener(PrintCommandListeners.sysOutPrintCommandListener()); client.connect(server); diff --git a/src/main/java/org/apache/commons/net/examples/nntp/ArticleReader.java b/src/main/java/org/apache/commons/net/examples/nntp/ArticleReader.java index 736ccc4f..4aa3a8bf 100644 --- a/src/main/java/org/apache/commons/net/examples/nntp/ArticleReader.java +++ b/src/main/java/org/apache/commons/net/examples/nntp/ArticleReader.java @@ -19,11 +19,9 @@ package org.apache.commons.net.examples.nntp; import java.io.BufferedReader; import java.io.IOException; -import java.io.PrintWriter; import java.net.SocketException; -import org.apache.commons.net.PrintCommandListener; -import org.apache.commons.net.io.Util; +import org.apache.commons.net.examples.PrintCommandListeners; import org.apache.commons.net.nntp.NNTPClient; import org.apache.commons.net.nntp.NewsgroupInfo; @@ -45,9 +43,7 @@ public class ArticleReader { final String articleSpec = args.length >= 3 ? args[2] : null; final NNTPClient client = new NNTPClient(); - @SuppressWarnings("resource") - final PrintWriter printWriter = Util.newPrintWriter(System.out); - client.addProtocolCommandListener(new PrintCommandListener(printWriter, true)); + client.addProtocolCommandListener(PrintCommandListeners.sysOutPrintCommandListener()); client.connect(hostname); if (args.length == 5) { // Optional auth diff --git a/src/main/java/org/apache/commons/net/examples/nntp/ExtendedNNTPOps.java b/src/main/java/org/apache/commons/net/examples/nntp/ExtendedNNTPOps.java index 94523dbd..8e7e3b28 100644 --- a/src/main/java/org/apache/commons/net/examples/nntp/ExtendedNNTPOps.java +++ b/src/main/java/org/apache/commons/net/examples/nntp/ExtendedNNTPOps.java @@ -19,8 +19,7 @@ package org.apache.commons.net.examples.nntp; import java.io.IOException; -import org.apache.commons.net.PrintCommandListener; -import org.apache.commons.net.io.Util; +import org.apache.commons.net.examples.PrintCommandListeners; import org.apache.commons.net.nntp.Article; import org.apache.commons.net.nntp.NNTPClient; import org.apache.commons.net.nntp.NewsgroupInfo; @@ -47,7 +46,7 @@ public class ExtendedNNTPOps { public ExtendedNNTPOps() { client = new NNTPClient(); - client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true)); + client.addProtocolCommandListener(PrintCommandListeners.sysOutPrintCommandListener()); } private void demo(final String host, final String user, final String password) { diff --git a/src/main/java/org/apache/commons/net/examples/nntp/MessageThreading.java b/src/main/java/org/apache/commons/net/examples/nntp/MessageThreading.java index 7711f69c..b27242d3 100644 --- a/src/main/java/org/apache/commons/net/examples/nntp/MessageThreading.java +++ b/src/main/java/org/apache/commons/net/examples/nntp/MessageThreading.java @@ -20,8 +20,7 @@ package org.apache.commons.net.examples.nntp; import java.io.IOException; import java.net.SocketException; -import org.apache.commons.net.PrintCommandListener; -import org.apache.commons.net.io.Util; +import org.apache.commons.net.examples.PrintCommandListeners; import org.apache.commons.net.nntp.Article; import org.apache.commons.net.nntp.NNTPClient; import org.apache.commons.net.nntp.NewsgroupInfo; @@ -42,7 +41,7 @@ public class MessageThreading { final String newsgroup = args[1]; final NNTPClient client = new NNTPClient(); - client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true)); + client.addProtocolCommandListener(PrintCommandListeners.sysOutPrintCommandListener()); client.connect(hostname); if (args.length == 4) { // Optional auth diff --git a/src/main/java/org/apache/commons/net/examples/nntp/PostMessage.java b/src/main/java/org/apache/commons/net/examples/nntp/PostMessage.java index 38e3cda1..b2144e1b 100644 --- a/src/main/java/org/apache/commons/net/examples/nntp/PostMessage.java +++ b/src/main/java/org/apache/commons/net/examples/nntp/PostMessage.java @@ -27,7 +27,7 @@ import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; -import org.apache.commons.net.PrintCommandListener; +import org.apache.commons.net.examples.PrintCommandListeners; import org.apache.commons.net.io.Util; import org.apache.commons.net.nntp.NNTPClient; import org.apache.commons.net.nntp.NNTPReply; @@ -132,7 +132,7 @@ public final class PostMessage { } client = new NNTPClient(); - client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true)); + client.addProtocolCommandListener(PrintCommandListeners.sysOutPrintCommandListener()); client.connect(server);