-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
TemplatesAspect.java
321 lines (240 loc) · 9.27 KB
/
TemplatesAspect.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* SPDX-FileCopyrightText: (c) 2023 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
package com.liferay.glowroot.plugin.freemarker;
import java.util.Map;
import org.glowroot.agent.plugin.api.Agent;
import org.glowroot.agent.plugin.api.MessageSupplier;
import org.glowroot.agent.plugin.api.OptionalThreadContext;
import org.glowroot.agent.plugin.api.TimerName;
import org.glowroot.agent.plugin.api.TraceEntry;
import org.glowroot.agent.plugin.api.weaving.BindParameter;
import org.glowroot.agent.plugin.api.weaving.BindParameterArray;
import org.glowroot.agent.plugin.api.weaving.BindThrowable;
import org.glowroot.agent.plugin.api.weaving.BindTraveler;
import org.glowroot.agent.plugin.api.weaving.OnBefore;
import org.glowroot.agent.plugin.api.weaving.OnReturn;
import org.glowroot.agent.plugin.api.weaving.OnThrow;
import org.glowroot.agent.plugin.api.weaving.Pointcut;
import org.glowroot.agent.plugin.api.weaving.Shim;
/**
* @author Fabian Bouché
*/
public class TemplatesAspect {
@Pointcut(
className = "com.liferay.fragment.entry.processor.freemarker.FreeMarkerFragmentEntryProcessor",
methodName = "processFragmentEntryLinkHTML",
methodParameterTypes = {
"com.liferay.fragment.model.FragmentEntryLink", "java.lang.String",
"com.liferay.fragment.processor.FragmentEntryProcessorContext"
},
timerName = "Fragment Entry Link FreeMarker Template"
)
public static class FreeMarkerFragmentEntryProcessorAdvice {
@OnBefore
public static TraceEntry onBefore(
OptionalThreadContext optionalThreadContext,
@BindParameter FragmentEntryLinkShim fragmentEntryLinkShim,
@BindParameter String html,
@BindParameter FragmentEntryProcessorContextShim
fragmentEntryProcessorContextShim) {
TraceEntry traceEntry = null;
StringBuilder sb = new StringBuilder();
sb.append("Fragment Entry Link FreeMarker Template (Company ID ");
sb.append(fragmentEntryLinkShim.getCompanyId());
sb.append(", Fragment Entry Link ID ");
sb.append(fragmentEntryLinkShim.getFragmentEntryLinkId());
sb.append(", and Group ID ");
sb.append(fragmentEntryLinkShim.getGroupId());
sb.append(")");
if (_INSTRUMENTATION_LEVEL_TRACE.equals(
TemplatesPluginProperties.instrumentationLevel())) {
traceEntry = optionalThreadContext.startTransaction(
"FreeMarker Templates", sb.toString(),
MessageSupplier.create(sb.toString()), _timerName);
optionalThreadContext.setTransactionOuter();
optionalThreadContext.addTransactionAttribute("HTML", html);
}
else if (_INSTRUMENTATION_LEVEL_DEBUG.equals(
TemplatesPluginProperties.instrumentationLevel())) {
traceEntry = optionalThreadContext.startTransaction(
"FreeMarker Templates", sb.toString(),
MessageSupplier.create(sb.toString()), _timerName);
optionalThreadContext.setTransactionOuter();
}
else {
traceEntry = optionalThreadContext.startTraceEntry(
MessageSupplier.create(sb.toString()), _timerName);
}
return traceEntry;
}
@OnReturn
public static void onReturn(@BindTraveler TraceEntry traceEntry) {
traceEntry.end();
}
@OnThrow
public static void onThrow(
@BindThrowable Throwable throwable,
@BindTraveler TraceEntry traceEntry) {
traceEntry.endWithError(throwable);
}
private static final TimerName _timerName = Agent.getTimerName(
FreeMarkerFragmentEntryProcessorAdvice.class);
}
@Pointcut(
className = "com.liferay.journal.internal.transformer.JournalTransformer",
methodName = "transform",
methodParameterTypes = {
"com.liferay.journal.model.JournalArticle",
"com.liferay.dynamic.data.mapping.model.DDMTemplate",
"com.liferay.journal.util.JournalHelper", "java.lang.String",
"com.liferay.layout.display.page.LayoutDisplayPageProviderRegistry",
"java.util.List",
"com.liferay.portal.kernel.portlet.PortletRequestModel", "boolean",
"java.lang.String", "com.liferay.portal.kernel.theme.ThemeDisplay",
"java.lang.String"
},
timerName = "Journal Article FreeMarker Template"
)
public static class JournalTransformerAdvice {
@OnBefore
public static TraceEntry onBefore(
OptionalThreadContext optionalThreadContext,
@BindParameterArray Object[] parameters) {
TraceEntry traceEntry = null;
StringBuilder sb = new StringBuilder();
sb.append("Journal Article FreeMarker Template (Company ID ");
ThemeDisplayShim themeDisplayShim = (ThemeDisplayShim)parameters[9];
sb.append(themeDisplayShim.getCompanyId());
sb.append(", Site Group ID ");
sb.append(themeDisplayShim.getSiteGroupId());
sb.append(", and Template ID ");
DDMTemplateShim dDMTemplateShim = (DDMTemplateShim)parameters[1];
sb.append(dDMTemplateShim.getTemplateId());
sb.append(")");
if (_INSTRUMENTATION_LEVEL_TRACE.equals(
TemplatesPluginProperties.instrumentationLevel())) {
traceEntry = optionalThreadContext.startTransaction(
"FreeMarker Templates", sb.toString(),
MessageSupplier.create(sb.toString()), _timerName);
optionalThreadContext.setTransactionOuter();
optionalThreadContext.addTransactionAttribute(
"Script", dDMTemplateShim.getScript());
}
else if (_INSTRUMENTATION_LEVEL_DEBUG.equals(
TemplatesPluginProperties.instrumentationLevel())) {
traceEntry = optionalThreadContext.startTransaction(
"FreeMarker Templates", sb.toString(),
MessageSupplier.create(sb.toString()), _timerName);
optionalThreadContext.setTransactionOuter();
}
else {
traceEntry = optionalThreadContext.startTraceEntry(
MessageSupplier.create(sb.toString()), _timerName);
}
return traceEntry;
}
@OnReturn
public static void onReturn(@BindTraveler TraceEntry traceEntry) {
traceEntry.end();
}
@OnThrow
public static void onThrow(
@BindThrowable Throwable throwable,
@BindTraveler TraceEntry traceEntry) {
traceEntry.endWithError(throwable);
}
private static final TimerName _timerName = Agent.getTimerName(
JournalTransformerAdvice.class);
}
@Pointcut(
className = "com.liferay.portal.templateparser.Transformer",
methodName = "transform",
methodParameterTypes = {
"com.liferay.portal.kernel.theme.ThemeDisplay", "java.util.Map",
"java.lang.String", "java.lang.String",
"com.liferay.portal.kernel.io.unsync.UnsyncStringWriter",
"javax.servlet.http.HttpServletRequest",
"javax.servlet.http.HttpServletResponse"
},
timerName = "Transformer FreeMarker Template"
)
public static class TransformerAdvice {
@OnBefore
public static TraceEntry onBefore(
OptionalThreadContext optionalThreadContext,
@BindParameter ThemeDisplayShim themeDisplayShim,
@BindParameter Map<String, Object> contextObjects,
@BindParameter String script, @BindParameter String type) {
TraceEntry traceEntry = null;
StringBuilder sb = new StringBuilder();
sb.append("Transformer FreeMarker Template (Company ID ");
sb.append(themeDisplayShim.getCompanyId());
sb.append(", Site Group ID ");
sb.append(themeDisplayShim.getSiteGroupId());
sb.append(", and Template ID ");
sb.append(contextObjects.get("template_id"));
sb.append(")");
if (_INSTRUMENTATION_LEVEL_TRACE.equals(
TemplatesPluginProperties.instrumentationLevel())) {
traceEntry = optionalThreadContext.startTransaction(
"FreeMarker Templates", sb.toString(),
MessageSupplier.create(sb.toString()), _timerName);
optionalThreadContext.setTransactionOuter();
optionalThreadContext.addTransactionAttribute("Script", script);
optionalThreadContext.addTransactionAttribute("Type", type);
}
else if (_INSTRUMENTATION_LEVEL_DEBUG.equals(
TemplatesPluginProperties.instrumentationLevel())) {
traceEntry = optionalThreadContext.startTransaction(
"FreeMarker Templates", sb.toString(),
MessageSupplier.create(sb.toString()), _timerName);
optionalThreadContext.setTransactionOuter();
}
else {
traceEntry = optionalThreadContext.startTraceEntry(
MessageSupplier.create(sb.toString()), _timerName);
}
return traceEntry;
}
@OnReturn
public static void onReturn(@BindTraveler TraceEntry traceEntry) {
traceEntry.end();
}
@OnThrow
public static void onThrow(
@BindThrowable Throwable throwable,
@BindTraveler TraceEntry traceEntry) {
traceEntry.endWithError(throwable);
}
private static final TimerName _timerName = Agent.getTimerName(
TransformerAdvice.class);
}
@Shim("com.liferay.dynamic.data.mapping.model.DDMTemplate")
public interface DDMTemplateShim {
public String getScript();
public long getTemplateId();
}
@Shim("com.liferay.fragment.model.FragmentEntryLink")
public interface FragmentEntryLinkShim {
public long getCompanyId();
public long getFragmentEntryLinkId();
public long getGroupId();
}
@Shim("com.liferay.fragment.processor.FragmentEntryProcessorContext")
public interface FragmentEntryProcessorContextShim {
}
@Shim("com.liferay.journal.model.JournalArticle")
public interface JournalArticleShim {
}
@Shim("com.liferay.portal.kernel.theme.ThemeDisplay")
public interface ThemeDisplayShim {
public long getCompanyGroupId();
public long getCompanyId();
public long getScopeGroupId();
public long getSiteGroupId();
}
private static final String _INSTRUMENTATION_LEVEL_DEBUG = "DEBUG";
private static final String _INSTRUMENTATION_LEVEL_TRACE = "TRACE";
}