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