Skip to content
CONFIG.java 4.47 KiB
Newer Older
xtof's avatar
xtof committed
package org.josast.property;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;

/**
xtof's avatar
xtof committed
 *
xtof's avatar
xtof committed
 * <b>Description : This class allows to save data in a configuration file.</b>
 * <br>
xtof's avatar
xtof committed
 * for the JOSAST project the org.josast.config.databinding is prefered to this
 * package for storing data.
 *
 * <p>
 * Projet : JOSAST <BR>
 *
xtof's avatar
xtof committed
 * <br>
xtof's avatar
xtof committed
 * <b>JOSAST</b> : Java Open Source Amateur Satellite Toolbox <br>
 * The aim of the project is to create a set of tools for amateur satellite
 * purpose. All this tools could be used together to create specific software.
 * <b>JOSAST</b> project is managed by AVMDTI
 * (<A HREF="http://www.avmdti.org">http://www.avmdti.org </a> ) <br>
 * This software is an open source software. Please read the <b><i>JOSAST
 * licence</b></i><BR>
 * (<A HREF="http://www.avmdti.org">http://www.avmdti.org </a> )
xtof's avatar
xtof committed
 * <p>
xtof's avatar
xtof committed
 * for more information contact
 * <a href="mailto:josast@avmdti.org">josast@avmdti.org</a>
 * </p>
 *
 * @author <a href="mailto:mercier.josast@avmdti.org">mercier</a>
 * @version 1.0
 *          <p>
 *          <b><i> Source Update </b></i>
 *          </p>
 *          <br>
 *          Version : date : name : comments <br>
 *          V1 : 3 mars 2004 : C. Mercier : create file <br>
 *          V2 : 01 mai 2019 : C. Mercier : Change log system
 *          <p>
xtof's avatar
xtof committed
 */
public class CONFIG {

xtof's avatar
xtof committed
    private static Logger log = Logger.getLogger("AmsatLogger");
    private Properties P = null;
    private String fileName = "configuration.ini";

    /** Constructeur priv� */
    private CONFIG() {
    }

    private String getName() {
        String path = System.getProperty("user.dir") + "/config";
        File apath = new File(path);

        if (!apath.exists()) {
            boolean res = apath.mkdir();
            if (!res) {
                log.severe("fail to create " + path);

            }
        }
        return path + "/" + fileName;
    }

xtof's avatar
xtof committed
    /** Holder */
xtof's avatar
xtof committed
    private static class SingletonHolder {
xtof's avatar
xtof committed
        /** Instance unique non pr�initialis�e */
xtof's avatar
xtof committed
        private static final  CONFIG instance = new CONFIG();
xtof's avatar
xtof committed
    }
xtof's avatar
xtof committed

xtof's avatar
xtof committed
    /** Point d'acc�s pour l'instance unique du singleton */
xtof's avatar
xtof committed
    public static CONFIG getInstance() {
xtof's avatar
xtof committed
        return SingletonHolder.instance;
    }
xtof's avatar
xtof committed

    public String GetProperty(final String Item) {

        if (P == null) {
            try {
                P = new Properties();
                FileInputStream in = new FileInputStream(getName());
                P.load(in);
                in.close();
            } catch (Exception e) {
                log.severe("erreur ouverture fichier de configuration"); //$NON-NLS-1$
                return null;
            }
        }
        String S = P.getProperty(Item);
        if (S == null) {
            return null;
        } else {
            return S.trim(); // $NON-NLS-1$
        }
    }

    /**
     * Set a property into the file
     *
     *
     * @param Item   Item to store
     * @param Valeur value to store
     *
     */
    public void SetProperty(final String Item, final String Valeur) {

        if (P == null) {

            P = new Properties();
            try {

                FileInputStream in = new FileInputStream(getName());
                P.load(in);
                in.close();
            } catch (Exception e2) {
                log.severe("Erreur de ouverture du fichier de configuration"); //$NON-NLS-1$
                e2.printStackTrace();
            }
        }

        P.put(Item, Valeur); // $NON-NLS-1$

        FileOutputStream out = null;
        try {
            out = new FileOutputStream(getName());
        } catch (FileNotFoundException e) {
            log.severe("erreur cr�ation de fichier");
            e.printStackTrace();
        }
        try {
            P.store(out, null);
        } catch (IOException e1) {
            log.severe("erreur sauvegarde");
            e1.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e3) {
            log.severe("erreur de fermeture");
            e3.printStackTrace();
        }

    }

    /**
     * Returns the nomFichier.
     *
     * @return String
     */
    public String getFileName() {
        return fileName;
    }

    /**
     * Sets the nomFichier.
     *
     * @param nomfichier The nomFichier to set
     */
    public void setFileName(final String nomfichier) {
        fileName = nomfichier;
    }
xtof's avatar
xtof committed

}