    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
    018    package examples.unix;
    019
    020    import java.io.IOException;
    021    import java.net.InetAddress;
    022    import java.net.UnknownHostException;
    023
    024    import 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     * <p>
    035     ***/
    036    public final class finger
    037    {
    038
    039        public static final void main(String[] args)
    040        {
    041            boolean longOutput = false;
    042            int arg = 0, index;
    043            String handle, host;
    044            FingerClient finger;
    045            InetAddress address = null;
    046
    047            // Get flags.  If an invalid flag is present, exit with usage message.
    048            while (arg < args.length && args[arg].startsWith("-"))
    049            {
    050                if (args[arg].equals("-l"))
    051                    longOutput = true;
    052                else
    053                {
    054                    System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]");
    055                    System.exit(1);
    056                }
    057                ++arg;
    058            }
    059
    060
    061            finger = new FingerClient();
    062            // We want to timeout if a response takes longer than 60 seconds
    063            finger.setDefaultTimeout(60000);
    064
    065            if (arg >= args.length)
    066            {
    067                // Finger local host
    068
    069                try
    070                {
    071                    address = InetAddress.getLocalHost();
    072                }
    073                catch (UnknownHostException e)
    074                {
    075                    System.err.println("Error unknown host: " + e.getMessage());
    076                    System.exit(1);
    077                }
    078
    079                try
    080                {
    081                    finger.connect(address);
    082                    System.out.print(finger.query(longOutput));
    083                    finger.disconnect();
    084                }
    085                catch (IOException e)
    086                {
    087                    System.err.println("Error I/O exception: " + e.getMessage());
    088                    System.exit(1);
    089                }
    090
    091                return ;
    092            }
    093
    094            // Finger each argument
    095            while (arg < args.length)
    096            {
    097
    098                index = args[arg].lastIndexOf("@");
    099
    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