blob: 9fcc3bd9e569834910a9e94fd7bbd9f2a0c278c3 [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
19
20 private ConfigLoader () {}
21
22
23 public static InputStream loadConfigStream (String name) {
24 InputStream stream = null;
25 try {
26 File f = new File(System.getProperty("user.dir"), name);
27
28 if (f.exists()) {
29 jlog.info("Loading config '" + name + "' from file!");
30 stream = new FileInputStream(f);
31 }
32 else {
33 jlog.info("Loading config '" + name + "' from classpath!");
34 stream = ConfigLoader.class.getClassLoader().getResourceAsStream(
35 name);
36 }
37 }
38 catch (IOException e) {
39 // do nothing
40 }
41 if (stream == null)
42 throw new RuntimeException("Config file '"+name+"' could not be loaded ...");
43 return stream;
44 }
45
46
Michael Hanl99cb9632016-06-29 16:24:40 +020047 public static Properties loadProperties (String name){
Michael Hanle8031912016-06-24 14:36:17 +020048 Properties p = new Properties();
Michael Hanl99cb9632016-06-29 16:24:40 +020049 try {
50 p.load(loadConfigStream(name));
51 } catch (IOException e) {
52 throw new RuntimeException("Properties from config file '"+name+"' could not be loaded ...");
53 }
Michael Hanle8031912016-06-24 14:36:17 +020054 return p;
55 }
56
57}