Skip to content

Commit

Permalink
Merge pull request #15 from jonx8/configurator
Browse files Browse the repository at this point in the history
Change properties files to preferences
  • Loading branch information
jonx8 authored Sep 9, 2023
2 parents 9903dc7 + 2595a41 commit 1aa2350
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 104 deletions.
13 changes: 0 additions & 13 deletions src/main/java/ru/etu/petci/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
75 changes: 30 additions & 45 deletions src/main/java/ru/etu/petci/configuration/Configurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Job> readJobsConfig() throws IOException {
public List<Job> readJobsConfig() throws BackingStoreException {
List<Job> 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);
}
}

}


9 changes: 1 addition & 8 deletions src/main/java/ru/etu/petci/handlers/AddCommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
Expand Down
26 changes: 4 additions & 22 deletions src/main/java/ru/etu/petci/handlers/InitCommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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!");
Expand Down
21 changes: 7 additions & 14 deletions src/main/java/ru/etu/petci/observers/RepositoryObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 1aa2350

Please sign in to comment.