com.ccg.net.tcp
Class InetAddressFilters

java.lang.Object
  extended by com.ccg.net.tcp.InetAddressFilters

public final class InetAddressFilters
extends Object

Collection of standard IP address filters.

This class provides many of the standard IP filters one is likely to need. You can filter to a specific host or particular network and do boolean OR, NOT and AND operations on sets of filters.

The fromString method is particularily handy as it can be used to dynamically load a set of OR conditions from a single string (handy when working with Java property files).

To see this in action, take a look at the Filter.java example.

Since:
1.0
Version:
$Revision: 1.2 $
Author:
$Author: pkb $
See Also:
InetAddressFilter, Filter.java

Method Summary
static InetAddressFilter acceptExactMatch(InetAddress addr)
          Get a IP filter which only "accepts" an IP address that matches a particular InetAddress exactly.
static InetAddressFilter acceptLocalHost()
          Get a IP filter which only "accepts" an IP address that is the local host (127.0.0.1).
static InetAddressFilter acceptMaskAndMatch(InetAddress match, InetAddress mask)
          Get a IP filter which only "accepts" an IP address that matches a particular InetAddress exactly.
static InetAddressFilter acceptThisHost()
          Get a IP filter which only "accepts" IP addresses that corresponds to this host.
static InetAddressFilter and(InetAddressFilter a, InetAddressFilter b)
          A boolean AND of two different filters.
static InetAddressFilter fromString(String s)
          A method to build a IP address filter from the contents of a string.
static InetAddressFilter not(InetAddressFilter a)
          A boolean NOT of a particular filter.
static InetAddressFilter or(InetAddressFilter a, InetAddressFilter b)
          A boolean OR of two different filters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

acceptLocalHost

public static InetAddressFilter acceptLocalHost()
Get a IP filter which only "accepts" an IP address that is the local host (127.0.0.1).

Returns:
A InetAddressFilter which only accepts local host.
Since:
1.0

acceptThisHost

public static InetAddressFilter acceptThisHost()
                                        throws UnknownHostException
Get a IP filter which only "accepts" IP addresses that corresponds to this host.

The filter returned by this method accepts any IP address that is associated with the local machine (the machine that the JVM is running on). This includes: 127.0.0.1 and ALL of the host names returned by InetAddress.getAllByName(java.lang.String).

Returns:
A InetAddressFilter which accepts all IP addresses that the host running the JVM is known as.
Throws:
UnknownHostException
Since:
1.0

acceptExactMatch

public static InetAddressFilter acceptExactMatch(InetAddress addr)
Get a IP filter which only "accepts" an IP address that matches a particular InetAddress exactly.

Parameters:
ip - The InetAddress to match - must not be null.
Returns:
A InetAddressFilter which only accepts a exact match.
Since:
1.0

acceptMaskAndMatch

public static InetAddressFilter acceptMaskAndMatch(InetAddress match,
                                                   InetAddress mask)
Get a IP filter which only "accepts" an IP address that matches a particular InetAddress exactly.

This method allows one to create filters for networks. For example, if the 'match' parameter were 192.168.0.0, and the 'mask' parameter was '255.255.0.0', then ALL IP addresses from 192.168.0.0 to 192.168.255.255 (inclusive) would be accepted.

Parameters:
match - The InetAddress to match - must not be null.
mask - The InetAddress to mask any address to check PRIOR to comparing it to the "match" address.
Returns:
A InetAddressFilter which does the mask/accept filtering.
Since:
1.0

or

public static InetAddressFilter or(InetAddressFilter a,
                                   InetAddressFilter b)
A boolean OR of two different filters.

This method allows one to combine two filters in a boolean OR. That is, it's accept method will return true if EITHER of the two filters passed returns true.

Parameters:
a - The first filter to check - may be null, in which case 'b' is returned.
b - The second filter to check - may be null, in which case 'a' is returned.
Returns:
A InetAddressFilter which to test IP addresses with, or null if BOTH 'a' and 'b' are null.
Since:
1.0

and

public static InetAddressFilter and(InetAddressFilter a,
                                    InetAddressFilter b)
A boolean AND of two different filters.

This method allows one to combine two filters in a boolean AND. That is, it's accept method will return true if BOTH of the two filters passed returns true. This is rarely used in a positive sense, but can be combined with the NOT operation to prevent certain hosts.

Parameters:
a - The first filter to check - may be null, in which case 'b' is returned.
b - The second filter to check - may be null, in which case 'a' is returned.
Returns:
A InetAddressFilter which to test IP addresses with, or null if BOTH 'a' and 'b' are null.
Since:
1.0

not

public static InetAddressFilter not(InetAddressFilter a)
A boolean NOT of a particular filter.

This method allows one to inverse the results of another filter. That is, it's accept method will return true if the filter passed to it returns false.

Parameters:
a - The filter to invert (do the NOT operation on).
Returns:
A InetAddressFilter which to test IP addresses with, or null if 'a' was null.
Since:
1.0

fromString

public static InetAddressFilter fromString(String s)
                                    throws UnknownHostException
A method to build a IP address filter from the contents of a string.

This method takes a string of the following form:

  MATCH0[/MASK0,MATCH1[/MASK1[,...]]]
 

For example, the following would accept a IP address if it matched the IP address of "mole.linux.bogus" or if it match the localhost "127.0.0.1", or if it was in the range 192.168.137.0 to 192.168.137.255:

  mole.linux.bogus,127.0.0.1,192.168.137.0/255.255.255.0
 

This can be really handy when you want to allow someone to configure IP filters in property files.

Parameters:
s - The string to parse for the filter list - must not be null.
Returns:
A InetAddressFilter which is a boolean OR of the comma separated host (and optional mask values).
Throws:
UnknownHostException
Since:
1.0


Copyright 1998-1998-2006 null. All Rights Reserved.