Java Reference
In-Depth Information
The main functionality of the class is in one method,
lookUpNames()
. The
lookUp
Names()
method returns a
String
containing the whois response to a given query. The
arguments specify the string to search for, what kind of record to search for, which
database to search in, and whether an exact match is required. I could have used strings
or
int
constants to specify the kind of record to search for and the database to search
in, but because there are only a small number of valid values,
lookUpNames()
defines
enums with a fixed number of members instead. This solution provides much stricter
compile-time type-checking and guarantees the
Whois
class won't have to handle an
unexpected value.
Example 8-7. The Whois class
import
java.net.*
;
import
java.io.*
;
public
class
Whois
{
public
final
static
int
DEFAULT_PORT
=
43
;
public
final
static
String
DEFAULT_HOST
=
"whois.internic.net"
;
private
int
port
=
DEFAULT_PORT
;
private
InetAddress
host
;
public
Whois
(
InetAddress
host
,
int
port
)
{
this
.
host
=
host
;
this
.
port
=
port
;
}
public
Whois
(
InetAddress
host
)
{
this
(
host
,
DEFAULT_PORT
);
}
public
Whois
(
String
hostname
,
int
port
)
throws
UnknownHostException
{
this
(
InetAddress
.
getByName
(
hostname
),
port
);
}
public
Whois
(
String
hostname
)
throws
UnknownHostException
{
this
(
InetAddress
.
getByName
(
hostname
),
DEFAULT_PORT
);
}
public
Whois
()
throws
UnknownHostException
{
this
(
DEFAULT_HOST
,
DEFAULT_PORT
);
}
// Items to search for
public
enum
SearchFor
{
ANY
(
"Any"
),
NETWORK
(
"Network"
),
PERSON
(
"Person"
),
HOST
(
"Host"
),
DOMAIN
(
"Domain"
),
ORGANIZATION
(
"Organization"
),
GROUP
(
"Group"
),
GATEWAY
(
"Gateway"
),
ASN
(
"ASN"
);