Project

General

Profile

1
package eu.dnetlib.dhp.common.java;
2

    
3
import java.lang.reflect.Constructor;
4

    
5
import org.apache.commons.cli.CommandLine;
6

    
7
/**
8
 * Handles parsing the command line arguments provided by the Oozie
9
 * to create a {@link Process}
10
 * @author Mateusz Kobos
11
 *
12
 */
13
public class CmdLineParserForProcessConstruction {
14
	public Process run(CommandLine cmdLine){
15
		String[] args = cmdLine.getArgs();
16
		if(args.length != 1){
17
			throw new CmdLineParserException("The name of the class has "+
18
					"to be specified as the first agrument");
19
		}
20
		String className = args[0];
21
		
22
		String[] constructorParams = cmdLine.getOptionValues(
23
				CmdLineParser.constructorPrefix);
24
		if(constructorParams == null){
25
			constructorParams = new String[0];
26
		}
27
		try {
28
			Class<?> processClass = Class.forName(className);
29
			Constructor<?> processConstructor = null;
30
			if(constructorParams.length == 0){
31
				try{
32
					processConstructor = processClass.getConstructor();
33
					return (Process) processConstructor.newInstance();
34
				} catch(NoSuchMethodException ex){
35
				}
36
			}
37
			processConstructor = processClass.getConstructor(String[].class);
38
			return (Process) processConstructor.newInstance(
39
						(Object)constructorParams);
40
		} catch (Exception e) {
41
			throw new CmdLineParserException(String.format(
42
					"Problem while creating class \"%s\"", className), e);
43
		}
44
	}
45
}
(3-3/11)