use of org.glassfish.flashlight.MonitoringRuntimeDataRegistry in project Payara by payara.
the class MonitoringResource method getChildNodes.
@GET
@Path("domain{path:.*}")
@Produces({ MediaType.TEXT_HTML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getChildNodes(@PathParam("path") List<PathSegment> pathSegments) {
Response.ResponseBuilder responseBuilder = Response.status(OK);
RestActionReporter ar = new RestActionReporter();
ar.setActionDescription("Monitoring Data");
ar.setMessage("");
ar.setSuccess();
String currentInstanceName = System.getProperty("com.sun.aas.instanceName");
// TODO this needs to come from an API. Check with admin team
boolean isRunningOnDAS = "server".equals(currentInstanceName);
MonitoringRuntimeDataRegistry monitoringRegistry = habitat.getRemoteLocator().getService(MonitoringRuntimeDataRegistry.class);
TreeNode rootNode = monitoringRegistry.get(currentInstanceName);
// The pathSegments will always contain "domain". Discard it
pathSegments = pathSegments.subList(1, pathSegments.size());
if (!pathSegments.isEmpty()) {
PathSegment lastSegment = pathSegments.get(pathSegments.size() - 1);
if (lastSegment.getPath().isEmpty()) {
// if there is a trailing '/' (like monitoring/domain/), a spurious pathSegment is added. Discard it.
pathSegments = pathSegments.subList(0, pathSegments.size() - 1);
}
}
if (!pathSegments.isEmpty()) {
String firstPathElement = pathSegments.get(0).getPath();
if (firstPathElement.equals(currentInstanceName)) {
// Query for current instance. Execute it
// iterate over pathsegments and build a dotted name to look up in monitoring registry
StringBuilder pathInMonitoringRegistry = new StringBuilder();
for (PathSegment pathSegment : pathSegments.subList(1, pathSegments.size())) {
if (pathInMonitoringRegistry.length() > 0) {
pathInMonitoringRegistry.append('.');
}
// Need to escape '.' before passing it to monitoring code
pathInMonitoringRegistry.append(pathSegment.getPath().replaceAll("\\.", "\\\\."));
}
TreeNode resultNode = pathInMonitoringRegistry.length() > 0 && rootNode != null ? rootNode.getNode(pathInMonitoringRegistry.toString()) : rootNode;
if (resultNode != null) {
List<TreeNode> list = new ArrayList<TreeNode>();
if (resultNode.hasChildNodes()) {
list.addAll(resultNode.getEnabledChildNodes());
} else {
list.add(resultNode);
}
constructEntity(list, ar);
responseBuilder.entity(new ActionReportResult(ar));
} else {
// No monitoring data, so nothing to list
responseBuilder.status(NOT_FOUND);
ar.setFailure();
responseBuilder.entity(new ActionReportResult(ar));
}
} else {
// firstPathElement != currentInstanceName => A proxy request
if (isRunningOnDAS) {
// Attempt to forward to instance if running on Das
// TODO validate that firstPathElement corresponds to a valid server name
Properties proxiedResponse = new MonitoringProxyImpl().proxyRequest(uriInfo, Util.getJerseyClient(), habitat.getRemoteLocator());
ar.setExtraProperties(proxiedResponse);
responseBuilder.entity(new ActionReportResult(ar));
} else {
// Not running on DAS and firstPathElement != currentInstanceName => Reject the request as invalid
return Response.status(FORBIDDEN).build();
}
}
} else {
// Called for /monitoring/domain/
List<TreeNode> list = new ArrayList<TreeNode>();
if (rootNode != null) {
// Add currentInstance to response
list.add(rootNode);
}
constructEntity(list, ar);
if (isRunningOnDAS) {
// Add links to instances from the cluster
Domain domain = habitat.getRemoteLocator().getService(Domain.class);
Map<String, String> links = (Map<String, String>) ar.getExtraProperties().get("childResources");
for (Server s : domain.getServers().getServer()) {
if (!s.getName().equals("server")) {
// add all non 'server' instances
links.put(s.getName(), getElementLink(uriInfo, s.getName()));
}
}
}
responseBuilder.entity(new ActionReportResult(ar));
}
return responseBuilder.build();
}
Aggregations