OperationsServiceImpl.java 3.64 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.settings.SlingSettingsService
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.operations.impl;

import com.adobe.granite.operations.OperationsService;
import java.lang.management.ManagementFactory;
import java.util.Set;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.QueryExp;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.settings.SlingSettingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={OperationsService.class})
public class OperationsServiceImpl
implements OperationsService {
    private final Logger logger;
    @Reference
    SlingSettingsService settings;
    private MBeanServer mbeanServer;

    public OperationsServiceImpl() {
        this.logger = LoggerFactory.getLogger((String)this.getClass().getName());
    }

    @Activate
    protected void activate() {
        this.mbeanServer = ManagementFactory.getPlatformMBeanServer();
    }

    @Deactivate
    protected void deactivate() {
        this.mbeanServer = null;
    }

    private boolean checkRunMode(ValueMap properties) {
        boolean enabled = true;
        String[] runModes = (String[])properties.get("granite.operations.condition.runmode", String[].class);
        if (runModes != null) {
            Set activeModes = this.settings.getRunModes();
            for (String r : runModes) {
                String mode;
                enabled = r.startsWith("!") ? !activeModes.contains(mode = r.substring(1)) : activeModes.contains(r);
                if (!enabled) break;
            }
        }
        return enabled;
    }

    private boolean checkMBean(String instr) {
        String mbeanName = instr;
        try {
            ObjectName oName = new ObjectName(mbeanName);
            return !this.mbeanServer.queryNames(oName, null).isEmpty();
        }
        catch (MalformedObjectNameException e) {
            this.logger.warn("Unable to check condition, malformed object name " + mbeanName, (Throwable)e);
            return false;
        }
    }

    private boolean checkMBean(ValueMap properties) {
        boolean enabled = true;
        String[] instr = (String[])properties.get("granite.operations.condition.mbean", String[].class);
        if (instr != null) {
            for (String r : instr) {
                if (this.checkMBean(r)) continue;
                enabled = false;
                break;
            }
        }
        return enabled;
    }

    @Override
    public boolean isEnabled(ValueMap properties) {
        boolean enabled = true;
        enabled = this.checkRunMode(properties) && this.checkMBean(properties);
        return enabled;
    }

    protected void bindSettings(SlingSettingsService slingSettingsService) {
        this.settings = slingSettingsService;
    }

    protected void unbindSettings(SlingSettingsService slingSettingsService) {
        if (this.settings == slingSettingsService) {
            this.settings = null;
        }
    }
}