From 7a203ca9a2c4b14edeec270b4305d57627215700 Mon Sep 17 00:00:00 2001 From: NotedSalmon Date: Fri, 20 Sep 2024 16:08:32 +0100 Subject: [PATCH 1/8] FISH-9771 random object is now only created once. before it was created twice. --- .../com/sun/enterprise/config/serverbeans/Cluster.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java b/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java index a5e9688171a..96f9a621805 100644 --- a/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java +++ b/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java @@ -37,7 +37,7 @@ * only if the new code is made subject to such option by the copyright * holder. */ -// Portions Copyright [2017-2021] [Payara Foundation and/or its affiliates] +// Portions Copyright [2017-2024] [Payara Foundation and/or its affiliates] package com.sun.enterprise.config.serverbeans; @@ -93,6 +93,8 @@ @ReferenceConstraint(skipDuringCreation=true, payload=Cluster.class) public interface Cluster extends ConfigBeanProxy, PropertyBag, Named, SystemPropertyBag, ReferenceContainer, RefContainer, Payload { + SecureRandom R = new SecureRandom(); + /** * Sets the cluster name * @param value cluster name @@ -723,7 +725,7 @@ public void decorate(AdminCommandContext context, final Cluster instance) throws // generate a random port since user did not provide one. // better fix in future would be to walk existing clusters and pick an unused port. - TCPPORT = Integer.toString(new SecureRandom().nextInt(9200 - 9090) + 9090); + TCPPORT = Integer.toString(R.nextInt(9200 - 9090) + 9090); // hardcode all instances to use same default port. // generate mode does not support multiple instances on one machine. @@ -744,7 +746,7 @@ public void decorate(AdminCommandContext context, final Cluster instance) throws gmsListenerPortSysProp.setName(propName); if (TCPPORT == null || TCPPORT.trim().charAt(0) == '$') { String generateGmsListenerPort = Integer.toString( - new SecureRandom().nextInt(9200 - 9090) + 9090); + R.nextInt(9200 - 9090) + 9090); gmsListenerPortSysProp.setValue(generateGmsListenerPort); } else { gmsListenerPortSysProp.setValue(TCPPORT); From 5194276acc0c5411c424b90022cff15bfe56e70f Mon Sep 17 00:00:00 2001 From: NotedSalmon Date: Fri, 20 Sep 2024 16:10:11 +0100 Subject: [PATCH 2/8] FISH-9771 random object is now only created once at the start. --- .../enterprise/admin/cli/cluster/ImportSyncBundleCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java b/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java index 886609743a6..07bdaaf1c12 100644 --- a/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java +++ b/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java @@ -37,7 +37,7 @@ * only if the new code is made subject to such option by the copyright * holder. */ -// Portions Copyright [2017-2019] [Payara Foundation and/or its affiliates] +// Portions Copyright [2017-2024] [Payara Foundation and/or its affiliates] package com.sun.enterprise.admin.cli.cluster; @@ -132,6 +132,7 @@ public class ImportSyncBundleCommand extends LocalInstanceCommand { private File backupDir; private static final String RENDEZVOUS_PROPERTY_NAME = "rendezvousOccurred"; + private static final SecureRandom r = new SecureRandom(); private String instanceDottedName; private String rendevousDottedName; @@ -334,7 +335,6 @@ private void writeDasProperties() throws IOException { private void backupInstanceDir() { File f = getServerDirs().getServerDir(); if (f != null && f.isDirectory()) { - SecureRandom r = new SecureRandom(); setBackupDir(r.nextInt()); File backup = getBackupDir(); if (!f.renameTo(backup)) { From c8ae79f713b785a9e036ad67cb8308ec73283dca Mon Sep 17 00:00:00 2001 From: NotedSalmon Date: Tue, 24 Sep 2024 16:36:43 +0100 Subject: [PATCH 3/8] FISH-9771 better naming convention and changed object to be inside the class instead of the interface. --- .../com/sun/enterprise/config/serverbeans/Cluster.java | 7 +++---- .../admin/cli/cluster/ImportSyncBundleCommand.java | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java b/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java index 96f9a621805..ae6c6b15e83 100644 --- a/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java +++ b/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java @@ -93,8 +93,6 @@ @ReferenceConstraint(skipDuringCreation=true, payload=Cluster.class) public interface Cluster extends ConfigBeanProxy, PropertyBag, Named, SystemPropertyBag, ReferenceContainer, RefContainer, Payload { - SecureRandom R = new SecureRandom(); - /** * Sets the cluster name * @param value cluster name @@ -614,6 +612,7 @@ class Decorator implements CreationDecorator { */ @Override public void decorate(AdminCommandContext context, final Cluster instance) throws TransactionFailure, PropertyVetoException { + SecureRandom random = new SecureRandom(); Logger logger = ConfigApiLoggerInfo.getLogger(); LocalStringManagerImpl localStrings = new LocalStringManagerImpl(Cluster.class); Transaction t = Transaction.getTransaction(instance); @@ -725,7 +724,7 @@ public void decorate(AdminCommandContext context, final Cluster instance) throws // generate a random port since user did not provide one. // better fix in future would be to walk existing clusters and pick an unused port. - TCPPORT = Integer.toString(R.nextInt(9200 - 9090) + 9090); + TCPPORT = Integer.toString(random.nextInt(9200 - 9090) + 9090); // hardcode all instances to use same default port. // generate mode does not support multiple instances on one machine. @@ -746,7 +745,7 @@ public void decorate(AdminCommandContext context, final Cluster instance) throws gmsListenerPortSysProp.setName(propName); if (TCPPORT == null || TCPPORT.trim().charAt(0) == '$') { String generateGmsListenerPort = Integer.toString( - R.nextInt(9200 - 9090) + 9090); + random.nextInt(9200 - 9090) + 9090); gmsListenerPortSysProp.setValue(generateGmsListenerPort); } else { gmsListenerPortSysProp.setValue(TCPPORT); diff --git a/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java b/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java index 07bdaaf1c12..17662d3f1de 100644 --- a/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java +++ b/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java @@ -132,7 +132,7 @@ public class ImportSyncBundleCommand extends LocalInstanceCommand { private File backupDir; private static final String RENDEZVOUS_PROPERTY_NAME = "rendezvousOccurred"; - private static final SecureRandom r = new SecureRandom(); + private static final Random random = new SecureRandom(); private String instanceDottedName; private String rendevousDottedName; @@ -335,7 +335,7 @@ private void writeDasProperties() throws IOException { private void backupInstanceDir() { File f = getServerDirs().getServerDir(); if (f != null && f.isDirectory()) { - setBackupDir(r.nextInt()); + setBackupDir(random.nextInt()); File backup = getBackupDir(); if (!f.renameTo(backup)) { logger.warning(Strings.get("import.sync.bundle.backupInstanceDirFailed", f.getAbsolutePath(), backup.getAbsolutePath())); From d7141322b4f2989193aebc20594f16aefe597fd8 Mon Sep 17 00:00:00 2001 From: NotedSalmon Date: Tue, 24 Sep 2024 17:50:24 +0100 Subject: [PATCH 4/8] FISH-9771 Changed from SecureRandom to Random --- .../java/com/sun/enterprise/config/serverbeans/Cluster.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java b/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java index ae6c6b15e83..55c1647d22d 100644 --- a/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java +++ b/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java @@ -77,6 +77,7 @@ import java.security.SecureRandom; import java.util.ArrayList; import java.util.List; +import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; @@ -612,9 +613,9 @@ class Decorator implements CreationDecorator { */ @Override public void decorate(AdminCommandContext context, final Cluster instance) throws TransactionFailure, PropertyVetoException { - SecureRandom random = new SecureRandom(); Logger logger = ConfigApiLoggerInfo.getLogger(); LocalStringManagerImpl localStrings = new LocalStringManagerImpl(Cluster.class); + Random random = new SecureRandom(); Transaction t = Transaction.getTransaction(instance); //check if cluster software is installed else fail , see issue 12023 final CopyConfig command = (CopyConfig) runner From 488abb9bb917a2547a80bccf5e75db614bba8760 Mon Sep 17 00:00:00 2001 From: NotedSalmon Date: Thu, 26 Sep 2024 16:46:28 +0100 Subject: [PATCH 5/8] FISH-9771 static SecureRandom moved in SecurityUtils By moving the SecureRandom to outside the previous method, I am able to use the random created in the other classes such as cluster and the command. --- .../sun/enterprise/config/serverbeans/Cluster.java | 5 ++--- .../admin/cli/cluster/ImportSyncBundleCommand.java | 3 +-- .../enterprise/universal/security/SecurityUtils.java | 11 ++++++++++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java b/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java index 55c1647d22d..02955650bd4 100644 --- a/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java +++ b/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java @@ -615,7 +615,6 @@ class Decorator implements CreationDecorator { public void decorate(AdminCommandContext context, final Cluster instance) throws TransactionFailure, PropertyVetoException { Logger logger = ConfigApiLoggerInfo.getLogger(); LocalStringManagerImpl localStrings = new LocalStringManagerImpl(Cluster.class); - Random random = new SecureRandom(); Transaction t = Transaction.getTransaction(instance); //check if cluster software is installed else fail , see issue 12023 final CopyConfig command = (CopyConfig) runner @@ -725,7 +724,7 @@ public void decorate(AdminCommandContext context, final Cluster instance) throws // generate a random port since user did not provide one. // better fix in future would be to walk existing clusters and pick an unused port. - TCPPORT = Integer.toString(random.nextInt(9200 - 9090) + 9090); + TCPPORT = Integer.toString(SecurityUtils.nextInt(9200 - 9090) + 9090); // hardcode all instances to use same default port. // generate mode does not support multiple instances on one machine. @@ -746,7 +745,7 @@ public void decorate(AdminCommandContext context, final Cluster instance) throws gmsListenerPortSysProp.setName(propName); if (TCPPORT == null || TCPPORT.trim().charAt(0) == '$') { String generateGmsListenerPort = Integer.toString( - random.nextInt(9200 - 9090) + 9090); + SecurityUtils.nextInt(9200 - 9090) + 9090); gmsListenerPortSysProp.setValue(generateGmsListenerPort); } else { gmsListenerPortSysProp.setValue(TCPPORT); diff --git a/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java b/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java index 17662d3f1de..7e0192f3a77 100644 --- a/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java +++ b/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java @@ -132,7 +132,6 @@ public class ImportSyncBundleCommand extends LocalInstanceCommand { private File backupDir; private static final String RENDEZVOUS_PROPERTY_NAME = "rendezvousOccurred"; - private static final Random random = new SecureRandom(); private String instanceDottedName; private String rendevousDottedName; @@ -335,7 +334,7 @@ private void writeDasProperties() throws IOException { private void backupInstanceDir() { File f = getServerDirs().getServerDir(); if (f != null && f.isDirectory()) { - setBackupDir(random.nextInt()); + setBackupDir(SecurityUtils.nextInt()); File backup = getBackupDir(); if (!f.renameTo(backup)) { logger.warning(Strings.get("import.sync.bundle.backupInstanceDirFailed", f.getAbsolutePath(), backup.getAbsolutePath())); diff --git a/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java b/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java index 3bae3d14541..ffb63d70104 100644 --- a/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java +++ b/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java @@ -49,8 +49,17 @@ */ public final class SecurityUtils { + private static SecureRandom random = new SecureRandom(); + + private static int nextInt() { + return random.nextInt(); + } + + private static int nextInt(int bound) { + return random.nextInt(bound); + } + public static String getSecureRandomHexString(int numBytes) { - SecureRandom random = new SecureRandom(); byte[] bb = new byte[numBytes]; random.nextBytes(bb); return toHexString(bb); From 7300fe7c7cdea57ff549ffd5673966aa2b2cb3b4 Mon Sep 17 00:00:00 2001 From: NotedSalmon Date: Thu, 26 Sep 2024 16:48:58 +0100 Subject: [PATCH 6/8] FISH-9771 copyright amendment, removed unused import. --- .../java/com/sun/enterprise/config/serverbeans/Cluster.java | 3 +-- .../admin/cli/cluster/ImportSyncBundleCommand.java | 3 +-- .../sun/enterprise/universal/security/SecurityUtils.java | 6 +++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java b/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java index 02955650bd4..4d5166deeb4 100644 --- a/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java +++ b/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java @@ -43,6 +43,7 @@ import com.sun.enterprise.config.serverbeans.customvalidators.*; import com.sun.enterprise.config.util.ConfigApiLoggerInfo; +import com.sun.enterprise.universal.security.SecurityUtils; import com.sun.enterprise.util.LocalStringManagerImpl; import com.sun.enterprise.util.io.FileUtils; import org.glassfish.api.ActionReport; @@ -74,10 +75,8 @@ import jakarta.validation.constraints.Pattern; import java.beans.PropertyVetoException; import java.io.File; -import java.security.SecureRandom; import java.util.ArrayList; import java.util.List; -import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; diff --git a/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java b/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java index 7e0192f3a77..96edce730b4 100644 --- a/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java +++ b/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java @@ -55,14 +55,13 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; - +import com.sun.enterprise.universal.security.SecurityUtils; import org.jvnet.hk2.annotations.Service; import org.glassfish.api.Param; import org.glassfish.api.admin.*; import static com.sun.enterprise.admin.cli.CLIConstants.*; import com.sun.enterprise.util.io.FileUtils; import java.io.FileInputStream; -import java.security.SecureRandom; import org.glassfish.admin.payload.PayloadImpl; import org.glassfish.admin.payload.PayloadFilesManager.Perm; import org.glassfish.hk2.api.PerLookup; diff --git a/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java b/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java index ffb63d70104..d238de991ba 100644 --- a/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java +++ b/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java @@ -37,7 +37,7 @@ * only if the new code is made subject to such option by the copyright * holder. */ -// Portions Copyright [2019] Payara Foundation and/or affiliates +// Portions Copyright [2019-2024] Payara Foundation and/or affiliates package com.sun.enterprise.universal.security; @@ -51,11 +51,11 @@ public final class SecurityUtils { private static SecureRandom random = new SecureRandom(); - private static int nextInt() { + public static int nextInt() { return random.nextInt(); } - private static int nextInt(int bound) { + public static int nextInt(int bound) { return random.nextInt(bound); } From 8127a56d321238800ac44faea226909b71004d87 Mon Sep 17 00:00:00 2001 From: Petr Aubrecht Date: Fri, 27 Sep 2024 13:33:32 +0200 Subject: [PATCH 7/8] FISH-9771 move random utils to common-util --- .../config/serverbeans/Cluster.java | 6 +- .../cli/cluster/ImportSyncBundleCommand.java | 4 +- .../universal/security/SecurityUtils.java | 11 +--- .../glassfish/common/util/RandomUtils.java | 59 +++++++++++++++++++ 4 files changed, 65 insertions(+), 15 deletions(-) create mode 100644 nucleus/common/common-util/src/main/java/org/glassfish/common/util/RandomUtils.java diff --git a/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java b/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java index 4d5166deeb4..5df22586071 100644 --- a/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java +++ b/nucleus/admin/config-api/src/main/java/com/sun/enterprise/config/serverbeans/Cluster.java @@ -43,7 +43,6 @@ import com.sun.enterprise.config.serverbeans.customvalidators.*; import com.sun.enterprise.config.util.ConfigApiLoggerInfo; -import com.sun.enterprise.universal.security.SecurityUtils; import com.sun.enterprise.util.LocalStringManagerImpl; import com.sun.enterprise.util.io.FileUtils; import org.glassfish.api.ActionReport; @@ -57,6 +56,7 @@ import org.glassfish.api.admin.config.PropertiesDesc; import org.glassfish.api.admin.config.PropertyDesc; import org.glassfish.api.admin.config.ReferenceContainer; +import org.glassfish.common.util.RandomUtils; import org.glassfish.config.support.CreationDecorator; import org.glassfish.config.support.DeletionDecorator; import org.glassfish.hk2.api.PerLookup; @@ -723,7 +723,7 @@ public void decorate(AdminCommandContext context, final Cluster instance) throws // generate a random port since user did not provide one. // better fix in future would be to walk existing clusters and pick an unused port. - TCPPORT = Integer.toString(SecurityUtils.nextInt(9200 - 9090) + 9090); + TCPPORT = Integer.toString(RandomUtils.nextInt(9200 - 9090) + 9090); // hardcode all instances to use same default port. // generate mode does not support multiple instances on one machine. @@ -744,7 +744,7 @@ public void decorate(AdminCommandContext context, final Cluster instance) throws gmsListenerPortSysProp.setName(propName); if (TCPPORT == null || TCPPORT.trim().charAt(0) == '$') { String generateGmsListenerPort = Integer.toString( - SecurityUtils.nextInt(9200 - 9090) + 9090); + RandomUtils.nextInt(9200 - 9090) + 9090); gmsListenerPortSysProp.setValue(generateGmsListenerPort); } else { gmsListenerPortSysProp.setValue(TCPPORT); diff --git a/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java b/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java index 96edce730b4..ccda26cadba 100644 --- a/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java +++ b/nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/ImportSyncBundleCommand.java @@ -55,7 +55,7 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; -import com.sun.enterprise.universal.security.SecurityUtils; +import org.glassfish.common.util.RandomUtils; import org.jvnet.hk2.annotations.Service; import org.glassfish.api.Param; import org.glassfish.api.admin.*; @@ -333,7 +333,7 @@ private void writeDasProperties() throws IOException { private void backupInstanceDir() { File f = getServerDirs().getServerDir(); if (f != null && f.isDirectory()) { - setBackupDir(SecurityUtils.nextInt()); + setBackupDir(RandomUtils.nextInt()); File backup = getBackupDir(); if (!f.renameTo(backup)) { logger.warning(Strings.get("import.sync.bundle.backupInstanceDirFailed", f.getAbsolutePath(), backup.getAbsolutePath())); diff --git a/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java b/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java index d238de991ba..6bd281486d8 100644 --- a/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java +++ b/nucleus/common/common-util/src/main/java/com/sun/enterprise/universal/security/SecurityUtils.java @@ -37,7 +37,7 @@ * only if the new code is made subject to such option by the copyright * holder. */ -// Portions Copyright [2019-2024] Payara Foundation and/or affiliates +// Portions Copyright [2019] Payara Foundation and/or affiliates package com.sun.enterprise.universal.security; @@ -48,17 +48,8 @@ * @author Byron Nevins */ public final class SecurityUtils { - private static SecureRandom random = new SecureRandom(); - public static int nextInt() { - return random.nextInt(); - } - - public static int nextInt(int bound) { - return random.nextInt(bound); - } - public static String getSecureRandomHexString(int numBytes) { byte[] bb = new byte[numBytes]; random.nextBytes(bb); diff --git a/nucleus/common/common-util/src/main/java/org/glassfish/common/util/RandomUtils.java b/nucleus/common/common-util/src/main/java/org/glassfish/common/util/RandomUtils.java new file mode 100644 index 00000000000..82169e96fa7 --- /dev/null +++ b/nucleus/common/common-util/src/main/java/org/glassfish/common/util/RandomUtils.java @@ -0,0 +1,59 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2024 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/main/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package org.glassfish.common.util; + +import java.security.SecureRandom; + +/** + * Centralize usage of random number generator. + * + * @author: Petr Aubrecht + */ +public class RandomUtils { + private static SecureRandom random = new SecureRandom(); + + public static int nextInt() { + return random.nextInt(); + } + + public static int nextInt(int bound) { + return random.nextInt(bound); + } +} From 132eb87311c73f16be59a01f1c88c6a3be9fabbb Mon Sep 17 00:00:00 2001 From: Petr Aubrecht Date: Mon, 30 Sep 2024 09:33:42 +0200 Subject: [PATCH 8/8] FISH-9771 make generator in random utils static --- .../src/main/java/org/glassfish/common/util/RandomUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nucleus/common/common-util/src/main/java/org/glassfish/common/util/RandomUtils.java b/nucleus/common/common-util/src/main/java/org/glassfish/common/util/RandomUtils.java index 82169e96fa7..3b20e9139d4 100644 --- a/nucleus/common/common-util/src/main/java/org/glassfish/common/util/RandomUtils.java +++ b/nucleus/common/common-util/src/main/java/org/glassfish/common/util/RandomUtils.java @@ -47,7 +47,7 @@ * @author: Petr Aubrecht */ public class RandomUtils { - private static SecureRandom random = new SecureRandom(); + private static final SecureRandom random = new SecureRandom(); public static int nextInt() { return random.nextInt();