org.bouncycastle.util.Properties Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcprov-jdk14 Show documentation
Show all versions of bcprov-jdk14 Show documentation
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.4.
package org.bouncycastle.util;
import java.math.BigInteger;
import java.security.AccessControlException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.Security;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
/**
* Utility method for accessing system properties.
*/
public class Properties
{
private Properties()
{
}
private static final ThreadLocal threadProperties = new ThreadLocal();
/**
* Return whether a particular override has been set to true.
*
* @param propertyName the property name for the override.
* @return true if the property is set to "true", false otherwise.
*/
public static boolean isOverrideSet(String propertyName)
{
try
{
String p = getPropertyValue(propertyName);
return "true".equalsIgnoreCase(p);
}
catch (AccessControlException e)
{
return false;
}
}
/**
* Enable the specified override property for the current thread only.
*
* @param propertyName the property name for the override.
* @param enable true if the override should be enabled, false if it should be disabled.
* @return true if the override was already set true, false otherwise.
*/
public static boolean setThreadOverride(String propertyName, boolean enable)
{
boolean isSet = isOverrideSet(propertyName);
Map localProps = (Map)threadProperties.get();
if (localProps == null)
{
localProps = new HashMap();
threadProperties.set(localProps);
}
localProps.put(propertyName, enable ? "true" : "false");
return isSet;
}
/**
* Remove any value for the specified override property for the current thread only.
*
* @param propertyName the property name for the override.
* @return true if the override was already set true in thread local, false otherwise.
*/
public static boolean removeThreadOverride(String propertyName)
{
Map localProps = (Map)threadProperties.get();
if (localProps != null)
{
String p = (String)localProps.remove(propertyName);
if (p != null)
{
//if (localProps.isEmpty())
//{
//threadProperties.remove();
//}
return "true".equalsIgnoreCase(p);
}
}
return false;
}
public static BigInteger asBigInteger(String propertyName)
{
String p = getPropertyValue(propertyName);
if (p != null)
{
return new BigInteger(p);
}
return null;
}
public static Set asKeySet(String propertyName)
{
Set set = new HashSet ();
String p = getPropertyValue(propertyName);
if (p != null)
{
StringTokenizer sTok = new StringTokenizer(p, ",");
while (sTok.hasMoreElements())
{
set.add(Strings.toLowerCase(sTok.nextToken()).trim());
}
}
return Collections.unmodifiableSet(set);
}
public static String getPropertyValue(final String propertyName)
{
String val = (String)AccessController.doPrivileged(new PrivilegedAction()
{
public Object run()
{
return Security.getProperty(propertyName);
}
});
if (val != null)
{
return val;
}
Map localProps = (Map)threadProperties.get();
if (localProps != null)
{
String p = (String)localProps.get(propertyName);
if (p != null)
{
return p;
}
}
return (String)AccessController.doPrivileged(new PrivilegedAction()
{
public Object run()
{
return System.getProperty(propertyName);
}
});
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy