Skip to content

Commit d841b08

Browse files
committed
refactor checksum processing
1 parent f409d42 commit d841b08

4 files changed

Lines changed: 23 additions & 57 deletions

File tree

‎src/main/java/zos/shell/controller/container/ControllerFactoryContainer.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
public class ControllerFactoryContainer {
5959

6060
private static final Logger LOG = LoggerFactory.getLogger(ControllerFactoryContainer.class);
61+
private final CheckSumService checkSumService = new CheckSumService();
6162
private final EnvVariableController envVariableController = new EnvVariableController(new EnvVariableService());
6263
private final PathService pathService = new PathService(ConnSingleton.getInstance(), this.envVariableController);
6364
private final Map<ContainerType.Name, Object> controllers = new HashMap<>();
@@ -240,8 +241,7 @@ public EditController getEditController(final ZosConnection connection, final lo
240241
if (controller == null || controller.isNotValid(dependency)) {
241242
var dsnGet = new DsnGet(connection);
242243
var download = new Download(dsnGet, this.pathService, false);
243-
var checkSumService = new CheckSumService();
244-
var editService = new EditService(download, this.pathService, checkSumService, timeout);
244+
var editService = new EditService(download, this.pathService, this.checkSumService, timeout);
245245
controller = new EditController(editService, dependency);
246246
this.controllers.put(ContainerType.Name.EDIT, controller);
247247
}
@@ -347,8 +347,7 @@ public SaveController getSaveController(final ZosConnection connection, final lo
347347
var controller = (SaveController) controllers.get(ContainerType.Name.SAVE);
348348
var dependency = new Dependency.Builder().zosConnection(connection).timeout(timeout).build();
349349
if (controller == null || controller.isNotValid(dependency)) {
350-
var checkSumService = new CheckSumService();
351-
var service = new SaveService(new DsnWrite(connection), this.pathService, checkSumService, timeout);
350+
var service = new SaveService(new DsnWrite(connection), this.pathService, this.checkSumService, timeout);
352351
controller = new SaveController(service, dependency);
353352
this.controllers.put(ContainerType.Name.SAVE, controller);
354353
}

‎src/main/java/zos/shell/service/checksum/CheckSumService.java‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,49 @@
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
5-
import zos.shell.singleton.CheckSumSingleton;
65

7-
import java.io.IOException;
8-
import java.math.BigInteger;
96
import java.nio.file.Files;
107
import java.nio.file.Paths;
118
import java.security.MessageDigest;
12-
import java.security.NoSuchAlgorithmException;
9+
import java.util.HashMap;
10+
import java.util.Map;
1311

1412
public class CheckSumService {
1513

1614
private static final Logger LOG = LoggerFactory.getLogger(CheckSumService.class);
1715

18-
private static final CheckSumSingleton checkSumSingleton = CheckSumSingleton.getInstance();
16+
private final Map<String, String> checksums = new HashMap<>();
1917

2018
public CheckSumService() {
2119
LOG.debug("*** CheckSumService ***");
2220
}
2321

2422
public void addCheckSum(final String target) {
2523
LOG.debug("*** addCheckSum ***");
26-
checkSumSingleton.put(target, calculateCheckSum(target));
24+
checksums.put(target, calculateCheckSum(target));
2725
}
2826

2927
public String getCheckSum(final String target) {
3028
LOG.debug("*** getCheckSum ***");
31-
return checkSumSingleton.get(target);
29+
return checksums.get(target);
3230
}
3331

3432
public String calculateCheckSum(final String target) {
35-
LOG.debug("*** calculateCheckSum ***");
36-
byte[] hash = new byte[0];
3733
try {
38-
byte[] data = Files.readAllBytes(Paths.get(target));
39-
hash = MessageDigest.getInstance("MD5").digest(data);
40-
} catch (IOException | NoSuchAlgorithmException ignored) {
34+
byte[] fileBytes = Files.readAllBytes(Paths.get(target));
35+
MessageDigest md = MessageDigest.getInstance("MD5");
36+
byte[] digest = md.digest(fileBytes);
37+
38+
StringBuilder sb = new StringBuilder();
39+
for (byte b : digest) {
40+
sb.append(String.format("%02x", b));
41+
}
42+
return sb.toString();
43+
44+
} catch (Exception e) {
45+
LOG.error("Error calculating checksum for {}", target, e);
46+
throw new RuntimeException(e);
4147
}
42-
return new BigInteger(1, hash).toString(16);
4348
}
4449

4550
}

‎src/main/java/zos/shell/service/dsn/save/Save.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public ResponseStatus save(final String dataset, final String target) {
5757

5858
try (var br = new BufferedReader(new FileReader(this.pathService.getPathWithFile()))) {
5959
var sb = new StringBuilder();
60-
var line = br.readLine();
60+
String line = br.readLine();
6161

6262
while (line != null) {
6363
sb.append(line);
@@ -66,7 +66,7 @@ public ResponseStatus save(final String dataset, final String target) {
6666
}
6767
var content = sb.toString().replaceAll("(\\r)", "");
6868

69-
var checksum = checkSumService.calculateCheckSum(this.pathService.getPathWithFile());
69+
String checksum = checkSumService.calculateCheckSum(this.pathService.getPathWithFile());
7070
if (checksum.equals(checkSumService.getCheckSum(this.pathService.getPathWithFile()))) {
7171
return new ResponseStatus("nothing to save, perform editor save and try again...", false);
7272
}

‎src/main/java/zos/shell/singleton/CheckSumSingleton.java‎

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)