1 |
26600
|
sandro.lab
|
package eu.dnetlib.msro.workflows.nodes;
|
2 |
|
|
|
3 |
|
|
import java.lang.reflect.Field;
|
4 |
|
|
import java.util.HashSet;
|
5 |
|
|
import java.util.Map;
|
6 |
|
|
import java.util.Map.Entry;
|
7 |
|
|
import java.util.Set;
|
8 |
|
|
|
9 |
|
|
import javax.annotation.Resource;
|
10 |
|
|
|
11 |
|
|
import org.apache.commons.logging.Log;
|
12 |
|
|
import org.apache.commons.logging.LogFactory;
|
13 |
|
|
import org.springframework.beans.factory.BeanNameAware;
|
14 |
|
|
|
15 |
|
|
import com.google.common.collect.Maps;
|
16 |
|
|
import com.google.gson.Gson;
|
17 |
|
|
import com.googlecode.sarasvati.Engine;
|
18 |
|
|
import com.googlecode.sarasvati.NodeToken;
|
19 |
|
|
import com.googlecode.sarasvati.mem.MemNode;
|
20 |
|
|
|
21 |
|
|
import eu.dnetlib.conf.PropertyFetcher;
|
22 |
|
|
|
23 |
|
|
public abstract class SarasvatiJobNode extends MemNode implements BeanNameAware {
|
24 |
|
|
|
25 |
|
|
/**
|
26 |
|
|
* logger.
|
27 |
|
|
*/
|
28 |
|
|
private static final Log log = LogFactory.getLog(SarasvatiJobNode.class);
|
29 |
|
|
|
30 |
|
|
private String beanName;
|
31 |
|
|
|
32 |
|
|
/**
|
33 |
|
|
* parameter values injected as json map into subclasses instance beans.
|
34 |
|
|
*/
|
35 |
|
|
private String params;
|
36 |
|
|
|
37 |
|
|
/**
|
38 |
|
|
* parameter values retrieved from the sarasvati Env.
|
39 |
|
|
*/
|
40 |
|
|
private String envParams;
|
41 |
|
|
|
42 |
|
|
/**
|
43 |
|
|
* parameter values retrieved from the system properties.
|
44 |
|
|
*/
|
45 |
|
|
private String sysParams;
|
46 |
|
|
|
47 |
|
|
@Resource(name = "propertyFetcher")
|
48 |
|
|
private PropertyFetcher propertyFetcher;
|
49 |
|
|
|
50 |
|
|
@Override
|
51 |
|
|
public void execute(final Engine engine, final NodeToken token) {
|
52 |
|
|
final String prefix = "system:node:" + getName() + ":";
|
53 |
|
|
|
54 |
|
|
for (Field field : getClass().getDeclaredFields()) {
|
55 |
|
|
try {
|
56 |
|
|
field.setAccessible(true);
|
57 |
|
|
token.getEnv().setAttribute(prefix + field.getName(), "" + field.get(this));
|
58 |
|
|
} catch (Exception e) {
|
59 |
|
|
log.warn("Error accessing value of field: " + field.getName());
|
60 |
|
|
}
|
61 |
|
|
}
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
/**
|
65 |
|
|
* builds a parameter map overriding values having the same name in the following order: sysparams < envparams < params
|
66 |
|
|
*
|
67 |
|
|
* @param token
|
68 |
|
|
* @return
|
69 |
|
|
*/
|
70 |
|
|
protected Map<String, String> parseJsonParameters(final NodeToken token) {
|
71 |
|
|
final Map<String, String> res = Maps.newHashMap();
|
72 |
|
|
for (Entry<?, ?> e : asMapEntries(getSysParams())) {
|
73 |
|
|
String propertyName = e.getValue().toString();
|
74 |
|
|
|
75 |
|
|
if (!getPropertyFetcher().getProps().containsKey(propertyName)) {
|
76 |
|
|
log.warn("unable to find system property: '" + propertyName + "'");
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
String propertyValue = getPropertyFetcher().getProperty(propertyName);
|
80 |
|
|
res.put(e.getKey().toString(), propertyValue);
|
81 |
|
|
}
|
82 |
|
|
for (Entry<?, ?> e : asMapEntries(getEnvParams())) {
|
83 |
|
|
final String param = e.getValue().toString();
|
84 |
|
|
|
85 |
|
|
if (token.getEnv().hasAttribute(param)) {
|
86 |
|
|
res.put(e.getKey().toString(), token.getEnv().getAttribute(param));
|
87 |
|
|
} else {
|
88 |
|
|
res.put(e.getKey().toString(), token.getFullEnv().getAttribute(param));
|
89 |
|
|
}
|
90 |
|
|
}
|
91 |
|
|
for (Entry<?, ?> e : asMapEntries(getParams())) {
|
92 |
|
|
res.put(e.getKey().toString(), e.getValue().toString());
|
93 |
|
|
}
|
94 |
|
|
return res;
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
@SuppressWarnings("unchecked")
|
98 |
|
|
private Set<Map.Entry<?, ?>> asMapEntries(final String s) {
|
99 |
|
|
return isSet(s) ? new Gson().fromJson(s, Map.class).entrySet() : new HashSet<Map.Entry<?, ?>>();
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
private boolean isSet(final String s) {
|
103 |
|
|
return (s != null) && !s.isEmpty();
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
// ///////////////
|
107 |
|
|
|
108 |
|
|
public String getBeanName() {
|
109 |
|
|
return beanName;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
@Override
|
113 |
|
|
public void setBeanName(final String beanName) {
|
114 |
|
|
this.beanName = beanName;
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
public PropertyFetcher getPropertyFetcher() {
|
118 |
|
|
return propertyFetcher;
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
public String getParams() {
|
122 |
|
|
return params;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
public void setParams(final String params) {
|
126 |
|
|
this.params = params;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
public String getEnvParams() {
|
130 |
|
|
return envParams;
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
public void setEnvParams(final String envParams) {
|
134 |
|
|
this.envParams = envParams;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
public String getSysParams() {
|
138 |
|
|
return sysParams;
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
public void setSysParams(final String sysParams) {
|
142 |
|
|
this.sysParams = sysParams;
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
}
|