001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package examples.unix;
019
020import java.io.IOException;
021import java.net.InetAddress;
022import java.net.UnknownHostException;
023
024import org.apache.commons.net.finger.FingerClient;
025
026/***
027 * This is an example of how you would implement the finger command
028 * in Java using NetComponents.  The Java version is much shorter.
029 * But keep in mind that the Unix finger command reads all sorts of
030 * local files to output local finger information.  This program only
031 * queries the finger daemon.
032 * <p>
033 * The -l flag is used to request long output from the server.
034 ***/
035public final class finger
036{
037
038    public static void main(String[] args)
039    {
040        boolean longOutput = false;
041        int arg = 0, index;
042        String handle, host;
043        FingerClient finger;
044        InetAddress address = null;
045
046        // Get flags.  If an invalid flag is present, exit with usage message.
047        while (arg < args.length && args[arg].startsWith("-"))
048        {
049            if (args[arg].equals("-l")) {
050                longOutput = true;
051            } else {
052                System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]");
053                System.exit(1);
054            }
055            ++arg;
056        }
057
058
059        finger = new FingerClient();
060        // We want to timeout if a response takes longer than 60 seconds
061        finger.setDefaultTimeout(60000);
062
063        if (arg >= args.length)
064        {
065            // Finger local host
066
067            try
068            {
069                address = InetAddress.getLocalHost();
070            }
071            catch (UnknownHostException e)
072            {
073                System.err.println("Error unknown host: " + e.getMessage());
074                System.exit(1);
075            }
076
077            try
078            {
079                finger.connect(address);
080                System.out.print(finger.query(longOutput));
081                finger.disconnect();
082            }
083            catch (IOException e)
084            {
085                System.err.println("Error I/O exception: " + e.getMessage());
086                System.exit(1);
087            }
088
089            return ;
090        }
091
092        // Finger each argument
093        while (arg < args.length)
094        {
095
096            index = args[arg].lastIndexOf("@");
097
098            if (index == -1)
099            {
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