-
Notifications
You must be signed in to change notification settings - Fork 4
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
24 changed files
with
196 additions
and
67 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
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
13 changes: 13 additions & 0 deletions
13
...in/java/io/github/vincemann/subtitleBuddy/config/configFileManager/ConfigFileManager.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,13 @@ | ||
package io.github.vincemann.subtitleBuddy.config.configFileManager; | ||
|
||
import java.io.File; | ||
|
||
public interface ConfigFileManager { | ||
|
||
/** | ||
* Finds the ConfigFile with name {@param fileName}. | ||
* ConfigFile must be writable. | ||
* @return FileObject representing ConfigFile | ||
*/ | ||
public File findConfigFile(String fileName) throws ConfigFileManagerException; | ||
} |
23 changes: 23 additions & 0 deletions
23
...o/github/vincemann/subtitleBuddy/config/configFileManager/ConfigFileManagerException.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,23 @@ | ||
package io.github.vincemann.subtitleBuddy.config.configFileManager; | ||
|
||
public class ConfigFileManagerException extends Exception { | ||
|
||
public ConfigFileManagerException() { | ||
} | ||
|
||
public ConfigFileManagerException(String message) { | ||
super(message); | ||
} | ||
|
||
public ConfigFileManagerException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public ConfigFileManagerException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public ConfigFileManagerException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
.../github/vincemann/subtitleBuddy/config/configFileManager/ExtractingConfigFileManager.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,74 @@ | ||
package io.github.vincemann.subtitleBuddy.config.configFileManager; | ||
|
||
import io.github.vincemann.subtitleBuddy.classpathFileFinder.LoadedClassPathFile; | ||
import io.github.vincemann.subtitleBuddy.classpathFileFinder.ReadOnlyClassPathFileFinder; | ||
import io.github.vincemann.subtitleBuddy.runningExecutableFinder.RunningExecutableFinder; | ||
import io.github.vincemann.subtitleBuddy.runningExecutableFinder.RunningExecutableNotFoundException; | ||
import lombok.extern.log4j.Log4j; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardCopyOption; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* Finds a configFile, with a specified name, inside the jar and extracts it to the folder, in which the jar is located. | ||
* If the configFile, with the specified name, is already in the folder, than this file is taken. | ||
* | ||
* This process ensures, that the file is editable. (And also easily editable by the user) | ||
*/ | ||
@Singleton | ||
@Log4j | ||
public class ExtractingConfigFileManager implements ConfigFileManager { | ||
|
||
private RunningExecutableFinder runningExecutableFinder; | ||
private ReadOnlyClassPathFileFinder readOnlyClassPathFileFinder; | ||
|
||
@Inject | ||
public ExtractingConfigFileManager(RunningExecutableFinder runningExecutableFinder, ReadOnlyClassPathFileFinder readOnlyClassPathFileFinder) { | ||
this.runningExecutableFinder = runningExecutableFinder; | ||
this.readOnlyClassPathFileFinder = readOnlyClassPathFileFinder; | ||
} | ||
|
||
@Override | ||
public File findConfigFile(String fileName) throws ConfigFileManagerException{ | ||
try { | ||
//is file in folder of jar? | ||
Path runningExecutable = runningExecutableFinder.findRunningExecutable(); | ||
Path jarFolder = runningExecutable.getParent(); | ||
AtomicReference<Path> foundConfigFileInJarFolder = new AtomicReference<>(); | ||
Files.walk(jarFolder, 1).forEach(path -> { | ||
if (path.getFileName().equals(Paths.get(fileName))) { | ||
//we found the configFile | ||
foundConfigFileInJarFolder.set(path); | ||
} | ||
}); | ||
if (foundConfigFileInJarFolder.get() != null) { | ||
//we found the configFile | ||
return foundConfigFileInJarFolder.get().toFile(); | ||
} | ||
//the config file is not in the folder of the jar | ||
//lets continue searching in the jar | ||
LoadedClassPathFile classPathFile = readOnlyClassPathFileFinder.findFileOnClassPath(fileName); | ||
//copy file from classPath to folder of jar | ||
Path targetPath = jarFolder.resolve(fileName); | ||
Files.copy(Paths.get(classPathFile.getFile().toURI()),targetPath, StandardCopyOption.COPY_ATTRIBUTES); | ||
//assert that copy operation worked | ||
File targetConfigFile = targetPath.toFile(); | ||
if(!targetConfigFile.exists()){ | ||
throw new FileNotFoundException("Copy Operation failed. Config File not found in jar Directory"); | ||
}else { | ||
return targetConfigFile; | ||
} | ||
}catch (RunningExecutableNotFoundException|IOException e) { | ||
throw new ConfigFileManagerException(e); | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.