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

org.apache.sling.settings.impl.SlingOptionsFileWriter 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 org.apache.sling.settings.impl;

import org.apache.sling.settings.impl.RunModes.Group;
import org.apache.sling.settings.impl.SlingSettingsServiceImpl.Options;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import static java.util.Objects.requireNonNull;

public class SlingOptionsFileWriter {
    private final SlingOptionsFileReader reader;

    public SlingOptionsFileWriter(final SlingOptionsFileReader reader) {
        this.reader = requireNonNull(reader);
    }

    public void writeOptions(final RunModes runModes, final File file) throws SlingOptionsWriteException {
        requireNonNull(runModes);
        requireNonNull(file);
        final List slingInternalOptions = getSlingInternalOptions(runModes);
        writeOptions(slingInternalOptions, file);
        verifyWrite(runModes, file);
    }

    private void verifyWrite(final RunModes runModes, final File file) {
        final SlingOptionsReadResult slingOptionsReadResult = reader.readOptions(file);
        if (!slingOptionsReadResult.isSuccessful()) {
            throw new SlingOptionsWriteVerifyException(String.format("Could not verify write to: %s", file.getAbsolutePath()));
        }
        final RunModes actualRunModes = slingOptionsReadResult.getRunModes();
        if (!actualRunModes.equals(runModes)) {
            throw new IllegalStateException(
                    String.format("The contents of run modes before write (%s) and after file (%s) differs.",
                            runModes, actualRunModes));
        }
    }

    private void writeOptions(final List optionsList, final File file) throws SlingOptionsWriteException {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream(file);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(optionsList);
        } catch (final IOException ioe) {
            throw new SlingOptionsWriteException("Unable to write to options data file.", ioe);
        } finally {
            closeQuietly(fos, oos);
        }
    }

    private void closeQuietly(final FileOutputStream fos, final ObjectOutputStream oos) {
        if ( oos != null ) {
            try { oos.close(); } catch ( final IOException ignore) {}
        }
        if ( fos != null ) {
            try { fos.close(); } catch ( final IOException ignore) {}
        }
    }

    private List getSlingInternalOptions(final RunModes runModes) {
        final List optionsList = new ArrayList<>();
        for (Group group : runModes.getGroups()) {
            Options options = new Options();
            options.modes = group.getAvailableModes().toArray(new String[0]);
            options.selected = group.getSelectedMode();
            optionsList.add(options);
        }
        return optionsList;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy