ScriptMBean.java 6.16 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.jmx.annotation.OpenTypeUtils
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONObject
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.monitoring.impl;

import com.adobe.granite.jmx.annotation.OpenTypeUtils;
import com.adobe.granite.monitoring.impl.ScriptConfig;
import com.adobe.granite.monitoring.impl.ShellScriptExecutor;
import java.util.HashMap;
import java.util.LinkedList;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.InvalidAttributeValueException;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.ReflectionException;
import javax.management.openmbean.OpenMBeanAttributeInfo;
import javax.management.openmbean.OpenMBeanAttributeInfoSupport;
import javax.management.openmbean.OpenMBeanConstructorInfo;
import javax.management.openmbean.OpenMBeanInfoSupport;
import javax.management.openmbean.OpenMBeanOperationInfo;
import javax.management.openmbean.OpenMBeanOperationInfoSupport;
import javax.management.openmbean.OpenMBeanParameterInfo;
import javax.management.openmbean.OpenType;
import org.apache.felix.scr.annotations.Property;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ScriptMBean
implements DynamicMBean {
    private static final Logger log = LoggerFactory.getLogger(ScriptMBean.class);
    private static final String[] attribNames = new String[]{"script.filename", "script.statuscode", "script.stdout", "script.stderr"};
    private static final String[] descriptions = new String[]{"Filename of the script", "Statuscode of the last execution of the script", "Unformatted output of the last execution of the script", "Unformatted error output the last execution of the script"};
    @Property(value={"com.adobe.granite.monitoring:type=OS"}, propertyPrivate=1)
    private final HashMap<String, Object> attribs = new HashMap();
    private JSONObject lastResult;
    private final ShellScriptExecutor executor;
    private final ScriptConfig cfg;

    protected ScriptMBean(ShellScriptExecutor executor, ScriptConfig cfg) {
        this.executor = executor;
        this.cfg = cfg;
        log.info("Script MBean instantiated with script " + cfg.getScriptFilename());
        this.attribs.put(attribNames[0], cfg.getScriptFilename());
        this.attribs.put(attribNames[1], -1);
        this.attribs.put(attribNames[2], "");
        this.attribs.put(attribNames[3], "");
    }

    public ScriptConfig getCfg() {
        return this.cfg;
    }

    public int invoke() {
        ShellScriptExecutor.Result result = this.executor.execute(this.cfg);
        if (result == null) {
            this.attribs.put(attribNames[1], -1);
            this.attribs.put(attribNames[2], "");
            this.attribs.put(attribNames[3], "");
            return -1;
        }
        this.attribs.put(attribNames[1], result.getExitCode());
        this.attribs.put(attribNames[2], result.getStdout());
        this.attribs.put(attribNames[3], result.getStderr());
        try {
            this.lastResult = new JSONObject(result.getStdout());
        }
        catch (Exception ignore) {
            this.lastResult = null;
        }
        return result.getExitCode();
    }

    private Object internalGetAttribute(String name) {
        Object value = this.attribs.get(name);
        if (value == null) {
            value = this.lastResult == null ? null : this.lastResult.opt(name);
        }
        return value;
    }

    public synchronized Object getAttribute(String name) throws AttributeNotFoundException {
        Object value = this.internalGetAttribute(name);
        if (value == null) {
            throw new AttributeNotFoundException("No such attribute: " + name);
        }
        return value;
    }

    public synchronized void setAttribute(Attribute attribute) throws InvalidAttributeValueException, MBeanException, AttributeNotFoundException {
        throw new InvalidAttributeValueException("attribute value not settable");
    }

    public synchronized AttributeList getAttributes(String[] names) {
        AttributeList list = new AttributeList();
        for (String name : names) {
            Object value = this.internalGetAttribute(name);
            if (value == null) continue;
            list.add(new Attribute(name, value));
        }
        return list;
    }

    public synchronized AttributeList setAttributes(AttributeList list) {
        return null;
    }

    public Object invoke(String name, Object[] args, String[] sig) throws MBeanException, ReflectionException {
        return this.invoke();
    }

    public synchronized MBeanInfo getMBeanInfo() {
        LinkedList<OpenMBeanAttributeInfoSupport> attrs = new LinkedList<OpenMBeanAttributeInfoSupport>();
        for (int i = 0; i < attribNames.length; ++i) {
            attrs.add(new OpenMBeanAttributeInfoSupport(attribNames[i], descriptions[i], OpenTypeUtils.getSimpleType(i == 1 ? Integer.class : String.class), true, false, false));
        }
        if (this.lastResult != null) {
            JSONArray jNames = this.lastResult.names();
            for (int i2 = 0; i2 < jNames.length(); ++i2) {
                String name = jNames.optString(i2);
                if (name == null) continue;
                attrs.add(new OpenMBeanAttributeInfoSupport(name, "Probe value for " + name, OpenTypeUtils.getSimpleType(String.class), true, false, false));
            }
        }
        OpenMBeanOperationInfo[] ops = new OpenMBeanOperationInfoSupport[]{new OpenMBeanOperationInfoSupport("execute", "execute the script " + this.cfg.getScriptFilename(), null, OpenTypeUtils.getSimpleType(Boolean.class), 2)};
        return new OpenMBeanInfoSupport(this.getClass().getName(), "Script Result for " + this.cfg.getScriptFilename(), attrs.toArray(new OpenMBeanAttributeInfoSupport[attrs.size()]), null, ops, null);
    }
}