Moves the StackProperties.initialize() method to HostCandidateHarvester.initializeInterfaceFilters(). Don\'t die if the host candidate interface filters have not been initialized. Instead, initialize them on demand.

This commit is contained in:
gp 2014-05-30 15:02:31 +00:00
parent d8552464e0
commit 53d69cd963
2 changed files with 167 additions and 117 deletions

View File

@ -152,21 +152,6 @@ public class StackProperties
public static final String BLOCKED_INTERFACES
= "org.ice4j.ice.harvest.BLOCKED_INTERFACES";
/**
* Holds the list of allowed interfaces. It's either a non-empty array or null.
*/
private static String[] allowedInterfaces;
/**
* Holds the list of blocked interfaces. It's either a non-empty array or null.
*/
private static String[] blockedInterfaces;
/**
* A boolean value that indicates whether the StackProperties class has been
* initialized or not.
*/
private static boolean initialized = false;
/**
* Returns the String value of the specified property (minus all
@ -209,7 +194,7 @@ public class StackProperties
* @return the array of strings computed by splitting the specified
* property value around matches of the given regular expression
*/
private static String[] getStringArray(String propertyName, String regex)
public static String[] getStringArray(String propertyName, String regex)
{
String str = getString(propertyName);
if (str == null)
@ -229,32 +214,6 @@ public class StackProperties
return res.toArray(new String[res.size()]);
}
/**
* Gets the array of allowed interfaces.
*
* @return the non-empty String array of allowed interfaces or null.
*/
public static String[] getAllowedInterfaces(){
if (!initialized)
throw new IllegalStateException("The StackProperties class has " +
"not been initialized.");
return allowedInterfaces;
}
/**
* Gets the array of blocked interfaces.
*
* @return the non-empty String array of blocked interfaces or null.
*/
public static String[] getBlockedInterfaces() {
if (!initialized)
throw new IllegalStateException("The StackProperties class has " +
"not been initialized.");
return blockedInterfaces;
}
/**
* Returns the value of a specific property as a signed decimal integer. If
* a property with the specified property name exists, its string
@ -318,73 +277,4 @@ public class StackProperties
return (str == null) ? defaultValue : Boolean.parseBoolean(str);
}
public static void initialize()
{
// Initialize the allowed interfaces array.
allowedInterfaces = StackProperties
.getStringArray(ALLOWED_INTERFACES, ";");
if (allowedInterfaces != null) // getStringArray returns null if the array is empty.
{
// Validate the allowed interfaces array.
// 1. Make sure the allowedInterfaces list contains interfaces that exist
// on the system.
for (String iface : allowedInterfaces)
try {
NetworkInterface.getByName(iface);
} catch (SocketException e) {
throw new IllegalStateException("there is no network interface " +
"with the name " + iface, e);
}
// the allowedInterfaces array is not empty and its items represent valid
// interfaces => there's at least one listening interface.
}
else
{
// NOTE The blocked interfaces list is taken into account only if the allowed
// interfaces list is not defined => initialize the blocked interfaces
// only if the allowed interfaces list is not defined.
// Initialize the blocked interfaces array.
blockedInterfaces = StackProperties
.getStringArray(BLOCKED_INTERFACES, ";");
if (blockedInterfaces != null) // getStringArray returns null if the array is empty.
{
// Validate the blocked interfaces array.
// 1. Make sure the blockedInterfaces list contains interfaces that exist
// on the system.
for (String iface : blockedInterfaces)
try {
NetworkInterface.getByName(iface);
} catch (SocketException e) {
throw new IllegalStateException("there is no network interface " +
"with the name " + iface, e);
}
// 2. Make sure there's at least one allowed interface.
Enumeration<NetworkInterface> allInterfaces;
try {
allInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
throw new IllegalStateException("could not get the list of the available " +
"network interfaces", e);
}
int count = 0;
while (allInterfaces.hasMoreElements()) {
allInterfaces.nextElement();
count++;
}
if (blockedInterfaces.length >= count)
throw new IllegalStateException("all network interfaces are blocked");
}
}
initialized = true;
}
}

View File

@ -38,6 +38,68 @@ public class HostCandidateHarvester
*/
private HarvestStatistics harvestStatistics = new HarvestStatistics();
/**
* Holds the list of allowed interfaces. It's either a non-empty array or
* null.
*/
private static String[] allowedInterfaces;
/**
* Holds the list of blocked interfaces. It's either a non-empty array or
* null.
*/
private static String[] blockedInterfaces;
/**
* A boolean value that indicates whether the host candidate interface
* filters have been initialized or not.
*/
private static boolean interfaceFiltersinitialized = false;
/**
* Gets the array of allowed interfaces.
*
* @return the non-empty String array of allowed interfaces or null.
*/
public static String[] getAllowedInterfaces(){
if (!interfaceFiltersinitialized)
{
try
{
initializeInterfaceFilters();
}
catch (Exception e)
{
logger.log(Level.WARNING, "There were errors during host " +
"candidate interface filters initialization.", e);
}
}
return allowedInterfaces;
}
/**
* Gets the array of blocked interfaces.
*
* @return the non-empty String array of blocked interfaces or null.
*/
public static String[] getBlockedInterfaces() {
if (!interfaceFiltersinitialized)
{
try
{
initializeInterfaceFilters();
}
catch (Exception e)
{
logger.log(Level.WARNING, "There were errors during host " +
"candidate interface filters initialization.", e);
}
}
return blockedInterfaces;
}
/**
* Gathers all candidate addresses on the local machine, binds sockets on
* them and creates {@link HostCandidate}s. The harvester would always
@ -212,11 +274,10 @@ public class HostCandidateHarvester
? iface.getDisplayName()
: iface.getName();
String[] allowedInterfaces = StackProperties
.getAllowedInterfaces();
String[] allowedInterfaces = getAllowedInterfaces();
// NOTE The blocked interfaces list is taken into account only if the allowed
// interfaces list is not defined.
// NOTE The blocked interfaces list is taken into account only if the
// allowed interfaces list is not defined.
// getAllowedInterfaces returns null if the array is empty.
if (allowedInterfaces != null)
@ -227,8 +288,7 @@ public class HostCandidateHarvester
else
{
// A list of allowed interfaces does not exist.
String[] blockedInterfaces = StackProperties
.getBlockedInterfaces();
String[] blockedInterfaces = getBlockedInterfaces();
// getBlockedInterfaces returns null if the array is empty.
if (blockedInterfaces != null)
@ -473,4 +533,104 @@ public class HostCandidateHarvester
{
return harvestStatistics;
}
/**
* Initializes the host candidate interface filters stored in the
* <tt>org.ice4j.ice.harvest.ALLOWED_INTERFACES</tt> and
* <tt>org.ice4j.ice.harvest.BLOCKED_INTERFACES</tt> properties.
*
* @throws java.lang.IllegalStateException if there were errors during host
* candidate interface filters initialization.
*/
public static void initializeInterfaceFilters()
{
synchronized (HostCandidateHarvester.class)
{
// We want this method to run only once.
if (interfaceFiltersinitialized)
return;
interfaceFiltersinitialized = true;
}
// Initialize the allowed interfaces array.
allowedInterfaces = StackProperties.getStringArray(
StackProperties.ALLOWED_INTERFACES, ";");
// getStringArray returns null if the array is empty.
if (allowedInterfaces != null)
{
// Validate the allowed interfaces array.
// 1. Make sure the allowedInterfaces list contains interfaces that
// exist on the system.
for (String iface : allowedInterfaces)
try
{
NetworkInterface.getByName(iface);
}
catch (SocketException e)
{
throw new IllegalStateException("there is no network " +
"interface with the name " + iface, e);
}
// the allowedInterfaces array is not empty and its items represent
// valid interfaces => there's at least one listening interface.
}
else
{
// NOTE The blocked interfaces list is taken into account only if
// the allowed interfaces list is not defined => initialize the
// blocked interfaces only if the allowed interfaces list is not
// defined.
// Initialize the blocked interfaces array.
blockedInterfaces = StackProperties.getStringArray(
StackProperties.BLOCKED_INTERFACES, ";");
// getStringArray returns null if the array is empty.
if (blockedInterfaces != null)
{
// Validate the blocked interfaces array.
// 1. Make sure the blockedInterfaces list contains interfaces
// that exist on the system.
for (String iface : blockedInterfaces)
try
{
NetworkInterface.getByName(iface);
}
catch (SocketException e)
{
throw new IllegalStateException("there is no " +
"network interface with the name " + iface,
e);
}
// 2. Make sure there's at least one allowed interface.
Enumeration<NetworkInterface> allInterfaces;
try
{
allInterfaces = NetworkInterface.getNetworkInterfaces();
}
catch (SocketException e)
{
throw new IllegalStateException("could not get the " +
"list of the available network interfaces", e);
}
int count = 0;
while (allInterfaces.hasMoreElements())
{
allInterfaces.nextElement();
count++;
}
if (blockedInterfaces.length >= count)
throw new IllegalStateException("all network " +
"interfaces are blocked");
}
}
}
}