Skip to content

Commit ce3e557

Browse files
committed
Extract IdGenerator into a top-level class
1 parent a5d87ff commit ce3e557

File tree

3 files changed

+99
-39
lines changed

3 files changed

+99
-39
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util;
18+
19+
import java.math.BigInteger;
20+
import java.security.SecureRandom;
21+
import java.util.Random;
22+
import java.util.UUID;
23+
24+
/**
25+
* A variation of {@link UUID#randomUUID()} that uses {@link SecureRandom} only for
26+
* the initial seed and {@link Random} thereafter. This provides better performance
27+
* in exchange for less securely random id's.
28+
*
29+
* @author Rossen Stoyanchev
30+
* @author Rob Winch
31+
* @since 4.0
32+
*/
33+
public class AlternativeJdkIdGenerator implements IdGenerator {
34+
35+
private final Random random;
36+
37+
38+
public AlternativeJdkIdGenerator() {
39+
byte[] seed = new SecureRandom().generateSeed(8);
40+
this.random = new Random(new BigInteger(seed).longValue());
41+
}
42+
43+
44+
public UUID generateId() {
45+
46+
byte[] randomBytes = new byte[16];
47+
this.random.nextBytes(randomBytes);
48+
49+
long mostSigBits = 0;
50+
for (int i = 0; i < 8; i++) {
51+
mostSigBits = (mostSigBits << 8) | (randomBytes[i] & 0xff);
52+
}
53+
54+
long leastSigBits = 0;
55+
for (int i = 8; i < 16; i++) {
56+
leastSigBits = (leastSigBits << 8) | (randomBytes[i] & 0xff);
57+
}
58+
59+
return new UUID(mostSigBits, leastSigBits);
60+
}
61+
62+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.util;
18+
19+
import java.util.UUID;
20+
21+
/**
22+
* Contract for generating {@link UUID} identifiers.
23+
*
24+
* @author Rossen Stoyanchev
25+
* @since 4.0
26+
*/
27+
public interface IdGenerator {
28+
29+
/**
30+
* Generate a new identifier.
31+
* @return the generated identifier
32+
*/
33+
UUID generateId();
34+
35+
}

spring-messaging/src/main/java/org/springframework/messaging/MessageHeaders.java

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import java.io.ObjectInputStream;
2121
import java.io.ObjectOutputStream;
2222
import java.io.Serializable;
23-
import java.math.BigInteger;
24-
import java.security.SecureRandom;
2523
import java.util.ArrayList;
2624
import java.util.Arrays;
2725
import java.util.Collection;
@@ -30,12 +28,13 @@
3028
import java.util.LinkedHashMap;
3129
import java.util.List;
3230
import java.util.Map;
33-
import java.util.Random;
3431
import java.util.Set;
3532
import java.util.UUID;
3633

3734
import org.apache.commons.logging.Log;
3835
import org.apache.commons.logging.LogFactory;
36+
import org.springframework.util.AlternativeJdkIdGenerator;
37+
import org.springframework.util.IdGenerator;
3938

4039
/**
4140
* The headers for a {@link Message}
@@ -243,40 +242,4 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
243242
in.defaultReadObject();
244243
}
245244

246-
public static interface IdGenerator {
247-
UUID generateId();
248-
}
249-
250-
/**
251-
* A variation of {@link UUID#randomUUID()} that uses {@link SecureRandom} only for
252-
* the initial seed and {@link Random} thereafter, which provides better performance
253-
* in exchange for less securely random id's.
254-
*/
255-
public static class AlternativeJdkIdGenerator implements IdGenerator {
256-
257-
private final Random random;
258-
259-
public AlternativeJdkIdGenerator() {
260-
byte[] seed = new SecureRandom().generateSeed(8);
261-
this.random = new Random(new BigInteger(seed).longValue());
262-
}
263-
264-
public UUID generateId() {
265-
266-
byte[] randomBytes = new byte[16];
267-
this.random.nextBytes(randomBytes);
268-
269-
long mostSigBits = 0;
270-
for (int i = 0; i < 8; i++) {
271-
mostSigBits = (mostSigBits << 8) | (randomBytes[i] & 0xff);
272-
}
273-
long leastSigBits = 0;
274-
for (int i = 8; i < 16; i++) {
275-
leastSigBits = (leastSigBits << 8) | (randomBytes[i] & 0xff);
276-
}
277-
278-
return new UUID(mostSigBits, leastSigBits);
279-
}
280-
}
281-
282245
}

0 commit comments

Comments
 (0)