Coverage report

  %line %branch
net.sf.infrared.web.util.TreeUtil
54% 
80% 

 1  
 /*
 2  
  *
 3  
  * Copyright 2005 Tavant Technologies and Contributors
 4  
  *
 5  
  * Licensed under the Apache License, Version 2.0 (the "License")
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  *
 17  
  *
 18  
  *
 19  
  * Original Author:  subin.p (Tavant Technologies)
 20  
  * Contributor(s):   -;
 21  
  *
 22  
  */
 23  
 package net.sf.infrared.web.util;
 24  
 
 25  
 import java.lang.reflect.InvocationTargetException;
 26  
 import java.util.ArrayList;
 27  
 import java.util.HashMap;
 28  
 import java.util.Iterator;
 29  
 import java.util.List;
 30  
 import java.util.Map;
 31  
 import java.util.Set;
 32  
 
 33  
 import org.apache.log4j.Logger;
 34  
 
 35  
 import net.sf.infrared.aspects.api.ApiContext;
 36  
 import net.sf.infrared.base.model.AggregateExecutionTime;
 37  
 import net.sf.infrared.base.model.AggregateOperationTree;
 38  
 import net.sf.infrared.base.model.ExecutionContext;
 39  
 import net.sf.infrared.base.util.LoggingFactory;
 40  
 import net.sf.infrared.base.util.TreeNode;
 41  
 
 42  0
 public class TreeUtil {
 43  
 
 44  1
     private static Logger log = LoggingFactory.getLogger("net.sf.infrared.web.util.TreeUtil");
 45  
 
 46  
     private static ExecutionContext context;
 47  
 
 48  
 
 49  
     /**
 50  
      * This method creates an ExecutionContext based on the ctxName, the actualLayer
 51  
      * and the ctxClassName using reflection. Further, this method merges all the
 52  
      * AggregateExecutionTimes in the given AggregateOperationTree if the ExecutionContexts
 53  
      * match. If the hierarchicalLayer parameter is a not null value, only those
 54  
      * AggregateExecutionTimes in the tree whose hierarchical layer mathces the
 55  
      * given hierarchicalLayer parameter shall be merged.
 56  
      *
 57  
      * @param rootOperTree
 58  
      * @param ctxName
 59  
      * @param layer
 60  
      * @return
 61  
      */
 62  
     public static TreeNode getMergedExecutionContextTreeNode(AggregateOperationTree rootOperTree,
 63  
             String ctxName, String actualLayer, String ctxClassName, String hierarchicalLayer) {
 64  
 
 65  3
         if(ctxName == null)
 66  0
             throw new IllegalArgumentException("The ctxName argument for" +
 67  
                     "the method getMergedApiContextTreeNode is null");
 68  
 
 69  3
         context = getExecutionContext(ctxName, actualLayer, ctxClassName);
 70  
 
 71  
         // Cloning the rootOperTree before merging the same.
 72  3
         AggregateOperationTree cloneTree = new AggregateOperationTree();
 73  3
         TreeNode cloneNode = (TreeNode) rootOperTree.getAggregateTree().getRoot().clone();
 74  3
         cloneTree.getAggregateTree().setRoot(cloneNode);
 75  
 
 76  3
         return getMergedExecutionContextTreeNode(cloneTree, context, hierarchicalLayer);
 77  
     }
 78  
 
 79  
 
 80  
 
 81  
 	static ExecutionContext getExecutionContext(String apiString, String layer, String ctx) {
 82  3
         ExecutionContext newExecutionContext = null;
 83  
 		try {
 84  3
             newExecutionContext = (ExecutionContext)Class.forName(ctx)
 85  
 							 .getConstructor(new Class[]{apiString.getClass(), layer.getClass()})
 86  
 							 .newInstance(new Object[]{apiString, layer});
 87  0
 		} catch (IllegalArgumentException e) {
 88  0
 			log.error("The arguments passed to the constructor are incorrect" , e);
 89  0
             return null;
 90  0
 		} catch (SecurityException e) {
 91  0
 			log.error("Security Exception " , e);
 92  0
             return null;
 93  0
 		} catch (InstantiationException e) {
 94  0
 			log.error("Instantiation Exception ", e);
 95  0
             return null;
 96  0
 		} catch (IllegalAccessException e) {
 97  0
 			log.error("Illegal Argument Exception ", e);
 98  0
             return null;
 99  0
 		} catch (InvocationTargetException e) {
 100  0
 			log.error("Invocation Target Exception ", e);
 101  0
             return null;
 102  0
 		} catch (NoSuchMethodException e) {
 103  0
 			log.error(e);
 104  0
             return null;
 105  0
 		} catch (ClassNotFoundException e) {
 106  0
 			log.error(e);
 107  0
             return null;
 108  3
 		}
 109  3
         return newExecutionContext;
 110  
 	}
 111  
 
 112  
 
 113  
     /**
 114  
      * This is the general utility method to merge all the statistics corresponding to an
 115  
      * ExecutionContext in a given AggregateOperationTree.
 116  
      * @param rootOperTree
 117  
      * @param context
 118  
      * @return TreeNode
 119  
      */
 120  
     static TreeNode getMergedExecutionContextTreeNode(AggregateOperationTree rootOperTree,
 121  
             ExecutionContext context, String hierarchicalLayer) {
 122  3
         if (rootOperTree == null || rootOperTree.getAggregateTree().getRoot() == class="keyword">null
 123  
                 || context == null) {
 124  
 
 125  0
             log.error("The rootOperTree or the ExecutionContext cannot be null" +
 126  
                     " for the getMergedExecutionContextTreeNode method");
 127  0
             return null;
 128  
         }
 129  3
         TreeNode rootNode = rootOperTree.getAggregateTree().getRoot();
 130  3
         List subTrees = extractMatchingOperationTrees(rootNode,
 131  
         		context, hierarchicalLayer, new ArrayList());
 132  3
         AggregateOperationTree mergedTree = new AggregateOperationTree();
 133  
 
 134  
         // Merging the subtrees.
 135  3
         for(Iterator itr = subTrees.iterator();itr.hasNext();) {//comment this
 136  6
         	AggregateOperationTree opTree = (AggregateOperationTree) itr.next();
 137  6
         	mergedTree.merge(opTree);
 138  
         }
 139  
 
 140  
         // Need to return the merged node matching the context parameter & not the dummy node.
 141  3
         List children = mergedTree.getAggregateTree().getRoot().getChildren();
 142  
 
 143  
         //@TODO do we need to check for call trace disabling early on ?
 144  
 
 145  3
         if(children == null || children.size() < 1) // probably call tracing is disabled.
 146  0
         	return null;
 147  
 
 148  3
         TreeNode resultantNode = (TreeNode) children.get(0);// The dummy root should have only one child.
 149  3
         resultantNode.setDepth(0);
 150  3
         return resultantNode;
 151  
     }
 152  
 
 153  
 
 154  
     static List extractMatchingOperationTrees(TreeNode rootTreeNode,
 155  
     		ExecutionContext matchingContext, String hierarchicalLayer, List subTrees) {
 156  34
     	Object rootValue = rootTreeNode.getValue();
 157  34
     	if(rootValue instanceof AggregateExecutionTime && matchingContext.equals(
 158  
 		  ((AggregateExecutionTime)rootValue).getContext()  ) ) {
 159  9
 			AggregateExecutionTime rootAggExecTime = (AggregateExecutionTime)rootValue;
 160  
 
 161  9
 			if(hierarchicalLayer == null || hierarchicalLayer.equals( rootAggExecTime.getLayerName()) ){
 162  6
 	            AggregateOperationTree matchingTree = new AggregateOperationTree();
 163  
 
 164  
 	            // The subtree should have the dummy node as the root so that they can be merged using the
 165  
 	            // merge api provided in the AggregateExecutionTime class.
 166  6
 	            rootTreeNode.setParent(null);
 167  6
 	            matchingTree.getAggregateTree().getRoot().addChild(rootTreeNode); //comment this
 168  6
 	            subTrees.add(matchingTree);
 169  6
 	            return subTrees;
 170  
 			}
 171  
     	}
 172  28
 		List childList = rootTreeNode.getChildren();
 173  28
         for(Iterator itr = childList.iterator();itr.hasNext();)
 174  
         {
 175  31
             TreeNode currNode = (TreeNode) itr.next();
 176  31
             extractMatchingOperationTrees(currNode,matchingContext,hierarchicalLayer,subTrees);
 177  
         }
 178  28
         return subTrees;
 179  
     }
 180  
 
 181  
 
 182  
     static Map getContextList(List children) {
 183  0
         Map returnMap = new HashMap();
 184  
 
 185  0
         for (Iterator iter = children.iterator(); iter.hasNext();) {
 186  0
             TreeNode element = (TreeNode) iter.next();
 187  0
             AggregateExecutionTime execTime = (AggregateExecutionTime)element.getValue();
 188  0
             returnMap.put(execTime.getContext(), execTime);
 189  
         }
 190  0
         return class="keyword">returnMap;
 191  
     }
 192  
 }

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