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