FileStoreBackupServiceImpl.java 6.04 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.crx.CRXRepository
 *  javax.jcr.RepositoryException
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.ReferencePolicyOption
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.oak.plugins.backup.FileStoreRestore
 *  org.apache.jackrabbit.oak.spi.state.NodeStore
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.crx.core.backup;

import com.day.crx.CRXRepository;
import com.day.crx.core.backup.FileStoreBackupService;
import com.day.crx.core.backup.crx.OakBackup;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.ReferencePolicyOption;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.oak.plugins.backup.FileStoreRestore;
import org.apache.jackrabbit.oak.spi.state.NodeStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={FileStoreBackupService.class})
public class FileStoreBackupServiceImpl
implements FileStoreBackupService {
    private static final Logger log = LoggerFactory.getLogger(FileStoreBackupServiceImpl.class);
    @Reference(policy=ReferencePolicy.STATIC, policyOption=ReferencePolicyOption.GREEDY)
    private NodeStore store;
    @Reference(policy=ReferencePolicy.STATIC, policyOption=ReferencePolicyOption.GREEDY)
    private CRXRepository repository;
    private int delay = 10;
    private OakBackup currentBackup;

    @Override
    public Boolean getBackupInProgress() {
        return this.isBackupInProgress();
    }

    synchronized boolean isBackupInProgress() {
        return this.currentBackup != null && !this.currentBackup.isFinished();
    }

    @Override
    public Integer getBackupProgress() {
        if (this.isBackupInProgress()) {
            return this.currentBackup.getProgress();
        }
        return 0;
    }

    @Override
    public String getCurrentBackupTarget() {
        if (this.isBackupInProgress()) {
            return this.currentBackup.getZipFile().getAbsolutePath();
        }
        return null;
    }

    @Override
    public Boolean getBackupWasSuccessful() {
        return this.currentBackup == null || this.currentBackup.getException() == null;
    }

    @Override
    public String getBackupResult() {
        return this.currentBackup == null ? "No backup excecuted so far." : this.currentBackup.getStatusMessage();
    }

    @Override
    public void startBackup(String target) throws RepositoryException {
        this.startBackup(null, target);
    }

    @Override
    public synchronized void startBackup(String install, String target) throws RepositoryException {
        if (this.isBackupInProgress()) {
            return;
        }
        File installDir = null;
        if (install == null || install.length() == 0) {
            installDir = this.repository.getHomeDir().getParentFile().getParentFile();
        } else {
            try {
                installDir = new File(install).getCanonicalFile();
            }
            catch (IOException e) {
                throw new RepositoryException("Unexpected repository directory: " + install, (Throwable)e);
            }
        }
        this.currentBackup = this.newBackup(installDir, target);
    }

    private OakBackup newBackup(File installDir, String target) {
        OakBackup b = new OakBackup(installDir, FileStoreBackupServiceImpl.targetToFile(target, installDir), this.store, this.repository);
        b.setDelay(this.delay);
        b.start();
        return b;
    }

    private static File targetToFile(String target, File installDir) {
        File targetFile;
        String targetFileName = target;
        if (targetFileName == null || targetFileName.length() == 0) {
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd-HHmm");
            targetFileName = "backup-" + df.format(new Date()) + ".zip";
        }
        if (!(targetFile = new File(targetFileName)).isAbsolute()) {
            File targetDir = installDir.getParentFile();
            targetFile = new File(targetDir, targetFileName);
        }
        return targetFile;
    }

    @Override
    public void cancelBackup() {
        if (this.isBackupInProgress()) {
            try {
                this.currentBackup.cancel();
            }
            catch (Exception e) {
                log.warn("Problem cancelling the backup process", (Throwable)e);
            }
        }
    }

    @Override
    public Integer getBackupDelay() {
        return this.delay;
    }

    @Override
    public void setBackupDelay(Integer delay) {
        this.delay = delay;
    }

    @Override
    public void nodeStoreRestore(String sourceDirectory) throws RepositoryException {
        try {
            long t = System.currentTimeMillis();
            FileStoreRestore.restore((File)new File(sourceDirectory), (NodeStore)this.store);
            log.info("Restore from {} finished in {} ms.", (Object)sourceDirectory, (Object)(System.currentTimeMillis() - t));
        }
        catch (Exception e) {
            throw new RepositoryException(e.getMessage(), (Throwable)e);
        }
    }

    @Override
    public String checkpoint(long lifetime) {
        return this.store.checkpoint(lifetime);
    }

    protected void bindStore(NodeStore nodeStore) {
        this.store = nodeStore;
    }

    protected void unbindStore(NodeStore nodeStore) {
        if (this.store == nodeStore) {
            this.store = null;
        }
    }

    protected void bindRepository(CRXRepository cRXRepository) {
        this.repository = cRXRepository;
    }

    protected void unbindRepository(CRXRepository cRXRepository) {
        if (this.repository == cRXRepository) {
            this.repository = null;
        }
    }
}