1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.FingerClient;
22
23 /***
24 * This is an example of how you would implement the finger command
25 * in Java using NetComponents. The Java version is much shorter.
26 * But keep in mind that the Unix finger command reads all sorts of
27 * local files to output local finger information. This program only
28 * queries the finger daemon.
29 * <p>
30 * The -l flag is used to request long output from the server.
31 * <p>
32 ***/
33 public final class finger
34 {
35
36 public static final void main(String[] args)
37 {
38 boolean longOutput = false;
39 int arg = 0, index;
40 String handle, host;
41 FingerClient finger;
42 InetAddress address = null;
43
44
45 while (arg < args.length && args[arg].startsWith("-"))
46 {
47 if (args[arg].equals("-l"))
48 longOutput = true;
49 else
50 {
51 System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]");
52 System.exit(1);
53 }
54 ++arg;
55 }
56
57
58 finger = new FingerClient();
59
60 finger.setDefaultTimeout(60000);
61
62 if (arg >= args.length)
63 {
64
65
66 try
67 {
68 address = InetAddress.getLocalHost();
69 }
70 catch (UnknownHostException e)
71 {
72 System.err.println("Error unknown host: " + e.getMessage());
73 System.exit(1);
74 }
75
76 try
77 {
78 finger.connect(address);
79 System.out.print(finger.query(longOutput));
80 finger.disconnect();
81 }
82 catch (IOException e)
83 {
84 System.err.println("Error I/O exception: " + e.getMessage());
85 System.exit(1);
86 }
87
88 return ;
89 }
90
91
92 while (arg < args.length)
93 {
94
95 index = args[arg].lastIndexOf("@");
96
97 if (index == -1)
98 {
99 handle = args[arg];
100 try
101 {
102 address = InetAddress.getLocalHost();
103 }
104 catch (UnknownHostException e)
105 {
106 System.err.println("Error unknown host: " + e.getMessage());
107 System.exit(1);
108 }
109 }
110 else
111 {
112 handle = args[arg].substring(0, index);
113 host = args[arg].substring(index + 1);
114
115 try
116 {
117 address = InetAddress.getByName(host);
118 }
119 catch (UnknownHostException e)
120 {
121 System.err.println("Error unknown host: " + e.getMessage());
122 System.exit(1);
123 }
124 }
125
126 System.out.println("[" + address.getHostName() + "]");
127
128 try
129 {
130 finger.connect(address);
131 System.out.print(finger.query(longOutput, handle));
132 finger.disconnect();
133 }
134 catch (IOException e)
135 {
136 System.err.println("Error I/O exception: " + e.getMessage());
137 System.exit(1);
138 }
139
140 ++arg;
141 if (arg != args.length)
142 System.out.print("\n");
143 }
144 }
145 }
146