1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package examples.unix;
19
20 import java.io.IOException;
21 import java.net.InetAddress;
22 import java.net.UnknownHostException;
23
24 import org.apache.commons.net.finger.FingerClient;
25
26 /***
27 * This is an example of how you would implement the finger command
28 * in Java using NetComponents. The Java version is much shorter.
29 * But keep in mind that the Unix finger command reads all sorts of
30 * local files to output local finger information. This program only
31 * queries the finger daemon.
32 * <p>
33 * The -l flag is used to request long output from the server.
34 * <p>
35 ***/
36 public final class finger
37 {
38
39 public static void main(String[] args)
40 {
41 boolean longOutput = false;
42 int arg = 0, index;
43 String handle, host;
44 FingerClient finger;
45 InetAddress address = null;
46
47 // Get flags. If an invalid flag is present, exit with usage message.
48 while (arg < args.length && args[arg].startsWith("-"))
49 {
50 if (args[arg].equals("-l")) {
51 longOutput = true;
52 } else {
53 System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]");
54 System.exit(1);
55 }
56 ++arg;
57 }
58
59
60 finger = new FingerClient();
61 // We want to timeout if a response takes longer than 60 seconds
62 finger.setDefaultTimeout(60000);
63
64 if (arg >= args.length)
65 {
66 // Finger local host
67
68 try
69 {
70 address = InetAddress.getLocalHost();
71 }
72 catch (UnknownHostException e)
73 {
74 System.err.println("Error unknown host: " + e.getMessage());
75 System.exit(1);
76 }
77
78 try
79 {
80 finger.connect(address);
81 System.out.print(finger.query(longOutput));
82 finger.disconnect();
83 }
84 catch (IOException e)
85 {
86 System.err.println("Error I/O exception: " + e.getMessage());
87 System.exit(1);
88 }
89
90 return ;
91 }
92
93 // Finger each argument
94 while (arg < args.length)
95 {
96
97 index = args[arg].lastIndexOf("@");
98
99 if (index == -1)
100 {
101 handle = args[arg];
102 try
103 {
104 address = InetAddress.getLocalHost();
105 }
106 catch (UnknownHostException e)
107 {
108 System.err.println("Error unknown host: " + e.getMessage());
109 System.exit(1);
110 }
111 }
112 else
113 {
114 handle = args[arg].substring(0, index);
115 host = args[arg].substring(index + 1);
116
117 try
118 {
119 address = InetAddress.getByName(host);
120 System.out.println("[" + address.getHostName() + "]");
121 }
122 catch (UnknownHostException e)
123 {
124 System.err.println("Error unknown host: " + e.getMessage());
125 System.exit(1);
126 }
127 }
128
129 try
130 {
131 finger.connect(address);
132 System.out.print(finger.query(longOutput, handle));
133 finger.disconnect();
134 }
135 catch (IOException e)
136 {
137 System.err.println("Error I/O exception: " + e.getMessage());
138 System.exit(1);
139 }
140
141 ++arg;
142 if (arg != args.length) {
143 System.out.print("\n");
144 }
145 }
146 }
147 }
148