All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.granite.crx2oak.util.FilesystemOps Maven / Gradle / Ivy

The newest version!
/*************************************************************************
 * ADOBE CONFIDENTIAL
 * ___________________
 *
 * Copyright 2016 Adobe
 * All Rights Reserved.
 *
 * NOTICE: All information contained herein is, and remains
 * the property of Adobe and its suppliers, if any. The intellectual
 * and technical concepts contained herein are proprietary to Adobe
 * and its suppliers and are protected by all applicable intellectual
 * property laws, including trade secret and copyright laws.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe.
 **************************************************************************/
package com.adobe.granite.crx2oak.util;

import com.adobe.granite.crx2oak.pipeline.InputAggregatingComponent;
import com.adobe.granite.crx2oak.pipeline.PipeData;
import com.adobe.granite.crx2oak.pipeline.Pipeline;
import com.adobe.granite.crx2oak.pipeline.PipelineComponent;
import com.adobe.granite.crx2oak.pipeline.PipelineExecutor;
import com.adobe.granite.crx2oak.util.filesystem.DirectoryActivator;
import com.adobe.granite.crx2oak.util.filesystem.DirectoryElementsSwitcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

public class FilesystemOps {


    /**
     * Create processor that archive the current blessed directory into backup directory (in the same path)
     * and activates a candidate directory. The backup directory is created with partial suffix
     * and with current date.
     *
     * @param blessedDirectory the blessed directory that content will be swapped
     * @param candidateDirectory the candidate directory which content will be moved to blessed directory
     * @param backupSuffix the backup suffix that will be used for the old blessed directory
     * @return the class that is able to activate candidate into a blessed directory
     * @throws IOException when the activate directory processor cannot be created
     */
    public PipelineComponent createDirectoryActivator(final File blessedDirectory,
                                                      final File candidateDirectory,
                                                      final String backupSuffix) throws IOException {
        return new DirectoryActivator(blessedDirectory, candidateDirectory, backupSuffix);
    }

    /**
     * Create processor that creates a directory but does not invoke processing on it. If directory cannot
     * be created, when processing is invoked, then that fact is ignored silently.
     *
     * @param directoryToCreate a directory that will be created when processor will be invoke.
     * @return a non-null instance of the processor that creates directory when invoked
     * @throws IOException when for given directory a canonical path cannot be determined
     */
    public PipelineComponent createUnsafeDirectoryCreator(final File directoryToCreate) throws IOException {
        final File aCanonicalFile = directoryToCreate.getCanonicalFile();
        return new PipelineExecutor() {
            @Override
            public void run() {
                //noinspection ResultOfMethodCallIgnored
                aCanonicalFile.mkdirs();
            }
        };
    }

    /**
     * Create processor that creates a directory but does not invoke processing on it. If directory cannot
     * be created, when processing is invoked, the the processing is aborted.
     *
     * @param directoryToCreate a directory that will be created when processor will be invoke.
     * @return a non-null instance of the processor that creates directory when invoked
     * @throws IOException when for given directory a canonical path cannot be determined
     */
    public PipelineComponent createSafeDirectoryCreator(final File directoryToCreate) throws IOException {
        final File canonicalDirectoryToCreate = directoryToCreate.getCanonicalFile();
        return new InputAggregatingComponent() {
            private final String LOGGER_NAME = FilesystemOps.class.getCanonicalName()
                    + ".SafeDirectoryCreatorProcessor";
            private final Logger log = LoggerFactory.getLogger(LOGGER_NAME);

            @Override
            protected PipeData preprocess(final PipeData input) {
                if (!canonicalDirectoryToCreate.exists() || !canonicalDirectoryToCreate.isDirectory()) {
                    //noinspection ResultOfMethodCallIgnored
                    canonicalDirectoryToCreate.mkdirs();
                    if (!canonicalDirectoryToCreate.exists() || !canonicalDirectoryToCreate.isDirectory()) {
                        log.error("Cannot create directory: {}. Aborting processing", canonicalDirectoryToCreate);
                        return Pipeline.streamClosed();
                    }
                }
                return PipeData.EMPTY;
            }
        };
    }

    /**
     * Create processor that activates/switches elements (based on names) under a candidate directory to
     * a current blessed directory leaving the other items of the blessed directory untouched. Elements with the same
     * names are archived (moved) to backup directory created in the same directory as blessed one.
     *
     * @param blessedDirectory the blessed directory that content will be swapped
     * @param candidateDirectory the candidate directory which content will be moved to blessed directory
     * @param backupSuffix the backup suffix that will be used for the old blessed directory
     * @return the runtime processor that is able to activate candidate into a blessed directory
     * @throws IOException when the activate directory processor cannot be created
     */
    public PipelineComponent createDirectoryElementsSwitcher(final File blessedDirectory,
                                                             final File candidateDirectory,
                                                             final String backupSuffix) throws IOException {
        return new DirectoryElementsSwitcher(blessedDirectory, candidateDirectory, backupSuffix);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy