Package biz.massivedynamics.sourcery.management

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

/*
* 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 java.io.*;
import org.yaml.snakeyaml.Yaml;

/**
* A simple Yaml resource manager
*
* @param <T> The type of resource to load
* @author Cruz Bishop
* @version 1.0.0.0
*/
public abstract class SimpleYamlResourceManager<T> extends FileResourceManager<T> {

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

    /**
     * Loads the resource
     *
     * @return The resource
     * @since 1.0.0.0
     */
    @Override
    public T 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 T data = (T) rawYaml.load(stream);
           
            //And return the data
            return 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 T raw) {
        //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(raw, 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(raw);
                }
                //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!
            }
        }
    }
}
TOP

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

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.