]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.acorn/src/org/simantics/db/javacore/HeadState.java
Fixing problems in the database unit testing environment with Acorn
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / db / javacore / HeadState.java
1 package org.simantics.db.javacore;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.io.ObjectInputStream;
6 import java.io.Serializable;
7 import java.nio.file.Files;
8 import java.nio.file.Path;
9 import java.security.MessageDigest;
10 import java.security.NoSuchAlgorithmException;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13
14 import org.simantics.acorn.exception.InvalidHeadStateException;
15
16 /**
17  * @deprecated exists only for backwards compatibility 
18  */
19 @Deprecated
20 public class HeadState implements Serializable {
21
22     private static final long serialVersionUID = -4135031566499790077L;
23
24     public int headChangeSetId = 0;
25     public long transactionId = 1;
26     public long reservedIds = 3;
27
28     public ArrayList<String> clusters = new ArrayList<>();
29     public ArrayList<String> files = new ArrayList<>();
30     public ArrayList<String> stream = new ArrayList<>();
31     public ArrayList<String> cs = new ArrayList<>();
32 //    public ArrayList<String> ccs = new ArrayList<String>();
33
34     public static HeadState load(Path directory) throws InvalidHeadStateException {
35         Path f = directory.resolve("head.state");
36         try {
37             byte[] bytes = Files.readAllBytes(f);
38             MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
39             int digestLength = sha1.getDigestLength();
40             sha1.update(bytes, digestLength, bytes.length - digestLength);
41             byte[] newChecksum = sha1.digest();
42             if (!Arrays.equals(newChecksum, Arrays.copyOfRange(bytes, 0, digestLength))) {
43                 throw new InvalidHeadStateException(
44                         "Checksum " + Arrays.toString(newChecksum) + " does not match excpected "
45                                 + Arrays.toString(Arrays.copyOfRange(bytes, 0, digestLength)) + " for " + f.toAbsolutePath());
46             }
47             try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes, digestLength, bytes.length - digestLength))) {
48                 HeadState state = (HeadState) ois.readObject();
49                 return state;
50             }
51         } catch (IOException i) {
52             return new HeadState();
53         } catch (ClassNotFoundException c) {
54 //            throw new Error("HeadState class not found", c);
55             return new HeadState();
56         } catch (NoSuchAlgorithmException e) {
57             throw new Error("SHA-1 Algorithm not found", e);
58         }
59     }
60
61     public static void validateHeadStateIntegrity(Path headState) throws InvalidHeadStateException, IOException {
62         try {
63             byte[] bytes = Files.readAllBytes(headState);
64             MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
65             int digestLength = sha1.getDigestLength();
66             sha1.update(bytes, digestLength, bytes.length - digestLength);
67             byte[] newChecksum = sha1.digest();
68             if (!Arrays.equals(newChecksum, Arrays.copyOfRange(bytes, 0, digestLength))) {
69                 throw new InvalidHeadStateException(
70                         "Checksum " + Arrays.toString(newChecksum) + " does not match excpected "
71                                 + Arrays.toString(Arrays.copyOfRange(bytes, 0, digestLength)) + " for " + headState.toAbsolutePath());
72             }
73         } catch (NoSuchAlgorithmException e) {
74             throw new Error("SHA-1 digest not found, should not happen", e);
75         }
76     }
77 }