01: /*
02: * Copyright 2001-2005 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package examples;
17:
18: import java.io.IOException;
19: import java.net.InetAddress;
20: import java.net.UnknownHostException;
21: import org.apache.commons.net.WhoisClient;
22:
23: /***
24: * This is an example of how you would implement the Linux fwhois command
25: * in Java using NetComponents. The Java version is much shorter.
26: * <p>
27: ***/
28: public final class fwhois {
29:
30: public static final void main(String[] args) {
31: int index;
32: String handle, host;
33: InetAddress address = null;
34: WhoisClient whois;
35:
36: if (args.length != 1) {
37: System.err.println("usage: fwhois handle[@<server>]");
38: System.exit(1);
39: }
40:
41: index = args[0].lastIndexOf("@");
42:
43: whois = new WhoisClient();
44: // We want to timeout if a response takes longer than 60 seconds
45: whois.setDefaultTimeout(60000);
46:
47: if (index == -1) {
48: handle = args[0];
49: host = WhoisClient.DEFAULT_HOST;
50: } else {
51: handle = args[0].substring(0, index);
52: host = args[0].substring(index + 1);
53: }
54:
55: try {
56: address = InetAddress.getByName(host);
57: } catch (UnknownHostException e) {
58: System.err.println("Error unknown host: " + e.getMessage());
59: System.exit(1);
60: }
61:
62: System.out.println("[" + address.getHostName() + "]");
63:
64: try {
65: whois.connect(address);
66: System.out.print(whois.query(handle));
67: whois.disconnect();
68: } catch (IOException e) {
69: System.err
70: .println("Error I/O exception: " + e.getMessage());
71: System.exit(1);
72: }
73: }
74:
75: }
|