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 java.io.File;
23  import java.io.UnsupportedEncodingException;
24  import java.security.MessageDigest;
25  import java.security.NoSuchAlgorithmException;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Map;
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.repository.ArtifactRepository;
31  
32  /**
33   * @author Kristian Rosenvold
34   */
35  public class ChecksumCalculator
36  {
37      private static final String HEX = "0123456789ABCDEF";
38  
39      private final List<Object> checksumItems = new ArrayList<Object>();
40  
41      private void appendObject( Object item )
42      {
43          checksumItems.add( item );
44      }
45  
46      public void add( boolean value )
47      {
48          checksumItems.add( value ? Boolean.TRUE : Boolean.FALSE );
49      }
50  
51      public void add( int value )
52      {
53          checksumItems.add( value );
54      }
55  
56      public void add( double value )
57      {
58          checksumItems.add( value );
59      }
60  
61      public void add( Map<?, ?> map )
62      {
63          if ( map != null )
64          {
65              appendObject( map.toString() );
66          }
67      }
68  
69      public void add( String string )
70      {
71          appendObject( string );
72      }
73  
74      public void add( File workingDirectory )
75      {
76          appendObject( workingDirectory );
77      }
78  
79      public void add( ArtifactRepository localRepository )
80      {
81          appendObject( localRepository );
82      }
83  
84      public void add( List<?> items )
85      {
86          if ( items != null )
87          {
88              for ( Object item : items )
89              {
90                  appendObject( item );
91              }
92          }
93          else
94          {
95              appendObject( null );
96          }
97  
98      }
99  
100     public void add( Object[] items )
101     {
102         if ( items != null )
103         {
104             for ( Object item : items )
105             {
106                 appendObject( item );
107             }
108         }
109         else
110         {
111             appendObject( null );
112         }
113     }
114 
115     public void add( Artifact artifact )
116     {
117         appendObject( artifact != null ? artifact.getId() : null );
118     }
119 
120     public void add( Boolean aBoolean )
121     {
122         appendObject( aBoolean );
123     }
124 
125     @SuppressWarnings( "checkstyle:magicnumber" )
126     private static String asHexString( byte[] bytes )
127     {
128         if ( bytes == null )
129         {
130             return null;
131         }
132         final StringBuilder result = new StringBuilder( 2 * bytes.length );
133         for ( byte b : bytes )
134         {
135             result.append( HEX.charAt( ( b & 0xF0 ) >> 4 ) ).append( HEX.charAt( ( b & 0x0F ) ) );
136         }
137         return result.toString();
138     }
139 
140     private String getConfig()
141     {
142         StringBuilder result = new StringBuilder();
143         for ( Object checksumItem : checksumItems )
144         {
145             result.append( checksumItem != null ? checksumItem.toString() : "null" );
146         }
147         return result.toString();
148     }
149 
150     public String getSha1()
151     {
152         try
153         {
154             MessageDigest md = MessageDigest.getInstance( "SHA-1" );
155             String configValue = getConfig();
156             md.update( configValue.getBytes( "iso-8859-1" ), 0, configValue.length() );
157             byte[] sha1hash = md.digest();
158             return asHexString( sha1hash );
159         }
160         catch ( NoSuchAlgorithmException e )
161         {
162             throw new RuntimeException( e );
163         }
164         catch ( UnsupportedEncodingException e )
165         {
166             throw new RuntimeException( e );
167         }
168     }
169 
170 }