Package biz.massivedynamics.sourcery.management

Source Code of biz.massivedynamics.sourcery.management.ComplexYamlResourceManager

/*
* The MIT License
*
* Copyright 2012 Massive Dynamics.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package biz.massivedynamics.sourcery.management;

import biz.massivedynamics.sourcery.resource.Resource;
import java.io.*;
import org.yaml.snakeyaml.Yaml;

/**
* A complex Yaml resource manager
*
* @param <RAW> The type of resource to load
* @param <FINAL> The type of resource to represent
* @author Cruz Bishop
* @version 1.0.0.0
*/
public abstract class ComplexYamlResourceManager<RAW, FINAL extends Resource> extends FileResourceManager<FINAL> {

    /**
     * Creates a new complex Yaml resource manager
     *
     * @param file The file to use
     * @since 1.0.0.0
     */
    public ComplexYamlResourceManager(final File file) {
        super(file);
    }

    /**
     * Loads the resource
     *
     * @return The resource
     * @since 1.0.0.0
     */
    @Override
    public FINAL load() {
        //Get the YAML loader
        final Yaml rawYaml = new Yaml();
        //Get a file input stream
        FileInputStream stream = null;
        try {
            //Set up the stream
            stream = new FileInputStream(this.getFile());
            //Load the raw data
            final RAW data = (RAW) rawYaml.load(stream);
           
            //And return the data
            return toFinal(data);
        } catch (FileNotFoundException ex) {
            //Save default values
            this.save(this.getDefault());
            //Load the default values
            return this.getDefault();
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
            } catch (IOException ex) {
                //Couldn't close the stream for some reason
            }
        }
    }
   
    @Override
    public void save(final FINAL data) {
        //Get the YAML loader
        final Yaml rawYaml = new Yaml();
        //And a file writer
        FileWriter writer = null;
        try {
            //Set up the file writer
            writer = new FileWriter(this.getFile());
            //And save it
            rawYaml.dump(data.getRaw(), writer);
        } catch (FileNotFoundException ex) {
            try {
                //Maky any parent directories
                this.getFile().getParentFile().mkdirs();
                //See if we can create a file
                if (this.getFile().createNewFile()) {
                    //Save it
                    this.save(data);
                }
                //Couldn't create the file! Nothing will be saved.
            } catch (IOException ex1) {
                //You don't want to know
            }
        } catch (IOException ex) {
            //No saving for you!         
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException ex) {
                //Seriously, this will NEVER happen unless you break something on purpose!
            }
        }
    }
   
    /**
     * Converts the RAW resource to a FINAL resource
     *
     * @param raw The RAW resource
     * @return The FINAL resource
     * @since 1.0.0.0
     */
    public abstract FINAL toFinal(RAW raw);
}
TOP

Related Classes of biz.massivedynamics.sourcery.management.ComplexYamlResourceManager

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.