Coverage Report - org.apache.myfaces.shared.trace.TracingSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
TracingSupport
0%
0/49
0%
0/18
2.231
 
 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.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  
 public class TracingSupport
 28  
 {
 29  0
     private static final Object[] EMPTY_PARAMS = new Object[0];
 30  
 
 31  
     private Logger _logger;
 32  0
     private Level _level = Level.FINE;
 33  
     private String _sourceClass;
 34  
 
 35  
     public TracingSupport()
 36  
     {
 37  0
         this(Logger.getAnonymousLogger());
 38  0
     }
 39  
 
 40  
     public TracingSupport(Class clazz)
 41  
     {
 42  0
         this(clazz.getName());
 43  0
     }
 44  
 
 45  
     public TracingSupport(String className)
 46  
     {
 47  0
         this(Logger.getLogger(className));
 48  0
     }
 49  
 
 50  
     public TracingSupport(Logger logger)
 51  0
     {
 52  0
         _logger = logger;
 53  0
         _sourceClass = logger.getName();
 54  0
     }
 55  
 
 56  
     /**
 57  
      * @return the log level
 58  
      */
 59  
     public Level getLevel()
 60  
     {
 61  0
         return _level;
 62  
     }
 63  
 
 64  
     /**
 65  
      * 
 66  
      * @param level
 67  
      *            the log level to use
 68  
      */
 69  
     public void setLevel(Level level)
 70  
     {
 71  0
         if (level == null)
 72  
         {
 73  0
             throw new IllegalArgumentException("log level can not be null");
 74  
         }
 75  0
         _level = level;
 76  0
     }
 77  
 
 78  
     public <T> T trace(String methodName, Closure<T> closure)
 79  
     {
 80  0
         return trace(methodName, closure, EMPTY_PARAMS);
 81  
     }
 82  
 
 83  
     public <T> T trace(String methodName, Closure<T> closure, Object... params)
 84  
     {
 85  0
         if (_logger.isLoggable(_level))
 86  
         {
 87  0
             _logger.logp(_level, _sourceClass, methodName, "ENTRY" + prepareParams(params), params);
 88  
             try
 89  
             {
 90  0
                 T result = closure.call();
 91  0
                 if (!Void.class.equals(result))
 92  
                 {
 93  0
                     _logger.logp(_level, _sourceClass, methodName, "RETURN {0}", result);
 94  
                 }
 95  
                 else
 96  
                 {
 97  0
                     _logger.logp(_level, _sourceClass, methodName, "RETURN");
 98  
                 }
 99  0
                 return result;
 100  
             }
 101  0
             catch (RuntimeException e)
 102  
             {
 103  0
                 _logger.logp(_level, _sourceClass, methodName, "THROW", e);
 104  0
                 throw e;
 105  
             }
 106  
         }
 107  
         else
 108  
         {
 109  0
             return closure.call();
 110  
         }
 111  
     }
 112  
 
 113  
     private String prepareParams(Object[] params)
 114  
     {
 115  0
         if (params == null || params.length == 0)
 116  
         {
 117  0
             return "";
 118  
         }
 119  0
         StringBuilder builder = new StringBuilder(" ");
 120  0
         for (int i = 0, size = params.length; i < size; i++)
 121  
         {
 122  0
             builder.append("{");
 123  0
             builder.append(i);
 124  0
             builder.append("}");
 125  0
             if (i + 1 < size)
 126  
             {
 127  0
                 builder.append(",");
 128  
             }
 129  
         }
 130  0
         return builder.toString();
 131  
     }
 132  
 
 133  
     public Logger getLogger()
 134  
     {
 135  0
         return _logger;
 136  
     }
 137  
 
 138  
     /**
 139  
      * @param logger
 140  
      *            the logger to set
 141  
      */
 142  
     public void setLogger(Logger logger)
 143  
     {
 144  0
         if (logger == null)
 145  
         {
 146  0
             throw new IllegalArgumentException("logger must not be null");
 147  
         }
 148  0
         _logger = logger;
 149  0
     }
 150  
 
 151  
     /**
 152  
      * @return the className
 153  
      */
 154  
     public String getSourceClass()
 155  
     {
 156  0
         return _sourceClass;
 157  
     }
 158  
 
 159  
     /**
 160  
      * @param className
 161  
      *            the className to set
 162  
      */
 163  
     public void setSourceClass(String className)
 164  
     {
 165  0
         if (className == null)
 166  
         {
 167  0
             throw new IllegalArgumentException("className must not be null");
 168  
         }
 169  0
         _sourceClass = className;
 170  0
     }
 171  
 }