View Javadoc

1   /*
2    * Copyright 2001-2005 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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          // Get flags.  If an invalid flag is present, exit with usage message.
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          // We want to timeout if a response takes longer than 60 seconds
60          finger.setDefaultTimeout(60000);
61  
62          if (arg >= args.length)
63          {
64              // Finger local host
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          // Finger each argument
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