-
Notifications
You must be signed in to change notification settings - Fork 299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PAYARA-2744 Monitoring REST API bulk reading #2617 #2697
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
...war/src/main/java/fish/payara/monitoring/rest/app/handler/MBeanAttributesReadHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
...ing-war/src/main/java/fish/payara/monitoring/rest/app/resource/MBeanBulkReadResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add the copyright header |
||
|
||
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); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add the copyright header