001: package org.esupportail.cas.server.util.log;
002:
003: import org.dom4j.Element;
004: import org.esupportail.cas.server.util.MisconfiguredHandlerException;
005:
006: /**
007: * This class implements a basic object owning debugging features.
008: *
009: * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
010: */
011: public abstract class Debug {
012: /**
013: * A debugging flag.
014: */
015: private boolean debug;
016:
017: /**
018: * Tell if the handler is in debugging mode or not.
019: *
020: * @return true when the handler is in debugging mode, false otherwise.
021: */
022: public final boolean isDebug() {
023: return debug;
024: }
025:
026: /**
027: * Set deubgging mode.
028: *
029: * @param d a boolean
030: */
031: public final void setDebug(final boolean d) {
032: this .debug = d;
033: }
034:
035: /**
036: * Debugging.
037: * @param str a string
038: */
039: protected final void trace(final String str) {
040: if (debug) {
041: Log.debug(str);
042: }
043: }
044:
045: /**
046: * Debugging.
047: * @param obj an object
048: */
049: protected final void trace(final Object obj) {
050: trace(obj.toString());
051: }
052:
053: /**
054: * Debugging (at the beginning of the execution of a method).
055: */
056: protected final void traceBegin() {
057: if (debug) {
058: Log.traceBegin();
059: }
060: }
061:
062: /**
063: * Debugging (at the end of the execution of a method).
064: * @param str a string
065: */
066: protected final void traceEnd(final String str) {
067: if (debug) {
068: Log.traceEnd(str);
069: }
070: }
071:
072: /**
073: * Debugging (at the end of the execution of a method).
074: * @param obj an object
075: */
076: protected final void traceEnd(final Object obj) {
077: traceEnd(obj.toString());
078: }
079:
080: /** Debugging (at the end of the execution of a method). */
081: protected final void traceEnd() {
082: traceEnd("");
083: }
084:
085: /**
086: * Debugging (throwing an exception).
087: *
088: * @param e the exception to throw
089: * @throws Exception Exception
090: */
091: protected final void traceThrow(final Exception e) throws Exception {
092: traceEnd("THROWS " + e.getClass().getName() + ": "
093: + e.getMessage());
094: throw e;
095: }
096:
097: /**
098: * Constructor.
099: *
100: * @param d true to set debugging mode, false otherwise.
101: */
102: protected Debug(final boolean d) {
103: super ();
104: debug = d;
105: traceBegin();
106: trace("debug = " + debug);
107: traceEnd();
108: }
109:
110: /**
111: * A convenient method to get debugging mode with a default value.
112: *
113: * @param element an XML element having a debug attribute ("on", "off" or "default")
114: * @param defaultValue the value to return when str equals to "default"
115: *
116: * @return a boolean.
117: */
118: public static boolean elementDebugValue(final Element element,
119: final boolean defaultValue) {
120: String str = element.attributeValue("debug", "default");
121: if (str.equals("on")) {
122: return true;
123: } else if (str.equals("off")) {
124: return false;
125: } else {
126: return defaultValue;
127: }
128: }
129:
130: /**
131: * Check that a class is present
132: *
133: * @param classname the name of the class
134: * @return the corresponding class
135: * @throws Exception Exception
136: */
137: protected final Class checkClass(final String classname)
138: throws Exception {
139: traceBegin();
140: trace("Checking if class \"" + classname + "\" is available...");
141: try {
142: traceEnd("ok");
143: return Class.forName(classname);
144: } catch (ClassNotFoundException e) {
145: trace("Class not found.");
146: traceThrow(new MisconfiguredHandlerException("Class \""
147: + classname + "\" is needed by \""
148: + getClass().getName() + "\" handlers."));
149: // never reached
150: return null;
151: }
152: }
153:
154: }
|