view src/org/tn5250j/GlobalConfigure.java @ 70:c56032728742 tn5250

use 5250 encryption config entry
author Carl Byington <carl@five-ten-sg.com>
date Fri, 13 Jun 2014 19:07:34 -0700
parents b29b39f386a4
children 8f23b05a51f7
line wrap: on
line source

/**
 * Title: GlobalConfigure.java
 * Copyright:   Copyright (c) 2001, 2002, 2003
 * Company:
 * @author  Kenneth J. Pouncey
 * @version 0.1
 *
 * Description:
 *
 * 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, 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 software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 *
 */
package org.tn5250j;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.Properties;

import com.five_ten_sg.connectbot.TerminalView;
import org.tn5250j.interfaces.ConfigureFactory;
import android.util.Log;


/**
 * Utility class for referencing global settings and functions of which at most
 * one instance can exist per VM.
 *
 * Use GlobalConfigure.instance() to access this instance.
 */
public class GlobalConfigure extends ConfigureFactory {
    private static final String TAG = "GlobalConfigure";
    public static final String TN5250J_FOLDER = ".tn5250j";

    /**
     * A handle to the unique GlobalConfigure class
     */
    static private GlobalConfigure _instance;

    /**
     * A handle to the the Global Properties
     */
    static private Properties settings;

    static private Hashtable registry = new Hashtable();
    static private Hashtable headers = new Hashtable();  //LUC GORRENS


    /**
     * The constructor is made protected to allow overriding.
     */
    public GlobalConfigure() {
        if (_instance == null) {
            // initialize the settings information
            initialize();
            // set our instance to this one.
            _instance = this;
        }
    }

    /**
     *
     * @return The unique instance of this class.
     */
    static public GlobalConfigure instance() {

        if (_instance == null) {
            _instance = new GlobalConfigure();
        }
        return _instance;

    }

    /**
     * Initialize the properties registry for use later.
     *
     */
    private void initialize() {
        verifySettingsFolder();
        loadSettings();
        loadSessions();
        loadMacros();
        loadKeyStrokes();
    }

    /**
     * check if folder %USERPROFILE%/.tn5250j exists
     * and create if necessary
     */
    private void verifySettingsFolder() {
        final String settingsfolder = settingsFolder();
        final File f = new File (settingsfolder);
        if (!f.exists()) {
            try {
                Log.i(TAG,"Settings folder '"+settingsfolder+"' doesn't exist. Will created now.");
                f.mkdir();
            } catch (Exception e) {
                Log.w(TAG,"Couldn't create settings folder '"+settingsfolder+"'", e);
            }
        }
    }

    /**
     * Load the sessions properties
     */
    private void loadSessions() {

        setProperties(SESSIONS,SESSIONS,"------ Sessions --------",true);
    }

    /**
     * Load the macros
     */
    private void loadMacros() {

        setProperties(MACROS,MACROS,"------ Macros --------",true);

    }

    private void loadKeyStrokes() {

        setProperties(KEYMAP,KEYMAP,
                "------ Key Map key=keycode,isShiftDown,isControlDown,isAltDown,isAltGrDown --------",
                true);

    }

    /**
     * Reload the environment settings.
     */
    @Override
    public void reloadSettings() {
        Log.i(TAG,"reloading settings");
        loadSettings();
        loadSessions();
        loadMacros();
        loadKeyStrokes();
        Log.i(TAG,"Done (reloading settings).");
    }

    /**
     * Loads the emulator setting from the setting(s) file
     */
    private void loadSettings() {

        FileInputStream in = null;
        settings = new Properties();

        try {
            in = new FileInputStream(settingsFile());
            settings.load(in);
        }
        catch (FileNotFoundException fnfea) {
            Log.i(TAG," Information Message: "
                    + fnfea.getMessage() + ".  The file " + settingsFile()
                    + " will be created for first time use.");
            saveSettings();
        }
        catch (IOException ioea) {
            Log.w(TAG,"IO Exception accessing File "
                    + settingsFile() + " for the following reason : "
                    + ioea.getMessage());
        }
        catch (SecurityException sea) {
            Log.w(TAG,"Security Exception for file "
                    + settingsFile() + "  This file can not be "
                    + "accessed because : " + sea.getMessage());
        }
    }

    /**
     * Save the settings for the global configuration
     */
    @Override
    public void saveSettings() {

        try {
            FileOutputStream out = new FileOutputStream(settingsFile());
            settings.store(out,"----------------- tn5250j Global Settings --------------");
        }
        catch (FileNotFoundException fnfe) {}
        catch (IOException ioe) {}
    }

    /**
     * Save the setting in the registry using the key passed in with no header
     * output.
     *
     * @param regKey
     */
    @Override
    public void saveSettings(String regKey) {

        saveSettings(regKey,"");
    }

    /**
     * Save the settings in the registry using the key passed with a header
     * in the output.
     *
     * @param regKey
     * @param Header
     */
    @Override
    public void saveSettings(String regKey, String header) {

        saveSettings(regKey,regKey,header);
    }

