001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package examples;
017:
018: import java.io.IOException;
019: import java.net.InetAddress;
020: import java.net.UnknownHostException;
021: import org.apache.commons.net.FingerClient;
022:
023: /***
024: * This is an example of how you would implement the finger command
025: * in Java using NetComponents. The Java version is much shorter.
026: * But keep in mind that the Unix finger command reads all sorts of
027: * local files to output local finger information. This program only
028: * queries the finger daemon.
029: * <p>
030: * The -l flag is used to request long output from the server.
031: * <p>
032: ***/
033: public final class finger {
034:
035: public static final void main(String[] args) {
036: boolean longOutput = false;
037: int arg = 0, index;
038: String handle, host;
039: FingerClient finger;
040: InetAddress address = null;
041:
042: // Get flags. If an invalid flag is present, exit with usage message.
043: while (arg < args.length && args[arg].startsWith("-")) {
044: if (args[arg].equals("-l"))
045: longOutput = true;
046: else {
047: System.err
048: .println("usage: finger [-l] [[[handle][@<server>]] ...]");
049: System.exit(1);
050: }
051: ++arg;
052: }
053:
054: finger = new FingerClient();
055: // We want to timeout if a response takes longer than 60 seconds
056: finger.setDefaultTimeout(60000);
057:
058: if (arg >= args.length) {
059: // Finger local host
060:
061: try {
062: address = InetAddress.getLocalHost();
063: } catch (UnknownHostException e) {
064: System.err.println("Error unknown host: "
065: + e.getMessage());
066: System.exit(1);
067: }
068:
069: try {
070: finger.connect(address);
071: System.out.print(finger.query(longOutput));
072: finger.disconnect();
073: } catch (IOException e) {
074: System.err.println("Error I/O exception: "
075: + e.getMessage());
076: System.exit(1);
077: }
078:
079: return;
080: }
081:
082: // Finger each argument
083: while (arg < args.length) {
084:
085: index = args[arg].lastIndexOf("@");
086:
087: if (index == -1) {
088: handle = args[arg];
089: try {
090: address = InetAddress.getLocalHost();
091: } catch (UnknownHostException e) {
092: System.err.println("Error unknown host: "
093: + e.getMessage());
094: System.exit(1);
095: }
096: } else {
097: handle = args[arg].substring(0, index);
098: host = args[arg].substring(index + 1);
099:
100: try {
101: address = InetAddress.getByName(host);
102: } catch (UnknownHostException e) {
103: System.err.println("Error unknown host: "
104: + e.getMessage());
105: System.exit(1);
106: }
107: }
108:
109: System.out.println("[" + address.getHostName() + "]");
110:
111: try {
112: finger.connect(address);
113: System.out.print(finger.query(longOutput, handle));
114: finger.disconnect();
115: } catch (IOException e) {
116: System.err.println("Error I/O exception: "
117: + e.getMessage());
118: System.exit(1);
119: }
120:
121: ++arg;
122: if (arg != args.length)
123: System.out.print("\n");
124: }
125: }
126: }
|