View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.surefire.booterclient;
20  
21  import java.io.File;
22  import java.security.MessageDigest;
23  import java.security.NoSuchAlgorithmException;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.maven.artifact.Artifact;
29  
30  import static java.nio.charset.StandardCharsets.UTF_8;
31  
32  /**
33   * @author Kristian Rosenvold
34   */
35  public class ChecksumCalculator {
36      private static final String HEX = "0123456789ABCDEF";
37  
38      private final List<Object> checksumItems = new ArrayList<>();
39  
40      private void appendObject(Object item) {
41          checksumItems.add(item);
42      }
43  
44      public void add(boolean value) {
45          checksumItems.add(value);
46      }
47  
48      public void add(int value) {
49          checksumItems.add(value);
50      }
51  
52      public void add(double value) {
53          checksumItems.add(value);
54      }
55  
56      public void add(Map<?, ?> map) {
57          if (map != null) {
58              appendObject(map.toString());
59          }
60      }
61  
62      public void add(String string) {
63          appendObject(string);
64      }
65  
66      public void add(File workingDirectory) {
67          appendObject(workingDirectory);
68      }
69  
70      public void add(List<?> items) {
71          if (items != null) {
72              for (Object item : items) {
73                  appendObject(item);
74              }
75          } else {
76              appendObject(null);
77          }
78      }
79  
80      public void add(Object[] items) {
81          if (items != null) {
82              for (Object item : items) {
83                  appendObject(item);
84              }
85          } else {
86              appendObject(null);
87          }
88      }
89  
90      public void add(Artifact artifact) {
91          appendObject(artifact != null ? artifact.getId() : null);
92      }
93  
94      public void add(Boolean aBoolean) {
95          appendObject(aBoolean);
96      }
97  
98      @SuppressWarnings("checkstyle:magicnumber")
99      private static String asHexString(byte[] bytes) {
100         if (bytes == null) {
101             return null;
102         }
103         final StringBuilder result = new StringBuilder(2 * bytes.length);
104         for (byte b : bytes) {
105             result.append(HEX.charAt((b & 0xF0) >> 4)).append(HEX.charAt((b & 0x0F)));
106         }
107         return result.toString();
108     }
109 
110     private String getConfig() {
111         StringBuilder result = new StringBuilder();
112         for (Object checksumItem : checksumItems) {
113             result.append(checksumItem != null ? checksumItem.toString() : "null");
114         }
115         return result.toString();
116     }
117 
118     public String getSha1() {
119         try {
120             MessageDigest md = MessageDigest.getInstance("SHA-1");
121             String configValue = getConfig();
122             byte[] configBytes = configValue.getBytes(UTF_8);
123             md.update(configBytes);
124             byte[] sha1hash = md.digest();
125             return asHexString(sha1hash);
126         } catch (NoSuchAlgorithmException e) {
127             throw new RuntimeException(e);
128         }
129     }
130 }