Project

General

Profile

1
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 com.google.gson.JsonSyntaxException;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.springframework.beans.factory.BeanNameAware;
15

    
16
import com.google.common.collect.Maps;
17
import com.google.gson.Gson;
18
import com.googlecode.sarasvati.Engine;
19
import com.googlecode.sarasvati.NodeToken;
20
import com.googlecode.sarasvati.mem.MemNode;
21

    
22
import eu.dnetlib.conf.PropertyFetcher;
23

    
24
public abstract class SarasvatiJobNode extends MemNode implements BeanNameAware {
25

    
26
	/**
27
	 * logger.
28
	 */
29
	private static final Log log = LogFactory.getLog(SarasvatiJobNode.class);
30

    
31
	private String beanName;
32

    
33
	/**
34
	 * parameter values injected as json map into subclasses instance beans.
35
	 */
36
	private String params;
37

    
38
	/**
39
	 * parameter values retrieved from the sarasvati Env.
40
	 */
41
	private String envParams;
42

    
43
	/**
44
	 * parameter values retrieved from the system properties.
45
	 */
46
	private String sysParams;
47

    
48
	@Resource(name = "propertyFetcher")
49
	private PropertyFetcher propertyFetcher;
50

    
51
	@Override
52
	public void execute(final Engine engine, final NodeToken token) {
53
		final String prefix = "system:node:" + getName() + ":";
54

    
55
		for (Field field : getClass().getDeclaredFields()) {
56
			try {
57
				field.setAccessible(true);
58
				token.getEnv().setAttribute(prefix + field.getName(), "" + field.get(this));
59
			} catch (Exception e) {
60
				log.warn("Error accessing value of field: " + field.getName());
61
			}
62
		}
63
	}
64

    
65
	/**
66
	 * builds a parameter map overriding values having the same name in the following order: sysparams < envparams < params
67
	 * 
68
	 * @param token
69
	 * @return
70
	 */
71
	protected Map<String, String> parseJsonParameters(final NodeToken token) {
72
		final Map<String, String> res = Maps.newHashMap();
73
		for (Entry<?, ?> e : asMapEntries(getSysParams())) {
74
			String propertyName = e.getValue().toString();
75

    
76
			if (!getPropertyFetcher().getProps().containsKey(propertyName)) {
77
				log.warn("unable to find system property: '" + propertyName + "'");
78
			}
79

    
80
			String propertyValue = getPropertyFetcher().getProperty(propertyName);
81
			res.put(e.getKey().toString(), propertyValue);
82
		}
83
		for (Entry<?, ?> e : asMapEntries(getEnvParams())) {
84
			final String param = e.getValue().toString();
85

    
86
			if (token.getEnv().hasAttribute(param)) {
87
				res.put(e.getKey().toString(), token.getEnv().getAttribute(param));
88
			} else {
89
				res.put(e.getKey().toString(), token.getFullEnv().getAttribute(param));
90
			}
91
		}
92
		for (Entry<?, ?> e : asMapEntries(getParams())) {
93
			res.put(e.getKey().toString(), e.getValue().toString());
94
		}
95
		return res;
96
	}
97

    
98
	@SuppressWarnings("unchecked")
99
	private Set<Map.Entry<?, ?>> asMapEntries(final String s) {
100
		try {
101
			return isSet(s) ? new Gson().fromJson(s, Map.class).entrySet() : new HashSet<>();
102
		} catch(JsonSyntaxException e) {
103
			log.error(String.format("invalid json: %s", s));
104
			throw e;
105
		}
106
	}
107

    
108
	private boolean isSet(final String s) {
109
		return (s != null) && !s.isEmpty();
110
	}
111

    
112
	// ///////////////
113

    
114
	public String getBeanName() {
115
		return beanName;
116
	}
117

    
118
	@Override
119
	public void setBeanName(final String beanName) {
120
		this.beanName = beanName;
121
	}
122

    
123
	public PropertyFetcher getPropertyFetcher() {
124
		return propertyFetcher;
125
	}
126

    
127
	public String getParams() {
128
		return params;
129
	}
130

    
131
	public void setParams(final String params) {
132
		this.params = params;
133
	}
134

    
135
	public String getEnvParams() {
136
		return envParams;
137
	}
138

    
139
	public void setEnvParams(final String envParams) {
140
		this.envParams = envParams;
141
	}
142

    
143
	public String getSysParams() {
144
		return sysParams;
145
	}
146

    
147
	public void setSysParams(final String sysParams) {
148
		this.sysParams = sysParams;
149
	}
150

    
151
}
(5-5/9)