001    /*
002        Copyright (C) 2003 Adam Olsen
003    
004            This program is free software; you can redistribute it and/or modify
005        it under the terms of the GNU General Public License as published by
006        the Free Software Foundation; either version 1, or (at your option)
007        any later version.
008    
009        This program is distributed in the hope that it will be useful,
010        but WITHOUT ANY WARRANTY; without even the implied warranty of
011        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012        GNU General Public License for more details.
013    
014        You should have received a copy of the GNU General Public License
015        along with this program; if not, write to the Free Software
016        Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017    */
018    
019    package com.valhalla.settings;
020    
021    import java.io.*;
022    import java.util.*;
023    
024    /**
025     * A singleton Properties class to access and save settings
026     *
027     * @author Adam Olsen
028     * @version 1.0
029    **/
030    public class Settings extends Properties
031    {
032            private static Settings instance;
033            private File settingsDir;
034            private File settingsFile;
035            private java.util.Timer writeTimer = new java.util.Timer();
036    
037            /**
038             * Reads the settings file and sets up the singleton
039             *
040             * @param dir the settings directory
041             * @param settingsFile the settings file
042            **/
043            public static void loadSettings( String dir, String settingsFile )
044            {
045                    instance = new Settings( dir, settingsFile );
046            }
047    
048            /**
049             * Gets the Settings instance
050             * @return the Settings singleton
051            */
052            public static Settings getInstance() { return instance; }
053    
054            /**
055             * Gets the settings directory
056             * @return the settings directory
057            **/
058            public File getSettingsDir() { return settingsDir; }
059    
060            /**
061             * Gets the settings file
062             *
063             * @return the settings file
064            **/
065            public File getSettingsFile() { return settingsFile; }
066    
067            /**
068             * Private constructor
069             *
070             * @param dir the settings directory
071             * @param settingsFile the settings file
072            **/
073            private Settings( String dir, String settingsFile )
074            {
075                    instance = this;
076                    settingsDir = new File( dir );
077                    this.settingsFile = new File( dir, settingsFile );
078    
079                    // make sure the settings directory and file is there
080                    if( !settingsDir.isDirectory() || !this.settingsFile.isFile() ) createDefaultSettings();
081    
082                    try {
083                            // load the file into the properties
084                            InputStream stream = new FileInputStream( this.settingsFile );
085                            load( stream );
086                            stream.close();
087    
088                            writeTimer.schedule( new TimerTask()
089                            {
090                                    public void run() { writeSettings(); }
091                            }, 10000, 10000 );
092                    }
093                    catch( Exception e )
094                    {
095                            com.valhalla.Logger.debug( "Could not load settings file" );
096                            com.valhalla.Logger.debug( e.getMessage() );
097                    }
098            }
099    
100            /**
101             * Gets a boolean value
102             * @param key the key to get a boolean for
103             * @return a boolean based on the key
104            **/
105            public boolean getBoolean( String key )
106            {
107                    return ( getProperty( key  ) != null );
108            }
109    
110            /**
111             * Sets a boolean
112             * @param key the key to set
113             * @param value the value to set the key to
114            **/
115            public void setBoolean( String key, boolean value )
116            {
117                    if( value ) setProperty( key, "true" );
118                    else remove( key );
119            }
120    
121            /**
122             * Sets the settings
123             * @param settings the settings to set it to
124            **/
125            public static void setSettings( Properties settings )
126            {
127                    if( instance != null ) instance = (Settings)settings;
128            }
129    
130            /**
131             * Writes the settings to the settings file
132            **/
133            public static void writeSettings()
134            {
135                    if( instance == null )
136                    {
137                            com.valhalla.Logger.debug( "Fatal Error: Settings has not been initiated, and an attempt to access a value was made. Potential NullPointerException." );
138                            com.valhalla.Logger.closeLog();
139                            System.exit( 1 );
140                    }
141    
142                    try {
143                            // create the output stream
144                            FileOutputStream stream = new FileOutputStream( instance.settingsFile );
145                            StringBuffer buf = new StringBuffer();
146    
147                            // store the properties file
148                            instance.store( stream, "JBother Settings File" );
149                    }
150                    catch( IOException e )
151                    {
152                            com.valhalla.Logger.debug( "There was an error saving your settings." );
153                            com.valhalla.Logger.debug( e.toString() );
154                    }
155            }
156    
157            /**
158             * If a settings file is not found, a default settings file is copied in to place.
159            **/
160            private void createDefaultSettings()
161            {
162                    if( !settingsDir.isDirectory() && !settingsDir.mkdirs() )
163                    {
164                            //creating the settings directory failed....
165                            com.valhalla.Logger.debug( "Fatal Error: Could not create the settings directory (" + settingsDir.getName() + ")." );
166                            com.valhalla.Logger.closeLog();
167                    }
168    
169                    try {
170    
171                            // copy the default file in to place
172                            InputStream stream = instance.getClass().getClassLoader().getResourceAsStream( "defaultsettings.properties" );
173                            if( stream == null ) throw new IOException();
174    
175                            BufferedReader in = new BufferedReader( new InputStreamReader( stream ) );
176    
177                            PrintWriter out = new PrintWriter( new FileWriter( settingsFile ) );
178                            String line = null;
179    
180                            while( ( line = in.readLine() ) != null )
181                            {
182                                    out.println( line );
183                            }
184    
185                            in.close();
186                            out.close();
187    
188                    }
189                    catch( IOException ex ) {
190                            //creating an initial settings file failed...
191                            com.valhalla.Logger.debug( "Fatal Error: Could not create the settings file (" +
192                                    settingsFile.getPath() + ")\n" + ex.getMessage() );
193                            com.valhalla.Logger.closeLog();
194                            System.exit( 1 );
195                    }
196            }
197    }