Skip to content

Commit

Permalink
Merge pull request #2804 from MeroRai/PAYARA-2745
Browse files Browse the repository at this point in the history
PAYARA-2745 Downport PAYARA-2744 Bulk Read for REST monitoring - Payara4
  • Loading branch information
Pandrex247 committed Jun 15, 2018
2 parents 85716d1 + b06ecae commit 5bbabbe
Show file tree
Hide file tree
Showing 4 changed files with 329 additions and 3 deletions.
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
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);
}
}
@@ -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 javax.inject.Singleton;
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 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;
import java.util.Arrays;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.Response;

/**
* @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() {
JSONObject requestObject = new JSONObject();
try {
requestObject.put(RestMonitoringAppResponseToken.getMbeanNameKey(), mbeanname);
requestObject.put(RestMonitoringAppResponseToken.getMbeanNameKey(), mbeanname);
requestObject.put(RestMonitoringAppResponseToken.getAttributeNameKey(),
new JSONArray(Arrays.asList(attributenames)));
requestObject.put(RestMonitoringAppResponseToken.getRequestTypeKey(), requesttype);

} catch (JSONException ex) {
Logger.getLogger(MBeanAttributesReadHandler.class.getName()).log(Level.SEVERE, null, ex);
}
return requestObject;
}

@Override
public Object 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
JSONArray jsonArray = new JSONArray();

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

JSONObject valueObject = new JSONObject();
valueObject.put(attribute.getName(), processor.processObject(attribute.getValue()));
jsonArray.put(valueObject);
}

return jsonArray;
} catch (InstanceNotFoundException | ReflectionException | MalformedObjectNameException ex) {
super.setStatus(Response.Status.NOT_FOUND);
return getTraceObject(ex);
}
}
}
@@ -0,0 +1,173 @@
/**
* 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.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
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;
import fish.payara.monitoring.rest.app.handler.ResourceHandler;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.*;

/**
* @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) throws JSONException {

if (content != null && !content.trim().isEmpty() && isJsonValid(content)) {
Object jsonTokener = new JSONTokener(content).nextValue();

if (jsonTokener instanceof JSONObject) {
JSONObject jsonObject = new JSONObject(content);

try {
JSONObject request = handleRequest(jsonObject);
if (request != null) {
return request.toString();
} else {
return "{}";
}
} catch (NullPointerException ex) {
Logger.getLogger(ResourceHandler.class.getName()).log(Level.CONFIG, "NullPointerException when getting Read resources", ex);
}
} else if (jsonTokener instanceof JSONArray) {
List<JSONObject> objects = new ArrayList<>();
JSONArray jsonArray = new JSONArray(content);

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = handleRequest(jsonArray.getJSONObject(i));
if (jsonObject != null) {
objects.add(jsonObject);
}
}

JSONArray resource = new JSONArray();

for (JSONObject jsonObject : objects) {
resource.put(jsonObject);
}

return resource.toString();
}
}

return "invalid JSON";
}

private JSONObject handleRequest(final JSONObject jsonObject) throws JSONException {
// ignore non-read requests
String type = jsonObject.optString("type");
if (!"read".equalsIgnoreCase(type)) {
return null;
}

String mbean = jsonObject.optString("mbean");
String attributes = jsonObject.optString("attribute");
ReadHandler handler = getReadHandler(mbean, attributes);
return handler.getResource();
}

private ReadHandler getReadHandler(final String mbean, final String attributes) throws JSONException {
// attributes can be null, a string or a list of strings
if (attributes != null && !attributes.trim().isEmpty()) {
Object jsonTokener = new JSONTokener(attributes).nextValue();
if (jsonTokener instanceof JSONArray) {
List<String> attributeNames = new ArrayList<>();

JSONArray jsonArray = new JSONArray(attributes);

for (int i = 0; i < jsonArray.length(); i++) {
attributeNames.add(jsonArray.get(i).toString());
}
return new MBeanAttributesReadHandler(mDelegate, mbean, attributeNames.toArray(new String[0]));

} else if (jsonTokener instanceof String) {
return new MBeanAttributeReadHandler(mDelegate, mbean, attributes);
}
}

return new MBeanReadHandler(mDelegate, mbean);
}

private boolean isJsonValid(String jsonString) {
try {
new JSONObject(jsonString);
} catch (JSONException jsonObjectException) {
try {
new JSONArray(jsonString);
} catch (JSONException jsonArrayException) {
return false;
}
}
return true;
}

}

0 comments on commit 5bbabbe

Please sign in to comment.