/*
 * 
 * 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.tool;

import java.io.FileNotFoundException;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import net.jini.url.httpmd.HttpmdUtil;

/**
 * Prints the message digest for the contents of a URL. This utility is run
 * from the {@linkplain #main command line}. <p>
 *
 * An example command line usage is:
 *
 * <blockquote>
 * <pre>
 * java -cp <var><b>install_dir</b></var>/lib/tools.jar
 *      com.sun.jini.tool.ComputeDigest
 *      <var><b>install_dir</b></var>/lib/reggie.jar
 *      sha
 * </pre>
 * </blockquote>
 *
 * where <var><b>install_dir</b></var> is the directory where the starter kit
 * is installed. This command prints out the message digest for the
 * <code>reggie.jar</code> JAR file, using the <code>sha</code> algorithm.
 *
 * @author Sun Microsystems, Inc.
 * @since 2.0
 */
public class ComputeDigest {
    private static ResourceBundle resources;
    private static boolean resinit = false;

    private ComputeDigest() { }

    /**
     * Prints the message digest for the contents of a URL. The command
     * line arguments are:
     * <pre>
     * <var><b>url</b></var> [ <var><b>algorithm</b></var> ]
     * </pre>
     * The first argument specifies the URL, which is parsed in the context
     * of a <code>file:</code> URL. The second argument, if present,
     * specifies the message digest algorithm, which defaults to
     * <code>md5</code>.
     */
    public static void main(String[] args) {
	if (args.length < 1 || args.length > 2) {
	    print("computedigest.usage", null);
	    System.exit(1);
	}
	String algorithm = args.length > 1 ? args[1] : "md5";
	try {
	    URL url = new URL(new URL("file:"), args[0]);
	    System.out.println(HttpmdUtil.computeDigest(url, algorithm));
	    return;
	} catch (FileNotFoundException e) {
	    print("computedigest.notfound", args[0]);
	} catch (NoSuchAlgorithmException e) {
	    print("computedigest.badalg", algorithm);
	} catch (Throwable t) {
	    t.printStackTrace();
	}
	System.exit(1);
    }

    private static synchronized String getString(String key) {
	if (!resinit) {
	    try {
		resources = ResourceBundle.getBundle(
				 "com.sun.jini.tool.resources.computedigest");
		resinit = true;
	    } catch (MissingResourceException e) {
		e.printStackTrace();
	    }
	}
	try {
	    return resources.getString(key);
	} catch (MissingResourceException e) {
	    return null;
	}
    }

    private static void print(String key, String val) {
	String fmt = getString(key);
	if (fmt == null)
	    fmt = "no text found: \"" + key + "\" {0}";
	System.err.println(MessageFormat.format(fmt, new String[]{val}));
    }
}
