Passed
Pull Request — master (#56)
by Filip
03:12
created

writeVauntFile(String,String,String,Properties)   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
package com.hltech.vaunt.generator;
2
3
import com.fasterxml.jackson.databind.JsonMappingException;
4
import com.hltech.vaunt.core.VauntSerializer;
5
import com.hltech.vaunt.core.domain.model.Service;
6
import com.hltech.vaunt.generator.domain.representation.RepresentationExtractor;
7
import com.hltech.vaunt.generator.domain.representation.RepresentationWriter;
8
import lombok.RequiredArgsConstructor;
9
10
import java.io.IOException;
11
import java.util.Properties;
12
13
@RequiredArgsConstructor
14
public class VauntGenerator {
15
16
    private final RepresentationExtractor extractor;
17
    private final RepresentationWriter writer;
18
19
    public VauntGenerator() {
20
        VauntSerializer serializer = new VauntSerializer();
21
        extractor = new RepresentationExtractor(serializer);
22
        writer = new RepresentationWriter(serializer);
23
    }
24
25
    public void writeVauntFile(String packageRoot, String serviceName, String targetDirectory) {
26
        try {
27
            writer.writeServiceRepresentation(
28
                    serviceRepresentation(packageRoot, serviceName, new Properties()), targetDirectory);
29
        } catch (IOException ex) {
30
            throw new RuntimeException("Error when trying to write service representation to file", ex);
31
        }
32
    }
33
34
    public void writeVauntFile(String packageRoot, String serviceName, String targetDirectory, Properties props) {
35
        try {
36
            writer.writeServiceRepresentation(
37
                    serviceRepresentation(packageRoot, serviceName, props), targetDirectory);
38
        } catch (IOException ex) {
39
            throw new RuntimeException("Error when trying to write service representation to file", ex);
40
        }
41
    }
42
43
    private Service serviceRepresentation(String packageRoot, String serviceName, Properties props) {
44
        try {
45
            return extractor.extractServiceRepresentation(packageRoot, serviceName, props);
46
        } catch (JsonMappingException ex) {
47
            throw new RuntimeException(
48
                    String.format(
49
                            "Error when trying to extract service representation: package=%s, service name=%s",
50
                            packageRoot, serviceName), ex);
51
        }
52
    }
53
}
54