    /**
     * Save the settings in the registry using the key passed with a header
     * in the output.
     *
     * @param regKey
     * @param Header
     */
    @Override
    public void saveSettings(String regKey, String fileName, String header) {

        if (registry.containsKey(regKey)) {
            try {
                FileOutputStream out = new FileOutputStream(
                        settingsDirectory() + fileName);
                Properties props = (Properties)registry.get(regKey);
                props.store(out,header);
                out.flush();
                out.close();
            }
            catch (FileNotFoundException fnfe) {
                Log.w(TAG,"File not found : writing file "
                        + fileName + ".  Description of error is "
                        + fnfe.getMessage());
            }
            catch (IOException ioe) {
                Log.w(TAG,"IO Exception : writing file "
                        + fileName + ".  Description of error is "
                        + ioe.getMessage());
            }
            catch (SecurityException se) {
                Log.w(TAG,"Security Exception : writing file "
                        + fileName + ".  Description of error is "
                        + se.getMessage());
            }

        }

    }

    /**
     * Place the Properties in the registry under a given registry name
     *
     * @param regKey
     * @param regProps
     */
    @Override
    public void setProperties(String regKey, Properties regProps) {

        registry.put(regKey, regProps);

    }

    /**
     * Set the properties for the given registry key.
     *
     * @param regKey
     * @param fileName
     * @param header
     */
    @Override
    public void setProperties(String regKey,  String fileName, String  header) {  //LG NEW
        setProperties(regKey, fileName, header, false);
    }

    /**
     * Set the properties for the given registry key.
     *
     * @param regKey
     * @param fileName
     * @param header
     * @param createFile
     */
    @Override
    public void setProperties(String regKey,  String fileName, String  header,
            boolean createFile) {

        FileInputStream in = null;
        Properties props = new Properties();
        headers.put(regKey, header);

        try {
            in = new FileInputStream(settingsDirectory() + fileName);
            props.load(in);

        }
        catch (FileNotFoundException fnfe) {

            if (createFile) {
                Log.i(TAG," Information Message: " + fnfe.getMessage()
                        + ".  The file " + fileName + " will"
                        + " be created for first time use.");

                saveSettings(regKey,header);

            }
            else {

                Log.i(TAG," Information Message: " + fnfe.getMessage()
                        + ".");

            }
        }
        catch (IOException ioe) {
            Log.w(TAG,"IO Exception accessing File "+ fileName +
                    " for the following reason : "
                    + ioe.getMessage());
        }
        catch (SecurityException se) {
            Log.w(TAG,"Security Exception for file "+ fileName
                    + ".  This file can not be accessed because : "
                    + se.getMessage());
        }

        registry.put(regKey,props);

    }

    /**
     * Returns the properties associated with a given registry key.
     *
     * @param regKey
     * @return
     */
    @Override
    public Properties getProperties(String regKey) {

        if (registry.containsKey(regKey)) {
            return (Properties)registry.get(regKey);
        }
        return null;
    }

    public Properties getProperties() {
        return settings;
    }

    @Override
    public Properties  getProperties(String regKey,String fileName) {
        return getProperties(regKey,fileName,false,"",false);
    }

    @Override
    public Properties  getProperties(String regKey,String fileName,
            boolean createFile, String header) {
        return getProperties(regKey,fileName,false,"",false);
    }

    @Override
    public Properties  getProperties(String regKey,
                                     String fileName,
                                     boolean createFile,
                                     String header,
                                     boolean reloadIfLoaded) {

        if (!registry.containsKey(regKey) || reloadIfLoaded) {

            FileInputStream in = null;
            Properties props = new Properties();
            headers.put(regKey, header);

            try {
                in = new FileInputStream(settingsDirectory()
                        +  fileName);
                props.load(in);

            }
            catch (FileNotFoundException fnfe) {

                if (createFile) {
                    Log.i(TAG," Information Message: " + fnfe.getMessage()
                            + ".  The file " + fileName + " will"
                            + " be created for first time use.");

                    registry.put(regKey,props);

                    saveSettings(regKey,header);

                    return props;

                }
                else {

                    Log.i(TAG," Information Message: " + fnfe.getMessage()
                            + ".");

                }
            }
            catch (IOException ioe) {
                Log.w(TAG,"IO Exception accessing File "+ fileName +
                        " for the following reason : "
                        + ioe.getMessage());
            }
            catch (SecurityException se) {
                Log.w(TAG,"Security Exception for file "+ fileName
                        + ".  This file can not be accessed because : "
                        + se.getMessage());
            }

            registry.put(regKey,props);

            return props;
        }
        else {
            return (Properties)registry.get(regKey);
        }
    }

    /**
     * Returns the setting from the given key of the global properties or the
     * default passed if the property does not exist.
     *
     * @param key
     * @param def
     * @return
     */
    @Override
    public String getProperty(String key, String def) {
        if (settings.containsKey(key))
            return settings.getProperty(key);
        else
            return def;
    }

    /**
     * Returns the setting from the given key of the global properties.
     *
     * @param key
     * @return
     */
    @Override
    public String getProperty(String key) {
        return settings.getProperty(key);
    }

    /**
     * Private helper to return the full path to the settings file
     *
     * @return
     */
    private String settingsFile() {
        return settingsDirectory() + "tn5250jstartup.cfg";

    }

    /**
     * Private helper to return the settings directory
     *
     * @return
     */
    private String settingsDirectory() {
        return settingsFolder() + File.separator;

    }

    /**
     * Private helper to return the settings folder path
     *
     * @return
     */
    private String settingsFolder() {
        return TerminalView.android_home_directory + File.separator + TN5250J_FOLDER;

    }

    /**
     * Not sure yet so be careful using this.
     *
     * @return
     */
    public ClassLoader getClassLoader() {

        ClassLoader loader = GlobalConfigure.class.getClassLoader();
        if (loader == null)
            loader = ClassLoader.getSystemClassLoader();

        return loader;
    }

}