-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
260 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 28 additions & 38 deletions
66
src/main/java/ru/etu/petci/configuration/Configurator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,57 @@ | ||
package ru.etu.petci.configuration; | ||
|
||
|
||
import ru.etu.petci.jobs.Job; | ||
import ru.etu.petci.observers.RepositoryObserver; | ||
|
||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.io.*; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
import java.util.Properties; | ||
|
||
import static ru.etu.petci.Main.JOBS_DIR_NAME; | ||
import static ru.etu.petci.Main.SETTINGS_FILE_NAME; | ||
|
||
public class Configurator { | ||
|
||
|
||
public RepositoryObserver readRepositoryConfig() { | ||
public RepositoryObserver readRepositoryConfig() throws IOException { | ||
RepositoryObserver observer = new RepositoryObserver(); | ||
try { | ||
Properties repoConfig = new Properties(); | ||
repoConfig.load(new FileReader("repository.properties")); | ||
if (!observer.setRepositoryPath(repoConfig.getProperty("repository_path"))) { | ||
System.exit(1); | ||
} | ||
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", null)); | ||
} catch (IOException e) { | ||
System.out.println("Error while reading repository config"); | ||
System.exit(1); | ||
observer.setLastHash(repoConfig.getProperty("last_hash")); | ||
} | ||
return observer; | ||
} | ||
|
||
|
||
public void saveRepositoryConfig(String repoPath, String branchName, String lastHash) { | ||
try { | ||
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"); | ||
properties.store(new FileWriter("repository.properties"), "Repository configuration"); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
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) { | ||
public void saveRepositoryConfig(String repoPath, String branchName) throws IOException { | ||
saveRepositoryConfig(repoPath, branchName, ""); | ||
} | ||
|
||
|
||
/** | ||
* <p>This method performs the check whether the current folder | ||
* is an application folder. An application folder must consist | ||
* of a configuration file with name {@link} | ||
* and a directory with name {@link}.</p> | ||
* <p>If there is no application directory, then the method returned false, | ||
* user should use "init" command.</p> | ||
*/ | ||
public boolean checkApplicationDir() { | ||
return Files.isDirectory(Path.of(JOBS_DIR_NAME)) && Files.isRegularFile(Path.of(SETTINGS_FILE_NAME)); | ||
public void saveJobConfig(Job job) { | ||
Objects.requireNonNull(job); | ||
try (var out = new BufferedWriter(new FileWriter(JOBS_DIR_NAME + File.separator + job.getName()))) { | ||
out.append(job.getName()); | ||
out.newLine(); | ||
out.append(job.getScriptFile().toString()); | ||
out.newLine(); | ||
out.append(String.valueOf(job.isActive())); | ||
} catch (IOException e) { | ||
System.out.println("Error with writing jobFile"); | ||
System.exit(1); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/ru/etu/petci/exceptions/RepositoryNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.etu.petci.exceptions; | ||
|
||
public class RepositoryNotFoundException extends Exception { | ||
public RepositoryNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/ru/etu/petci/handlers/ContinueCommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package ru.etu.petci.handlers; | ||
|
||
|
||
import ru.etu.petci.configuration.Configurator; | ||
import ru.etu.petci.exceptions.RepositoryNotFoundException; | ||
import ru.etu.petci.jobs.Job; | ||
import ru.etu.petci.jobs.JobsExecutor; | ||
import ru.etu.petci.observers.RepositoryObserver; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class ContinueCommandHandler implements CommandHandler { | ||
|
||
private static final Logger LOGGER; | ||
|
||
static { | ||
LOGGER = Logger.getLogger(ContinueCommandHandler.class.getName()); | ||
LOGGER.setLevel(Level.WARNING); | ||
} | ||
|
||
@Override | ||
public int handle(String arg) { | ||
Objects.requireNonNull(arg); | ||
var configurator = new Configurator(); | ||
try { | ||
RepositoryObserver repositoryObserver = configurator.readRepositoryConfig(); | ||
List<Job> jobs = new ArrayList<>(); | ||
// TODO Reading jobs from properties | ||
repositoryObserver.setExecutor(new JobsExecutor(jobs)); | ||
repositoryObserver.start(); | ||
} catch (InterruptedException e) { | ||
LOGGER.log(Level.SEVERE, "The program was interrupted"); | ||
Thread.currentThread().interrupt(); | ||
} catch (IOException | RepositoryNotFoundException e) { | ||
LOGGER.log(Level.SEVERE, e.getMessage()); | ||
return 1; | ||
} | ||
return 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package ru.etu.petci.jobs; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
public class Job { | ||
private boolean isActive = true; | ||
private String name; | ||
private final Path scriptFile; | ||
|
||
public Job(Path scriptFile, String name) { | ||
this.scriptFile = scriptFile.toAbsolutePath().normalize(); | ||
this.name = name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Path getScriptFile() { | ||
return scriptFile; | ||
} | ||
|
||
|
||
public void setActive(boolean active) { | ||
isActive = active; | ||
} | ||
|
||
public boolean isActive() { | ||
return isActive; | ||
} | ||
|
||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof Job job)) return false; | ||
return Objects.equals(name, job.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name); | ||
} | ||
|
||
|
||
public int execute() throws IOException { | ||
Process scriptProcess = Runtime.getRuntime().exec(scriptFile.toString()); | ||
try { | ||
var input = new BufferedReader(new InputStreamReader(scriptProcess.getInputStream())); | ||
String line; | ||
while ((line = input.readLine()) != null) { | ||
System.out.println(line); | ||
} | ||
scriptProcess.waitFor(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
return scriptProcess.exitValue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package ru.etu.petci.jobs; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class JobsExecutor { | ||
|
||
private final List<Job> jobs; | ||
private static final Logger LOGGER = Logger.getLogger(JobsExecutor.class.getName()); | ||
|
||
static { | ||
LOGGER.setLevel(Level.WARNING); | ||
} | ||
|
||
public JobsExecutor(List<Job> jobs) { | ||
this.jobs = jobs; | ||
} | ||
|
||
public void runJobs() throws IOException { | ||
Objects.requireNonNull(jobs); | ||
for (Job job : jobs) { | ||
System.out.println(job.getScriptFile().toAbsolutePath()); | ||
System.out.printf("Run job \"%s\"...%n%n", job.getName()); | ||
if (!job.isActive()) { | ||
System.out.println(": Deactivated"); | ||
continue; | ||
} | ||
if (job.execute() == 0) { | ||
System.out.printf("%n--Succeed--%n"); | ||
} else { | ||
System.out.printf("%n--Failed--%n"); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.