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