From d268e8a891faa023c1c1c518fe6c8dca20be5a28 Mon Sep 17 00:00:00 2001 From: jonx8 Date: Sun, 24 Sep 2023 00:04:14 +0300 Subject: [PATCH 1/2] Change application directories --- .../ru/etu/petci/configuration/Configurator.java | 10 ++++------ .../ru/etu/petci/handlers/InitCommandHandler.java | 13 ++++--------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/main/java/ru/etu/petci/configuration/Configurator.java b/src/main/java/ru/etu/petci/configuration/Configurator.java index be44390..60509ba 100644 --- a/src/main/java/ru/etu/petci/configuration/Configurator.java +++ b/src/main/java/ru/etu/petci/configuration/Configurator.java @@ -11,14 +11,12 @@ public final class Configurator { - public static final String JOBS_DIR = "jobs/"; - public static final String REPOSITORY_PROPERTY = ".repo.properties"; + public static final String JOBS_DIR = "petCI/jobs/"; + public static final String LOGS_DIR = "petCI/logs/"; - private static final Logger LOGGER = Logger.getLogger(Configurator.class.getName()); + public static final String REPOSITORY_PROPERTY = "petCI/repo.properties"; - static { - LOGGER.setLevel(Level.ALL); - } + private static final Logger LOGGER = Logger.getLogger(Configurator.class.getName()); private Configurator() { } diff --git a/src/main/java/ru/etu/petci/handlers/InitCommandHandler.java b/src/main/java/ru/etu/petci/handlers/InitCommandHandler.java index e72fbea..50cb03f 100644 --- a/src/main/java/ru/etu/petci/handlers/InitCommandHandler.java +++ b/src/main/java/ru/etu/petci/handlers/InitCommandHandler.java @@ -9,14 +9,11 @@ import java.util.logging.Logger; import static ru.etu.petci.configuration.Configurator.JOBS_DIR; +import static ru.etu.petci.configuration.Configurator.LOGS_DIR; public class InitCommandHandler implements CommandHandler { private static final Logger LOGGER = Logger.getLogger(InitCommandHandler.class.getName()); - static { - LOGGER.setLevel(Level.INFO); - } - @Override public int handle(String[] args) { @@ -30,12 +27,10 @@ public int handle(String[] args) { } try { + new File(JOBS_DIR).mkdirs(); + new File(LOGS_DIR).mkdirs(); Configurator.saveRepositoryConfig(branchName); - if (new File(JOBS_DIR).mkdir()) { - LOGGER.log(Level.CONFIG, "Directory \"{0}\" has been created", JOBS_DIR); - } else { - LOGGER.log(Level.WARNING, "Failed while creating directory \"{0}\"", JOBS_DIR); - } + } catch (IOException e) { LOGGER.severe(e.getMessage()); return 1; From fbab90948fcbc17191b8db40de3a094e73dd27c8 Mon Sep 17 00:00:00 2001 From: jonx8 Date: Sun, 24 Sep 2023 00:33:44 +0300 Subject: [PATCH 2/2] Configure work with logging.properties --- src/main/java/ru/etu/petci/Main.java | 24 +++++++++++++++---- .../etu/petci/configuration/Configurator.java | 4 ++++ .../petci/handlers/AddJobCommandHandler.java | 5 ---- .../ru/etu/petci/handlers/CommandHandler.java | 2 +- .../handlers/ContinueCommandHandler.java | 6 +---- .../petci/handlers/InitCommandHandler.java | 8 +++++-- .../petci/observers/RepositoryObserver.java | 7 ++---- .../resources/ru/etu/petci/logging.properties | 13 ++++++++++ .../ru/etu/petci/settings.properties | 1 - 9 files changed, 46 insertions(+), 24 deletions(-) create mode 100644 src/main/resources/ru/etu/petci/logging.properties delete mode 100644 src/main/resources/ru/etu/petci/settings.properties diff --git a/src/main/java/ru/etu/petci/Main.java b/src/main/java/ru/etu/petci/Main.java index 60cf394..8812df5 100644 --- a/src/main/java/ru/etu/petci/Main.java +++ b/src/main/java/ru/etu/petci/Main.java @@ -1,22 +1,32 @@ package ru.etu.petci; + import ru.etu.petci.handlers.AddJobCommandHandler; import ru.etu.petci.handlers.CommandHandler; import ru.etu.petci.handlers.ContinueCommandHandler; import ru.etu.petci.handlers.InitCommandHandler; +import java.io.IOException; +import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.logging.Level; +import java.util.logging.LogManager; import java.util.logging.Logger; public class Main { - private static final Logger LOGGER = Logger.getLogger(Main.class.getName()); private static final Map commandHandlersMap = new HashMap<>(); + private static final Logger LOGGER; static { - LOGGER.setLevel(Level.WARNING); + try (InputStream input = Main.class.getClassLoader().getResourceAsStream("logging.properties")) { + LogManager.getLogManager().readConfiguration(input); + } catch (IOException e) { + System.out.println(e.getMessage()); + } + LOGGER = Logger.getLogger(Main.class.getName()); + commandHandlersMap.put("init", new InitCommandHandler()); commandHandlersMap.put("continue", new ContinueCommandHandler()); commandHandlersMap.put("add", new AddJobCommandHandler()); @@ -24,12 +34,16 @@ public class Main { public static void main(String[] args) { + int exitStatus; if (args.length == 0) { showHelp(); - System.exit(1); + exitStatus = 1; + } else { + CommandHandler handler = commandHandlersMap.get(args[0].trim()); + exitStatus = handler.handle(args); } - CommandHandler handler = commandHandlersMap.get(args[0].trim()); - System.exit(handler.handle(args)); + LOGGER.log(Level.FINE, "The program finished with exit code {0}", exitStatus); + System.exit(exitStatus); } diff --git a/src/main/java/ru/etu/petci/configuration/Configurator.java b/src/main/java/ru/etu/petci/configuration/Configurator.java index 60509ba..c7e1625 100644 --- a/src/main/java/ru/etu/petci/configuration/Configurator.java +++ b/src/main/java/ru/etu/petci/configuration/Configurator.java @@ -38,6 +38,7 @@ public static void saveRepositoryConfig(String branchName, String lastHash) thro properties.setProperty("last_hash", lastHash); properties.setProperty("branch_name", branchName); properties.store(writer, "Repository config"); + LOGGER.log(Level.FINE, "File {0} was updated", REPOSITORY_PROPERTY); } } @@ -58,9 +59,11 @@ public static List readJobsConfig() throws IOException { try (var reader = new FileReader(jobProperty)) { Properties properties = new Properties(); properties.load(reader); + String jobName = properties.getProperty("name"); String scriptName = properties.getProperty("script_name"); boolean isActive = Boolean.parseBoolean(properties.getProperty("active")); + if (jobName != null && scriptName != null) { jobs.add(new Job(jobName, scriptName, isActive)); } else { @@ -84,6 +87,7 @@ public static void saveJobConfig(Job job) throws IOException { properties.setProperty("script_name", job.scriptName()); properties.setProperty("active", String.valueOf(job.isActive())); properties.store(writer, "Job settings"); + LOGGER.log(Level.FINE, "File {0} was updated", JOBS_DIR + job.name() + ".properties"); } } } diff --git a/src/main/java/ru/etu/petci/handlers/AddJobCommandHandler.java b/src/main/java/ru/etu/petci/handlers/AddJobCommandHandler.java index 01e8ab2..7948118 100644 --- a/src/main/java/ru/etu/petci/handlers/AddJobCommandHandler.java +++ b/src/main/java/ru/etu/petci/handlers/AddJobCommandHandler.java @@ -11,11 +11,6 @@ public class AddJobCommandHandler implements CommandHandler { private static final Logger LOGGER = Logger.getLogger(AddJobCommandHandler.class.getName()); - - static { - LOGGER.setLevel(Level.INFO); - } - @Override public int handle(String[] args) { try (var scanner = new Scanner(System.in)) { diff --git a/src/main/java/ru/etu/petci/handlers/CommandHandler.java b/src/main/java/ru/etu/petci/handlers/CommandHandler.java index b71ab37..4fc83b9 100644 --- a/src/main/java/ru/etu/petci/handlers/CommandHandler.java +++ b/src/main/java/ru/etu/petci/handlers/CommandHandler.java @@ -5,7 +5,7 @@ public interface CommandHandler { /** * This method should handle a certain command which is entered by user. * - * @param arg argument after name of the command. Arg is NotNull. + * @param args from the main method * @return exit code for the program */ int handle(String[] args); diff --git a/src/main/java/ru/etu/petci/handlers/ContinueCommandHandler.java b/src/main/java/ru/etu/petci/handlers/ContinueCommandHandler.java index d372ada..a15e921 100644 --- a/src/main/java/ru/etu/petci/handlers/ContinueCommandHandler.java +++ b/src/main/java/ru/etu/petci/handlers/ContinueCommandHandler.java @@ -16,10 +16,6 @@ public class ContinueCommandHandler implements CommandHandler { private static final Logger LOGGER = Logger.getLogger(ContinueCommandHandler.class.getName()); - static { - LOGGER.setLevel(Level.INFO); - } - @Override public int handle(String[] args) { try { @@ -28,7 +24,7 @@ public int handle(String[] args) { repositoryObserver.setExecutor(new JobsExecutor(jobs)); repositoryObserver.start(); } catch (InterruptedException e) { - LOGGER.severe("The program was interrupted"); + LOGGER.warning("The program was interrupted"); Thread.currentThread().interrupt(); } catch (IOException e) { LOGGER.severe(e.getMessage()); diff --git a/src/main/java/ru/etu/petci/handlers/InitCommandHandler.java b/src/main/java/ru/etu/petci/handlers/InitCommandHandler.java index 50cb03f..b36aed9 100644 --- a/src/main/java/ru/etu/petci/handlers/InitCommandHandler.java +++ b/src/main/java/ru/etu/petci/handlers/InitCommandHandler.java @@ -27,8 +27,12 @@ public int handle(String[] args) { } try { - new File(JOBS_DIR).mkdirs(); - new File(LOGS_DIR).mkdirs(); + if (new File(JOBS_DIR).mkdirs()) { + LOGGER.log(Level.WARNING, "Unable to create {0} directory.", JOBS_DIR); + } + if (new File(LOGS_DIR).mkdirs()) { + LOGGER.log(Level.WARNING, "Unable to create {0} directory.", LOGS_DIR); + } Configurator.saveRepositoryConfig(branchName); } catch (IOException e) { diff --git a/src/main/java/ru/etu/petci/observers/RepositoryObserver.java b/src/main/java/ru/etu/petci/observers/RepositoryObserver.java index c0322ed..c884af1 100644 --- a/src/main/java/ru/etu/petci/observers/RepositoryObserver.java +++ b/src/main/java/ru/etu/petci/observers/RepositoryObserver.java @@ -23,9 +23,6 @@ public class RepositoryObserver { private final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); private static final Logger LOGGER = Logger.getLogger(RepositoryObserver.class.getName()); - static { - LOGGER.setLevel(Level.INFO); - } public static RepositoryObserver of(String branchName, String lastHash) { Objects.requireNonNull(branchName); @@ -71,14 +68,13 @@ private void checkRepositoryUpdate() { if (newHash.length() == 40 && !newHash.equals(lastHash)) { LOGGER.log(Level.INFO, "Commits checked. New commit was found. Hash: {0}", newHash); if (executor.runJobs()) { + LOGGER.fine("Jobs completed successfully. Update last commit hash."); lastHash = newHash; Configurator.saveRepositoryConfig(branchName, lastHash); } else { LOGGER.info("Running jobs failed. New commit has been rejected"); rejectCommit(); } - } else { - LOGGER.fine("Commits checked. No new commits found."); } } catch (IOException e) { LOGGER.severe(e.getMessage()); @@ -103,6 +99,7 @@ private void rejectCommit() throws IOException { throw new IOException(e); } catch (InterruptedException e) { Thread.currentThread().interrupt(); + LOGGER.warning("The program was interrupted."); } finally { if (gitProcess != null) { gitProcess.destroy(); diff --git a/src/main/resources/ru/etu/petci/logging.properties b/src/main/resources/ru/etu/petci/logging.properties new file mode 100644 index 0000000..26eab96 --- /dev/null +++ b/src/main/resources/ru/etu/petci/logging.properties @@ -0,0 +1,13 @@ +handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler +.level=INFO +java.util.logging.FileHandler.pattern=petCI/logs/%u.log +java.util.logging.FileHandler.limit=20000 +java.util.logging.FileHandler.count=1 +java.util.logging.FileHandler.maxLocks=5 +java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter +java.util.logging.FileHandler.level=FINE +java.util.logging.FileHandler.append=true + +java.util.logging.ConsoleHandler.level=INFO +java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter +java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n diff --git a/src/main/resources/ru/etu/petci/settings.properties b/src/main/resources/ru/etu/petci/settings.properties deleted file mode 100644 index f1aca49..0000000 --- a/src/main/resources/ru/etu/petci/settings.properties +++ /dev/null @@ -1 +0,0 @@ -JOBS_DIR_NAME = jobs \ No newline at end of file