Coverage Report - org.apache.myfaces.shared_impl.trace.TracingSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
TracingSupport
0%
0/49
0%
0/18
0
 
 1  
 /*
 2  
  *  Licensed to the Apache Software Foundation (ASF) under one
 3  
  *  or more contributor license agreements.  See the NOTICE file
 4  
  *  distributed with this work for additional information
 5  
  *  regarding copyright ownership.  The ASF licenses this file
 6  
  *  to you under the Apache License, Version 2.0 (the
 7  
  *  "License"); you may not use this file except in compliance
 8  
  *  with the License.  You may obtain a copy of the License at
 9  
  * 
 10  
  *  http://www.apache.org/licenses/LICENSE-2.0
 11  
  * 
 12  
  *  Unless required by applicable law or agreed to in writing,
 13  
  *  software distributed under the License is distributed on an
 14  
  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  *  KIND, either express or implied.  See the License for the
 16  
  *  specific language governing permissions and limitations
 17  
  *  under the License.
 18  
  */
 19  
 package org.apache.myfaces.shared_impl.trace;
 20  
 
 21  
 import java.util.logging.Level;
 22  
 import java.util.logging.Logger;
 23  
 
 24  
 /**
 25  
  * Support class for tracing method calls providing params, exceptions and return types
 26  
  * 
 27  
  * @author Mathias Broekelmann (latest modification by $Author: skitching $)
 28  
  * @version $Revision: 676197 $ $Date: 2008-07-12 11:07:17 -0500 (Sat, 12 Jul 2008) $
 29  
  */
 30  
 public class TracingSupport
 31  
 {
 32  0
     private static final Object[] EMPTY_PARAMS = new Object[0];
 33  
 
 34  
     private Logger _logger;
 35  0
     private Level _level = Level.FINE;
 36  
     private String _sourceClass;
 37  
 
 38  
     public TracingSupport()
 39  
     {
 40  0
         this(Logger.getAnonymousLogger());
 41  0
     }
 42  
 
 43  
     public TracingSupport(Class clazz)
 44  
     {
 45  0
         this(clazz.getName());
 46  0
     }
 47  
 
 48  
     public TracingSupport(String className)
 49  
     {
 50  0
         this(Logger.getLogger(className));
 51  0
     }
 52  
 
 53  
     public TracingSupport(Logger logger)
 54  0
     {
 55  0
         _logger = logger;
 56  0
         _sourceClass = logger.getName();
 57  0
     }
 58  
 
 59  
     /**
 60  
      * @return the log level
 61  
      */
 62  
     public Level getLevel()
 63  
     {
 64  0
         return _level;
 65  
     }
 66  
 
 67  
     /**
 68  
      * 
 69  
      * @param level
 70  
      *            the log level to use
 71  
      */
 72  
     public void setLevel(Level level)
 73  
     {
 74  0
         if (level == null)
 75  
         {
 76  0
             throw new IllegalArgumentException("log level can not be null");
 77  
         }
 78  0
         _level = level;
 79  0
     }
 80  
 
 81  
     public <T> T trace(String methodName, Closure<T> closure)
 82  
     {
 83  0
         return trace(methodName, closure, EMPTY_PARAMS);
 84  
     }
 85  
 
 86  
     public <T> T trace(String methodName, Closure<T> closure, Object... params)
 87  
     {
 88  0
         if (_logger.isLoggable(_level))
 89  
         {
 90  0
             _logger.logp(_level, _sourceClass, methodName, "ENTRY" + prepareParams(params), params);
 91  
             try
 92  
             {
 93  0
                 T result = closure.call();
 94  0
                 if (!Void.class.equals(result))
 95  
                 {
 96  0
                     _logger.logp(_level, _sourceClass, methodName, "RETURN {0}", result);
 97  
                 }
 98  
                 else
 99  
                 {
 100  0
                     _logger.logp(_level, _sourceClass, methodName, "RETURN");
 101  
                 }
 102  0
                 return result;
 103  
             }
 104  0
             catch (RuntimeException e)
 105  
             {
 106  0
                 _logger.logp(_level, _sourceClass, methodName, "THROW", e);
 107  0
                 throw e;
 108  
             }
 109  
         }
 110  
         else
 111  
         {
 112  0
             return closure.call();
 113  
         }
 114  
     }
 115  
 
 116  
     private String prepareParams(Object[] params)
 117  
     {
 118  0
         if (params == null || params.length == 0)
 119  
         {
 120  0
             return "";
 121  
         }
 122  0
         StringBuilder builder = new StringBuilder(" ");
 123  0
         for (int i = 0, size = params.length; i < size; i++)
 124  
         {
 125  0
             builder.append("{");
 126  0
             builder.append(i);
 127  0
             builder.append("}");
 128  0
             if (i + 1 < size)
 129  
             {
 130  0
                 builder.append(",");
 131  
             }
 132  
         }
 133  0
         return builder.toString();
 134  
     }
 135  
 
 136  
     public Logger getLogger()
 137  
     {
 138  0
         return _logger;
 139  
     }
 140  
 
 141  
     /**
 142  
      * @param logger
 143  
      *            the logger to set
 144  
      */
 145  
     public void setLogger(Logger logger)
 146  
     {
 147  0
         if (logger == null)
 148  
         {
 149  0
             throw new IllegalArgumentException("logger must not be null");
 150  
         }
 151  0
         _logger = logger;
 152  0
     }
 153  
 
 154  
     /**
 155  
      * @return the className
 156  
      */
 157  
     public String getSourceClass()
 158  
     {
 159  0
         return _sourceClass;
 160  
     }
 161  
 
 162  
     /**
 163  
      * @param className
 164  
      *            the className to set
 165  
      */
 166  
     public void setSourceClass(String className)
 167  
     {
 168  0
         if (className == null)
 169  
         {
 170  0
             throw new IllegalArgumentException("className must not be null");
 171  
         }
 172  0
         _sourceClass = className;
 173  0
     }
 174  
 }