BackupConfigImpl.java 5.88 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  javax.jcr.ValueFormatException
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Service
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.upgrades.backup.impl;

import com.adobe.cq.upgrades.backup.BackupConfig;
import com.day.cq.commons.jcr.JcrUtil;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service
public class BackupConfigImpl
implements BackupConfig {
    private final Logger log;
    private Set<String> pathsToBackup;
    private Set<String> pathsToOverwrite;
    static final int CONFIG_VERSION_NUMBER = 61000;
    static final String CONFIG_BACKUP_PATH = "/var/upgrade/PreUpgradeBackup/configuration" + "-old-" + 61000;
    static final String DEFAULT_CONFIG_BACKUP_PATH = "/var/upgrade/PreUpgradeBackup" + "/default-config-" + 61000;

    public BackupConfigImpl() {
        this.log = LoggerFactory.getLogger(this.getClass());
    }

    @Override
    public synchronized void readConfig(Session s) throws RepositoryException {
        this.pathsToBackup = new HashSet<String>();
        this.pathsToOverwrite = new HashSet<String>();
        String configSavePath = "/var/upgrade/PreUpgradeBackup/configuration";
        boolean isAlreadySaved = false;
        if (s.nodeExists("/var/upgrade/PreUpgradeBackup/configuration")) {
            Node configRoot = s.getNode("/var/upgrade/PreUpgradeBackup/configuration");
            try {
                if (configRoot.hasProperty("ignoreDefaultConfig") && configRoot.getProperty("ignoreDefaultConfig").getBoolean()) {
                    configSavePath = DEFAULT_CONFIG_BACKUP_PATH;
                    if (s.itemExists(configSavePath)) {
                        isAlreadySaved = true;
                    }
                } else if (s.itemExists(CONFIG_BACKUP_PATH)) {
                    isAlreadySaved = true;
                } else {
                    this.backupConfig(s, CONFIG_BACKUP_PATH);
                }
            }
            catch (ValueFormatException ex) {
                this.log.warn("Found property {} but could not cast to boolean.", (Object)"ignoreDefaultConfig");
            }
        }
        if (!isAlreadySaved) {
            this.createDefaultConfig(s, configSavePath);
        }
        if (!s.nodeExists("/var/upgrade/PreUpgradeBackup/configuration")) {
            throw new IllegalStateException("Config node not found: /var/upgrade/PreUpgradeBackup/configuration");
        }
        this.log.info("Only paths found under {} will be backed up", (Object)"/var/upgrade/PreUpgradeBackup/configuration");
        Node n = s.getNode("/var/upgrade/PreUpgradeBackup/configuration");
        this.readConfig(n, n);
    }

    private static String configPath(String rootPath, String path) {
        return rootPath + path;
    }

    private void createDefaultConfig(Session s, String configSavePath) throws RepositoryException {
        this.log.info("Creating default configuration at {}", (Object)configSavePath);
        JcrUtil.createPath(BackupConfigImpl.configPath(configSavePath, "/etc/tags"), "nt:unstructured", s);
        JcrUtil.createPath(BackupConfigImpl.configPath(configSavePath, "/etc/segmentation"), "nt:unstructured", s);
        JcrUtil.createPath(BackupConfigImpl.configPath(configSavePath, "/etc/cloudsettings/default"), "nt:unstructured", s);
        Node cs = JcrUtil.createPath(BackupConfigImpl.configPath(configSavePath, "/etc/cloudservices"), "nt:unstructured", s);
        cs.setProperty("overwritePaths", new String[]{"/etc/cloudservices/proxy"});
        s.save();
    }

    private void backupConfig(Session s, String destinationPath) throws RepositoryException {
        s.move("/var/upgrade/PreUpgradeBackup/configuration", destinationPath);
        s.save();
    }

    @Override
    public Set<String> getPathsToBackup() {
        return Collections.unmodifiableSet(this.pathsToBackup);
    }

    @Override
    public Set<String> getPathsToOverwrite() {
        return Collections.unmodifiableSet(this.pathsToOverwrite);
    }

    private void readConfig(Node base, Node current) throws RepositoryException {
        if (current.hasNodes()) {
            NodeIterator it = current.getNodes();
            while (it.hasNext()) {
                this.readConfig(base, it.nextNode());
            }
        } else {
            String path = current.getPath().substring(base.getPath().length());
            if (path.length() == 0) {
                return;
            }
            this.pathsToBackup.add(path);
            this.log.info("{} added to paths to backup due to config node {}", (Object)path, (Object)current.getPath());
            if (current.hasProperty("overwritePaths")) {
                Property p = current.getProperty("overwritePaths");
                if (p.isMultiple()) {
                    Value[] vs;
                    for (Value v : vs = p.getValues()) {
                        String s = v.getString();
                        this.pathsToOverwrite.add(s);
                        this.log.info("path to overwrite '{}' set for {}", (Object)s, (Object)path);
                    }
                } else {
                    String s = p.getValue().getString();
                    this.pathsToOverwrite.add(s);
                    this.log.info("path to overwrite '{}' set for {}", (Object)s, (Object)path);
                }
            }
        }
    }
}