/*
 * Copyright 
 *   2008 axYus - www.axyus.com
 *   2008 L.Meckert - laurent.meckert@axyus.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
 */
package fr.gouv.finances.cp.xemelios.plugin.signature;

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;

/**
 * Cette classe sert a impleenter la verification de signature PES comme un plugin Xemelios
 * @author laurent.meckert
 */
@MenuTitle(value = "Vérification de Signatures PES")
   public class GlobalSignaturePlugin extends AbstractTool {

      private JFrame parentWindow = null;
      static Logger logger = Logger.getLogger(GlobalSignaturePlugin.class);

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

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

      /**
       * cree un objet DlglSignatureFull et appelle sa methode run
       */
      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-signature-pes.jar") ;
                              if(myjar.exists()) {
                                 jardirectory = dir;
                                 break;
                              }
                           }
                        }
                        
                     }
                     File file = new File(jardirectory, "APXS-signature.jar");
                     URL url = file.toURL();
                     String url1 = url.toString();

                     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);

                     // CustomFactory.essaie();

                     if (!isInXemelios()) {
//                        DlglSignatureFull dlg = new DlglSignatureFull(parentWindow, true);
//                        dlg.run();
                     } else {
                        DlgSignatureLight dlg = new DlgSignatureLight(parentWindow, true);
                        dlg.run();
                     }
                     Thread.currentThread().setContextClassLoader(prevCl);

//                     CustomFactory.essaie();

                  } catch (Exception e) {
                     e.printStackTrace();
                     return;
                  }
               }
            };
         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("GlobalSignaturePlugin: main: call GlobalSignaturePlugin");
         GlobalSignaturePlugin gsp = new GlobalSignaturePlugin();
         // System.out.println("GlobalSignaturePlugin: main: call GlobalSignaturePlugin.run");
         gsp.run();
      }
   }
