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