Project

General

Profile

1
package eu.dnetlib.dhp.common.oozie.property;
2

    
3
import java.io.File;
4
import java.io.FileOutputStream;
5
import java.io.OutputStream;
6
import java.util.Collections;
7
import java.util.Map;
8
import java.util.Properties;
9

    
10
import eu.dnetlib.dhp.common.java.PortBindings;
11
import eu.dnetlib.dhp.common.java.Process;
12
import eu.dnetlib.dhp.common.java.porttype.PortType;
13
import org.apache.hadoop.conf.Configuration;
14

    
15
import static eu.dnetlib.dhp.common.WorkflowRuntimeParameters.OOZIE_ACTION_OUTPUT_FILENAME;
16

    
17
/**
18
 * This process is a solution for setting dynamic properties in oozie workflow definition.
19
 * 
20
 * Expects three parameters to be provided: the first 'condition' parameter is boolean value 
21
 * based on which either first 'inCaseOfTrue' or second 'elseCase' parameter value is set as 
22
 * the 'result' property.
23
 *  
24
 * This can be understood as the: 
25
 * 
26
 * condition ? inCaseOfTrue : elseCase
27
 * 
28
 * java syntax equivalent.
29
 * 
30
 * @author mhorst
31
 *
32
 */
33
public class ConditionalPropertySetter implements Process {
34

    
35
	public static final String PARAM_CONDITION = "condition";
36
	public static final String PARAM_INCASEOFTRUE = "inCaseOfTrue";
37
	public static final String PARAM_ELSECASE = "elseCase";
38
	
39
	public static final String OUTPUT_PROPERTY_RESULT = "result";
40
	
41
	@Override
42
	public Map<String, PortType> getInputPorts() {
43
		return Collections.emptyMap();
44
	}
45

    
46
	@Override
47
	public Map<String, PortType> getOutputPorts() {
48
		return Collections.emptyMap();
49
	}
50

    
51
	@Override
52
	public void run(PortBindings portBindings, Configuration conf,
53
			Map<String, String> parameters) throws Exception {
54

    
55
		String condition = parameters.get(PARAM_CONDITION);
56
		if (condition == null) {
57
			throw new RuntimeException("unable to make decision: " + 
58
					PARAM_CONDITION + " parameter was not set!");
59
		}
60

    
61
		Properties props = new Properties();
62
        props.setProperty(OUTPUT_PROPERTY_RESULT, 
63
        		Boolean.parseBoolean(condition)?
64
        				parameters.get(PARAM_INCASEOFTRUE):
65
        					parameters.get(PARAM_ELSECASE));
66
        OutputStream os = new FileOutputStream(
67
        		new File(System.getProperty(OOZIE_ACTION_OUTPUT_FILENAME)));
68
        try {
69
        	props.store(os, "");	
70
        } finally {
71
        	os.close();	
72
        }
73

    
74
	}
75

    
76
}
    (1-1/1)