JarUtilities.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */


  20. package org.openspcoop2.utils.io;

  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.net.JarURLConnection;
  25. import java.net.MalformedURLException;
  26. import java.net.URL;
  27. import java.util.jar.JarEntry;
  28. import java.util.jar.JarFile;
  29. import java.util.jar.Manifest;

  30. import org.openspcoop2.utils.Utilities;
  31. import org.openspcoop2.utils.UtilsException;


  32. /**
  33.  * JarUtilities
  34.  *
  35.  *
  36.  * @author Poli Andrea (apoli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class JarUtilities {

  41.     public static boolean isJar(File file,boolean checkExtension){
  42.         try{    
  43.             JarFile jar = JarUtilities.getJar(file, checkExtension);
  44.             return jar!=null;          
  45.         }catch (Exception e){
  46.             return false;
  47.         }
  48.     }
  49.    
  50.     public static JarFile getJar(File file,boolean checkExtension) throws UtilsException{
  51.         try{
  52.            
  53.             if(checkExtension){
  54.                 if(file.getName().endsWith(".jar")==false){
  55.                     throw new Exception("Estensione del file diversa da quella attesa");
  56.                 }
  57.             }
  58.             JarURLConnection conn = JarUtilities.getConnection(file, null);
  59.             return conn.getJarFile();
  60.                        
  61.         }catch (Exception e){
  62.             throw new UtilsException(e.getMessage(),e);
  63.         }
  64.     }
  65.    
  66.     public static Manifest getManifest(File file) throws UtilsException{
  67.         try{
  68.             JarURLConnection conn = JarUtilities.getConnection(file, null);
  69.             return conn.getManifest();
  70.         }catch (Exception e){
  71.             throw new UtilsException(e.getMessage(),e);
  72.         }
  73.     }
  74.    
  75.     public static byte[] getEntry(File jarFile,String jarEntry) throws UtilsException{
  76.        
  77.         InputStream is = null;
  78.         try{
  79.            
  80.             JarURLConnection conn = JarUtilities.getConnection(jarFile, jarEntry);
  81.             JarFile jar = conn.getJarFile();
  82.             JarEntry entry = conn.getJarEntry();
  83.             if(entry==null){
  84.                 return null;
  85.             }
  86.            
  87.             is = jar.getInputStream(entry);
  88.             byte [] buffer = Utilities.getAsByteArray(is);
  89.             return buffer;
  90.            
  91.         }catch (Exception e){
  92.             throw new UtilsException(e.getMessage(),e);
  93.         }finally{
  94.             try{
  95.                 if(is!=null) {
  96.                     is.close();
  97.                 }
  98.             }catch(Exception eclose){
  99.                 // close
  100.             }
  101.         }
  102.     }
  103.    
  104.     private static JarURLConnection getConnection(File jarFile,String jarEntry) throws MalformedURLException, IOException{
  105.         URL archivio = new URL ("file://" + jarFile.getCanonicalPath ());
  106.         String url = "jar:" + archivio.toExternalForm () + "!/";
  107.         if(jarEntry!=null){
  108.             url = url + jarEntry;
  109.         }
  110.         archivio = new URL (url);
  111.         return (JarURLConnection) archivio.openConnection ();
  112.     }
  113.    
  114. }