Search in sources :

Example 56 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project Payara by payara.

the class JsonMetadataWriter method getJsonFromMetadata.

private JsonObject getJsonFromMetadata(Metadata metadata) {
    JsonObjectBuilder payloadBuilder = Json.createObjectBuilder();
    payloadBuilder.add(NAME, sanitizeMetadata(metadata.getName()));
    payloadBuilder.add(DISPLAY_NAME, sanitizeMetadata(metadata.getDisplayName()));
    payloadBuilder.add(DESCRIPTION, sanitizeMetadata(metadata.getDescription()));
    payloadBuilder.add(TYPE, sanitizeMetadata(metadata.getType()));
    payloadBuilder.add(UNIT, sanitizeMetadata(metadata.getUnit()));
    payloadBuilder.add(TAGS, getPairFromMap(metadata.getTags()));
    return payloadBuilder.build();
}
Also used : JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 57 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project Payara by payara.

the class JsonMetricWriter method getJsonFromMetrics.

private JsonObjectBuilder getJsonFromMetrics(Map<String, Metric> metricMap) {
    JsonObjectBuilder payloadBuilder = Json.createObjectBuilder();
    for (Map.Entry<String, Metric> entry : metricMap.entrySet()) {
        String metricName = entry.getKey();
        Metric metric = entry.getValue();
        if (Counter.class.isInstance(metric)) {
            payloadBuilder.add(metricName, ((Counter) metric).getCount());
        } else if (Gauge.class.isInstance(metric)) {
            Number value;
            Object gaugeValue;
            try {
                gaugeValue = ((Gauge) metric).getValue();
            } catch (IllegalStateException e) {
                // The forwarding gauge is unloaded
                continue;
            }
            if (!Number.class.isInstance(gaugeValue)) {
                LOGGER.log(Level.FINER, "Skipping JSON output for Gauge: {0} of type {1}", new Object[] { metricName, gaugeValue.getClass() });
                continue;
            }
            value = (Number) gaugeValue;
            addValueToJsonObject(payloadBuilder, metricName, value);
        } else if (Histogram.class.isInstance(metric)) {
            payloadBuilder.add(metricName, getJsonFromMap(getHistogramNumbers((Histogram) metric)));
        } else if (Meter.class.isInstance(metric)) {
            payloadBuilder.add(metricName, getJsonFromMap(getMeterNumbers((Meter) metric)));
        } else if (Timer.class.isInstance(metric)) {
            payloadBuilder.add(metricName, getJsonFromMap(getTimerNumbers((Timer) metric)));
        } else {
            LOGGER.log(Level.WARNING, "Metric type '{0} for {1} is invalid", new Object[] { metric.getClass(), metricName });
        }
    }
    return payloadBuilder;
}
Also used : Meter(org.eclipse.microprofile.metrics.Meter) Metric(org.eclipse.microprofile.metrics.Metric) JsonObjectBuilder(javax.json.JsonObjectBuilder) HashMap(java.util.HashMap) Map(java.util.Map) Gauge(org.eclipse.microprofile.metrics.Gauge)

Example 58 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project Payara by payara.

the class InstanceDescriptorImpl method toJsonObject.

