2023-08-26 18:30:49 +02:00
|
|
|
package stirling.software.SPDF.config;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
|
|
|
import org.springframework.context.ApplicationContextInitializer;
|
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
|
|
|
|
public class ConfigInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void initialize(ConfigurableApplicationContext applicationContext) {
|
|
|
|
try {
|
|
|
|
ensureConfigExists();
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("Failed to initialize application configuration", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ensureConfigExists() throws IOException {
|
|
|
|
// Define the path to the external config directory
|
2023-08-26 23:33:23 +02:00
|
|
|
Path destPath = Paths.get("configs", "settings.yml");
|
2023-08-26 18:30:49 +02:00
|
|
|
|
|
|
|
// Check if the file already exists
|
|
|
|
if (Files.notExists(destPath)) {
|
|
|
|
// Ensure the destination directory exists
|
|
|
|
Files.createDirectories(destPath.getParent());
|
|
|
|
|
|
|
|
// Copy the resource from classpath to the external directory
|
2023-08-26 23:33:23 +02:00
|
|
|
try (InputStream in = getClass().getClassLoader().getResourceAsStream("settings.yml.template")) {
|
2023-08-26 18:30:49 +02:00
|
|
|
if (in != null) {
|
|
|
|
Files.copy(in, destPath);
|
|
|
|
} else {
|
2023-08-26 23:33:23 +02:00
|
|
|
throw new FileNotFoundException("Resource file not found: settings.yml.template");
|
2023-08-26 18:30:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|