    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.whois.WhoisClient;
    025
    026    /***
    027     * This is an example of how you would implement the Linux fwhois command
    028     * in Java using NetComponents.  The Java version is much shorter.
    029     * <p>
    030     ***/
    031    public final class fwhois
    032    {
    033
    034        public static final void main(String[] args)
    035        {
    036            int index;
    037            String handle, host;
    038            InetAddress address = null;
    039            WhoisClient whois;
    040
    041            if (args.length != 1)
    042            {
    043                System.err.println("usage: fwhois handle[@<server>]");
    044                System.exit(1);
    045            }
    046
    047            index = args[0].lastIndexOf("@");
    048
    049            whois = new WhoisClient();
    050            // We want to timeout if a response takes longer than 60 seconds
    051            whois.setDefaultTimeout(60000);
    052
    053            if (index == -1)
    054            {
    055                handle = args[0];
    056                host = WhoisClient.DEFAULT_HOST;
    057            }
    058            else
    059            {
    060                handle = args[0].substring(0, index);
    061                host = args[0].substring(index + 1);
    062            }
    063
    064            try
    065            {
    066                address = InetAddress.getByName(host);
    067            }
    068            catch (UnknownHostException e)
    069            {
    070                System.err.println("Error unknown host: " + e.getMessage());
    071                System.exit(1);
    072            }
    073
    074            System.out.println("[" + address.getHostName() + "]");
    075
    076            try
    077            {
    078                whois.connect(address);
    079                System.out.print(whois.query(handle));
    080                whois.disconnect();
    081            }
    082            catch (IOException e)
    083            {
    084                System.err.println("Error I/O exception: " + e.getMessage());
    085                System.exit(1);
    086            }
    087        }
    088
    089    }