Package ds.pjftp.settings.storage.impl

Source Code of ds.pjftp.settings.storage.impl.XMLSettingsStorage

/*
* pjftp FTP server.
* Copyright (C) 2012 Dmitriy Simbiriatin <dmitriy.simbiriatin@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package ds.pjftp.settings.storage.impl;

import ds.pjftp.utils.NotNull;

import ds.pjftp.settings.ServerSettings;
import ds.pjftp.settings.storage.SettingsStorage;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.charset.Charset;

import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.WRITE;

/**
* Implementation of {@link SettingsStorage} class.
* This class stores server settings to XML
* configuration file.
*/
public class XMLSettingsStorage extends SettingsStorage {

    /**
     * Default configuration encoding.
     */
    public static final String ENCODING = "UTF-8";

    /**
     * Default settings file name.
     */
    public static final String SETTINGS_FILE = "settings.xml";

    /**
     * XML file where the settings will be stored.
     */
    private final Path settingsFile;

    /**
     * Constructs XMLSettingsStorage object.
     */
    public XMLSettingsStorage() {
        settingsFile = Paths.get(SETTINGS_FILE);
    }

    /**
     * Loads server settings from XML file.
     * @return server settings.
     * @throws IOException if failed to load the settings.
     */
    @Override
    public ServerSettings load() throws IOException {

        ServerSettings settings = null;

        try (BufferedReader reader = Files.newBufferedReader(
                settingsFile, Charset.forName(ENCODING)))
        {
            final Unmarshaller unmarshaller = JAXBContext.newInstance(
                    ServerSettings.class).createUnmarshaller();

            settings = (ServerSettings) unmarshaller.unmarshal(reader);

        } catch (JAXBException ex) {
            throw new IOException(ex);
        }
        return settings;
    }

    /**
     * Saves server settings to XML file.
     * @param settings server settings to be saved.
     * @throws IOException if failed to save the settings.
     */
    @Override
    public void save(@NotNull final ServerSettings settings) throws IOException {

        try (BufferedWriter writer = Files.newBufferedWriter(settingsFile,
                Charset.forName(ENCODING), WRITE, CREATE))
        {
            final Marshaller marshaller = JAXBContext.newInstance(
                    ServerSettings.class).createMarshaller();

            marshaller.setProperty("jaxb.formatted.output", true);
            marshaller.marshal(settings, writer);

        } catch (JAXBException ex) {
            throw new IOException(ex);
        }
    }
}
TOP

Related Classes of ds.pjftp.settings.storage.impl.XMLSettingsStorage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.