From 73f2c7cc7895e091c45aa94b7a3ee82ca0008f6c Mon Sep 17 00:00:00 2001 From: Manfred Riem Date: Thu, 10 Mar 2022 18:14:39 -0700 Subject: [PATCH] Fixes issue #2510 - Fix code smells (#2511) --- .../webxml/internal/InternalWebXmlParser.java | 4 +- .../cloud/piranha/server/ServerPiranha.java | 50 +++++++++++-------- .../piranha/test/soteria/form/FormTest.java | 2 +- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/extension/webxml/src/main/java/cloud/piranha/extension/webxml/internal/InternalWebXmlParser.java b/extension/webxml/src/main/java/cloud/piranha/extension/webxml/internal/InternalWebXmlParser.java index f60e7e5eb0..823ae743c3 100644 --- a/extension/webxml/src/main/java/cloud/piranha/extension/webxml/internal/InternalWebXmlParser.java +++ b/extension/webxml/src/main/java/cloud/piranha/extension/webxml/internal/InternalWebXmlParser.java @@ -141,8 +141,8 @@ public WebXml parse(InputStream inputStream) { parseWebApp(webXml, xPath, document); parseWelcomeFiles(webXml, xPath, document); parseDataSources(webXml, xPath, document); - } catch (Throwable t) { - LOGGER.log(WARNING, "Unable to parse web.xml", t); + } catch (Exception e) { + LOGGER.log(WARNING, "Unable to parse web.xml", e); } return webXml; } diff --git a/server/dist/src/main/java/cloud/piranha/server/ServerPiranha.java b/server/dist/src/main/java/cloud/piranha/server/ServerPiranha.java index cf52e412b1..80489b6e7e 100644 --- a/server/dist/src/main/java/cloud/piranha/server/ServerPiranha.java +++ b/server/dist/src/main/java/cloud/piranha/server/ServerPiranha.java @@ -54,6 +54,7 @@ import java.lang.module.ModuleDescriptor; import java.lang.module.ModuleFinder; import java.lang.module.ModuleReference; +import java.nio.file.Files; import java.util.List; import java.util.ServiceLoader; import java.util.zip.ZipEntry; @@ -139,7 +140,7 @@ public class ServerPiranha implements Piranha, Runnable { * @throws IOException when an I/O error occurs. */ private void extractZipInputStream(ZipInputStream zipInput, String filePath) throws IOException { - try (BufferedOutputStream bufferOutput = new BufferedOutputStream(new FileOutputStream(filePath))) { + try ( BufferedOutputStream bufferOutput = new BufferedOutputStream(new FileOutputStream(filePath))) { byte[] bytesIn = new byte[8192]; int read; while ((read = zipInput.read(bytesIn)) != -1) { @@ -155,7 +156,7 @@ private void extractWarFile(File warFile, File webApplicationDirectory) { if (!webApplicationDirectory.exists()) { webApplicationDirectory.mkdirs(); } - try (ZipInputStream zipInput = new ZipInputStream(new FileInputStream(warFile))) { + try ( ZipInputStream zipInput = new ZipInputStream(new FileInputStream(warFile))) { ZipEntry entry = zipInput.getNextEntry(); while (entry != null) { String filePath = webApplicationDirectory + File.separator + entry.getName(); @@ -287,18 +288,21 @@ public void run() { File startedFile = new File("tmp/piranha.started"); File stoppedFile = new File("tmp/piranha.stopped"); - - try { - if (startedFile.exists()) { - if (startedFile.delete()) { - LOGGER.log(WARNING, "Unable to delete existing piranha.stopped file"); - } + + if (stoppedFile.exists()) { + try { + Files.delete(stoppedFile.toPath()); + } catch (IOException ioe) { + LOGGER.log(WARNING, "Error while deleting existing piranha.stopped file", ioe); } - if (!startedFile.exists()) { + } + + if (!startedFile.exists()) { + try { startedFile.createNewFile(); + } catch (IOException ioe) { + LOGGER.log(WARNING, "Unable to create piranha.started file", ioe); } - } catch (IOException ioe) { - LOGGER.log(WARNING, "Unable to create piranha.started file", ioe); } File pidFile = new File(PID_FILE); @@ -324,17 +328,19 @@ public void run() { LOGGER.log(INFO, "Stopped Piranha"); LOGGER.log(INFO, "We ran for {0} milliseconds", finishTime - startTime); - try { - if (startedFile.exists()) { - if (startedFile.delete()) { - LOGGER.log(WARNING, "Unable to delete existing piranha.started file"); - } + if (startedFile.exists()) { + try { + Files.delete(startedFile.toPath()); + } catch (IOException ioe) { + LOGGER.log(WARNING, "Error while deleting existing piranha.started file", ioe); } - if (!stoppedFile.exists()) { + } + if (!stoppedFile.exists()) { + try { stoppedFile.createNewFile(); + } catch (IOException ioe) { + LOGGER.log(WARNING, "Unable to create piranha.stopped file", ioe); } - } catch (IOException ioe) { - LOGGER.log(WARNING, "Unable to create piranha.stopped file", ioe); } if (exitOnStop) { @@ -525,7 +531,11 @@ public void stop() { File pidFile = new File(PID_FILE); if (pidFile.exists()) { - pidFile.delete(); + try { + Files.delete(pidFile.toPath()); + } catch (IOException ioe) { + LOGGER.log(WARNING, "Error occurred while deleting PID file", ioe); + } } started = false; diff --git a/test/soteria/form/src/test/java/cloud/piranha/test/soteria/form/FormTest.java b/test/soteria/form/src/test/java/cloud/piranha/test/soteria/form/FormTest.java index a91ab76d8c..e8bca71eb7 100644 --- a/test/soteria/form/src/test/java/cloud/piranha/test/soteria/form/FormTest.java +++ b/test/soteria/form/src/test/java/cloud/piranha/test/soteria/form/FormTest.java @@ -106,7 +106,7 @@ void testAuthenticated() throws Exception { .build(); response = piranha.service(request); - assertEquals(response.getStatus(), 302,"Should redirect"); + assertEquals(302, response.getStatus(), "Should redirect"); URL redirectUrl = new URL(response.getHeader("Location")); request = new EmbeddedRequestBuilder()