private JsonObject toJsonObject(boolean verbose) {
    // Create a builder to store the object
    JsonObjectBuilder configBuilder = Json.createObjectBuilder();
    // Add all the instance information
    configBuilder.add("Host", hostName.getCanonicalHostName());
    configBuilder.add("Http Port(s)", Arrays.toString(getHttpPorts().toArray()).replaceAll("[\\[\\]]", ""));
    configBuilder.add("Https Port(s)", Arrays.toString(getHttpsPorts().toArray()).replaceAll("[\\[\\]]", ""));
    configBuilder.add("Instance Name", this.instanceName);
    configBuilder.add("Instance Group", this.instanceGroup);
    if (memberUUID != null) {
        configBuilder.add("Hazelcast Member UUID", this.memberUUID);
    }
    // Create array of applications
    JsonArrayBuilder deploymentBuilder = Json.createArrayBuilder();
    getDeployedApplications().forEach(app -> {
        // Construct object for each application
        JsonObjectBuilder appBuilder = Json.createObjectBuilder();
        // Add the name of the application
        appBuilder.add("Name", app.getName());
        // If there's only one module in the application, print the module info with the application name
        if (app.getModuleDescriptors().size() == 1) {
            ModuleDescriptor module = app.getModuleDescriptors().get(0);
            appBuilder.add("Type", module.getType());
            appBuilder.add("Context Root", (module.getContextRoot() == null) ? "N/A" : module.getContextRoot());
            // List the module mappings if verbose is specified
            if (verbose) {
                JsonObjectBuilder servletMappings = Json.createObjectBuilder();
                module.getServletMappings().forEach((key, value) -> {
                    servletMappings.add(key, value);
                });
                appBuilder.add("Mappings", servletMappings.build());
            }
        // If there's more modules, print info for each module
        } else {
            JsonArrayBuilder modules = Json.createArrayBuilder();
            // Construct object for each module
            app.getModuleDescriptors().forEach(module -> {
                JsonObjectBuilder moduleBuilder = Json.createObjectBuilder();
                // Add basic information for the module
                moduleBuilder.add("Name", module.getName());
                moduleBuilder.add("Type", module.getType());
                moduleBuilder.add("Context Root", (module.getContextRoot() == null) ? "***" : module.getContextRoot());
                // Create an object of mappings if verbose is specified
                if (verbose) {
                    JsonObjectBuilder servletMappings = Json.createObjectBuilder();
                    module.getServletMappings().forEach((key, value) -> {
                        servletMappings.add(key, value);
                    });
                    moduleBuilder.add("Mappings", servletMappings.build());
                }
                modules.add(moduleBuilder.build());
            });
            appBuilder.add("Modules", modules.build());
        }
        deploymentBuilder.add(appBuilder.build());
    });
    configBuilder.add("Deployed", deploymentBuilder.build());
    return Json.createObjectBuilder().add("Instance Configuration", configBuilder.build()).build();
}
Also used : ModuleDescriptor(fish.payara.micro.data.ModuleDescriptor) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 59 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project Payara by payara.

the class MBeanReadHandler method getRequestObject.

@Override
public JsonObject getRequestObject() {
    JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
    try {
        objectBuilder.add(RestMonitoringAppResponseToken.getMbeanNameKey(), mbeanname);
        objectBuilder.add(RestMonitoringAppResponseToken.getRequestTypeKey(), requesttype);
    } catch (JsonException ex) {
        super.setStatus(Response.Status.INTERNAL_SERVER_ERROR);
    }
    return objectBuilder.build();
}
Also used : JsonException(javax.json.JsonException) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 60 with JsonObjectBuilder

use of javax.json.JsonObjectBuilder in project Payara by payara.

the class MBeanReadHandler method buildAttributes.

private JsonObject buildAttributes(MBeanInfo mbean) throws JsonException {
    JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
    MBeanAttributeInfo[] attributes = mbean.getAttributes();
    for (MBeanAttributeInfo attribute : attributes) {
        String attributeName = attribute.getName();
        MBeanAttributeReadHandler attributeHandler = new MBeanAttributeReadHandler(delegate, mbeanname, attributeName);
        objectBuilder.add(attributeName, attributeHandler.getValueObject());
    }
    return objectBuilder.build();
}
Also used : JsonObjectBuilder(javax.json.JsonObjectBuilder) MBeanAttributeInfo(javax.management.MBeanAttributeInfo)

Aggregations

JsonObjectBuilder (javax.json.JsonObjectBuilder)76 JsonArrayBuilder (javax.json.JsonArrayBuilder)31 Map (java.util.Map)19 JsonObject (javax.json.JsonObject)19 JsonException (javax.json.JsonException)14 HashMap (java.util.HashMap)9 StringWriter (java.io.StringWriter)8 ArrayList (java.util.ArrayList)6 JsonValue (javax.json.JsonValue)6 List (java.util.List)5 JsonString (javax.json.JsonString)5 PrintWriter (java.io.PrintWriter)3 Entry (java.util.Map.Entry)3 JsonArray (javax.json.JsonArray)3 ActionReporter (com.sun.enterprise.v3.common.ActionReporter)2 JavaSource (de.strullerbaumann.visualee.source.entity.JavaSource)2 File (java.io.File)2 JsonWriter (javax.json.JsonWriter)2 Resource (org.apache.sling.api.resource.Resource)2 ValueMap (org.apache.sling.api.resource.ValueMap)2