blob: a8ec0a0c59bcc079ba4cbb339634f03856c056ff [file] [log] [blame]
Michael Hanle8031912016-06-24 14:36:17 +02001package de.ids_mannheim.korap.config;
2
Michael Hanle8031912016-06-24 14:36:17 +02003import java.io.File;
4import java.io.FileInputStream;
5import java.io.IOException;
6import java.io.InputStream;
7import java.util.Properties;
8
margaretha49cb6882018-07-04 04:19:54 +02009import org.apache.logging.log4j.LogManager;
10import org.apache.logging.log4j.Logger;
11
Michael Hanle8031912016-06-24 14:36:17 +020012/**
13 * Created by hanl on 08.06.16.
14 */
15public class ConfigLoader {
16
margaretha49cb6882018-07-04 04:19:54 +020017 private static final Logger jlog = LogManager.getLogger(ConfigLoader.class);
Michael Hanle8031912016-06-24 14:36:17 +020018
Michael Hanle8031912016-06-24 14:36:17 +020019 private ConfigLoader () {}
20
Michael Hanle8031912016-06-24 14:36:17 +020021 public static InputStream loadConfigStream (String name) {
22 InputStream stream = null;
23 try {
24 File f = new File(System.getProperty("user.dir"), name);
25
26 if (f.exists()) {
27 jlog.info("Loading config '" + name + "' from file!");
28 stream = new FileInputStream(f);
29 }
30 else {
31 jlog.info("Loading config '" + name + "' from classpath!");
margaretha35e1ca22023-11-16 22:00:01 +010032 stream = ConfigLoader.class.getClassLoader()
33 .getResourceAsStream(name);
Michael Hanle8031912016-06-24 14:36:17 +020034 }
35 }
36 catch (IOException e) {
37 // do nothing
38 }
39 if (stream == null)
margaretha35e1ca22023-11-16 22:00:01 +010040 throw new RuntimeException(
41 "Config file '" + name + "' could not be loaded ...");
Michael Hanle8031912016-06-24 14:36:17 +020042 return stream;
43 }
44
margaretha35e1ca22023-11-16 22:00:01 +010045 public static Properties loadProperties (String name) {
Michael Hanle8031912016-06-24 14:36:17 +020046 Properties p = new Properties();
Michael Hanl99cb9632016-06-29 16:24:40 +020047 try {
48 p.load(loadConfigStream(name));
margaretha35e1ca22023-11-16 22:00:01 +010049 }
50 catch (IOException e) {
51 throw new RuntimeException("Properties from config file '" + name
52 + "' could not be loaded ...");
Michael Hanl99cb9632016-06-29 16:24:40 +020053 }
Michael Hanle8031912016-06-24 14:36:17 +020054 return p;
55 }
56
57}