Skip to content

Commit 52b8f34

Browse files
committed
Add JdkIdGenerator and use it in SockJS client
Issue: SPR-12658
1 parent 0bfa124 commit 52b8f34

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed

spring-core/src/main/java/org/springframework/util/AlternativeJdkIdGenerator.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import java.util.UUID;
2323

2424
/**
25-
* A variation of {@link UUID#randomUUID()} that uses {@link SecureRandom} only for the
26-
* initial seed and {@link Random} thereafter. This provides better performance in
27-
* exchange for less securely random id's.
25+
* An {@link org.springframework.util.IdGenerator IdGenerator} that uses
26+
* {@link SecureRandom} for the initial seed and {@link Random} thereafter
27+
* instead of calling {@link UUID#randomUUID()} every time as
28+
* {@link org.springframework.util.JdkIdGenerator JdkIdGenerator} does.
29+
* This provides a better balance between securely random id's and performance.
2830
*
2931
* @author Rossen Stoyanchev
3032
* @author Rob Winch
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
* An IdGenerator that calls {@link java.util.UUID#randomUUID()}.
23+
*
24+
* @author Rossen Stoyanchev
25+
* @since 4.2
26+
*/
27+
public class JdkIdGenerator implements IdGenerator {
28+
29+
30+
public UUID generateId() {
31+
return UUID.randomUUID();
32+
}
33+
34+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
import java.util.concurrent.atomic.AtomicLong;
21+
22+
/**
23+
* An simple IdGenerator that starts at 1 and increments by 1 with each call.
24+
*
25+
* @author Rossen Stoyanchev
26+
* @since 4.2
27+
*/
28+
public class SimpleIdGenerator implements IdGenerator {
29+
30+
private final AtomicLong mostSigBits = new AtomicLong(0);
31+
32+
private final AtomicLong leastSigBits = new AtomicLong(0);
33+
34+
35+
@Override
36+
public UUID generateId() {
37+
long leastSigBits = this.leastSigBits.incrementAndGet();
38+
if (leastSigBits == 0) {
39+
this.mostSigBits.incrementAndGet();
40+
}
41+
return new UUID(this.mostSigBits.get(), leastSigBits);
42+
}
43+
44+
}

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsUrlInfo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,8 @@
1919
import java.net.URI;
2020
import java.util.UUID;
2121

22-
import org.springframework.util.AlternativeJdkIdGenerator;
2322
import org.springframework.util.IdGenerator;
23+
import org.springframework.util.JdkIdGenerator;
2424
import org.springframework.web.socket.sockjs.transport.TransportType;
2525
import org.springframework.web.util.UriComponentsBuilder;
2626

@@ -33,7 +33,7 @@
3333
*/
3434
public class SockJsUrlInfo {
3535

36-
private static final IdGenerator idGenerator = new AlternativeJdkIdGenerator();
36+
private static final IdGenerator idGenerator = new JdkIdGenerator();
3737

3838

3939
private final URI sockJsUrl;

0 commit comments

Comments
 (0)