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;
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 final 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              {
54                  System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]");
55                  System.exit(1);
56              }
57              ++arg;
58          }
59  
60  
61          finger = new FingerClient();
62          // We want to timeout if a response takes longer than 60 seconds
63          finger.setDefaultTimeout(60000);
64  
65          if (arg >= args.length)
66          {
67              // Finger local host
68  
69              try
70              {
71                  address = InetAddress.getLocalHost();
72              }
73              catch (UnknownHostException e)
74              {
75                  System.err.println("Error unknown host: " + e.getMessage());
76                  System.exit(1);
77              }
78  
79              try
80              {
81                  finger.connect(address);
82                  System.out.print(finger.query(longOutput));
83                  finger.disconnect();
84              }
85              catch (IOException e)
86              {
87                  System.err.println("Error I/O exception: " + e.getMessage());
88                  System.exit(1);
89              }
90  
91              return ;
92          }
93  
94          // Finger each argument
95          while (arg < args.length)
96          {
97  
98              index = args[arg].lastIndexOf("@");
99  
100             if (index == -1)
101             {
102                 handle = args[arg];
103                 try
104                 {
105                     address = InetAddress.getLocalHost();
106                 }
107                 catch (UnknownHostException e)
108                 {
109                     System.err.println("Error unknown host: " + e.getMessage());
110                     System.exit(1);
111                 }
112             }
113             else
114             {
115                 handle = args[arg].substring(0, index);
116                 host = args[arg].substring(index + 1);
117 
118                 try
119                 {
120                     address = InetAddress.getByName(host);
121                 }
122                 catch (UnknownHostException e)
123                 {
124                     System.err.println("Error unknown host: " + e.getMessage());
125                     System.exit(1);
126                 }
127             }
128 
129             System.out.println("[" + address.getHostName() + "]");
130 
131             try
132             {
133                 finger.connect(address);
134                 System.out.print(finger.query(longOutput, handle));
135                 finger.disconnect();
136             }
137             catch (IOException e)
138             {
139                 System.err.println("Error I/O exception: " + e.getMessage());
140                 System.exit(1);
141             }
142 
143             ++arg;
144             if (arg != args.length)
145                 System.out.print("\n");
146         }
147     }
148 }
149