From 058b412c63dad6eb3c0fcf16cae11e78523e8a29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mat=C4=9Bj=C4=8Dek?= Date: Sun, 6 Oct 2024 15:28:46 +0200 Subject: [PATCH 1/2] CLI Environment - log file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - load formatter just once - more robust log file creation - don't ignore exceptions - Use ISO 8061 timestamp format Signed-off-by: David Matějček --- .../com/sun/enterprise/admin/cli/CLIUtil.java | 32 ++++-------------- .../sun/enterprise/admin/cli/Environment.java | 33 ++++++++++++------- 2 files changed, 29 insertions(+), 36 deletions(-) diff --git a/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/CLIUtil.java b/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/CLIUtil.java index d4259517f4c..7bd72b6f353 100644 --- a/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/CLIUtil.java +++ b/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/CLIUtil.java @@ -29,13 +29,10 @@ import java.io.InputStream; import java.io.PrintWriter; import java.io.StringWriter; -import java.nio.charset.Charset; -import java.text.DateFormat; -import java.text.SimpleDateFormat; +import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -50,6 +47,8 @@ import org.glassfish.api.admin.CommandValidationException; import org.glassfish.api.admin.InvalidCommandException; +import static java.nio.charset.StandardCharsets.UTF_8; + /** * CLI Utility class */ @@ -248,14 +247,9 @@ public static void writeCommandToDebugLog(String cname, Environment env, String[ return; } - BufferedWriter out = null; - try { - out = new BufferedWriter(new FileWriter(log, Charset.defaultCharset(), true)); - DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); - Date date = new Date(); - out.write(dateFormat.format(date)); + try (BufferedWriter out = new BufferedWriter(new FileWriter(log, UTF_8, true))) { + out.write(Instant.now().toString()); out.write(" EXIT: " + exit); - out.write(" " + cname + " "); if (args != null) { @@ -273,21 +267,9 @@ public static void writeCommandToDebugLog(String cname, Environment env, String[ out.write(arg + " "); } } + out.write("\n"); } catch (IOException e) { - // It is just a debug file. - } finally { - if (out != null) { - try { - out.write("\n"); - } catch (Exception e) { - // ignore - } - try { - out.close(); - } catch (Exception e) { - // ignore - } - } + throw new IllegalStateException("Could not write to the asadmin log file " + log, e); } } diff --git a/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/Environment.java b/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/Environment.java index 7ef64a26222..85797dd8006 100644 --- a/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/Environment.java +++ b/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/Environment.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Contributors to the Eclipse Foundation + * Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the @@ -42,6 +42,7 @@ public final class Environment { private boolean debug; private boolean trace; private File logfile; + private Formatter formatter; /** * Set the prefix for environment variables referenced from the system environment by Environment objects. @@ -109,13 +110,29 @@ public Environment(boolean ignoreEnvironment) { } if (fname != null) { File f = new File(fname); - try { - if ((f.exists() || f.createNewFile()) && f.isFile() && f.canWrite()) { + if (f.isFile() && f.canWrite()) { + logfile = f; + } else { + File dir = f.getParentFile(); + if (!dir.isDirectory() && !dir.mkdirs()) { + throw new IllegalStateException("Could not create a log file: " + f); + } + if (!f.createNewFile()) { + throw new IllegalStateException("Could not create a log file: " + f); + } logfile = f; } } catch (IOException e) { - /* ignore */ } + throw new IllegalStateException("Could not create a log file: " + f, e); + } + } + final String formatterClass = env.get(PREFIX + "LOG_FORMATTER"); + try { + formatter = formatterClass == null ? null + : (Formatter) Class.forName(formatterClass).getDeclaredConstructor().newInstance(); + } catch (Exception e) { + throw new IllegalStateException("This is not a valid formatter: " + formatterClass, e); } } @@ -220,12 +237,6 @@ public File getDebugLogfile() { } public Formatter getLogFormatter() { - final String formatterClass = env.get(PREFIX + "LOG_FORMATTER"); - try { - return formatterClass == null ? null - : (Formatter) Class.forName(formatterClass).getDeclaredConstructor().newInstance(); - } catch (Exception e) { - throw new IllegalStateException("This is not a formatter: " + formatterClass, e); - } + return formatter; } } From 181d9d5c589fd7d56db7cfa4a433a78104d54486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mat=C4=9Bj=C4=8Dek?= Date: Wed, 9 Oct 2024 16:25:25 +0200 Subject: [PATCH 2/2] Refactored log file opening MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: David Matějček --- .../sun/enterprise/admin/cli/Environment.java | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/Environment.java b/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/Environment.java index 85797dd8006..c55e8533b35 100644 --- a/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/Environment.java +++ b/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/Environment.java @@ -109,23 +109,7 @@ public Environment(boolean ignoreEnvironment) { fname = System.getenv(logProp); } if (fname != null) { - File f = new File(fname); - try { - if (f.isFile() && f.canWrite()) { - logfile = f; - } else { - File dir = f.getParentFile(); - if (!dir.isDirectory() && !dir.mkdirs()) { - throw new IllegalStateException("Could not create a log file: " + f); - } - if (!f.createNewFile()) { - throw new IllegalStateException("Could not create a log file: " + f); - } - logfile = f; - } - } catch (IOException e) { - throw new IllegalStateException("Could not create a log file: " + f, e); - } + logfile = prepareFile(new File(fname)); } final String formatterClass = env.get(PREFIX + "LOG_FORMATTER"); try { @@ -239,4 +223,29 @@ public File getDebugLogfile() { public Formatter getLogFormatter() { return formatter; } + + + private static File prepareFile(File file) { + try { + if (file.isFile()) { + if (file.canWrite()) { + return file; + } + throw new IllegalStateException("The file already exists, but we cannot write to it: " + file); + } + final File dir = file.getCanonicalFile().getParentFile(); + if (!dir.isDirectory() && !dir.mkdirs()) { + throw new IllegalStateException("The directory " + dir + + " doesn't exist and isn't even possible to create it. Output to file '" + file + + "' is not possible."); + } + if (file.createNewFile()) { + return file; + } + // createNewFile returns false just if the file already existed. + throw new IllegalStateException("Seems somebody else created the file right now, was it intentional? " + file); + } catch (IOException e) { + throw new IllegalStateException("Could not create a log file: " + file, e); + } + } }