1
|
package eu.dnetlib.data.hadoop.config;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.Map.Entry;
|
5
|
import java.util.Properties;
|
6
|
|
7
|
import org.apache.commons.logging.Log;
|
8
|
import org.apache.commons.logging.LogFactory;
|
9
|
import org.apache.hadoop.conf.Configuration;
|
10
|
import org.springframework.beans.factory.FactoryBean;
|
11
|
import org.springframework.beans.factory.annotation.Required;
|
12
|
import org.springframework.core.io.Resource;
|
13
|
|
14
|
/**
|
15
|
* Factory bean for hadoop cluster configuration object
|
16
|
*
|
17
|
* @author claudio
|
18
|
*
|
19
|
*/
|
20
|
public class ConfigurationFactory implements FactoryBean<Configuration> {
|
21
|
|
22
|
private static final Log log = LogFactory.getLog(ConfigurationFactory.class); // NOPMD by marko on 11/24/08 5:02 PM
|
23
|
|
24
|
private Resource defaults;
|
25
|
|
26
|
public Configuration getConfiguration() {
|
27
|
try {
|
28
|
return getObject();
|
29
|
} catch (Exception e) {
|
30
|
throw new IllegalStateException("Unable to load hadoop configuration", e);
|
31
|
}
|
32
|
}
|
33
|
|
34
|
@Override
|
35
|
public Configuration getObject() throws Exception {
|
36
|
return initConfiguration(defaultProperties());
|
37
|
}
|
38
|
|
39
|
@Override
|
40
|
public Class<?> getObjectType() {
|
41
|
return Configuration.class;
|
42
|
}
|
43
|
|
44
|
@Override
|
45
|
public boolean isSingleton() {
|
46
|
return true;
|
47
|
}
|
48
|
|
49
|
private Configuration initConfiguration(final Properties p) {
|
50
|
final Configuration conf = new Configuration(false);
|
51
|
for (Entry<Object, Object> e : p.entrySet()) {
|
52
|
conf.set(e.getKey().toString(), e.getValue().toString());
|
53
|
log.info(e.getKey().toString() + ": " + conf.get(e.getKey().toString()));
|
54
|
}
|
55
|
return conf;
|
56
|
}
|
57
|
|
58
|
private Properties defaultProperties() throws IOException {
|
59
|
Properties p = new Properties();
|
60
|
p.load(defaults.getInputStream());
|
61
|
return p;
|
62
|
}
|
63
|
|
64
|
public Resource getDefaults() {
|
65
|
return defaults;
|
66
|
}
|
67
|
|
68
|
@Required
|
69
|
public void setDefaults(Resource defaults) {
|
70
|
this.defaults = defaults;
|
71
|
}
|
72
|
|
73
|
}
|