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             * Debugging. 
046             * @param obj an object 
047             */
048            protected final void trace(final Object obj) { 
049                    trace(obj.toString());
050            }
051    
052            /** 
053             * Debugging (at the beginning of the execution of a method). 
054             */
055            protected final void traceBegin() { 
056                    if (debug) {
057                            Log.traceBegin(); 
058                    }
059            }
060            /** 
061             * Debugging (at the end of the execution of a method). 
062             * @param str a string 
063             */
064            protected final void traceEnd(final String str) { 
065                    if (debug) {
066                            Log.traceEnd(str); 
067                    }
068            }
069            /** 
070             * Debugging (at the end of the execution of a method). 
071             * @param obj an object 
072             */
073            protected final void traceEnd(final Object obj) { 
074                    traceEnd(obj.toString()); 
075            }
076            /** Debugging (at the end of the execution of a method). */
077            protected final void traceEnd() { 
078                    traceEnd(""); 
079            }
080            
081            /** 
082             * Debugging (throwing an exception).
083             * 
084             * @param e the exception to throw
085             * @throws Exception Exception
086             */
087            protected final void traceThrow(final Exception e) throws Exception { 
088                    traceEnd("THROWS " + e.getClass().getName() + ": " + e.getMessage());
089                    throw e;
090            }
091            
092            /**
093             * Constructor.
094             *
095             * @param d true to set debugging mode, false otherwise.
096             */
097            protected Debug(final boolean d) {
098                    super();
099                    debug = d;
100                    traceBegin();
101                    trace("debug = " + debug);
102                    traceEnd();
103            }
104    
105            /**
106             * A convenient method to get debugging mode with a default value.
107             *
108             * @param element an XML element having a debug attribute ("on", "off" or "default")
109             * @param defaultValue the value to return when str equals to "default"
110             *
111             * @return a boolean.
112             */
113            public static boolean elementDebugValue(
114                            final Element element,
115                            final boolean defaultValue) {
116                    String str = element.attributeValue("debug", "default");
117                    if (str.equals("on")) {
118                            return true;
119                    } else if (str.equals("off")) {
120                            return false;
121                    } else {
122                            return defaultValue;
123                    }
124            }
125            
126            /**
127             * Check that a class is present
128             * 
129             * @param classname the name of the class
130             * @return the corresponding class
131             * @throws Exception Exception
132             */
133            protected final Class checkClass(final String classname) throws Exception {
134                    traceBegin();
135                    trace("Checking if class \"" + classname + "\" is available...");
136                    try {
137                            traceEnd("ok");
138                            return Class.forName(classname);
139                    } catch (ClassNotFoundException e) {
140                            trace("Class not found.");
141                            traceThrow(new MisconfiguredHandlerException(
142                                            "Class \"" + classname + "\" is needed by \"" 
143                                            + getClass().getName() 
144                                            + "\" handlers."
145                                            ));
146                            // never reached
147                            return null;
148                    }
149            }
150            
151    }
152