📄 environmentcheck.java
字号:
/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: EnvironmentCheck.java,v 1.2.4.1 2005/09/09 07:13:59 pvedula Exp $ */package com.sun.org.apache.xalan.internal.xslt;import java.io.File;import java.io.FileWriter;import java.io.PrintWriter;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.Enumeration;import java.util.Hashtable;import java.util.StringTokenizer;import java.util.Vector;import 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 com.sun.org.apache.xalan.internal.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> * <?xml version="1.0"?> * <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"> * <xsl:output indent="yes"/> * <xsl:template match="/"> * <xsl:copy-of select="xalan:checkEnvironment()"/> * </xsl:template> * </xsl:stylesheet> * </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: * 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.4 2005/08/30 10:06:03 neerajbj 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.4 $): Useful stuff found: ----"); // Fake the Properties-like output for (Enumeration keys = h.keys(); keys.hasMoreElements(); /* no increment portion */ ) { Object key = keys.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; } /** Prefixed to hash keys that signify serious problems. */ public static final String ERROR = "ERROR."; /** Added to descriptions that signify potential problems. */ public static final String WARNING = "WARNING."; /** Value for any error found. */ public static final String ERROR_FOUND = "At least one error was found!"; /** Prefixed to hash keys that signify version numbers. */ public static final String VERSION = "version."; /** Prefixed to hash keys that signify .jars found in classpath. */ public static final String FOUNDCLASSES = "foundclasses."; /** Marker that a class or .jar was found. */ public static final String CLASS_PRESENT = "present-unknown-version"; /** Marker that a class or .jar was not found. */ public static final String CLASS_NOTPRESENT = "not-present"; /** Listing of common .jar files that include Xalan-related classes. */ public String[] jarNames = { "xalan.jar", "xalansamples.jar", "xalanj1compat.jar", "xalanservlet.jar", "serializer.jar", // Serializer (shared between Xalan & Xerces) "xerces.jar", // Xerces-J 1.x "xercesImpl.jar", // Xerces-J 2.x "testxsl.jar", "crimson.jar", "lotusxsl.jar", "jaxp.jar", "parser.jar", "dom.jar", "sax.jar", "xml.jar", "xml-apis.jar", "xsltc.jar" }; /** * Print out report of .jars found in a classpath. * * Takes the information encoded from a checkPathForJars() * call and dumps it out to our PrintWriter. * * @param v Vector of Hashtables of .jar file info * @param desc description to print out in header * * @return false if OK, true if any .jars were reported * as having errors * @see #checkPathForJars(String, String[]) */ protected boolean logFoundJars(Vector v, String desc) { if ((null == v) || (v.size() < 1)) return false; boolean errors = false; logMsg("#---- BEGIN Listing XML-related jars in: " + desc + " ----"); for (int i = 0; i < v.size(); i++) { Hashtable subhash = (Hashtable) v.elementAt(i); for (Enumeration keys = subhash.keys(); keys.hasMoreElements(); /* no increment portion */ ) { Object key = keys.nextElement(); String keyStr = (String) key; try { if (keyStr.startsWith(ERROR)) { errors = true; } logMsg(keyStr + "=" + subhash.get(keyStr)); } catch (Exception e) { errors = true; logMsg("Reading-" + key + "= threw: " + e.toString()); } } } logMsg("#----- END Listing XML-related jars in: " + desc + " -----"); return errors; } /** * Stylesheet extension entrypoint: Dump a basic Xalan * environment report from getEnvironmentHash() to a Node. * * <p>Copy of writeEnvironmentReport that creates a Node suitable * for other processing instead of a properties-like text output. * </p> * @param container Node to append our report to * @param factory Document providing createElement, etc. services * @param h Hash presumably from {@link #getEnvironmentHash()} * @see #writeEnvironmentReport(Hashtable) * for an equivalent that writes to a PrintWriter instead */ public void appendEnvironmentReport(Node container, Document factory, Hashtable h) { if ((null == container) || (null == factory)) { return; } try { Element envCheckNode = factory.createElement("EnvironmentCheck"); envCheckNode.setAttribute("version", "$Revision: 1.4 $"); container.appendChild(envCheckNode); if (null == h) { Element statusNode = factory.createElement("status"); statusNode.setAttribute("result", "ERROR"); statusNode.appendChild(factory.createTextNode("appendEnvironmentReport called with null Hashtable!")); envCheckNode.appendChild(statusNode); return; } boolean errors = false; Element hashNode = factory.createElement("environment"); envCheckNode.appendChild(hashNode);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -