Project

General

Profile

1
package eu.dnetlib.dhp.wf.importer.facade;
2

    
3
import static eu.dnetlib.dhp.wf.importer.ImportWorkflowRuntimeParameters.IMPORT_FACADE_FACTORY_CLASS;
4

    
5
import java.lang.reflect.Constructor;
6
import java.util.Map;
7

    
8
import org.apache.commons.lang.StringUtils;
9
import org.apache.hadoop.conf.Configuration;
10

    
11
import com.google.common.collect.ImmutableMap;
12

    
13
import eu.dnetlib.dhp.wf.importer.ImportWorkflowRuntimeParameters;
14

    
15
/**
16
 * Service facade utility methods.
17
 * @author mhorst
18
 *
19
 */
20
public final class ServiceFacadeUtils {
21

    
22
    //------------------------ CONSTRUCTORS -------------------
23
    
24
    private ServiceFacadeUtils() {}
25
    
26
    //------------------------ LOGIC --------------------------
27
    
28
    /**
29
     * Instantiates service based on provided parameters.
30
     * 
31
     * Service factory class name is mandatory and has to be provided as {@value ImportWorkflowRuntimeParameters#IMPORT_FACADE_FACTORY_CLASS} parameter.
32
     * Other parameters will be used by factory itself. Factory must be instantiable with no-argument construtor.
33
     * 
34
     * @param parameters set of parameters required for service instantiation
35
     * 
36
     */
37
    public static <T> T instantiate(Map<String, String> parameters) throws ServiceFacadeException {
38
        String serviceFactoryClassName = parameters.get(IMPORT_FACADE_FACTORY_CLASS);
39
        if (StringUtils.isBlank(serviceFactoryClassName)) {
40
            throw new ServiceFacadeException("unknown service facade factory, no " + IMPORT_FACADE_FACTORY_CLASS + " parameter provided!");
41
        }
42
        try {
43
            Class<?> clazz = Class.forName(serviceFactoryClassName);
44
            Constructor<?> constructor = clazz.getConstructor();
45
            @SuppressWarnings("unchecked")
46
            ServiceFacadeFactory<T> serviceFactory = (ServiceFacadeFactory<T>) constructor.newInstance();
47
            return serviceFactory.instantiate(parameters);    
48
        } catch (Exception e) {
49
            throw new ServiceFacadeException("exception occurred while instantiating service by facade factory: " + IMPORT_FACADE_FACTORY_CLASS, e);
50
        }
51
        
52
    }
53
    
54
    /**
55
     * Instantiates service based on provided configuration.
56
     * 
57
     * Service factory class name is mandatory and has to be provided as {@value ImportWorkflowRuntimeParameters#IMPORT_FACADE_FACTORY_CLASS} configuration entry.
58
     * Other parameters will be used by factory itself. Factory must be instantiable with no-argument construtor.
59
     * 
60
     * @param config set of configuration entries required for service instantiation
61
     */
62
    public static <T> T instantiate(Configuration config) throws ServiceFacadeException {
63
        return instantiate(buildParameters(config));
64
    }
65
    
66

    
67
    // ------------------------ PRIVATE --------------------------
68
    
69
    /**
70
     * Converts configuration entries into plain map.
71
     */
72
    private static Map<String, String> buildParameters(Configuration config) {
73
        ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
74
        for (Map.Entry<String, String> entry : config) {
75
          builder.put(entry);
76
        }
77
        return builder.build();
78
    }
79
    
80
}
(7-7/13)