View Javadoc
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   ***/
35  public final class finger
36  {
37  
38      public static void main(String[] args)
39      {
40          boolean longOutput = false;
41          int arg = 0, index;
42          String handle, host;
43          FingerClient finger;
44          InetAddress address = null;
45  
46          // Get flags.  If an invalid flag is present, exit with usage message.
47          while (arg < args.length && args[arg].startsWith("-"))
48          {
49              if (args[arg].equals("-l")) {
50                  longOutput = true;
51              } else {
52                  System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]");
53                  System.exit(1);
54              }
55              ++arg;
56          }
57  
58  
59          finger = new FingerClient();
60          // We want to timeout if a response takes longer than 60 seconds
61          finger.setDefaultTimeout(60000);
62  
63          if (arg >= args.length)
64          {
65              // Finger local host
66  
67              try
68              {
69                  address = InetAddress.getLocalHost();
70              }
71              catch (UnknownHostException e)
72              {
73                  System.err.println("Error unknown host: " + e.getMessage());
74                  System.exit(1);
75              }
76  
77              try
78              {
79                  finger.connect(address);
80                  System.out.print(finger.query(longOutput));
81                  finger.disconnect();
82              }
83              catch (IOException e)
84              {
85                  System.err.println("Error I/O exception: " + e.getMessage());
86                  System.exit(1);
87              }
88  
89              return ;
90          }
91  
92          // Finger each argument
93          while (arg < args.length)
94          {
95  
96              index = args[arg].lastIndexOf("@");
97  
98              if (index == -1)
99              {
100                 handle = args[arg];
101                 try
102                 {
103                     address = InetAddress.getLocalHost();
104                 }
105                 catch (UnknownHostException e)
106                 {
107                     System.err.println("Error unknown host: " + e.getMessage());
108                     System.exit(1);
109                 }
110             }
111             else
112             {
113                 handle = args[arg].substring(0, index);
114                 host = args[arg].substring(index + 1);
115 
116                 try
117                 {
118                     address = InetAddress.getByName(host);
119                     System.out.println("[" + address.getHostName() + "]");
120                 }
121                 catch (UnknownHostException e)
122                 {
123                     System.err.println("Error unknown host: " + e.getMessage());
124                     System.exit(1);
125                 }
126             }
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 }
147