【Java开源代码栏目提醒】:网学会员Java开源代码为您提供Code.java参考,解决您在Code.java学习中工作中的难题,参考学习。
// ========================================================================
// Copyright (c) 1997 MortBay Consulting, Sydney
// $Id: Code.java,v 3.14 2001/01/10 23:22:39 gregwilkins Exp $
// ========================================================================
package com.mortbay.Util;
import java.applet.Applet;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;
/* ----------------------------------------------------------------------- */
/** Coding Standards support.
* Support for defensive programming
*
* Coding defensively as follows:
* <pre>
* {
* ...
* Code.assert(booleanExpression(),"Assert that expression evaluates true");
* ...
* Code.assertEquals(objectA,objectB,"Assert equality");
* Code.assertEquals(1,2,"Assert equality");
* ...
* Code.debug("Debug message");
* ...
* Code.debug("Debug message",exception,object);
* ...
* Code.warning("warning message");
* ...
* Code.warning("warning message",exception);
* ...
* Code.notImplemented();
* Code.fail("fatal error");
* ...
* }
* </pre>
* The debug output can be controlled with java properties:
* <UL>
* <LI>DEBUG - If set, debug output is enabled
* <LI>DEBUG_PATTERNS - If set to a comma separated list of strings,
* Then debug output is only produced from those classes whose fully
* qualified class name contains one of the strings as a substring.
* <LI>DEBUG_OPTIONS - A string of option character. 'W' - suppress
* warnings unless debug is on, 'S' suppress stack frame dumps
* <LI>DEBUG_VERBOSE - Set to an integer verbosity level for
* use in controlling debug verbosity (see verbose(int))
* <UL><p>
* <h4>Usage</h4>
* <pre>
* java [-DDEBUG [-DDEBUG_PATTERNS="MyClass,my.package"] \\
* [-DDEBUG_OPTIONS=[S][W]] \\
* [-DDEBUG_VERBOSE=n] ] \\
* my.package.main
* </pre>
*
* @see ISS.Base.Log
* @version $Id: Code.java,v 3.14 2001/01/10 23:22:39 gregwilkins Exp $
* @author Greg Wilkins
*/
public class Code
{
/*-------------------------------------------------------------------*/
private static final String __lock="LOCK";
/*-------------------------------------------------------------------*/
/** Shared static instances, reduces object creation at expense
* of lock contention in multi threaded debugging */
private static StringWriter __stringWriter = new StringWriter();
private static StringBuffer __writerBuffer = __stringWriter.getBuffer();
private static PrintWriter __out = new PrintWriter(__stringWriter,false);
/*-------------------------------------------------------------------*/
private static class Singleton {static Code __instance=new Code();}
static Code instance()
{
return Singleton.__instance;
}
/*-------------------------------------------------------------------*/
/** Initialize default behaviour from applet parameters.
*
* Look for applet parameters of the same name as the
* system properties used to config Code.
*/
public static void initParamsFromApplet(java.applet.Applet appl)
{
boolean d = appl.getParameter("DEBUG") != null;
setDebug(d);
String o = appl.getParameter("DEBUG_OPTIONS");
if (o!=null)
{
setSuppressStack(o.indexOf("S")>=0);
setSuppressWarnings(o.indexOf("W")>=0);
}
else
{
setSuppressStack(false);
setSuppressWarnings(false);
}
String dp = appl.getParameter("DEBUG_PATTERNS");
setDebugPatterns(dp);
String dt = appl.getParameter("DEBUG_TRIGGERS");
setDebugTriggers(dt);
String v = appl.getParameter("DEBUG_VERBOSE");
if (v!=null)
setVerbose(Integer.parseInt(v));
}
/*-------------------------------------------------------------------*/
boolean _debugOn=false;
private boolean _suppressStack=true;
private boolean _suppressWarnings=false;
private int _verbose=0;
Vector _debugPatterns=null;
private Vector _debugTriggers=null;
private String _patterns=null;
private String _triggers=null;
private Hashtable _triggerSet = new Hashtable();
/*-------------------------------------------------------------------*/
/** Construct shared instance that decodes the options setup
* environments properties.
*/
protected Code()
{
Singleton.__instance=this;
try{
_debugOn= System.getProperty("DEBUG") != null;
String o = System.getProperty("DEBUG_OPTIONS");
if (o!=null)
{
setSuppressStack(!(o.indexOf("S")>=0));
setSuppressWarnings(!(o.indexOf("W")>=0));
}
else
{
setSuppressStack(false);
setSuppressWarnings(false);
}
String dp = System.getProperty("DEBUG_PATTERNS");
setDebugPatterns(dp);
String dt = System.getProperty("DEBUG_TRIGGERS");
setDebugTriggers(dt);
String v = System.getProperty("DEBUG_VERBOSE");
if (v!=null)
setVerbose(Integer.parseInt(v));
}
catch (Exception e){
System.err.println("Exception from getProperty!\n"+
"Probably running in applet\n"+
"Use Code.initParamsFromApplet or Code.setOption to control debug output.");
}
}
/* ------------------------------------------------------------ */
/** Set if debugging is on or off.
* @param debug
*/
public static synchronized void setDebug(boolean debug)
{
Code code =instance();
boolean oldDebug=code._debugOn;
if (code._debugOn && !debug)
Code.debug(2,"DEBUG OFF");
code._debugOn=debug;
if (!oldDebug && debug)
Code.debug(2,"DEBUG ON");
}
/* ------------------------------------------------------------ */
/** Get the debug status.
* @return the debug status
*/
public static boolean getDebug()
{
return instance()._debugOn;
}
/* ------------------------------------------------------------ */
/** Suppress stack trace.
* @param stack if true stacks are not produced
*/
public static void setSuppressStack(boolean stack)
{
instance()._suppressStack=stack;
}
/* ------------------------------------------------------------ */
/** Get the stack suppression status.
* @return the stack suppression status
*/
public static boolean getSuppressStack()
{
return instance()._suppressStack;
}
/* ------------------------------------------------------------ */
/** Set warning suppression.
* @param warnings Warnings suppress if this is true and debug is false
*/
public static void setSuppressWarnings(boolean warnings)
{
instance()._suppressWarnings=warnings;
}
/* ------------------------------------------------------------ */
/** Get the warnings suppression status.
* @return the warnings suppression status
*/
public static boolean getSuppressWarnings()
{
return instance()._suppressWarnings;
}
/* ------------------------------------------------------------ */
/** Set verbosity level.
* @param verbose
*/
public static void setVerbose(int verbose)
{
instance()._ver
上一篇:
CNZipOutputStream.java
下一篇:
让我掉下眼泪的