| Michael Hanl | e803191 | 2016-06-24 14:36:17 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.config; |
| 2 | |
| Michael Hanl | e803191 | 2016-06-24 14:36:17 +0200 | [diff] [blame] | 3 | import java.io.File; |
| 4 | import java.io.FileInputStream; |
| 5 | import java.io.IOException; |
| 6 | import java.io.InputStream; |
| 7 | import java.util.Properties; |
| 8 | |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 9 | import org.apache.logging.log4j.LogManager; |
| 10 | import org.apache.logging.log4j.Logger; |
| 11 | |
| Michael Hanl | e803191 | 2016-06-24 14:36:17 +0200 | [diff] [blame] | 12 | /** |
| 13 | * Created by hanl on 08.06.16. |
| 14 | */ |
| 15 | public class ConfigLoader { |
| 16 | |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 17 | private static final Logger jlog = LogManager.getLogger(ConfigLoader.class); |
| Michael Hanl | e803191 | 2016-06-24 14:36:17 +0200 | [diff] [blame] | 18 | |
| 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 Hanl | 99cb963 | 2016-06-29 16:24:40 +0200 | [diff] [blame] | 47 | public static Properties loadProperties (String name){ |
| Michael Hanl | e803191 | 2016-06-24 14:36:17 +0200 | [diff] [blame] | 48 | Properties p = new Properties(); |
| Michael Hanl | 99cb963 | 2016-06-29 16:24:40 +0200 | [diff] [blame] | 49 | 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 Hanl | e803191 | 2016-06-24 14:36:17 +0200 | [diff] [blame] | 54 | return p; |
| 55 | } |
| 56 | |
| 57 | } |