environmentcheck.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,376 行 · 第 1/4 页

JAVA
1,376
字号
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights  * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer.  * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment:   *       "This product includes software developed by the *        Apache Software Foundation (http://www.apache.org/)." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xalan" and "Apache Software Foundation" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written  *    permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", *    nor may "Apache" appear in their name, without prior written *    permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, Lotus * Development Corporation., http://www.lotus.com.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package org.apache.xalan.xslt;import java.io.File;import java.io.FileWriter;import java.io.PrintWriter;import java.lang.reflect.Method;import java.lang.reflect.Field;import java.util.Enumeration;import java.util.Hashtable;import java.util.Properties;import java.util.StringTokenizer;import java.util.Vector;// Used in append* methods onlyimport org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;/** * Utility class to report simple information about the environment. * Simplistic reporting about certain classes found in your JVM may  * help answer some FAQs for simple problems. * * <p>Usage-command line:   * <code> * java org.apache.xalan.xslt.EnvironmentCheck [-out outFile] * </code></p> *  * <p>Usage-from program:   * <code> * boolean environmentOK =  * (new EnvironmentCheck()).checkEnvironment(yourPrintWriter); * </code></p> * * <p>Usage-from stylesheet:   * <code><pre> *    &lt;?xml version="1.0"?&gt; *    &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" *        xmlns:xalan="http://xml.apache.org/xalan" *        exclude-result-prefixes="xalan"&gt; *    &lt;xsl:output indent="yes"/&gt; *    &lt;xsl:template match="/"&gt; *      &lt;xsl:copy-of select="xalan:checkEnvironment()"/&gt; *    &lt;/xsl:template&gt; *    &lt;/xsl:stylesheet&gt; * </pre></code></p> *   * <p>Xalan users reporting problems are encouraged to use this class  * to see if there are potential problems with their actual  * Java environment <b>before</b> reporting a bug.  Note that you  * should both check from the JVM/JRE's command line as well as  * temporarily calling checkEnvironment() directly from your code,  * since the classpath may differ (especially for servlets, etc).</p> * * <p>Also see http://xml.apache.org/xalan-j/faq.html</p> * * <p>Note: This class is pretty simplistic: it does a fairly simple  * unordered search of the classpath; it only uses Class.forName()  * to load things, not actually querying the classloader; so the  * results are not necessarily definitive nor will it find all  * problems related to environment setup.  Also, you should avoid  * calling this in deployed production code, both because it is  * quite slow and because it forces classes to get loaded.</p> * * <p>Note: This class explicitly has very limited compile-time  * dependencies to enable easy compilation and usage even when  * Xalan, DOM/SAX/JAXP, etc. are not present.</p> *  * <p>Note: for an improved version of this utility, please see  * the xml-commons' project Which utility which does the same kind  * of thing but in a much simpler manner.</p> * * @author Shane_Curcuru@us.ibm.com * @version $Id: EnvironmentCheck.java,v 1.14 2002/10/31 15:06:43 ilene Exp $ */public class EnvironmentCheck{  /**   * Command line runnability: checks for [-out outFilename] arg.   * <p>Command line entrypoint; Sets output and calls    * {@link #checkEnvironment(PrintWriter)}.</p>   * @param args command line args   */  public static void main(String[] args)  {    // Default to System.out, autoflushing    PrintWriter sendOutputTo = new PrintWriter(System.out, true);    // Read our simplistic input args, if supplied    for (int i = 0; i < args.length; i++)    {      if ("-out".equalsIgnoreCase(args[i]))      {        i++;        if (i < args.length)        {          try          {            sendOutputTo = new PrintWriter(new FileWriter(args[i], true));          }          catch (Exception e)          {            System.err.println("# WARNING: -out " + args[i] + " threw "                               + e.toString());          }        }        else        {          System.err.println(            "# WARNING: -out argument should have a filename, output sent to console");        }      }    }    EnvironmentCheck app = new EnvironmentCheck();    app.checkEnvironment(sendOutputTo);  }  /**   * Programmatic entrypoint: Report on basic Java environment    * and CLASSPATH settings that affect Xalan.   *   * <p>Note that this class is not advanced enough to tell you    * everything about the environment that affects Xalan, and    * sometimes reports errors that will not actually affect    * Xalan's behavior.  Currently, it very simplistically    * checks the JVM's environment for some basic properties and    * logs them out; it will report a problem if it finds a setting    * or .jar file that is <i>likely</i> to cause problems.</p>   *   * <p>Advanced users can peruse the code herein to help them    * investigate potential environment problems found; other users    * may simply send the output from this tool along with any bugs    * they submit to help us in the debugging process.</p>   *   * @param pw PrintWriter to send output to; can be sent to a    * file that will look similar to a Properties file; defaults    * to System.out if null   * @return true if your environment appears to have no major    * problems; false if potential environment problems found   * @see #getEnvironmentHash()   */  public boolean checkEnvironment(PrintWriter pw)  {    // Use user-specified output writer if non-null    if (null != pw)      outWriter = pw;    // Setup a hash to store various environment information in    Hashtable hash = getEnvironmentHash();    // Check for ERROR keys in the hashtable, and print report    boolean environmentHasErrors = writeEnvironmentReport(hash);    if (environmentHasErrors)    {      // Note: many logMsg calls have # at the start to       //  fake a property-file like output      logMsg("# WARNING: Potential problems found in your environment!");      logMsg("#    Check any 'ERROR' items above against the Xalan FAQs");      logMsg("#    to correct potential problems with your classes/jars");      logMsg("#    http://xml.apache.org/xalan-j/faq.html");      if (null != outWriter)        outWriter.flush();      return false;    }    else    {      logMsg("# YAHOO! Your environment seems to be OK.");      if (null != outWriter)        outWriter.flush();      return true;    }  }  /**   * Fill a hash with basic environment settings that affect Xalan.   *   * <p>Worker method called from various places.</p>   * <p>Various system and CLASSPATH, etc. properties are put into    * the hash as keys with a brief description of the current state    * of that item as the value.  Any serious problems will be put in    * with a key that is prefixed with {@link #ERROR 'ERROR.'} so it   * stands out in any resulting report; also a key with just that    * constant will be set as well for any error.</p>   * <p>Note that some legitimate cases are flaged as potential    * errors - namely when a developer recompiles xalan.jar on their    * own - and even a non-error state doesn't guaruntee that    * everything in the environment is correct.  But this will help    * point out the most common classpath and system property   * problems that we've seen.</p>      *   * @return Hashtable full of useful environment info about Xalan    * and related system properties, etc.   */  public Hashtable getEnvironmentHash()  {    // Setup a hash to store various environment information in    Hashtable hash = new Hashtable();    // Call various worker methods to fill in the hash    //  These are explicitly separate for maintenance and so     //  advanced users could call them standalone    checkJAXPVersion(hash);    checkProcessorVersion(hash);    checkParserVersion(hash);    checkAntVersion(hash);    checkDOMVersion(hash);    checkSAXVersion(hash);    checkSystemProperties(hash);    return hash;  }  /**   * Dump a basic Xalan environment report to outWriter.     *   * <p>This dumps a simple header and then each of the entries in    * the Hashtable to our PrintWriter; it does special processing    * for entries that are .jars found in the classpath.</p>   *   * @param h Hashtable of items to report on; presumably   * filled in by our various check*() methods   * @return true if your environment appears to have no major    * problems; false if potential environment problems found   * @see #appendEnvironmentReport(Node, Document, Hashtable)   * for an equivalent that appends to a Node instead   */  protected boolean writeEnvironmentReport(Hashtable h)  {    if (null == h)    {      logMsg("# ERROR: writeEnvironmentReport called with null Hashtable");      return false;    }    boolean errors = false;    logMsg(      "#---- BEGIN writeEnvironmentReport($Revision: 1.14 $): Useful stuff found: ----");    // Fake the Properties-like output    for (Enumeration enum = h.keys();          enum.hasMoreElements();        /* no increment portion */        )    {      Object key = enum.nextElement();      String keyStr = (String) key;      try      {        // Special processing for classes found..        if (keyStr.startsWith(FOUNDCLASSES))        {          Vector v = (Vector) h.get(keyStr);          errors |= logFoundJars(v, keyStr);        }        // ..normal processing for all other entries        else        {          // Note: we could just check for the ERROR key by itself,           //    since we now set that, but since we have to go           //    through the whole hash anyway, do it this way,          //    which is safer for maintenance          if (keyStr.startsWith(ERROR))          {            errors = true;          }          logMsg(keyStr + "=" + h.get(keyStr));        }      }      catch (Exception e)      {        logMsg("Reading-" + key + "= threw: " + e.toString());      }    }    logMsg(      "#----- END writeEnvironmentReport: Useful properties found: -----");    return errors;  }

⌨️ 快捷键说明

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