1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.velocity.internal.jmx; |
21 |
|
|
22 |
|
import java.lang.reflect.Field; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.Map; |
25 |
|
|
26 |
|
import javax.management.openmbean.ArrayType; |
27 |
|
import javax.management.openmbean.CompositeData; |
28 |
|
import javax.management.openmbean.CompositeDataSupport; |
29 |
|
import javax.management.openmbean.CompositeType; |
30 |
|
import javax.management.openmbean.OpenType; |
31 |
|
import javax.management.openmbean.SimpleType; |
32 |
|
import javax.management.openmbean.TabularData; |
33 |
|
import javax.management.openmbean.TabularDataSupport; |
34 |
|
import javax.management.openmbean.TabularType; |
35 |
|
|
36 |
|
import org.xwiki.velocity.VelocityEngine; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@version |
44 |
|
@since |
45 |
|
|
|
|
| 97.8% |
Uncovered Elements: 1 (45) |
Complexity: 6 |
Complexity Density: 0.15 |
|
46 |
|
public class JMXVelocityEngine implements JMXVelocityEngineMBean |
47 |
|
{ |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
private VelocityEngine engine; |
52 |
|
|
53 |
|
|
54 |
|
@param |
55 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
56 |
52 |
public JMXVelocityEngine(VelocityEngine engine)... |
57 |
|
{ |
58 |
52 |
this.engine = engine; |
59 |
|
} |
60 |
|
|
|
|
| 93.8% |
Uncovered Elements: 1 (16) |
Complexity: 2 |
Complexity Density: 0.12 |
|
61 |
4 |
@Override... |
62 |
|
public TabularData getTemplates() |
63 |
|
{ |
64 |
4 |
TabularData data; |
65 |
|
|
66 |
4 |
try { |
67 |
4 |
Map<String, String[]> result = getInternalTemplates(); |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
4 |
ArrayType macroNameType = new ArrayType(1, SimpleType.STRING); |
74 |
|
|
75 |
|
|
76 |
4 |
String[] columnNames = new String[] { "templateName", "macroNames" }; |
77 |
4 |
String[] descriptions = new String[] { "The Template Name (namespace)", "The names of registered Macros" }; |
78 |
4 |
CompositeType rowType = new CompositeType("template", |
79 |
|
"Template management data (namespaces, macros) for a row", columnNames, descriptions, |
80 |
|
new OpenType[] { SimpleType.STRING, macroNameType }); |
81 |
|
|
82 |
4 |
TabularType type = new TabularType("templates", "Template management data (namespaces, macros)", rowType, |
83 |
|
columnNames); |
84 |
4 |
data = new TabularDataSupport(type); |
85 |
|
|
86 |
4 |
for (Map.Entry<String, String[]> entry : result.entrySet()) { |
87 |
|
|
88 |
5 |
String templateName = entry.getKey(); |
89 |
5 |
String[] macroNames = entry.getValue(); |
90 |
|
|
91 |
5 |
CompositeData rowData = new CompositeDataSupport(rowType, columnNames, new Object[] { |
92 |
|
templateName, macroNames }); |
93 |
5 |
data.put(rowData); |
94 |
|
} |
95 |
|
|
96 |
|
} catch (Exception e) { |
97 |
0 |
throw new RuntimeException("Failed to gather information on Velocity Templates/Macros", e); |
98 |
|
} |
99 |
|
|
100 |
4 |
return data; |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
@return@link |
105 |
|
|
106 |
|
@throws |
107 |
|
@throws |
108 |
|
@see |
109 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 2 |
Complexity Density: 0.11 |
|
110 |
4 |
private Map<String, String[]> getInternalTemplates() throws NoSuchFieldException, IllegalAccessException... |
111 |
|
{ |
112 |
|
|
113 |
4 |
Object velocityEngine = getField(this.engine, "engine"); |
114 |
|
|
115 |
4 |
Object runtimeInstance = getField(velocityEngine, "ri"); |
116 |
4 |
Object vmFactory = getField(runtimeInstance, "vmFactory"); |
117 |
4 |
Object vmManager = getField(vmFactory, "vmManager"); |
118 |
|
|
119 |
4 |
Map<String, Map<String, ?>> namespaceHash = |
120 |
|
(Map<String, Map<String, ?>>) getField(vmManager, "namespaceHash"); |
121 |
|
|
122 |
4 |
Map<String, ?> globalNamespace = (Map<String, ?>) getField(vmManager, "globalNamespace"); |
123 |
|
|
124 |
4 |
Map<String, String[]> result = new HashMap<String, String[]>(); |
125 |
|
|
126 |
4 |
for (Map.Entry<String, Map<String, ?>> entry : namespaceHash.entrySet()) { |
127 |
5 |
String nameSpaceName = entry.getKey(); |
128 |
5 |
Map<String, ?> namespace = entry.getValue(); |
129 |
5 |
if (globalNamespace.equals(namespace)) { |
130 |
4 |
nameSpaceName = "<global>"; |
131 |
|
} |
132 |
5 |
String[] macroNames = new String[namespace.size()]; |
133 |
5 |
int i = 0; |
134 |
5 |
for (String macroName : namespace.keySet()) { |
135 |
1 |
macroNames[i] = macroName; |
136 |
1 |
i++; |
137 |
|
} |
138 |
|
|
139 |
5 |
result.put(nameSpaceName, macroNames); |
140 |
|
} |
141 |
|
|
142 |
4 |
return result; |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
@param |
149 |
|
@param |
150 |
|
@return |
151 |
|
@throws |
152 |
|
@throws |
153 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
154 |
24 |
private Object getField(Object instance, String fieldName) throws NoSuchFieldException, IllegalAccessException... |
155 |
|
{ |
156 |
24 |
Field field = instance.getClass().getDeclaredField(fieldName); |
157 |
24 |
field.setAccessible(true); |
158 |
24 |
return field.get(instance); |
159 |
|
} |
160 |
|
} |