com.strider.datadefender.Anonymize   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 51
ccs 0
cts 17
cp 0
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B call() 0 30 5
1
/*
2
 * Copyright 2014, Armenak Grigoryan, and individual contributors as indicated
3
 * by the @authors tag. See the copyright.txt in the distribution for a
4
 * full listing of individual contributors.
5
 *
6
 * This is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation; either version 2.1 of
9
 * the License, or (at your option) any later version.
10
 *
11
 * This software is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
 * Lesser General Public License for more details.
15
 */
16
package com.strider.datadefender;
17
18
import com.strider.datadefender.anonymizer.DatabaseAnonymizer;
19
import com.strider.datadefender.anonymizer.IAnonymizer;
20
import com.strider.datadefender.database.IDbFactory;
21
import com.strider.datadefender.requirement.Requirement;
22
import com.strider.datadefender.requirement.registry.ClassAndFunctionRegistry;
23
24
import java.util.concurrent.Callable;
25
import java.util.List;
26
27
import org.apache.commons.collections4.CollectionUtils;
28
import org.apache.commons.lang3.StringUtils;
29
30
import picocli.CommandLine.ArgGroup;
31
import picocli.CommandLine.Option;
32
import picocli.CommandLine.Command;
33
import picocli.CommandLine.Parameters;
34
35
import lombok.extern.log4j.Log4j2;
36
37
/**
38
 * Anonymize picocli subcommand, configures and executes the database
39
 * anonymizer.
40
 *
41
 * @author Zaahid Bateson
42
 */
43
@Command(
44
    name = "anonymize",
45
    version = "1.0",
46
    mixinStandardHelpOptions = true,
47
    description = "Run anonymization utility"
48
)
49
@Log4j2
50
public class Anonymize implements Callable<Integer> {
51
52
    @Option(names = { "-r", "--requirement-file" }, paramLabel = "<requirementFile>", description = "Requirement XML file", required = true)
53
    private Requirement requirement;
54
55
    @Option(names = { "-b", "--batch-size" }, description = "Number of update queries to batch together", defaultValue = "1000")
56
    private Integer batchSize;
57
58
    @ArgGroup(exclusive = false, multiplicity = "1", heading = "Database connection settings%n")
59
    private DbConfig dbConfig;
60
61
    @Parameters(paramLabel = "tables", description = "Limit anonymization to specified tables")
62
    private List<String> tables;
63
64
    @Override
65
    public Integer call() throws Exception {
66
        System.out.println("");
67
        System.out.println("Starting anonymizer");
68
        log.info("Datasource URL: {}, vendor: {}, schema: {}", dbConfig.getUrl(), dbConfig.getVendor(), dbConfig.getSchema());
69
        log.info("Username: {}, Password provided: {}", dbConfig.getUsername(), (StringUtils.isNotBlank(dbConfig.getPassword()) ? "yes" : "no"));
70
        log.info("Batch size: {}", batchSize);
71
        log.info("Limiting to tables: {}", CollectionUtils.isEmpty(tables) ? "<all tables selected>" : StringUtils.join(tables, ", "));
72
73
        IDbFactory factory = IDbFactory.get(dbConfig);
74
        ClassAndFunctionRegistry.singleton().initialize(factory);
75
76
        final IAnonymizer anonymizer = new DatabaseAnonymizer(
77
            factory,
78
            dbConfig,
79
            batchSize,
80
            requirement,
81
            tables
82
        );
83
        try {
84
            anonymizer.anonymize();
85
        } catch (DataDefenderException e) {
86
            log.error(e.getMessage());
87
            log.debug("Exception occurred during anonymization", e);
88
            return 1;
89
        } catch (Throwable e) {
90
            log.error(e.getMessage(), e);
91
            return 1;
92
        }
93
        return 0;
94
    }
95
}
96