diff --git a/spring-core/src/main/java/org/springframework/util/AlternativeJdkIdGenerator.java b/spring-core/src/main/java/org/springframework/util/AlternativeJdkIdGenerator.java index 4a2d84350c..d850272eb2 100644 --- a/spring-core/src/main/java/org/springframework/util/AlternativeJdkIdGenerator.java +++ b/spring-core/src/main/java/org/springframework/util/AlternativeJdkIdGenerator.java @@ -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 diff --git a/spring-core/src/main/java/org/springframework/util/JdkIdGenerator.java b/spring-core/src/main/java/org/springframework/util/JdkIdGenerator.java new file mode 100644 index 0000000000..6d772de9e4 --- /dev/null +++ b/spring-core/src/main/java/org/springframework/util/JdkIdGenerator.java @@ -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(); + } + +} diff --git a/spring-core/src/main/java/org/springframework/util/SimpleIdGenerator.java b/spring-core/src/main/java/org/springframework/util/SimpleIdGenerator.java new file mode 100644 index 0000000000..7c8b2400e5 --- /dev/null +++ b/spring-core/src/main/java/org/springframework/util/SimpleIdGenerator.java @@ -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); + } + +} diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsUrlInfo.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsUrlInfo.java index 371b695dfa..97ecd1f863 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsUrlInfo.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/SockJsUrlInfo.java @@ -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; 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; */ public class SockJsUrlInfo { - private static final IdGenerator idGenerator = new AlternativeJdkIdGenerator(); + private static final IdGenerator idGenerator = new JdkIdGenerator(); private final URI sockJsUrl;