Skip to content

Commit

Permalink
Add method for setting property if not defined
Browse files Browse the repository at this point in the history
Added a StaticUtils.setSystemPropertyIfNotAlreadyDefined method that
can be used to set the value of a system property, but only if that
property is not already set.  If the property already has a value,
then the existing value will be retained regardless of whether it is
the same as or different from the provided value.
  • Loading branch information
dirmgr committed May 24, 2024
1 parent ed92d12 commit 45c422f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 58 deletions.
9 changes: 9 additions & 0 deletions docs/release-notes.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ <h3>Version 7.0.1</h3>
<br><br>
</li>

<li>
Added a StaticUtils.setSystemPropertyIfNotAlreadyDefined method that can be used
to set the value of a system property, but only if that property is not already
set. If the property already has a value, then the existing value will be
retained regardless of whether it is the same as or different from the provided
value.
<br><br>
</li>

<li>
Updated the OID registry to include records for a number of collation matching
rules.
Expand Down
29 changes: 6 additions & 23 deletions src/com/unboundid/util/BouncyCastleFIPSHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,29 +360,12 @@ static void setPropertiesForPingIdentityServer()
return;
}

setPropertyIfNotDefined(PROPERTY_APPROVED_ONLY, "true");
setPropertyIfNotDefined(PROPERTY_ALLOW_RSA_MULTI_USE, "true");
setPropertyIfNotDefined(PROPERTY_ENABLE_MD5, "true");
}



/**
* Sets the specified system property with the given value, but only if it is
* not already set.
*
* @param name The name of the property to set. It must not be
* {@code null}.
* @param value The value to use for the property. It must not be
* {@code null}.
*/
static void setPropertyIfNotDefined(@NotNull final String name,
@NotNull final String value)
{
if (StaticUtils.getSystemProperty(name) == null)
{
StaticUtils.setSystemProperty(name, value);
}
StaticUtils.setSystemPropertyIfNotAlreadyDefined(PROPERTY_APPROVED_ONLY,
"true");
StaticUtils.setSystemPropertyIfNotAlreadyDefined(
PROPERTY_ALLOW_RSA_MULTI_USE, "true");
StaticUtils.setSystemPropertyIfNotAlreadyDefined(PROPERTY_ENABLE_MD5,
"true");
}


Expand Down
33 changes: 32 additions & 1 deletion src/com/unboundid/util/StaticUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public static String getSystemProperty(@NotNull final String name,
* may not be permitted by some security managers, in which case the attempt
* will have no effect.
*
* @param name The name of the System property to set. It must not be
* @param name The name of the system property to set. It must not be
* {@code null}.
* @param value The value to use for the system property. If it is
* {@code null}, then the property will be cleared.
Expand Down Expand Up @@ -513,6 +513,37 @@ public static String setSystemProperty(@NotNull final String name,



/**
* Attempts to set the value of the specified system property, but only if the
* specified property does not already have a value. If the specified
* property is already set, then it will remain set to its current value.
*
* @param name The name of the system property to set. It must not be
* {@code null}.
* @param value The value to use for the system property if it is not
* already set. It must not be {@code null}.
*
* @return The existing value for the system property, if it was previously
* defined, or {@code null} if it did not already have a value.
*/
@NotNull()
public static String setSystemPropertyIfNotAlreadyDefined(
@NotNull final String name,
@NotNull final String value)
{
final String existingValue = getSystemProperty(name);
if (existingValue == null)
{
return setSystemProperty(name, value);
}
else
{
return existingValue;
}
}



/**
* Attempts to clear the value of the specified system property. Note that
* this may not be permitted by some security managers, in which case the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public void testSystemClearProperty(final File f)
throws Exception
{
final Map<String,Set<Integer>> allowedExceptions = StaticUtils.mapOf(
"StaticUtils.java", StaticUtils.setOf(496, 506, 533, 537));
"StaticUtils.java", StaticUtils.setOf(496, 506, 564, 568));

final Map<Integer,String> unwrappedLines =
unwrapSourceLines(readFileLines(f));
Expand Down Expand Up @@ -252,7 +252,7 @@ public void testSystemGetEnv(final File f)
throws Exception
{
final Map<String,Set<Integer>> allowedExceptions = StaticUtils.mapOf(
"StaticUtils.java", StaticUtils.setOf(560, 564, 589, 593));
"StaticUtils.java", StaticUtils.setOf(591, 595, 620, 624));

final Map<Integer,String> unwrappedLines =
unwrapSourceLines(readFileLines(f));
Expand Down Expand Up @@ -299,7 +299,7 @@ public void testLoggerLogLevel(final File f)
{
final Map<String,Set<Integer>> allowedExceptions = StaticUtils.mapOf(
"Debug.java", StaticUtils.setOf(99),
"StaticUtils.java", StaticUtils.setOf(647, 670));
"StaticUtils.java", StaticUtils.setOf(678, 701));

final Map<Integer,String> unwrappedLines =
unwrapSourceLines(readFileLines(f));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@


import java.security.NoSuchProviderException;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -109,34 +108,4 @@ public void testLogging()
BouncyCastleFIPSHelper.disableLogging();
assertEquals(logger.getLevel(), Level.OFF);
}



/**
* Provides test coverage for the ability to set system properties if they are
* not already defined.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testSetPropertyIfNotDefined()
throws Exception
{
final String propertyName = UUID.randomUUID().toString();
final String propertyValue1 = UUID.randomUUID().toString();
assertNull(StaticUtils.getSystemProperty(propertyName));

BouncyCastleFIPSHelper.setPropertyIfNotDefined(propertyName,
propertyValue1);
assertNotNull(StaticUtils.getSystemProperty(propertyName));
assertEquals(StaticUtils.getSystemProperty(propertyName), propertyValue1);

final String propertyValue2 = UUID.randomUUID().toString();
assertFalse(propertyValue1.equals(propertyValue2));

BouncyCastleFIPSHelper.setPropertyIfNotDefined(propertyName,
propertyValue2);
assertNotNull(StaticUtils.getSystemProperty(propertyName));
assertEquals(StaticUtils.getSystemProperty(propertyName), propertyValue1);
}
}
35 changes: 35 additions & 0 deletions tests/unit/src/com/unboundid/util/StaticUtilsTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -3269,6 +3269,41 @@ public void testGetSystemProperties()



/**
* Provides test coverage for the ability to set system properties if they are
* not already defined.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testSetSystemPropertyIfNotAlreadyDefined()
throws Exception
{
final String propertyName = UUID.randomUUID().toString();
final String propertyValue1 = UUID.randomUUID().toString();
assertNull(StaticUtils.getSystemProperty(propertyName));

String returnValue = StaticUtils.setSystemPropertyIfNotAlreadyDefined(
propertyName, propertyValue1);
assertNull(returnValue);

assertNotNull(StaticUtils.getSystemProperty(propertyName));
assertEquals(StaticUtils.getSystemProperty(propertyName), propertyValue1);

final String propertyValue2 = UUID.randomUUID().toString();
assertFalse(propertyValue1.equals(propertyValue2));

returnValue = StaticUtils.setSystemPropertyIfNotAlreadyDefined(propertyName,
propertyValue2);
assertNotNull(returnValue);
assertEquals(returnValue, propertyValue1);

assertNotNull(StaticUtils.getSystemProperty(propertyName));
assertEquals(StaticUtils.getSystemProperty(propertyName), propertyValue1);
}



/**
* Tests the behavior of methods for interacting with environment variables.
*
Expand Down

0 comments on commit 45c422f

Please sign in to comment.