Skip to content

Commit

Permalink
PAYARA-2744 Monitoring REST API bulk reading #2617 (#2697)
Browse files Browse the repository at this point in the history
* Monitoring REST AI bulk reading

* Copyright header
  • Loading branch information
kvalev authored and smillidge committed May 16, 2018
1 parent ed717d4 commit 48a8e19
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
package fish.payara.monitoring.rest.app;

import static java.lang.management.ManagementFactory.getPlatformMBeanServer;

import javax.ejb.Startup;
import javax.inject.Singleton;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
Expand All @@ -53,7 +55,7 @@
import javax.management.ReflectionException;

/**
*
*
* @author Fraser Savage
*/
@Startup
Expand Down Expand Up @@ -102,6 +104,28 @@ public Object getMBeanAttribute(String mbeanname, String attributename) throws M
return platformServer.getAttribute(getMBeanName(mbeanname), attributename);
}

/**
* Retrieves the values of several attributes of a named MBean. The MBean is identified by its object name.
* If one or more attributes cannot be retrieved for some reason, they will be omitted from the returned
* {@code AttributeList}.
*
* @param mbeanname
* The object name of the MBean from which the attributes are retrieved.
* @param attributenames
* A list of the attributes to be retrieved.
* @return The list of the retrieved attributes.
* @throws InstanceNotFoundException
* {@inheritDoc}
* @throws MalformedObjectNameException
* {@inheritDoc}
* @throws ReflectionException
* {@inheritDoc}
*/
public AttributeList getMBeanAttributes(final String mbeanname, final String[] attributenames)
throws InstanceNotFoundException, MalformedObjectNameException, ReflectionException {
return platformServer.getAttributes(getMBeanName(mbeanname), attributenames);
}

