AgentConfigGroupImpl.java 3.76 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  org.apache.jackrabbit.util.Text
 *  org.apache.sling.settings.SlingSettingsService
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.replication.impl;

import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.AgentConfigGroup;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.settings.SlingSettingsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class AgentConfigGroupImpl
implements AgentConfigGroup {
    private static final Logger log = LoggerFactory.getLogger(AgentConfigGroupImpl.class);
    private final String id;
    private final String name;
    private String title;
    private final String[] runModes;
    private final boolean active;
    private Map<String, AgentConfig> configs = new TreeMap<String, AgentConfig>();

    private AgentConfigGroupImpl(String id, String name, String title, String[] runModes, boolean active) {
        this.id = id;
        this.name = name;
        this.title = title;
        this.runModes = runModes;
        this.active = active;
    }

    public static String[] getRunModes(Node node) throws RepositoryException {
        String[] runModes = null;
        String name = node.getName();
        if ("agents".equals(name)) {
            runModes = new String[]{};
        } else if (name.startsWith("agents.")) {
            String[] segs = Text.explode((String)name, (int)46);
            runModes = new String[segs.length - 1];
            System.arraycopy(segs, 1, runModes, 0, runModes.length);
        }
        return runModes;
    }

    public static AgentConfigGroupImpl create(Node groupNode, SlingSettingsService slingSettings) {
        try {
            String[] runModes = AgentConfigGroupImpl.getRunModes(groupNode);
            if (runModes == null) {
                return null;
            }
            String title = "";
            if (groupNode.hasProperty("jcr:content/jcr:title")) {
                title = groupNode.getProperty("jcr:content/jcr:title").getString();
            } else if (groupNode.hasProperty("jcr:title")) {
                title = groupNode.getProperty("jcr:title").getString();
            }
            boolean active = false;
            for (String m : runModes) {
                if ("*".equals(m = m.trim())) {
                    active = true;
                    break;
                }
                if (!slingSettings.getRunModes().contains(m)) continue;
                active = true;
                break;
            }
            return new AgentConfigGroupImpl(groupNode.getPath(), groupNode.getName(), title, runModes, active);
        }
        catch (RepositoryException e) {
            log.error("Error while reading agent config group node", (Throwable)e);
            return null;
        }
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public String getTitle() {
        return this.title;
    }

    @Override
    public String[] getRunModes() {
        return this.runModes;
    }

    @Override
    public boolean isActive() {
        return this.active;
    }

    @Override
    public Map<String, AgentConfig> getConfigurations() {
        return this.configs;
    }

    protected void update(Node node) throws RepositoryException {
        if (node.hasProperty("jcr:content/jcr:title")) {
            this.title = node.getProperty("jcr:content/jcr:title").getString();
        }
    }
}