/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.0 (the
 * "License"). You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */
package com.sun.jini.start;

import java.net.ConnectException;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.net.URL;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.MissingResourceException; 
import java.util.StringTokenizer;

/**
 * Class which can be used to print a descriptive warning message 
 * if a codebase accessibility problem is found.
 *
 * @author Sun Microsystems, Inc.
 *
 */
public class HTTPDStatus {
    /** Config logger. */
    private static final Logger logger = ServiceStarter.logger;

    /**
     * Command line interface that checks the accessability of a desired
     * JAR file(s), given its <code>URL</code> address. 
     * Note: The provided <code>URL</code>(s) cannot contain embedded spaces.
     *
     * @param args <code>String</code> array containing the command line
     *             arguments
     */
    public static void main(String[] args) {
        if(args.length < 1) {
            System.err.println("Usage: HTTPDStatus URL1 [URL2 ... URLN]");
            return;
        }//endif
	for (int i=0; i < args.length; i++) {
	    httpdWarning(args[i]);
	}
    }//end main


    /**
     * Method that takes a <code>codebase</code>
     * parameter and displays a warning message if
     * it is determined that a potential codebase
     * accessibility problem exists.
     *
     * @param codebase  <code>String</code> containing the codebase to poll
     *                  for the existence of a running HTTP server with
     *                  access to the JAR file referenced in this parameter
     */
    public static void httpdWarning(String codebase) {
        if (codebase == null) {
	    logger.log(Level.WARNING, "httpserver.warning",
	        new Object[] {codebase, "Codebase is null"});
	    return;
	}
        StringTokenizer st = new StringTokenizer(codebase," ");
        String url = null;
	URL u = null;
        for (int i = 0; st.hasMoreTokens(); i++) {
            url = st.nextToken();
            try {
                u = new URL(url);
		String fileName = u.getFile();
		//Skip file check for directories
		if (fileName == null ||
		    fileName.endsWith("/")) {
		    logger.log(Level.FINEST, "httpserver.skipping", url);
		} else {
	            try {
	                drainStream(u.openStream());
	            } catch (Exception ioe) {
                        logger.log(Level.WARNING, "httpserver.warning",
		            new Object[] {url, ioe.toString()});
		        logger.log(Level.FINEST, "httpserver.exception", ioe);
	            }
		}
            } catch(MalformedURLException e) {
	        logger.log(Level.WARNING, "httpserver.unknownprotocol", url);
		logger.log(Level.FINEST, "httpserver.exception", e);
            }
	}
        return;
    }//end httpdStatus
    
    /**
     * Reads and discards all data from a given input stream.
     *
     * @param is the <code>InputStream</code> from which to read data
     */
    private static void drainStream(InputStream is) throws IOException {
        BufferedInputStream reader =
            new BufferedInputStream(is);
        while (reader.read() != -1) {}
    }//end drainSocket
    
}//end class HTTPDStatus
