UndoConfigServiceImpl.java 7.27 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.Modified
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.apache.sling.commons.osgi.OsgiUtil
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.undo.impl;

import com.day.cq.wcm.undo.UndoConfigService;
import java.io.IOException;
import java.io.Writer;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(label="Day CQ WCM Undo Configuration", immediate=1, metatype=1, description="Provides configuration for CQ's undo facilities.", name="com.day.cq.wcm.undo.UndoConfig")
@Service
public class UndoConfigServiceImpl
implements UndoConfigService {
    @Property(boolValue={1})
    protected static final String UNDO_ENABLED = "cq.wcm.undo.enabled";
    @Property(value={"/var/undo"})
    protected static final String UNDO_PATH = "cq.wcm.undo.path";
    @Property(intValue={10})
    protected static final String UNDO_DATA_VALIDITY = "cq.wcm.undo.validity";
    @Property(intValue={20})
    protected static final String UNDO_STEPS = "cq.wcm.undo.steps";
    @Property(value={""})
    protected static final String UNDO_PERSISTENCE = "cq.wcm.undo.persistence";
    @Property(boolValue={1})
    protected static final String UNDO_PERSISTENCE_MODE = "cq.wcm.undo.persistence.mode";
    @Property(value={""})
    protected static final String UNDO_MARKER_MODE = "cq.wcm.undo.markermode";
    @Property(value={"foundation/components/*"}, cardinality=4096)
    protected static final String UNDO_WHITELIST = "cq.wcm.undo.whitelist";
    @Property(value={"foundation/components/form/start:removeParagraph"}, cardinality=4096)
    protected static final String UNDO_BLACKLIST = "cq.wcm.undo.blacklist";
    private Map<String, Object> config;
    private static final Logger log = LoggerFactory.getLogger(UndoConfigServiceImpl.class);

    private String toString(String[] array) {
        StringBuilder str = new StringBuilder(128);
        boolean isFirst = true;
        for (String item : array) {
            if (!isFirst) {
                str.append(", ");
            } else {
                isFirst = false;
            }
            str.append(item);
        }
        return str.toString();
    }

    private void configure(ComponentContext context) {
        Boolean isStepSave;
        this.config = new HashMap<String, Object>();
        Dictionary dict = context.getProperties();
        Boolean enabled = (Boolean)dict.get("cq.wcm.undo.enabled");
        enabled = enabled != null ? enabled : Boolean.TRUE;
        this.config.put("enabled", enabled);
        String undoPath = (String)dict.get("cq.wcm.undo.path");
        this.config.put("path", undoPath);
        log.info("Path for undo: {}", (Object)undoPath);
        Integer undoDataValidity = (Integer)dict.get("cq.wcm.undo.validity");
        this.config.put("dataValidity", undoDataValidity);
        log.info("Undo data validity: {} hours", (Object)undoDataValidity);
        Integer undoSteps = (Integer)dict.get("cq.wcm.undo.steps");
        this.config.put("steps", undoSteps);
        log.info("Number of undoable steps: {}", (Object)undoSteps);
        String persistence = (String)dict.get("cq.wcm.undo.persistence");
        if (persistence.length() > 0) {
            this.config.put("persistence", persistence);
            log.info("Undo persistence module: {}", (Object)persistence);
        }
        String persistenceMode = (isStepSave = (Boolean)dict.get("cq.wcm.undo.persistence.mode")) != false ? "step" : "unload";
        this.config.put("persistenceMode", persistenceMode);
        log.info("Undo persistence mode: {}", (Object)persistenceMode);
        String markerMode = (String)dict.get("cq.wcm.undo.markermode");
        if (markerMode.length() > 0) {
            this.config.put("markerMode", markerMode);
            log.info("Undo marker mode: {}", (Object)markerMode);
        }
        String[] whitelist = OsgiUtil.toStringArray(dict.get("cq.wcm.undo.whitelist"));
        this.config.put("whitelisted", whitelist);
        log.info("Whitelisted components: {}", (Object)this.toString(whitelist));
        String[] blacklist = OsgiUtil.toStringArray(dict.get("cq.wcm.undo.blacklist"));
        this.config.put("blacklisted", blacklist);
        log.info("Blacklisted component oeprations: {}", (Object)this.toString(blacklist));
    }

    @Activate
    protected void activate(ComponentContext context) {
        this.configure(context);
    }

    @Modified
    protected void modified(ComponentContext context) {
        this.configure(context);
    }

    @Override
    public <T> T getConfig(String name, Class<T> type) {
        Object value = this.config.get(name);
        try {
            return (T)value;
        }
        catch (ClassCastException cce) {
            return null;
        }
    }

    @Override
    public void writeClientConfig(Writer out) throws IOException {
        try {
            String[] blacklist;
            String[] whitelist;
            JSONWriter json = new JSONWriter(out);
            json.object();
            json.key("enabled");
            json.value((Object)this.getConfig("enabled", Boolean.class));
            json.key("maxUndoSteps");
            json.value((Object)this.getConfig("steps", Integer.class));
            String persistenceMode = this.getConfig("persistence", String.class);
            if (persistenceMode != null) {
                json.key("persistence");
                json.value((Object)persistenceMode);
            }
            json.key("persistenceMode");
            json.value((Object)this.getConfig("persistenceMode", String.class));
            String markerMode = this.getConfig("markerMode", String.class);
            if (markerMode != null) {
                json.key("markerMode");
                json.value((Object)markerMode);
            }
            json.key("whitelist");
            json.array();
            for (String item : whitelist = this.getConfig("whitelisted", String[].class)) {
                json.value((Object)item);
            }
            json.endArray();
            json.key("blacklist");
            json.array();
            for (String item2 : blacklist = this.getConfig("blacklisted", String[].class)) {
                json.value((Object)item2);
            }
            json.endArray();
            json.endObject();
        }
        catch (JSONException je) {
            IOException ioe = new IOException("Could not create JavaScript representation");
            ioe.initCause((Throwable)je);
            throw ioe;
        }
    }
}