Coverage Report - org.codehaus.plexus.logging.BaseLoggerManager
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseLoggerManager
0 %
0/53
0 %
0/22
2,583
 
 1  
 package org.codehaus.plexus.logging;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2006 Codehaus Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * 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, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
 20  
 
 21  
 import java.util.HashMap;
 22  
 import java.util.Iterator;
 23  
 import java.util.Locale;
 24  
 import java.util.Map;
 25  
 
 26  
 /**
 27  
  * Base class for all LoggerManagers which use cache of Loggers.
 28  
  *
 29  
  * @author <a href="mailto:michal@codehaus.org">Michal Maczka</a>
 30  
  * @version $Id$
 31  
  */
 32  0
 public abstract class BaseLoggerManager
 33  
         extends AbstractLoggerManager implements Initializable
 34  
 {
 35  
     /** */
 36  0
     private Map loggerCache = new HashMap();
 37  
 
 38  0
     private String threshold = "info";
 39  
 
 40  
     private int currentThreshold;
 41  
 
 42  
     public void initialize()
 43  
     {
 44  0
         currentThreshold = parseThreshold( threshold );
 45  
 
 46  0
         if ( currentThreshold == -1 )
 47  
         {
 48  0
             currentThreshold = Logger.LEVEL_DEBUG;
 49  
         }
 50  0
     }
 51  
 
 52  
     protected int parseThreshold( String text )
 53  
     {
 54  0
         text = text.trim().toLowerCase( Locale.ENGLISH );
 55  
 
 56  0
         if ( text.equals( "debug" ) )
 57  
         {
 58  0
             return Logger.LEVEL_DEBUG;
 59  
         }
 60  0
         else if ( text.equals( "info" ) )
 61  
         {
 62  0
             return Logger.LEVEL_INFO;
 63  
         }
 64  0
         else if ( text.equals( "warn" ) )
 65  
         {
 66  0
             return Logger.LEVEL_WARN;
 67  
         }
 68  0
         else if ( text.equals( "error" ) )
 69  
         {
 70  0
             return Logger.LEVEL_ERROR;
 71  
         }
 72  0
         else if ( text.equals( "fatal" ) )
 73  
         {
 74  0
             return Logger.LEVEL_FATAL;
 75  
         }
 76  
 
 77  0
         return -1;
 78  
     }
 79  
 
 80  
     /**
 81  
      * Sets the threshold for all new loggers. It will NOT affect the existing loggers.
 82  
      * <p/>
 83  
      * This is usually only set once while the logger manager is configured.
 84  
      *
 85  
      * @param currentThreshold The new threshold.
 86  
      */
 87  
     public void setThreshold( int currentThreshold )
 88  
     {
 89  0
         this.currentThreshold = currentThreshold;
 90  0
     }
 91  
 
 92  
     /**
 93  
      * Sets the threshold for all new loggers. It will NOT affect the existing loggers.
 94  
      * <p/>
 95  
      * This is usually only set once while the logger manager is configured.
 96  
      *
 97  
      * @param currentThreshold The new threshold.
 98  
      */
 99  
     public void setThresholds( int currentThreshold )
 100  
     {
 101  0
         this.currentThreshold = currentThreshold;
 102  
 
 103  0
         for (Object o : loggerCache.values()) {
 104  0
             Logger logger = (Logger) o;
 105  0
             logger.setThreshold(currentThreshold);
 106  0
         }
 107  0
     }
 108  
 
 109  
     /**
 110  
      * Returns the current threshold for all new loggers.
 111  
      *
 112  
      * @return Returns the current threshold for all new loggers.
 113  
      */
 114  
     public int getThreshold()
 115  
     {
 116  0
         return currentThreshold;
 117  
     }
 118  
 
 119  
     public void setThreshold( String role, String roleHint, int threshold )
 120  
     {
 121  
         AbstractLogger logger;
 122  
 
 123  0
         String key = toMapKey( role, roleHint );
 124  
 
 125  0
         logger = ( AbstractLogger ) loggerCache.get( key );
 126  
 
 127  0
         if ( logger == null )
 128  
         {
 129  0
             return; // nothing to do
 130  
         }
 131  
 
 132  0
         logger.setThreshold( threshold );
 133  0
     }
 134  
 
 135  
     public int getThreshold( String role, String roleHint )
 136  
     {
 137  
         AbstractLogger logger;
 138  
 
 139  0
         String key = toMapKey( role, roleHint );
 140  
 
 141  0
         logger = ( AbstractLogger ) loggerCache.get( key );
 142  
 
 143  0
         if ( logger == null )
 144  
         {
 145  0
             return Logger.LEVEL_DEBUG; // does not return null because that could create a NPE
 146  
         }
 147  
 
 148  0
         return logger.getThreshold();
 149  
     }
 150  
 
 151  
     public Logger getLoggerForComponent( String role, String roleHint )
 152  
     {
 153  
         Logger logger;
 154  
 
 155  0
         String key = toMapKey( role, roleHint );
 156  
 
 157  0
         logger = ( Logger ) loggerCache.get( key );
 158  
 
 159  0
         if ( logger != null )
 160  
         {
 161  0
             return logger;
 162  
         }
 163  
 
 164  0
         logger = createLogger( key );
 165  
 
 166  0
         loggerCache.put( key, logger );
 167  
 
 168  0
         return logger;
 169  
     }
 170  
 
 171  
     protected abstract Logger createLogger( String key );
 172  
 
 173  
     public void returnComponentLogger( String role, String roleHint )
 174  
     {
 175  
         Object obj;
 176  
 
 177  0
         String key = toMapKey( role, roleHint );
 178  
 
 179  0
         obj = loggerCache.remove( key );
 180  
 
 181  0
         if ( obj == null )
 182  
         {
 183  
             // TODO: use a logger!
 184  0
             System.err.println( "There was no such logger '" + key + "' " + hashCode() + "." );
 185  
         }
 186  0
     }
 187  
 
 188  
     public int getActiveLoggerCount()
 189  
     {
 190  0
         return loggerCache.size();
 191  
     }
 192  
 
 193  
     public String getThresholdAsString()
 194  
     {
 195  0
         return threshold;
 196  
     }
 197  
 
 198  
 }