/**
* Returns an {@link ObjectName}.
* The mbeanname argument must be a valid {@link ObjectName}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

Expand All @@ -51,16 +52,17 @@
// @HOLD - FANG-3: Implement Fang 'State' handler and resource.
@ApplicationPath("/rest")
public class RestMonitoringAppConfig extends Application {

@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet<>();
addRestResourceClasses(resources);
return resources;
}

private void addRestResourceClasses(Set<Class<?>> resources) {
private void addRestResourceClasses(final Set<Class<?>> resources) {
resources.add(fish.payara.monitoring.rest.app.resource.MBeanReadResource.class);
resources.add(fish.payara.monitoring.rest.app.resource.MBeanBulkReadResource.class);
resources.add(fish.payara.monitoring.rest.app.resource.RestMonitoringAppStateResource.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2018] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.monitoring.rest.app.handler;

import java.util.Arrays;

import javax.inject.Singleton;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonException;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonValue;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.InstanceNotFoundException;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.ws.rs.core.Response;

import fish.payara.monitoring.rest.app.MBeanServerDelegate;
import fish.payara.monitoring.rest.app.RestMonitoringAppResponseToken;
import fish.payara.monitoring.rest.app.processor.ProcessorFactory;
import fish.payara.monitoring.rest.app.processor.TypeProcessor;

/**
* @author Krassimir Valev
*/
public class MBeanAttributesReadHandler extends ReadHandler {

private final String mbeanname;
private final String[] attributenames;

/**
* Creates an instance of MBeanAttributeReadHandler, which handles bulk MBean
* attribute read requests.
*
* @param delegate
* The {@link MBeanServerDelegate} to get information from.
* @param mbeanname
* The {@link ObjectName} of the MBean to get information from.
* @param attributename
* The name of the MBean attribute to get values for.
*/
public MBeanAttributesReadHandler(@Singleton final MBeanServerDelegate delegate,
final String mbeanname, final String[] attributenames) {
super(delegate);
this.mbeanname = mbeanname;
this.attributenames = attributenames;
}

@Override
public JsonObject getRequestObject() {
JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
try {
objectBuilder.add(RestMonitoringAppResponseToken.getMbeanNameKey(), mbeanname);
objectBuilder.add(RestMonitoringAppResponseToken.getAttributeNameKey(),
Json.createArrayBuilder(Arrays.asList(attributenames)));
objectBuilder.add(RestMonitoringAppResponseToken.getRequestTypeKey(), requesttype);
} catch (JsonException ex) {
super.setStatus(Response.Status.INTERNAL_SERVER_ERROR);
}
return objectBuilder.build();
}

@Override
public JsonValue getValueObject() throws JsonException {
try {
AttributeList attributes = delegate.getMBeanAttributes(mbeanname, attributenames);

// the javax.management.Attribute type does not inherit from OpenType<T>, so the existing
// ProcessorFactory and TypeProcessor infrastructure cannot be used
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();

for (Attribute attribute : attributes.asList()) {
TypeProcessor<?> processor = ProcessorFactory.getTypeProcessor(attribute.getValue());

JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
objectBuilder.add(attribute.getName(), processor.processObject(attribute.getValue()));
arrayBuilder.add(objectBuilder);
}

return arrayBuilder.build();
} catch (InstanceNotFoundException | ReflectionException | MalformedObjectNameException ex) {
super.setStatus(Response.Status.NOT_FOUND);
return getTraceObject(ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2018] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.monitoring.rest.app.resource;

import java.io.StringReader;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonString;
import javax.json.JsonStructure;
import javax.json.JsonValue;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import fish.payara.monitoring.rest.app.MBeanServerDelegate;
import fish.payara.monitoring.rest.app.handler.MBeanAttributeReadHandler;
import fish.payara.monitoring.rest.app.handler.MBeanAttributesReadHandler;
import fish.payara.monitoring.rest.app.handler.MBeanReadHandler;
import fish.payara.monitoring.rest.app.handler.ReadHandler;

/**
* @author Krassimir Valev
*/
@Path("")
@RequestScoped
public class MBeanBulkReadResource {

@Inject
private MBeanServerDelegate mDelegate;

/**
* Returns the {@link String} form of the {@link JSONObject} resource from the ResourceHandler.
*
* @param content
* The JSON request payload, describing the beans and attributes to read.
* @return The {@link String} representation of the MBeanRead/MBeanAttributeRead {@link JSONObject}.
*/
@POST
@Produces(MediaType.APPLICATION_JSON)
public String getReadResource(final String content) {
try (JsonReader reader = Json.createReader(new StringReader(content))) {
// the payload can be either a single request or a bulk one (array)
JsonStructure struct = reader.read();
switch (struct.getValueType()) {
case ARRAY:
List<JsonObject> objects = struct.asJsonArray().stream()
.map(value -> handleRequest(value.asJsonObject()))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());

JsonArrayBuilder builder = Json.createArrayBuilder();
for (JsonObject jsonObject : objects) {
builder.add(jsonObject);
}

return builder.build().toString();
case OBJECT:
return handleRequest(struct.asJsonObject()).orElse(JsonValue.EMPTY_JSON_OBJECT).toString();
default:
return "invalid JSON structure";
}
}
}

private Optional<JsonObject> handleRequest(final JsonObject jsonObject) {
// ignore non-read requests
String type = jsonObject.getString("type", "");
if (!"read".equalsIgnoreCase(type)) {
return Optional.empty();
}

String mbean = jsonObject.getString("mbean", "");
JsonValue attributes = jsonObject.getOrDefault("attribute", JsonValue.NULL);
ReadHandler handler = getReadHandler(mbean, attributes);
return Optional.of(handler.getResource());
}

private ReadHandler getReadHandler(final String mbean, final JsonValue attributes) {
// attributes can be null, a string or a list of strings
switch (attributes.getValueType()) {
case ARRAY:
String[] attributeNames = attributes.asJsonArray().stream()
.map(v -> ((JsonString) v).getString())
.toArray(String[]::new);
return new MBeanAttributesReadHandler(mDelegate, mbean, attributeNames);
case STRING:
String attribute = ((JsonString) attributes).getString();
return new MBeanAttributeReadHandler(mDelegate, mbean, attribute);
default:
return new MBeanReadHandler(mDelegate, mbean);
}
}

}

0 comments on commit 48a8e19

Please sign in to comment.