/*
 * Copyright
 *   2008 axYus - www.axyus.com
 *   2008 L.Meckert - laurent.meckert.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS 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 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.gouv.finances.cp.xemelios.plugin.signerfichier;

import fr.gouv.finances.dgfip.xemelios.tools.AbstractTool;
import fr.gouv.finances.dgfip.xemelios.tools.MenuTitle;
import javax.swing.JFrame;

import java.net.URLClassLoader;
import java.net.URL;
import org.apache.log4j.Logger;



import fr.gouv.finances.dgfip.xemelios.common.Constants;
import fr.gouv.finances.cp.utils.PropertiesExpansion;

import java.io.File;

/**
 *
 * @author laurent.meckert
 */

/**
 * Cette classe sert a impleenter la  signature de fichier PES comme un plugin Xemelios
 * @author laurent.meckert
 */
@MenuTitle(value = "Signature de fichier PES")
   public class SignerFichierPlugin  extends AbstractTool   {
      private JFrame parentWindow = null;
      static Logger logger = Logger.getLogger(SignerFichierPlugin.class);

      /** Creates a new instance of SignerFichierPlugin */
      public SignerFichierPlugin() {
         super();
      }

      /**
       * Donne le parent au plugin
       * @param parentWindow
       */
      public void setParentWindow(JFrame parentWindow) {
         this.parentWindow = parentWindow;
      }

      /**
       * @param args the command line arguments
       */
      public void run() {
         Runnable mainTask = new Runnable() {
               public void run() {
                  try {
          
                     //   CustomFactory.essaie();
                     File jardirectory = null;
                     // Essaie de determiner d'ou vient le jar du plugin
                     // On 
                     PropertiesExpansion  pe = new PropertiesExpansion();

                     pe.putAll(System.getProperties());
                     String sDir = pe.getProperty(Constants.SYS_PROP_XEMELIOS_TOOLS_DIR);
                     if(sDir!=null) {
                        String[] directories = sDir.split(",");
                        for(String currentDirectory:directories) {
                           // replacing ${....} variables
                           currentDirectory=pe.replace(currentDirectory);
                           logger.debug("looking for tool into "+currentDirectory);
                           File dir = new File(currentDirectory);
                           if(dir.exists() && dir.isDirectory()) {
                              File myjar = new File(dir, "plugin-signerfichier-pes.jar") ;
                              if(myjar.exists()) {
                                 jardirectory = dir;
                                 break;
                              }
                           }
                        }
                     }
                 
                     File file = new File(jardirectory, "APXS-file-signer.jar");
                     URL url = file.toURL();
                     String url1 = url.toString();

                     //  file = new File(jardirectory, "xmlsec-1.4.1.jar");
                     file = new File(jardirectory, "xmlsec-1.4.2.jar");
                     url = file.toURL();
                     String url2 = url.toString();
                     file = new File(jardirectory, "xalan.jar");
                     url = file.toURL();
                     String url3 = url.toString();

                     file = new File(jardirectory, "bc-jce-jdk13-129.jar");
                     url = file.toURL();
                     String url4 = url.toString();

                     ClassLoader prevCl = Thread.currentThread().getContextClassLoader();
                     
                     ClassLoader loader = URLClassLoader.newInstance(new URL[]{new URL(url1),
                                                                               new URL(url2),
                                                                               new URL(url3),
                                                                               new URL(url4)}, prevCl);
                     Thread.currentThread().setContextClassLoader(loader);
                     //  DlgSignerfichier dialog = new DlgSignerfichier(parentWindow, true);
                  WizardSignerFichier dialog = new WizardSignerFichier(parentWindow, false);
                     dialog.run();
                     //  dialog.dispose();
                     Thread.currentThread().setContextClassLoader(prevCl);
                  } catch(Exception e) {
                     e.printStackTrace();
                  }
               }
            };
            
         System.gc();
         Thread t = new Thread(mainTask);
         t.setPriority(Thread.NORM_PRIORITY);
         java.awt.EventQueue.invokeLater(t);
         
      }
            
      
      /**
       * 
       * @param args ligne de commande
       */
      public static void main(String args[]) {
         // System.out.println("SignerFichierPlugin: main: call SignerFichierPlugin");
         SignerFichierPlugin gsp = new SignerFichierPlugin();
         // System.out.println("SignerFichierPlugin: main: call SignerFichierPlugin.run");
         gsp.run();
      }
   }
            
