Project

General

Profile

1
package eu.dnetlib.msro.worker.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 org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11
import org.springframework.beans.factory.BeanNameAware;
12

    
13
import com.google.common.collect.Maps;
14
import com.google.gson.Gson;
15
import com.googlecode.sarasvati.Engine;
16
import com.googlecode.sarasvati.NodeToken;
17
import com.googlecode.sarasvati.mem.MemNode;
18

    
19
public abstract class SarasvatiJobNode extends MemNode implements BeanNameAware {
20

    
21
	/**
22
	 * logger.
23
	 */
24
	private static final Log log = LogFactory.getLog(SarasvatiJobNode.class);
25

    
26
	private String beanName;
27

    
28
	/**
29
	 * parameter values injected as json map into subclasses instance beans.
30
	 */
31
	private String params;
32

    
33
	/**
34
	 * parameter values retrieved from the sarasvati Env.
35
	 */
36
	private String envParams;
37

    
38
	@Override
39
	public void execute(final Engine engine, final NodeToken token) {
40
		final String prefix = "system:node:" + getName() + ":";
41

    
42
		for (Field field : getClass().getDeclaredFields()) {
43
			try {
44
				field.setAccessible(true);
45
				token.getEnv().setAttribute(prefix + field.getName(), "" + field.get(this));
46
			} catch (Exception e) {
47
				log.warn("Error accessing value of field: " + field.getName());
48
			}
49
		}
50
	}
51

    
52
	/**
53
	 * builds a parameter map overriding values having the same name in the following order: sysparams < envparams < params
54
	 * 
55
	 * @param token
56
	 * @return
57
	 */
58
	protected Map<String, String> parseJsonParameters(final NodeToken token) {
59
		final Map<String, String> res = Maps.newHashMap();
60

    
61
		for (Entry<?, ?> e : asMapEntries(getEnvParams())) {
62
			final String param = e.getValue().toString();
63

    
64
			if (token.getEnv().hasAttribute(param)) {
65
				res.put(e.getKey().toString(), token.getEnv().getAttribute(param));
66
			} else {
67
				res.put(e.getKey().toString(), token.getFullEnv().getAttribute(param));
68
			}
69
		}
70
		for (Entry<?, ?> e : asMapEntries(getParams())) {
71
			res.put(e.getKey().toString(), e.getValue().toString());
72
		}
73
		return res;
74
	}
75

    
76
	@SuppressWarnings("unchecked")
77
	private Set<Map.Entry<?, ?>> asMapEntries(final String s) {
78
		return isSet(s) ? new Gson().fromJson(s, Map.class).entrySet() : new HashSet<Map.Entry<?, ?>>();
79
	}
80

    
81
	private boolean isSet(final String s) {
82
		return s != null && !s.isEmpty();
83
	}
84

    
85
	// ///////////////
86

    
87
	public String getBeanName() {
88
		return beanName;
89
	}
90

    
91
	@Override
92
	public void setBeanName(final String beanName) {
93
		this.beanName = beanName;
94
	}
95

    
96
	public String getParams() {
97
		return params;
98
	}
99

    
100
	public void setParams(final String params) {
101
		this.params = params;
102
	}
103

    
104
	public String getEnvParams() {
105
		return envParams;
106
	}
107

    
108
	public void setEnvParams(final String envParams) {
109
		this.envParams = envParams;
110
	}
111

    
112
}
(5-5/9)