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