-
Notifications
You must be signed in to change notification settings - Fork 683
/
CorePlugin.java
318 lines (298 loc) · 12.5 KB
/
CorePlugin.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
package play;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.jamonapi.Monitor;
import com.jamonapi.MonitorFactory;
import com.jamonapi.utils.Misc;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.commons.lang.StringUtils;
import play.Play.Mode;
import play.classloading.ApplicationClasses.ApplicationClass;
import play.classloading.enhancers.ContinuationEnhancer;
import play.classloading.enhancers.ControllersEnhancer;
import play.classloading.enhancers.Enhancer;
import play.classloading.enhancers.LVEnhancer;
import play.classloading.enhancers.MailerEnhancer;
import play.classloading.enhancers.PropertiesEnhancer;
import play.classloading.enhancers.SigEnhancer;
import play.exceptions.UnexpectedException;
import play.libs.Crypto;
import play.mvc.Http.Header;
import play.mvc.Http.Request;
import play.mvc.Http.Response;
import static java.util.Arrays.asList;
/**
* Plugin used for core tasks
*/
public class CorePlugin extends PlayPlugin {
/**
* Get the application status
*/
public static String computeApplicationStatus(boolean json) {
if (json) {
JsonObject o = new JsonObject();
for (PlayPlugin plugin : Play.pluginCollection.getEnabledPlugins()) {
try {
JsonObject status = plugin.getJsonStatus();
if (status != null) {
o.add(plugin.getClass().getName(), status);
}
} catch (Throwable e) {
JsonObject error = new JsonObject();
error.add("error", new JsonPrimitive(e.getMessage()));
o.add(plugin.getClass().getName(), error);
}
}
return o.toString();
}
StringBuilder dump = new StringBuilder(16);
for (PlayPlugin plugin : Play.pluginCollection.getEnabledPlugins()) {
try {
String status = plugin.getStatus();
if (status != null) {
dump.append(status);
dump.append("\n");
}
} catch (Throwable e) {
dump.append(plugin.getClass().getName()).append(".getStatus() has failed (").append(e.getMessage()).append(")");
}
}
return dump.toString();
}
/**
* Intercept /@status and check that the Authorization header is valid.
* Then ask each plugin for a status dump and send it over the HTTP response.
*/
@Override
public boolean rawInvocation(Request request, Response response) throws Exception {
if (Play.mode == Mode.DEV && request.path.equals("/@kill")) {
System.out.println("@KILLED");
if (Play.standalonePlayServer) {
System.exit(0);
} else {
Logger.error("Cannot execute @kill since Play is not running as standalone server");
}
}
if (request.path.equals("/@status") || request.path.equals("/@status.json")) {
if(!Play.started) {
response.print("Application is not started");
response.status = 503;
return true;
}
response.contentType = request.path.contains(".json") ? "application/json" : "text/plain";
Header authorization = request.headers.get("authorization");
if (request.isLoopback || (authorization != null && Crypto.sign("@status").equals(authorization.value()))) {
response.print(computeApplicationStatus(request.path.contains(".json")));
response.status = 200;
return true;
}
response.status = 401;
if (response.contentType.equals("application/json")) {
response.print("{\"error\": \"Not authorized\"}");
} else {
response.print("Not authorized");
}
return true;
}
return super.rawInvocation(request, response);
}
/**
* Retrieve status about play core.
*/
@Override
public String getStatus() {
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
out.println("Java:");
out.println("~~~~~");
out.println("Version: " + System.getProperty("java.version"));
out.println("Home: " + System.getProperty("java.home"));
out.println("Max memory: " + Runtime.getRuntime().maxMemory());
out.println("Free memory: " + Runtime.getRuntime().freeMemory());
out.println("Total memory: " + Runtime.getRuntime().totalMemory());
out.println("Available processors: " + Runtime.getRuntime().availableProcessors());
out.println();
out.println("Play framework:");
out.println("~~~~~~~~~~~~~~~");
out.println("Version: " + Play.version);
out.println("Path: " + Play.frameworkPath);
out.println("ID: " + (StringUtils.isEmpty(Play.id) ? "(not set)" : Play.id));
out.println("Mode: " + Play.mode);
out.println("Tmp dir: " + (Play.tmpDir == null ? "(no tmp dir)" : Play.tmpDir));
out.println();
out.println("Application:");
out.println("~~~~~~~~~~~~");
out.println("Path: " + Play.applicationPath);
out.println("Name: " + Play.configuration.getProperty("application.name", "(not set)"));
out.println("Started at: " + (Play.started ? new SimpleDateFormat("MM/dd/yyyy HH:mm").format(new Date(Play.startedAt)) : "Not yet started"));
out.println();
out.println("Loaded modules:");
out.println("~~~~~~~~~~~~~~");
for (String module : Play.modules.keySet()) {
out.println(module + " at " + Play.modules.get(module).getRealFile());
}
out.println();
out.println("Loaded plugins:");
out.println("~~~~~~~~~~~~~~");
for (PlayPlugin plugin : Play.pluginCollection.getAllPlugins()) {
out.println(plugin.index + ":" + plugin.getClass().getName() + " [" + (Play.pluginCollection.isEnabled(plugin) ? "enabled" : "disabled") + "]");
}
out.println();
out.println("Configuration:");
out.println("~~~~~~~~~~~~~~");
for (Object key : Play.configuration.keySet()) {
out.println(key + "=" + Play.configuration.getProperty(key.toString()));
}
out.println();
out.println("Threads:");
out.println("~~~~~~~~");
try {
visit(out, getRootThread(), 0);
} catch (Throwable e) {
out.println("Oops; " + e.getMessage());
}
out.println();
out.println("Requests execution pool:");
out.println("~~~~~~~~~~~~~~~~~~~~~~~~");
out.println("Pool size: " + Invoker.executor.getPoolSize());
out.println("Active count: " + Invoker.executor.getActiveCount());
out.println("Scheduled task count: " + Invoker.executor.getTaskCount());
out.println("Queue size: " + Invoker.executor.getQueue().size());
out.println();
try {
out.println("Monitors:");
out.println("~~~~~~~~");
List<Monitor> monitors = new ArrayList<Monitor>(asList(MonitorFactory.getRootMonitor().getMonitors()));
Collections.sort(monitors, new Comparator<Monitor>() {
@Override public int compare(Monitor m1, Monitor m2) {
return Double.compare(m2.getTotal(), m1.getTotal());
}
});
int lm = 10;
for (Monitor monitor : monitors) {
if (monitor.getLabel().length() > lm) {
lm = monitor.getLabel().length();
}
}
for (Monitor monitor : monitors) {
if (monitor.getHits() > 0) {
out.println(String.format("%-" + lm + "s -> %8.0f hits; %8.1f avg; %8.1f min; %8.1f max;",
monitor.getLabel(), monitor.getHits(), monitor.getAvg(), monitor.getMin(), monitor.getMax()));
}
}
} catch (Exception e) {
out.println("No monitors found: " + e);
}
return sw.toString();
}
@Override
public JsonObject getJsonStatus() {
JsonObject status = new JsonObject();
{
JsonObject java = new JsonObject();
java.addProperty("version", System.getProperty("java.version"));
status.add("java", java);
}
{
JsonObject memory = new JsonObject();
memory.addProperty("max", Runtime.getRuntime().maxMemory());
memory.addProperty("free", Runtime.getRuntime().freeMemory());
memory.addProperty("total", Runtime.getRuntime().totalMemory());
status.add("memory", memory);
}
{
JsonObject application = new JsonObject();
application.addProperty("uptime", Play.started ? System.currentTimeMillis() - Play.startedAt : -1);
application.addProperty("path", Play.applicationPath.getAbsolutePath());
status.add("application", application);
}
{
JsonObject pool = new JsonObject();
pool.addProperty("size", Invoker.executor.getPoolSize());
pool.addProperty("active", Invoker.executor.getActiveCount());
pool.addProperty("scheduled", Invoker.executor.getTaskCount());
pool.addProperty("queue", Invoker.executor.getQueue().size());
status.add("pool", pool);
}
{
JsonArray monitors = new JsonArray();
try {
Object[][] data = Misc.sort(MonitorFactory.getRootMonitor().getBasicData(), 3, "desc");
for (Object[] row : data) {
if (((Double) row[1]) > 0) {
JsonObject o = new JsonObject();
o.addProperty("name", row[0].toString());
o.addProperty("hits", (Double) row[1]);
o.addProperty("avg", (Double) row[2]);
o.addProperty("min", (Double) row[6]);
o.addProperty("max", (Double) row[7]);
monitors.add(o);
}
}
} catch (Exception e) {
e.printStackTrace();
}
status.add("monitors", monitors);
}
return status;
}
/**
* Recursively visit all JVM threads
*/
static void visit(PrintWriter out, ThreadGroup group, int level) {
// Get threads in `group'
int numThreads = group.activeCount();
Thread[] threads = new Thread[numThreads * 2];
numThreads = group.enumerate(threads, false);
// Enumerate each thread in `group'
for (int i = 0; i < numThreads; i++) {
// Get thread
Thread thread = threads[i];
out.println(thread + " " + thread.getState());
}
// Get thread subgroups of `group'
int numGroups = group.activeGroupCount();
ThreadGroup[] groups = new ThreadGroup[numGroups * 2];
numGroups = group.enumerate(groups, false);
// Recursively visit each subgroup
for (int i = 0; i < numGroups; i++) {
visit(out, groups[i], level + 1);
}
}
/**
* Retrieve the JVM root thread group.
*/
static ThreadGroup getRootThread() {
ThreadGroup root = Thread.currentThread().getThreadGroup().getParent();
while (root.getParent() != null) {
root = root.getParent();
}
return root;
}
@Override
public void enhance(ApplicationClass applicationClass) throws Exception {
Class<?>[] enhancers = new Class[]{
PropertiesEnhancer.class,
ContinuationEnhancer.class,
SigEnhancer.class,
ControllersEnhancer.class,
MailerEnhancer.class,
LVEnhancer.class
};
for (Class<?> enhancer : enhancers) {
try {
long start = System.currentTimeMillis();
((Enhancer) enhancer.newInstance()).enhanceThisClass(applicationClass);
if (Logger.isTraceEnabled()) {
Logger.trace("%sms to apply %s to %s", System.currentTimeMillis() - start, enhancer.getSimpleName(), applicationClass.name);
}
} catch (Exception e) {
throw new UnexpectedException("While applying " + enhancer + " on " + applicationClass.name, e);
}
}
}
}