/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2005 L.Meckert - lmeckert@club-internet.fr
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.utils;
/**
 * La classe RegAccess a 2 utilites 1) une fois compilee permettre le build de
 * la partie C en generant RegAccess.h par javah -jni RegAccess 2) faire un test
 * avec le main
 * 
 * Appel = try { RegAccess.getRegistryEntry(entry);} catch(Exception e) { gerer
 * e}; l'exception etant essentiellement le probleme de la dll
 */
public class RegAccess {
    /** Pour ne pas tenter de recharger la dll a chaque appel */
    private static boolean dllLoaded = false;

    /** Charge la DLL associee */
    private static void loadTheDll() throws Exception {
        if (!dllLoaded) {
            try {
                // Charger la DLL en C elle doit etre sur place ou dans les
                // emplacements standard
                System.loadLibrary("RegAccess");
                dllLoaded = true;
            }
            catch (Exception e) {
                throw (e);
            }
        }
    }

    /** Valeur trouvee dans le registre */
    private String strValue;

    /** Methode appellee en C implementee dans RegAccess.cpp */
    private native String getRegistryEntryC(String entry);

    /** Methode static exportee utilisee de l'exterieur */
    public static String getRegistryEntry(String entry) throws Exception {
        // Essaye de charger la Dll
        try {
            loadTheDll();
        }
        catch (Exception e) {
            throw (e);
        }
        // Cree un objet jni RegAcces grace a la DLL
        RegAccess objSys = new RegAccess();
        // Appelle la methode getRegistryEntry de l'objet
        objSys.strValue = objSys.getRegistryEntryC(entry);
        return objSys.strValue;
    }

    /** petit main de test */
    public static void main(String[] argv) {
        // Appelle la methode getRegistryEntry de l'objet
        String testKey = "HKEY_CLASSES_ROOT\\HTTP\\SHELL\\OPEN\\COMMAND";
        try {
            String strValue = getRegistryEntry(testKey);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}
