Initial commit for site for forms
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package com.alttd.forms.properties;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Optional;
|
||||
|
||||
public class JarPath {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(JarPath.class);
|
||||
|
||||
public static Optional<File> getCurrentJarDir() {
|
||||
try {
|
||||
String path = JarPath.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
|
||||
File parentFile = new File(path).getParentFile();
|
||||
if (!parentFile.isDirectory()) {
|
||||
logger.error("Parent file of jar is not a directory");
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(parentFile);
|
||||
} catch (Exception e) {
|
||||
logger.error("Error getting current jar directory", e);
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.alttd.forms.properties;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
|
||||
public class PropertiesLoader {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
|
||||
|
||||
public static Optional<Properties> loadProperties(String fileName) {
|
||||
Properties prop = new Properties();
|
||||
Optional<File> currentJarPath = JarPath.getCurrentJarDir();
|
||||
if (currentJarPath.isEmpty()) {
|
||||
logger.error("Unable to find jar path to load properties (" + fileName + ")");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
File file = new File(currentJarPath.get(), fileName);
|
||||
if (!file.exists()) {
|
||||
logger.warn("Tried to load properties file that doesnt exists (" + fileName + ")");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
if (!file.canRead()) {
|
||||
logger.error("Unable to read properties file (" + fileName + ")");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
if (!file.isFile()) {
|
||||
logger.error("Invalid properties file (" + fileName + ")");
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
try (FileInputStream input = new FileInputStream(file)) {
|
||||
prop.load(input);
|
||||
return Optional.of(prop);
|
||||
} catch (IOException ex) {
|
||||
logger.error("Failed to load properties due to an unexpected error", ex);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.alttd.forms.properties;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
|
||||
public class PropertiesWriter {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PropertiesWriter.class);
|
||||
|
||||
public static void writeProperties(Properties prop, String fileName) {
|
||||
Optional<File> currentJarPath = JarPath.getCurrentJarDir();
|
||||
if (currentJarPath.isEmpty()) {
|
||||
logger.error("Failed to get current jar directory");
|
||||
return;
|
||||
}
|
||||
File file = new File(currentJarPath.get(), fileName);
|
||||
if (file.exists()) {
|
||||
logger.error("File already exists (" + fileName + ")");
|
||||
return;
|
||||
}
|
||||
|
||||
try (FileOutputStream output = new FileOutputStream(file)) {
|
||||
logger.debug("Creating new properties file at " + file.getAbsolutePath());
|
||||
prop.store(output, null);
|
||||
} catch (IOException ex) {
|
||||
logger.error("Failed to write properties to file", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user