| Line | Hits | Note | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * $Id: ArraysUtil.java 1011 2008-06-16 17:57:36Z amandel $ | ||
| 3 | * | ||
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. | ||
| 5 | * | ||
| 6 | * Redistribution and use in source and binary forms, with or without | ||
| 7 | * modification, are permitted provided that the following conditions are | ||
| 8 | * met: | ||
| 9 | * | ||
| 10 | * * Redistributions of source code must retain the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer. | ||
| 12 | * * Redistributions in binary form must reproduce the above | ||
| 13 | * copyright notice, this list of conditions and the following | ||
| 14 | * disclaimer in the documentation and/or other materials | ||
| 15 | * provided with the distribution. | ||
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of | ||
| 17 | * its contributors may be used to endorse or promote products | ||
| 18 | * derived from this software without specific prior written | ||
| 19 | * permission. | ||
| 20 | * | ||
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND | ||
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS | ||
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | ||
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 32 | */ | ||
| 33 | package org.jcoderz.commons.tracing; | ||
| 34 | |||
| 35 | import java.util.logging.Level; | ||
| 36 | import java.util.logging.Logger; | ||
| 37 | |||
| 38 | /** | ||
| 39 | * A Tracer implementation that used {@link java.util.logging} | ||
| 40 | * entering exiting logger as tracing sink. | ||
| 41 | * See {@link Logger#entering(String, String)} for an example. | ||
| 42 | * | ||
| 43 | * @author Andreas Mandel | ||
| 44 | */ | ||
| 45 | (1) | public class JulTracer | |
| 46 | implements Tracer | ||
| 47 | { | ||
| 48 | /** The backing java logger. */ | ||
| 49 | (2) | private final Logger mLogger; | |
| 50 | /** Name of the class where this logger belongs to. */ | ||
| 51 | private final String mClassName; | ||
| 52 | |||
| 53 | |||
| 54 | /** | ||
| 55 | * Create a new instance of this Tracer. | ||
| 56 | * The underlying java logger is instantly created. | ||
| 57 | * @param tracerName the name of the tracer to be created, should be | ||
| 58 | * equal to the name of the class that uses the tracer. | ||
| 59 | */ | ||
| 60 | private JulTracer (String tracerName) | ||
| 61 | 0 | { | |
| 62 | 0 | mLogger = Logger.getLogger(tracerName); | |
| 63 | 0 | mClassName = tracerName; | |
| 64 | 0 | } | |
| 65 | |||
| 66 | /** | ||
| 67 | * Factory for a tracer for classes with the given name. | ||
| 68 | * @param className the name of the class that uses this tracer. | ||
| 69 | * @return a tracer for the given class that dumps trace output to | ||
| 70 | * java logging. | ||
| 71 | */ | ||
| 72 | public static JulTracer getTracer (String className) | ||
| 73 | { | ||
| 74 | 0 | return new JulTracer(className); | |
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | /** | ||
| 79 | * Returns true, if the level of the underlying java logger is | ||
| 80 | * {@link Level#FINER} or below. | ||
| 81 | * @see org.jcoderz.commons.tracing.Tracer#isTracing() | ||
| 82 | */ | ||
| 83 | (3) | public boolean isTracing () | |
| 84 | { | ||
| 85 | 0 | return mLogger.isLoggable(Level.FINER); | |
| 86 | } | ||
| 87 | |||
| 88 | /** | ||
| 89 | * Returns always true. | ||
| 90 | * @see org.jcoderz.commons.tracing.Tracer#isTracingArguments() | ||
| 91 | */ | ||
| 92 | (4) | public boolean isTracingArguments () | |
| 93 | { | ||
| 94 | 0 | return true; | |
| 95 | } | ||
| 96 | |||
| 97 | /** | ||
| 98 | * Returns the Name of the class where this tracer belongs to. | ||
| 99 | * @return the Name of the class where this tracer belongs to. | ||
| 100 | */ | ||
| 101 | public String getClassName () | ||
| 102 | { | ||
| 103 | 0 | return mClassName; | |
| 104 | } | ||
| 105 | |||
| 106 | /** | ||
| 107 | * Delegates to {@link Logger#entering(String, String)}. | ||
| 108 | * The className is expected to be the same as the tracer name and | ||
| 109 | * is silently ignored. | ||
| 110 | * @see Tracer#entering(java.lang.String, java.lang.String) | ||
| 111 | */ | ||
| 112 | (5)(6)(7) | public TracingToken entering (String className, String methodName) | |
| 113 | { | ||
| 114 | final TracingToken result; | ||
| 115 | 0 | if (isTracing()) | |
| 116 | { | ||
| 117 | 0 | mLogger.entering(getClassName(), methodName); | |
| 118 | 0 | result = new JulTracingToken(methodName); | |
| 119 | } | ||
| 120 | else | ||
| 121 | { | ||
| 122 | 0 | result = null; | |
| 123 | } | ||
| 124 | 0 | return result; | |
| 125 | } | ||
| 126 | |||
| 127 | /** | ||
| 128 | * Delegates to {@link Logger#entering(String, String, Object)}. | ||
| 129 | * The className is expected to be the same as the tracer name and | ||
| 130 | * is silently ignored. | ||
| 131 | * @see Tracer#entering(String, String, Object) | ||
| 132 | */ | ||
| 133 | (8)(9)(10) | public TracingToken entering (String className, String methodName, | |
| 134 | (11) | Object argument) | |
| 135 | { | ||
| 136 | final TracingToken result; | ||
| 137 | 0 | if (isTracing()) | |
| 138 | { | ||
| 139 | 0 | mLogger.entering(getClassName(), methodName, argument); | |
| 140 | 0 | result = new JulTracingToken(methodName); | |
| 141 | } | ||
| 142 | else | ||
| 143 | { | ||
| 144 | 0 | result = null; | |
| 145 | } | ||
| 146 | 0 | return result; | |
| 147 | } | ||
| 148 | |||
| 149 | /** | ||
| 150 | * Delegates to {@link Logger#entering(String, String, Object)}. | ||
| 151 | * The className is expected to be the same as the tracer name and | ||
| 152 | * is silently ignored. | ||
| 153 | * @see Tracer#entering(String, String, Object[]) | ||
| 154 | */ | ||
| 155 | (12)(13)(14) | public TracingToken entering (String className, String methodName, | |
| 156 | (15) | Object[] arguments) | |
| 157 | { | ||
| 158 | final TracingToken result; | ||
| 159 | 0 | if (isTracing()) | |
| 160 | { | ||
| 161 | 0 | mLogger.entering(getClassName(), methodName, arguments); | |
| 162 | 0 | result = new JulTracingToken(methodName); | |
| 163 | } | ||
| 164 | else | ||
| 165 | { | ||
| 166 | 0 | result = null; | |
| 167 | } | ||
| 168 | 0 | return result; | |
| 169 | } | ||
| 170 | |||
| 171 | /** | ||
| 172 | * Delegates to {@link Logger#exiting(String, String)}. | ||
| 173 | * If the token is not a {@link JulTracingToken} the call is | ||
| 174 | * silently ignored. | ||
| 175 | * @see Tracer#exiting(TracingToken) | ||
| 176 | */ | ||
| 177 | (16)(17) | public void exiting (TracingToken token) | |
| 178 | { | ||
| 179 | 0 | if (token instanceof JulTracingToken) | |
| 180 | { | ||
| 181 | 0 | final JulTracingToken julTracingToken = (JulTracingToken) token; | |
| 182 | 0 | mLogger.exiting(getClassName(), julTracingToken.getMethodName()); | |
| 183 | } | ||
| 184 | 0 | } | |
| 185 | |||
| 186 | /** | ||
| 187 | * Delegates to {@link Logger#exiting(String, String)}. | ||
| 188 | * If the token is not a {@link JulTracingToken} the call is | ||
| 189 | * silently ignored. | ||
| 190 | * @see Tracer#exiting(TracingToken, java.lang.Object) | ||
| 191 | */ | ||
| 192 | (18)(19)(20) | public void exiting (TracingToken token, Object result) | |
| 193 | { | ||
| 194 | 0 | if (token instanceof JulTracingToken) | |
| 195 | { | ||
| 196 | 0 | final JulTracingToken julTracingToken = (JulTracingToken) token; | |
| 197 | 0 | mLogger.exiting( | |
| 198 | getClassName(), julTracingToken.getMethodName(), result); | ||
| 199 | } | ||
| 200 | 0 | } | |
| 201 | |||
| 202 | /** | ||
| 203 | * Delegates to {@link Logger#throwing(String, String, Throwable)}. | ||
| 204 | * If the token is not a {@link JulTracingToken} the call is | ||
| 205 | * silently ignored. | ||
| 206 | * @see Tracer#throwing(TracingToken, java.lang.Throwable) | ||
| 207 | */ | ||
| 208 | (21)(22)(23) | public void throwing (TracingToken token, Throwable thrown) | |
| 209 | { | ||
| 210 | 0 | if (token instanceof JulTracingToken) | |
| 211 | { | ||
| 212 | 0 | final JulTracingToken julTracingToken = (JulTracingToken) token; | |
| 213 | 0 | mLogger.throwing(getClassName(), julTracingToken.getMethodName(), | |
| 214 | thrown); | ||
| 215 | } | ||
| 216 | 0 | } | |
| 217 | |||
| 218 | /** | ||
| 219 | * Stores context information for the {@link JulTracer}. | ||
| 220 | * @see TracingToken | ||
| 221 | * @author Andreas Mandel | ||
| 222 | */ | ||
| 223 | private class JulTracingToken implements TracingToken | ||
| 224 | { | ||
| 225 | private final String mMethodName; | ||
| 226 | |||
| 227 | public JulTracingToken (String methodName) | ||
| 228 | 0 | (24) | { |
| 229 | 0 | mMethodName = methodName; | |
| 230 | 0 | } | |
| 231 | |||
| 232 | public String getMethodName () | ||
| 233 | { | ||
| 234 | 0 | return mMethodName; | |
| 235 | } | ||
| 236 | |||
| 237 | } | ||
| 238 | } |