-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathConfig.java
More file actions
88 lines (74 loc) · 2.75 KB
/
Config.java
File metadata and controls
88 lines (74 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.codefork.refine;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Application configuration
*/
@Component
public class Config {
public static final String PROP_CACHE_ENABLED = "cache.enabled";
public static final String PROP_CACHE_TTL = "cache.ttl";
public static final String PROP_CACHE_SIZE = "cache.size";
public static final String PROP_DATASOURCE_THREADPOOL_SIZE = "threadpool.size";
public static final String PROP_DATASOURCE_DELAY = "delay";
private static final String CONFIG_FILENAME = "conciliator.properties";
private Log log = LogFactory.getLog(Config.class);
private Properties properties = new Properties();
public Config() {
properties.put(PROP_CACHE_ENABLED, "true");
properties.put(PROP_CACHE_TTL, "3600");
properties.put(PROP_CACHE_SIZE, "64MB");
properties.put("datasource.orcid.name", "ORCID");
properties.put("datasource.orcidsmartnames.name", "ORCID - Smart Names Mode");
properties.put("datasource.openlibrary.name", "OpenLibrary");
loadFromFile();
}
public void loadFromFile() {
if(new File(CONFIG_FILENAME).exists()) {
try {
log.info("Loading configuration from " + CONFIG_FILENAME);
Properties p = new Properties();
p.load(new FileInputStream(CONFIG_FILENAME));
merge(p);
} catch (IOException ex) {
log.error("Error reading config file, skipping it: " + ex);
}
}
}
public void merge(Properties properties) {
this.properties.putAll(properties);
}
public Properties getProperties() {
return properties;
}
/**
* Returns the properties relevant to a given data source name,
* with the datasource.NAME prefix stripped off.
*
* e.g.
* datasource.mysource.timeout=2000
*
* the Properties object returned will have key "timeout" with value "2000"
*
* @param name
* @return
*/
public Properties getDataSourceProperties(String name) {
Properties props = new Properties();
for(String key : getProperties().stringPropertyNames()) {
String prefix = "datasource." + name;
if(key.startsWith(prefix)) {
String shortenedKey = key.substring(prefix.length()).replaceAll("^\\.+", "");
if(shortenedKey.length() > 0) {
props.setProperty(shortenedKey, getProperties().getProperty(key));
}
}
}
return props;
}
}