1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
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 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
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 |
|
|
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 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
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 |
|
|
135 |
3 |
for(Iterator itr = subTrees.iterator();itr.hasNext();) { |
136 |
6 |
AggregateOperationTree opTree = (AggregateOperationTree) itr.next(); |
137 |
6 |
mergedTree.merge(opTree); |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
3 |
List children = mergedTree.getAggregateTree().getRoot().getChildren(); |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
3 |
if(children == null || children.size() < 1) |
146 |
0 |
return null; |
147 |
|
|
148 |
3 |
TreeNode resultantNode = (TreeNode) children.get(0); |
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 |
|
|
165 |
|
|
166 |
6 |
rootTreeNode.setParent(null); |
167 |
6 |
matchingTree.getAggregateTree().getRoot().addChild(rootTreeNode); |
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 |
|
} |