diff --git a/src/main/java/ru/etu/petci/Main.java b/src/main/java/ru/etu/petci/Main.java index 59c819e..8973689 100644 --- a/src/main/java/ru/etu/petci/Main.java +++ b/src/main/java/ru/etu/petci/Main.java @@ -5,14 +5,10 @@ import ru.etu.petci.handlers.ContinueCommandHandler; import ru.etu.petci.handlers.InitCommandHandler; -import java.io.IOException; -import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; public class Main { - public static final String SETTINGS_FILE_NAME = "settings.properties"; - public static final String JOBS_SETTINGS_FILE = "jobs.properties"; private static final Logger LOGGER = Logger.getLogger(Main.class.getName()); static { @@ -22,15 +18,6 @@ public class Main { public static void main(String[] args) { - try { - Properties settings = new Properties(); - settings.load(Main.class.getClassLoader().getResourceAsStream(SETTINGS_FILE_NAME)); - settings.getProperty("JOBS_DIR_NAME", "jobs"); - } catch (IOException e) { - System.out.println("Unable to read settings property"); - System.exit(1); - } - int exitStatus; if (args.length > 0) { CommandHandler handler; diff --git a/src/main/java/ru/etu/petci/configuration/Configurator.java b/src/main/java/ru/etu/petci/configuration/Configurator.java index 548405e..e12e882 100644 --- a/src/main/java/ru/etu/petci/configuration/Configurator.java +++ b/src/main/java/ru/etu/petci/configuration/Configurator.java @@ -4,73 +4,58 @@ import ru.etu.petci.jobs.Job; import ru.etu.petci.observers.RepositoryObserver; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; -import java.util.Properties; - -import static ru.etu.petci.Main.JOBS_SETTINGS_FILE; +import java.util.prefs.BackingStoreException; +import java.util.prefs.Preferences; public class Configurator { + public static final String JOBS_PREFERENCES = "petCI/jobs"; + public static final String REPOSITORY_PREFERENCES = "petCI/repositorySettings"; - public RepositoryObserver readRepositoryConfig() throws IOException { - RepositoryObserver observer = new RepositoryObserver(); - Properties repoConfig = new Properties(); - try (var propertyReader = new FileReader("repository.properties")) { - repoConfig.load(propertyReader); - observer.setRepositoryPath(repoConfig.getProperty("repository_path")); - observer.setBranchName(repoConfig.getProperty("branch_name", "master")); - observer.setLastHash(repoConfig.getProperty("last_hash")); - } + public RepositoryObserver readRepositoryConfig() { + var observer = new RepositoryObserver(); + var repoPreferences = Preferences.userRoot().node(REPOSITORY_PREFERENCES); + observer.setRepositoryPath(repoPreferences.get("repository_path", ".")); + observer.setBranchName(repoPreferences.get("branch_name", "master")); + observer.setLastHash(repoPreferences.get("last_hash", "")); return observer; } - - public void saveRepositoryConfig(String repoPath, String branchName, String lastHash) throws IOException { - var properties = new Properties(); - properties.setProperty("repository_path", Path.of(repoPath).toAbsolutePath().normalize().toString()); - properties.setProperty("branch_name", branchName); - properties.setProperty("last_hash", lastHash.length() == 40 ? lastHash : "null"); - try (var propertyWriter = new FileWriter("repository.properties")) { - properties.store(propertyWriter, "Repository configuration"); - } + public void saveRepositoryConfig(String repoPath, String branchName, String lastHash) { + Preferences repoPreferences = Preferences.userRoot().node(REPOSITORY_PREFERENCES); + repoPreferences.put("repository_path", Path.of(repoPath).toAbsolutePath().normalize().toString()); + repoPreferences.put("branch_name", branchName); + repoPreferences.put("last_hash", lastHash.length() == 40 ? lastHash : "null"); } - public void saveRepositoryConfig(String repoPath, String branchName) throws IOException { + public void saveRepositoryConfig(String repoPath, String branchName) { saveRepositoryConfig(repoPath, branchName, ""); } - public List readJobsConfig() throws IOException { + public List readJobsConfig() throws BackingStoreException { List jobsList = new ArrayList<>(); - try (var propertiesReader = new FileReader(JOBS_SETTINGS_FILE)) { - var properties = new Properties(); - properties.load(propertiesReader); - for (String jobName : properties.stringPropertyNames()) { - Path scriptPath = Path.of(properties.getProperty(jobName)); - jobsList.add(new Job(scriptPath, jobName)); - } + var jobsPreferences = Preferences.userRoot().node(JOBS_PREFERENCES); + for (String jobName : jobsPreferences.keys()) { + Path scriptPath = Path.of(jobsPreferences.get(jobName, null)); + jobsList.add(new Job(scriptPath, jobName)); } return jobsList; } - public void saveJobsConfig(String jobName, String scriptPath) throws IOException { - try (var propertiesReader = new FileReader(JOBS_SETTINGS_FILE); - var propertiesWriter = new FileWriter(JOBS_SETTINGS_FILE)) { - var properties = new Properties(); - properties.load(propertiesReader); - if (properties.getProperty(jobName) == null) { - properties.setProperty(jobName, Path.of(scriptPath).toAbsolutePath().normalize().toString()); - properties.store(propertiesWriter, "Jobs settings properties"); - } else { - System.out.printf("Job with name \"%s\" has already existed%n", jobName); - } + public void saveJobsConfig(String jobName, String scriptPath) { + var jobsPreferences = Preferences.userRoot().node(JOBS_PREFERENCES); + + if (jobsPreferences.get(jobName, null) == null) { + jobsPreferences.put(jobName, Path.of(scriptPath).toAbsolutePath().normalize().toString()); + } else { + System.out.printf("Job with name \"%s\" has already existed%n", jobName); } } - } + + diff --git a/src/main/java/ru/etu/petci/handlers/AddCommandHandler.java b/src/main/java/ru/etu/petci/handlers/AddCommandHandler.java index 505152a..6a3c290 100644 --- a/src/main/java/ru/etu/petci/handlers/AddCommandHandler.java +++ b/src/main/java/ru/etu/petci/handlers/AddCommandHandler.java @@ -2,7 +2,6 @@ import ru.etu.petci.configuration.Configurator; -import java.io.IOException; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; @@ -35,14 +34,8 @@ public int handle(String arg) { return 1; } + configurator.saveJobsConfig(jobName, scriptPath); scanner.close(); - - try { - configurator.saveJobsConfig(jobName, scriptPath); - } catch (IOException e) { - LOGGER.severe(e.getMessage()); - return 1; - } return 0; } } diff --git a/src/main/java/ru/etu/petci/handlers/ContinueCommandHandler.java b/src/main/java/ru/etu/petci/handlers/ContinueCommandHandler.java index 0b0f428..42ac818 100644 --- a/src/main/java/ru/etu/petci/handlers/ContinueCommandHandler.java +++ b/src/main/java/ru/etu/petci/handlers/ContinueCommandHandler.java @@ -7,11 +7,11 @@ import ru.etu.petci.jobs.JobsExecutor; import ru.etu.petci.observers.RepositoryObserver; -import java.io.IOException; import java.util.List; import java.util.Objects; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.prefs.BackingStoreException; public class ContinueCommandHandler implements CommandHandler { @@ -34,7 +34,7 @@ public int handle(String arg) { } catch (InterruptedException e) { LOGGER.log(Level.SEVERE, "The program was interrupted"); Thread.currentThread().interrupt(); - } catch (IOException | RepositoryNotFoundException e) { + } catch (RepositoryNotFoundException | BackingStoreException e) { LOGGER.log(Level.SEVERE, e.getMessage()); } return 1; diff --git a/src/main/java/ru/etu/petci/handlers/InitCommandHandler.java b/src/main/java/ru/etu/petci/handlers/InitCommandHandler.java index d363f2d..1580ecc 100644 --- a/src/main/java/ru/etu/petci/handlers/InitCommandHandler.java +++ b/src/main/java/ru/etu/petci/handlers/InitCommandHandler.java @@ -2,15 +2,11 @@ import ru.etu.petci.configuration.Configurator; -import java.io.IOException; -import java.nio.file.Path; import java.util.Objects; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; -import static ru.etu.petci.Main.JOBS_SETTINGS_FILE; - public class InitCommandHandler implements CommandHandler { private static final Logger LOGGER = Logger.getLogger(InitCommandHandler.class.getName()); @@ -46,24 +42,10 @@ public int handle(String arg) { scanner.close(); - - - try { - if (Path.of(JOBS_SETTINGS_FILE).toFile().createNewFile()) { - LOGGER.log(Level.INFO, "File \"{0}\" has been created", JOBS_SETTINGS_FILE); - } - else { - LOGGER.log(Level.INFO, "File \"{0}\" has already existed", JOBS_SETTINGS_FILE); - } - - if (branchName.isEmpty()) { - configurator.saveRepositoryConfig(repositoryPath, "master"); - } else { - configurator.saveRepositoryConfig(repositoryPath, branchName); - } - } catch (IOException e) { - LOGGER.severe(e.getMessage()); - return 1; + if (branchName.isEmpty()) { + configurator.saveRepositoryConfig(repositoryPath, "master"); + } else { + configurator.saveRepositoryConfig(repositoryPath, branchName); } System.out.println("Successful initialization!"); diff --git a/src/main/java/ru/etu/petci/observers/RepositoryObserver.java b/src/main/java/ru/etu/petci/observers/RepositoryObserver.java index fb9838e..260672e 100644 --- a/src/main/java/ru/etu/petci/observers/RepositoryObserver.java +++ b/src/main/java/ru/etu/petci/observers/RepositoryObserver.java @@ -3,20 +3,22 @@ import ru.etu.petci.exceptions.RepositoryNotFoundException; import ru.etu.petci.jobs.JobsExecutor; -import java.io.*; +import java.io.FileNotFoundException; +import java.io.IOException; import java.nio.file.Path; import java.util.Objects; -import java.util.Properties; import java.util.Scanner; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.prefs.Preferences; + +import static ru.etu.petci.configuration.Configurator.REPOSITORY_PREFERENCES; public class RepositoryObserver { - public static final String REPOSITORY_PROPERTIES = "repository.properties"; private Path repositoryPath; private String lastHash; // Hash of last commit private String branchName; // Name of the observed branch @@ -85,18 +87,9 @@ private void checkRepositoryUpdate() { try (Scanner scanner = new Scanner(lastCommitMaster.toFile())) { String currentHash = scanner.nextLine(); if (currentHash.length() == 40 && !currentHash.equals(lastHash)) { + // Update hash lastHash = currentHash; - - // Save new hash to properties - Properties properties = new Properties(); - try (var propertyReader = new FileReader(REPOSITORY_PROPERTIES); - var propertyWriter = new FileWriter(REPOSITORY_PROPERTIES)) { - properties.load(propertyReader); - properties.setProperty("repository_path", repositoryPath.toString()); - properties.setProperty("branch_name", branchName); - properties.setProperty("last_hash", lastHash); - properties.store(propertyWriter, "repository settings"); - } + Preferences.userRoot().node(REPOSITORY_PREFERENCES).put("last_hash", lastHash); LOGGER.log(Level.INFO, "Commits checked. New commit was found. Hash: {0}", lastHash); executor.runJobs();