j2metckframeworkinfo.java.svn-base

来自「cqME :java framework for TCK test.」· SVN-BASE 代码 · 共 202 行

SVN-BASE
202
字号
/* * $Id$ * * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package com.sun.tck.j2me;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FilenameFilter;import java.io.IOException;import java.io.InputStream;import java.io.File;import java.security.DigestInputStream;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Properties;import static com.sun.midp.jadtool.Base64.*;/** * Utility class to get some useful info regarding the J2ME TCK Framework. * */public final class J2meTckFrameworkInfo {    private static Properties props = loadProps();    private static File libDir;    private static final String J2ME_FW_JAR = "j2mefw_jt.jar";private static final byte[] BUFFER = new byte[32 * 1024];    private J2meTckFrameworkInfo() {        // empty private constructor    }    /**     * Prints out framework's information, such as name and version, to the     * standard output.     *     * @param args ignored.     */    public static void main(String[] args) {        System.out.println("Name: " + getName());        System.out.println("Version: " + getVersion());        System.out.println(                "Build Date: " + props.getProperty("framework.date"));        System.out.println();        try {            System.out.println("Install Dir: "                    + getFrameworkLibDir().getCanonicalPath());        } catch (IOException ignore) {            // ignore        }        System.out.println();        listAvailableJarFiles();    }    // lists all present in the Framework directory JAR files,    // and their checksums (SHA-1 digests, Base64 encoded)    private static void listAvailableJarFiles() {        File baseDir = getFrameworkLibDir();        File[] jarFiles = baseDir.listFiles(new FilenameFilter() {            public boolean accept(File dir, String name) {                return name.endsWith(".jar") || name.endsWith(".JAR");            }        });        if (jarFiles != null && jarFiles.length > 0) {            System.out.println(                    "Available JAR files (" + jarFiles.length + " total):");            for (File file : jarFiles) {                System.out.printf("  %1$-35s Checksum: %2$s",                        file.getName(), calculateDigest(file));                System.out.println();            }        }    }    // might return null if the digest cannot be calculated for any reason    private static String calculateDigest(File file) {        String digest = null;        InputStream fileContent = null;        try {            MessageDigest shaDigest = MessageDigest.getInstance("SHA-1");            fileContent = new DigestInputStream(                    new FileInputStream(file), shaDigest);            while (fileContent.read(BUFFER) != -1) {                // do nothing            }            digest = encode(shaDigest.digest());        } catch (NoSuchAlgorithmException ignore) {            // ignore        } catch (FileNotFoundException e) {            // ignore        } catch (IOException e) {            // ignore        } finally {            if (fileContent != null) {                try {                    fileContent.close();                } catch (IOException ignore) {                    // ignore                }            }        }        return digest;    }    /**     * Returns framework's name, or <code>null</code> if the name is unknown.     *     * @return framework's name.     */    public static String getName() {        return props.getProperty("framework.name");    }    /**     * Returns framework's version, or <code>null</code> if the version is     * unknown. E.g. <code>1.0.2</code>.     *     * @return framework's name.     */    public static String getVersion() {        return props.getProperty("framework.version");    }    /**     * Returns J2ME TCK Framework lib directory, where     * all framework jars files are.     *     * @return J2ME TCK Framework lib directory     *     */    public static File getFrameworkLibDir() {        if (libDir != null) {            // already calculated the location            return libDir;        }        String location =                 J2meTckFrameworkInfo.class.getProtectionDomain().getCodeSource().getLocation().getFile();        location = location.substring(0, location.indexOf(J2ME_FW_JAR));                libDir = new File(location);                assert (libDir.exists() && libDir.isDirectory()) :                "J2ME TCK Framework Directory not found:" + libDir.toString();        return libDir;    }    private static Properties loadProps() {        if (props != null) {            return props;        }        props = new Properties();        InputStream propsStream = null;        try {            propsStream = J2meTckFrameworkInfo.class.getResourceAsStream(                    "framework.properties");            if (propsStream != null) {                props.load(propsStream);            }        } catch (IOException e) {            assert false : "IOException during properties loading";        } finally {            if (propsStream != null) {                try {                    propsStream.close();                } catch (IOException e1) {                    assert false : "IOException during propsStream.close()";                }            }        }        return props;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?