Coverage report

  %line %branch
net.sf.infrared.collector.StatisticsRepository
0% 
0% 

 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:  binil.thomas (Tavant Technologies)
 19  
  * Contributor(s):   subin.p;
 20  
  *
 21  
  */
 22  
 package net.sf.infrared.collector;
 23  
 
 24  
 import java.util.ArrayList;
 25  
 import java.util.Collection;
 26  
 import java.util.Date;
 27  
 import java.util.HashMap;
 28  
 import java.util.HashSet;
 29  
 import java.util.Iterator;
 30  
 import java.util.List;
 31  
 import java.util.Map;
 32  
 import java.util.Set;
 33  
 import java.util.StringTokenizer;
 34  
 
 35  
 import org.apache.log4j.Logger;
 36  
 
 37  
 import net.sf.infrared.base.model.ApplicationStatistics;
 38  
 import net.sf.infrared.base.model.StatisticsSnapshot;
 39  
 import net.sf.infrared.base.util.LoggingFactory;
 40  
 import net.sf.infrared.collector.impl.persistence.SpringContext;
 41  
 
 42  0
 public class StatisticsRepository {
 43  
     // map of names -> ApplicationStatistics, collected since the collector started
 44  
     // name = applicationName#instanceId
 45  0
     private Map mapOfStatsSinceStartup = new HashMap();
 46  
 
 47  
     // map of names -> ApplicationStatistics, collected since the last persist
 48  
     // name = applicationName#instanceId
 49  0
     private Map mapOfStatsSinceLastPersist = new HashMap();
 50  
 
 51  
     private static final String SEPERATOR = "#";
 52  
     
 53  0
     private SpringContext springContext = new SpringContext();
 54  
     
 55  0
     private static final Logger log = LoggingFactory.getLogger(StatisticsRepository.class);
 56  
 
 57  
     public void addStatistics(ApplicationStatistics stats) {
 58  0
         addStatsToStatsMap(stats, mapOfStatsSinceStartup);
 59  0
         addStatsToStatsMap(stats, mapOfStatsSinceLastPersist);
 60  0
     }
 61  
 
 62  
     public List getStatisticsToPersist() {
 63  0
         Map old = mapOfStatsSinceLastPersist;
 64  0
         Map mapOfStatsSinceLastPersist = new HashMap();
 65  
         
 66  0
         List statsList = new ArrayList();
 67  0
         for (Iterator i = old.values().iterator(); i.hasNext();) {
 68  0
             ApplicationStatistics stats = (ApplicationStatistics) i.next();
 69  0
             statsList.add(stats);
 70  
         }
 71  0
         return statsList;
 72  
     }
 73  
 
 74  
     public StatisticsSnapshot fetchStatsSinceStartup(Collection appNames, 
 75  
             Collection instanceIds) {
 76  0
         String appName = null;
 77  0
         String instanceName = null;
 78  0
         StringTokenizer tokenizer = null;
 79  
 
 80  0
         StatisticsSnapshot stats = new StatisticsSnapshot();
 81  0
         Set keySet = mapOfStatsSinceStartup.keySet();
 82  0
         for (Iterator iter = keySet.iterator(); iter.hasNext();) {
 83  0
             String element = (String) iter.next();
 84  0
             tokenizer = new StringTokenizer(element, SEPERATOR);
 85  0
             appName = tokenizer.nextToken();
 86  0
             instanceName = tokenizer.nextToken();
 87  
 
 88  0
             if (appNames.contains(appName) && instanceIds.contains(instanceName)) {
 89  0
                 stats.merge((ApplicationStatistics) mapOfStatsSinceStartup.get(element));
 90  
             }
 91  
         }
 92  0
         return stats;
 93  
     }
 94  
 
 95  
     public StatisticsSnapshot fetchStatsFromDB(Collection appNames, Collection instanceIds,
 96  
             Date from, Date to) {
 97  
 
 98  0
         return springContext.getDao().fetchStatistics(appNames, instanceIds, from, to);
 99  
     }
 100  
     
 101  
     public Set getApplicationNames(){
 102  0
         Set keys = mapOfStatsSinceStartup.keySet();
 103  0
         Set applicationNames = new HashSet();
 104  
         
 105  0
         for (Iterator iter = keys.iterator(); iter.hasNext();) {
 106  0
             String key = (String) iter.next();
 107  0
             String [] tokens = key.split(SEPERATOR);
 108  0
             applicationNames.add(tokens[0]);                       
 109  
         }        
 110  0
         return applicationNames;
 111  
     }
 112  
     
 113  
 
 114  
     public Set getInstanceNames(Set applicationNames){
 115  0
         Set keys = mapOfStatsSinceStartup.keySet();
 116  0
         Set instanceNames = new HashSet();
 117  
         
 118  0
         for (Iterator iter = keys.iterator(); iter.hasNext();) {
 119  0
             String key = (String) iter.next();
 120  0
             String [] tokens = key.split(SEPERATOR);
 121  0
             if(applicationNames.contains(tokens[0])){
 122  0
                 instanceNames.add(tokens[1]);
 123  
             }                                  
 124  
         }        
 125  0
         return instanceNames;        
 126  
     }
 127  
 
 128  
     // @TODO Need to decide if we need to persist the data that needs to be cleared during 
 129  
     // this call in this method or need to continue with the timer persistence. 
 130  
     public void clearStatsSinceStartup() {
 131  0
         if(mapOfStatsSinceStartup != null) {
 132  0
             mapOfStatsSinceStartup.clear();
 133  
         }
 134  0
     }
 135  
 
 136  
 
 137  
     public void clearStatsSinceLastPersist() {
 138  0
         if(mapOfStatsSinceLastPersist != null) {
 139  0
             mapOfStatsSinceLastPersist.clear();            
 140  
         }
 141  0
         log.debug("Cleared the statistics that were persisted.");
 142  0
     }
 143  
     
 144  
     
 145  
     private void addStatsToStatsMap(ApplicationStatistics stats, Map map) {
 146  0
         String applicationName = stats.getApplicationName();
 147  0
         String instanceId = stats.getInstanceId();
 148  
 
 149  0
         String statsKey = constructStatsKey(applicationName, instanceId);
 150  
 
 151  0
         synchronized (map) {
 152  0
             ApplicationStatistics appStats = (ApplicationStatistics) map.get(statsKey);
 153  
 
 154  0
             if (appStats == null) {
 155  0
                 map.put(statsKey, stats);
 156  
             } else {
 157  0
                 appStats.merge(stats);
 158  
             }
 159  0
         }
 160  0
     }
 161  
 
 162  
     private String constructStatsKey(String applicationName, String instanceName) {
 163  0
         if (applicationName == null)
 164  0
             throw new IllegalArgumentException("The application name cannot be null");
 165  0
         else if (instanceName == null)
 166  0
             throw new IllegalArgumentException("the instance name cannot be null");
 167  
 
 168  0
         return applicationName + SEPERATOR + instanceName;
 169  
     }
 170  
 }

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