Coverage report

  %line %branch
net.sf.infrared.agent.MonitorFactory
74% 
88% 

 1  
 /* 
 2  
  * Copyright 2005 Tavant Technologies and Contributors
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License")
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  * 
 16  
  *
 17  
  *
 18  
  * Original Author:  roopali.agrawal (Tavant Technologies)
 19  
  * Contributor(s):   binil.thomas, kamal.govindraj
 20  
  *
 21  
  */
 22  
 package net.sf.infrared.agent;
 23  
 
 24  
 import java.util.Iterator;
 25  
 import java.util.Map;
 26  
 import java.util.WeakHashMap;
 27  
 
 28  
 import net.sf.infrared.agent.util.AgentHelper;
 29  
 import net.sf.infrared.base.util.LoggingFactory;
 30  
 
 31  
 import org.apache.log4j.Logger;
 32  
 
 33  
 /**
 34  
  * Acts as the factory for creating MonitorFacade objects. <p/> The aspects that
 35  
  * need to advice a method call/execution for collecting monitoring data, gets
 36  
  * the MonitorFacade instances from this factory.
 37  
  * 
 38  
  * @author roopali.agrawal
 39  
  * @author binil.thomas
 40  
  * @author kamal.govindraj
 41  
  */
 42  0
 public class MonitorFactory {
 43  
     public static final String DEFAULT_CONFIG_LOCATION = "infrared-agent-root.properties";
 44  
     
 45  
     private static Logger log;
 46  
 
 47  2
     private static Map classLoaderToMonitorFacadeMap = new WeakHashMap();
 48  
 
 49  2
     private static MonitorFacade defaultFacade = null;
 50  
 
 51  
     static {
 52  2
         init();
 53  2
     }
 54  
 
 55  
     public static MonitorFacade getFacade() {
 56  10
         ClassLoader loaderForThisApp = Thread.currentThread().getContextClassLoader();
 57  10
         MonitorFacade facade = getFacadeImplForCurrentApplication(loaderForThisApp);
 58  10
         if (facade == null) {
 59  6
             facade = defaultFacade;
 60  
         }
 61  10
         return facade;
 62  
     }
 63  
 
 64  
     public static void registerFacadeImpl(MonitorFacade facade) {
 65  3
         registerFacadeImpl(facade, Thread.currentThread().getContextClassLoader());
 66  3
     }
 67  
 
 68  
     public static void registerFacadeImpl(MonitorFacade facade, ClassLoader loader) {
 69  3
         classLoaderToMonitorFacadeMap.put(loader, facade);
 70  
 
 71  3
         if (log.isDebugEnabled()) {
 72  0
             log.debug("Registered " + facade);
 73  
         }
 74  3
     }
 75  
 
 76  
     public static void unregisterFacadeImpl(MonitorFacade facade) {
 77  0
         for (Iterator i = classLoaderToMonitorFacadeMap.entrySet().iterator(); i.hasNext();) {
 78  0
             Map.Entry entry = (Map.Entry) i.next();
 79  0
             if (entry.getValue() == facade) {
 80  0
                 classLoaderToMonitorFacadeMap.remove(entry.getKey());
 81  
             }
 82  
         }
 83  0
         if (log.isDebugEnabled()) {
 84  0
             log.debug("Unregistered " + facade);
 85  
         }
 86  0
     }
 87  
     
 88  
     public static MonitorFacade unregisterFacadeImpl() {
 89  3
         MonitorFacade removedFacade = null;
 90  3
         removedFacade = (MonitorFacade) classLoaderToMonitorFacadeMap.remove(
 91  
                 Thread.currentThread().getContextClassLoader());
 92  3
         if (log.isDebugEnabled()) {
 93  0
             log.debug("UnRegistered " + removedFacade);
 94  
         }
 95  3
         return removedFacade;
 96  
     }
 97  
 
 98  
     public static void reset() {
 99  6
         classLoaderToMonitorFacadeMap.clear();
 100  6
     }
 101  
     
 102  
     public static MonitorFacade getDefaultFacade() {
 103  7
         return defaultFacade;
 104  
     }
 105  
 
 106  
     private static MonitorFacade getFacadeImplForCurrentApplication(ClassLoader loader) {
 107  18
     	    MonitorFacade facade = (MonitorFacade) classLoaderToMonitorFacadeMap.get(loader);
 108  18
     	    if (facade == null) {
 109  14
     	        ClassLoader parent = loader.getParent();
 110  14
     	        if (parent != null) {
 111  8
     	            facade = getFacadeImplForCurrentApplication(parent);
 112  
     	        }
 113  
     	    }
 114  18
     	    return facade;
 115  
     }
 116  
     
 117  
     private static void init() {
 118  
         try {
 119  4
             log = LoggingFactory.getLogger(MonitorFactory.class);
 120  0
         } catch (Throwable th) {
 121  0
             System.out.println("Error calling LoggingFactory.getLogger(MonitorFactory.class)");
 122  0
             th.printStackTrace();
 123  2
         }
 124  
 
 125  2
         setupDefaultMonitorFacade();
 126  2
         System.out.println(getStartupMessage());
 127  2
     }
 128  
 
 129  
     private static String getStartupMessage() {
 130  2
         String version = AgentHelper.getVersion();
 131  
         
 132  2
         String msg = "\n" +
 133  
                      "***************************************************************\n" +
 134  
                      "* InfraRED version " + version + "\n" +
 135  
                      "*\n";
 136  2
         if (LoggingFactory.isLoggingConfigured()) {
 137  2
             if (LoggingFactory.isDefultLoggingUsed()) {
 138  0
                 msg += "* Configured default logging system from " + 
 139  
                     LoggingFactory.getLoggingConfiguration() + ". No debug messages would be logged.\n"; 
 140  
             } else {
 141  2
                 if (LoggingFactory.isDebugLoggingEnabled()) {
 142  0
                     msg += "* Configured logging system from " + 
 143  
                         LoggingFactory.getLoggingConfiguration() + ".\n"; 
 144  
                 } else {
 145  2
                     msg += "* Configured logging system from " + 
 146  
                         LoggingFactory.getLoggingConfiguration() + 
 147  
                         ". No debug messages would be logged as -D" + 
 148  
                         LoggingFactory.DEBUG_KEY + "=true JVM PARAM is not set.\n"; 
 149  
                 }
 150  
             }
 151  
         } else {
 152  0
             msg +=   "* Failed to configure logging system\n";  
 153  
         }
 154  2
         msg +=       "***************************************************************\n";
 155  2
         return msg;
 156  
     }
 157  
 
 158  
     private static void setupDefaultMonitorFacade() {
 159  2
         String applicationName = "default-facade";
 160  2
         String hostName = AgentHelper.getLocalHostName();
 161  2
         MonitorConfig defaultConfig = new MonitorConfigImpl(DEFAULT_CONFIG_LOCATION);
 162  
  
 163  2
         MonitorFacade facade = new MonitorFacadeImpl(applicationName, hostName, defaultConfig, false);
 164  2
         defaultFacade = new MultipleEntryGuard(facade);
 165  2
     }
 166  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.