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