Browse Source

Add JdkIdGenerator and use it in SockJS client

Issue: SPR-12658
pull/725/merge
Rossen Stoyanchev 10 years ago
parent
commit
52b8f34468
  1. 8
      spring-core/src/main/java/org/springframework/util/AlternativeJdkIdGenerator.java
  2. 34
      spring-core/src/main/java/org/springframework/util/JdkIdGenerator.java
  3. 44
      spring-core/src/main/java/org/springframework/util/SimpleIdGenerator.java
  4. 6
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsUrlInfo.java

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

@ -22,9 +22,11 @@ import java.util.Random; @@ -22,9 +22,11 @@ import java.util.Random;
import java.util.UUID;
/**
* A variation of {@link UUID#randomUUID()} that uses {@link SecureRandom} only for the
* initial seed and {@link Random} thereafter. This provides better performance in
* exchange for less securely random id's.
* An {@link org.springframework.util.IdGenerator IdGenerator} that uses
* {@link SecureRandom} for the initial seed and {@link Random} thereafter
* instead of calling {@link UUID#randomUUID()} every time as
* {@link org.springframework.util.JdkIdGenerator JdkIdGenerator} does.
* This provides a better balance between securely random id's and performance.
*
* @author Rossen Stoyanchev
* @author Rob Winch

34
spring-core/src/main/java/org/springframework/util/JdkIdGenerator.java

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util;
import java.util.UUID;
/**
* An IdGenerator that calls {@link java.util.UUID#randomUUID()}.
*
* @author Rossen Stoyanchev
* @since 4.2
*/
public class JdkIdGenerator implements IdGenerator {
public UUID generateId() {
return UUID.randomUUID();
}
}

44
spring-core/src/main/java/org/springframework/util/SimpleIdGenerator.java

@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
/**
* An simple IdGenerator that starts at 1 and increments by 1 with each call.
*
* @author Rossen Stoyanchev
* @since 4.2
*/
public class SimpleIdGenerator implements IdGenerator {
private final AtomicLong mostSigBits = new AtomicLong(0);
private final AtomicLong leastSigBits = new AtomicLong(0);
@Override
public UUID generateId() {
long leastSigBits = this.leastSigBits.incrementAndGet();
if (leastSigBits == 0) {
this.mostSigBits.incrementAndGet();
}
return new UUID(this.mostSigBits.get(), leastSigBits);
}
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,8 +19,8 @@ package org.springframework.web.socket.sockjs.client; @@ -19,8 +19,8 @@ package org.springframework.web.socket.sockjs.client;
import java.net.URI;
import java.util.UUID;
import org.springframework.util.AlternativeJdkIdGenerator;
import org.springframework.util.IdGenerator;
import org.springframework.util.JdkIdGenerator;
import org.springframework.web.socket.sockjs.transport.TransportType;
import org.springframework.web.util.UriComponentsBuilder;
@ -33,7 +33,7 @@ import org.springframework.web.util.UriComponentsBuilder; @@ -33,7 +33,7 @@ import org.springframework.web.util.UriComponentsBuilder;
*/
public class SockJsUrlInfo {
private static final IdGenerator idGenerator = new AlternativeJdkIdGenerator();
private static final IdGenerator idGenerator = new JdkIdGenerator();
private final URI sockJsUrl;

Loading…
Cancel
Save