~massive-dynamics-staff/sourcery/1.0

« back to all changes in this revision

Viewing changes to Sourcery/src/main/java/biz/massivedynamics/sourcery/management/SimpleYamlResourceManager.java

  • Committer: Cruz Bishop
  • Date: 2012-02-07 01:26:00 UTC
  • Revision ID: cruzjbishop@gmail.com-20120207012600-920tcjpi1idbqbdj
Tags: 1.0.0.0-ALPHA
Sourcery 1.0.0.0-ALPHA

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The MIT License
 
3
 *
 
4
 * Copyright 2012 Massive Dynamics.
 
5
 *
 
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 
7
 * of this software and associated documentation files (the "Software"), to deal
 
8
 * in the Software without restriction, including without limitation the rights
 
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
10
 * copies of the Software, and to permit persons to whom the Software is
 
11
 * furnished to do so, subject to the following conditions:
 
12
 *
 
13
 * The above copyright notice and this permission notice shall be included in
 
14
 * all copies or substantial portions of the Software.
 
15
 *
 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
22
 * THE SOFTWARE.
 
23
 */
 
24
package biz.massivedynamics.sourcery.management;
 
25
 
 
26
import java.io.*;
 
27
import org.yaml.snakeyaml.Yaml;
 
28
 
 
29
/**
 
30
 * A simple Yaml resource manager
 
31
 *
 
32
 * @param <T> The type of resource to load
 
33
 * @author Cruz Bishop
 
34
 * @version 1.0.0.0
 
35
 */
 
36
public abstract class SimpleYamlResourceManager<T> extends FileResourceManager<T> {
 
37
 
 
38
    /**
 
39
     * Creates a new simple Yaml resource manager
 
40
     *
 
41
     * @param file The file to use
 
42
     * @since 1.0.0.0
 
43
     */
 
44
    public SimpleYamlResourceManager(final File file) {
 
45
        super(file);
 
46
    }
 
47
 
 
48
    /**
 
49
     * Loads the resource
 
50
     * 
 
51
     * @return The resource
 
52
     * @since 1.0.0.0
 
53
     */
 
54
    @Override
 
55
    public T load() {
 
56
        //Get the YAML loader
 
57
        final Yaml rawYaml = new Yaml();
 
58
        //Get a file input stream
 
59
        FileInputStream stream = null;
 
60
        try {
 
61
            //Set up the stream
 
62
            stream = new FileInputStream(this.getFile());
 
63
            //Load the raw data
 
64
            final T data = (T) rawYaml.load(stream);
 
65
            //And return the data
 
66
            return data;
 
67
        } catch (FileNotFoundException ex) {
 
68
            //Save default values
 
69
            this.save(this.getDefault());
 
70
            //Load the default values
 
71
            return this.getDefault();
 
72
        } finally {
 
73
            try {
 
74
                if (stream != null) {
 
75
                    stream.close();
 
76
                }
 
77
            } catch (IOException ex) {
 
78
                //Couldn't close the stream for some reason
 
79
            }
 
80
        }
 
81
    }
 
82
    
 
83
    @Override
 
84
    public void save(final T raw) {
 
85
        //Get the YAML loader
 
86
        final Yaml rawYaml = new Yaml();
 
87
        //And a file writer
 
88
        FileWriter writer = null;
 
89
        try {
 
90
            //Set up the file writer
 
91
            writer = new FileWriter(this.getFile());
 
92
            //And save it
 
93
            rawYaml.dump(raw, writer);
 
94
        } catch (FileNotFoundException ex) {
 
95
            try {
 
96
                //Maky any parent directories
 
97
                this.getFile().getParentFile().mkdirs();
 
98
                //See if we can create a file
 
99
                if (this.getFile().createNewFile()) {
 
100
                    //Save it
 
101
                    this.save(raw);
 
102
                }
 
103
                //Couldn't create the file! Nothing will be saved.
 
104
            } catch (IOException ex1) {
 
105
                //You don't want to know
 
106
            }
 
107
        } catch (IOException ex) {
 
108
            //No saving for you!          
 
109
        } finally {
 
110
            try {
 
111
                if (writer != null) {
 
112
                    writer.close();
 
113
                }
 
114
            } catch (IOException ex) {
 
115
                //Seriously, this will NEVER happen unless you break something on purpose!
 
116
            }
 
117
        }
 
118
    }
 
119
}