GeneralInstanceProperties.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.pdd.config;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.File;
  23. import java.io.InputStream;
  24. import java.lang.reflect.Constructor;
  25. import java.lang.reflect.Method;
  26. import java.net.URI;
  27. import java.net.URL;
  28. import java.util.ArrayList;
  29. import java.util.Enumeration;
  30. import java.util.List;
  31. import java.util.Properties;
  32. import java.util.jar.Attributes;
  33. import java.util.jar.JarEntry;
  34. import java.util.jar.JarFile;
  35. import java.util.jar.Manifest;

  36. import org.openspcoop2.pdd.core.CostantiPdD;
  37. import org.openspcoop2.utils.io.JarUtilities;
  38. import org.openspcoop2.utils.io.ZipUtilities;
  39. import org.openspcoop2.utils.resources.FileSystemUtilities;
  40. import org.slf4j.Logger;

  41. /**
  42. * GeneralInstanceProperties
  43. *
  44. * @author Andrea Poli (apoli@link.it)
  45.  * @author $Author$
  46.  * @version $Rev$, $Date$
  47. */

  48. public class GeneralInstanceProperties {

  49.     private File archiveFile = null;
  50.    
  51.     private int RESULT_LENGTH = 8;
  52.    
  53.    
  54.     public Object[] reads(Logger log){
  55.        
  56.        
  57.         // Properties
  58.         Object[] oR = readLoadProperties(log);
  59.         if(oR!=null){
  60.             return oR;
  61.         }
  62.        
  63.        
  64.         // La ricerca viene attivata SOLO se la proprieta' e' presente e assume il valore true
  65.         boolean lookup = false;
  66.        
  67.         String properties = System.getenv(CostantiPdD.OPENSPCOOP2_LOOKUP);
  68.         if(properties!=null && "true".equalsIgnoreCase(properties)){
  69.             lookup = true;
  70.         }
  71.        
  72.         if(properties==null){
  73.             properties = System.getProperty(CostantiPdD.OPENSPCOOP2_LOOKUP);
  74.             if(properties!=null && "true".equalsIgnoreCase(properties)){
  75.                 lookup = true;
  76.             }
  77.         }
  78.        
  79.         if(!lookup){
  80.             return null;
  81.         }      
  82.        
  83.         // Lookup
  84.         File fClasspath = null;
  85.         try{
  86.             URL urlClasspath = GeneralInstanceProperties.class.getResource("/govway.log4j2.properties");
  87.             if(urlClasspath!=null){
  88.                 URI uri = urlClasspath.toURI();
  89.                 String uriS = uri.toString();
  90.                 if(uriS.startsWith("vfsfile:")){
  91.                     // jboss 5
  92.                     uriS = uriS.substring("vfsfile:".length());
  93.                 }
  94.                 else if(uriS.startsWith("vfszip:")){
  95.                     // jboss 5
  96.                     uriS = uriS.substring("vfszip:".length());
  97.                 }
  98.                 else if(uriS.contains(":")){
  99.                     // jboss 4
  100.                     String [] tmp = uriS.split(":");
  101.                     if(tmp.length>1){
  102.                         uriS = tmp[1].trim();
  103.                     }
  104.                 }
  105.                 File f = (new File(uriS)).getParentFile();
  106.                 if(f!=null){                    
  107.                     // OpenSPCoop.ear/properties/govway.log4j2.properties
  108.                     // openspcoop.war/WEB-INF/classes/govway.log4j2.properties
  109.                     if("properties".equals(f.getName())){
  110.                         fClasspath = f.getParentFile().getParentFile();
  111.                     }else if("classes".equals(f.getName())){
  112.                         fClasspath = f.getParentFile().getParentFile().getParentFile();
  113.                     }
  114.                 }
  115.             }
  116.         }catch(Exception e){
  117.             e.printStackTrace(System.err);
  118.         }
  119.         if(fClasspath!=null){
  120.            
  121.             List<File> files = new ArrayList<File>();
  122.             findArchives(log, fClasspath, files);
  123.                        
  124.             if(files.size()<=0){
  125.                
  126.                 // JBOSS
  127.                 String dir = fClasspath.getName();
  128.                 fClasspath = fClasspath.getParentFile();
  129.                 if(fClasspath!=null){
  130.                     if("tmp".equalsIgnoreCase(fClasspath.getName())){
  131.                         fClasspath = fClasspath.getParentFile();
  132.                         if(fClasspath!=null){
  133.                             fClasspath = new File(fClasspath,dir);
  134.                             findArchives(log, fClasspath, files);
  135.                         }
  136.                     }
  137.                     else if("deploy".equalsIgnoreCase(dir)){
  138.                         if(fClasspath!=null){
  139.                             fClasspath = new File(fClasspath,"tmp");
  140.                             if(fClasspath.exists() && fClasspath.canRead()){
  141.                                 fClasspath = new File(fClasspath,"deploy");
  142.                                 if(fClasspath.exists() && fClasspath.canRead()){
  143.                                     findArchives(log, fClasspath, files);
  144.                                 }
  145.                             }
  146.                         }
  147.                     }
  148.                 }
  149.                
  150.             }
  151.            
  152.             for (int i = 0; i < files.size(); i++) {
  153.                 Object[] o = null;
  154.                 this.archiveFile = files.get(i);
  155.                 if(this.archiveFile.isFile()){
  156.                     log.debug("Archive ["+this.archiveFile.getAbsolutePath()+"] find in classpath");
  157.                     o = readFile(this.archiveFile,log);
  158.                 }
  159.                 else{
  160.                     log.debug("Directory ["+this.archiveFile.getAbsolutePath()+"] find in classpath");
  161.                     o = readDir(this.archiveFile,log);
  162.                 }
  163.                 if(o!=null){
  164.                     return o;
  165.                 }
  166.             }
  167.            
  168.         }
  169.        
  170.        
  171.        
  172.         return null;
  173.     }
  174.    
  175.     private Object[] readLoadProperties(Logger log){
  176.         try{
  177.            
  178.             String loaderP = System.getenv(CostantiPdD.OPENSPCOOP2_LOADER);
  179.             if(loaderP==null){
  180.                 loaderP = System.getProperty(CostantiPdD.OPENSPCOOP2_LOADER);
  181.             }
  182.             if(loaderP==null){
  183.                 InputStream is = GeneralInstanceProperties.class.getResourceAsStream("/op2loader.properties");
  184.                 if(is!=null){
  185.                     try{
  186.                         Properties p = new Properties();
  187.                         p.load(is);
  188.                         loaderP = p.getProperty("loader");
  189.                         if(loaderP!=null){
  190.                             loaderP=loaderP.trim();
  191.                         }
  192.                     }finally{
  193.                         try{
  194.                             is.close();
  195.                         }catch(Exception eClose){
  196.                             // close
  197.                         }
  198.                     }
  199.                 }
  200.             }
  201.             if(loaderP == null){
  202.                 return null;
  203.             }
  204.            
  205.             // Loader
  206.             Method method = this.getClass().getClassLoader().getClass().getMethod("loadClass", String.class);
  207.             Object c = method.invoke(this.getClass().getClassLoader(), loaderP);
  208.            
  209.             java.lang.ClassLoader loader = null;
  210.             if(c!=null){
  211.                 Constructor<?> constructor = ((Class<?>)c).getConstructor(java.lang.ClassLoader.class);
  212.                 loader = (java.lang.ClassLoader) constructor.newInstance(this.getClass().getClassLoader());
  213.             }
  214.             if(loader==null){
  215.                 throw new Exception("Loader ["+loaderP+"] non caricato");
  216.             }
  217.            
  218.             // Metodo per properties
  219.             Method methodProperties = null;
  220.             try{
  221.                 methodProperties = loader.getClass().getMethod("getClassProperties");
  222.             }catch(Throwable e){
  223.                 // ingore
  224.             }
  225.             if(methodProperties!=null){
  226.                 Object result = methodProperties.invoke(loader);
  227.                 if(result!=null && (result instanceof String) ){
  228.                     Object[] o = readProperties(log, ((String)result), loader, "getClassProperties");
  229.                     if(o!=null){
  230.                         return o;
  231.                     }
  232.                 }
  233.             }
  234.            
  235.             // Proprieta' inserite nel sistema
  236.             String tipo = "systemProperties";
  237.             String properties = System.getenv(CostantiPdD.OPENSPCOOP2_LOADER_PROPERTIES);
  238.             if(properties==null){
  239.                 tipo = "javaProperties";
  240.                 properties = System.getProperty(CostantiPdD.OPENSPCOOP2_LOADER_PROPERTIES);
  241.             }
  242.             if(properties == null){
  243.                 throw new Exception("Trovata proprieta' per loader ma non sono state fornite le properties");
  244.             }
  245.            
  246.             Object[] o = readProperties(log, properties, loader, tipo);
  247.             if(o!=null){
  248.                 return o;
  249.             }
  250.            
  251.             throw new Exception("Trovata proprieta' per loader ma non sono state fornite correttamente le properties");
  252.            
  253.         }catch(Exception e){
  254.             e.printStackTrace(System.out);
  255.             log.debug("LoadProperties ERROR",e);
  256.         }
  257.         catch(Throwable e){
  258.             e.printStackTrace(System.out);
  259.             log.debug("LoadProperties ERROR",e);
  260.         }
  261.        
  262.         return null;
  263.     }
  264.    
  265.     private Object[] readProperties(Logger log,String properties,java.lang.ClassLoader loader,String tipoRicerca) throws Exception{
  266.         String [] split = null;
  267.         if(properties.contains(",")){
  268.             split = properties.split(",");
  269.         }else if(properties.contains(":")){
  270.             split = properties.split(":");
  271.         }else if(properties.contains(";")){
  272.             split = properties.split(";");
  273.         }else if(properties.contains(" ")){
  274.             split = properties.split(" ");
  275.         }else{
  276.             throw new Exception("Trovata proprieta' per loader ma non sono state fornite correttamente le properties");
  277.         }
  278.         if(split==null || split.length!=(this.RESULT_LENGTH-1)){
  279.             throw new Exception("Trovata proprieta' per loader ma non sono state fornite correttamente le properties");
  280.         }
  281.        
  282.         // Verifico properties
  283.         Properties opP = null;
  284.         Properties clP = null;
  285.         Properties pddP = null;
  286.         Properties configP = null;
  287.         Properties logP = null;
  288.         Properties msgDiagP = null;
  289.         Properties cacheP = null;
  290.         for (int i = 0; i < split.length; i++) {
  291.             String v = split[i].trim();
  292.             try{
  293.                 //System.out.println("V["+v+"]");
  294.                 //System.out.println("CLASS["+this.getClass().getName()+"]");
  295.                 //System.out.println("CLASS2["+this.getClass().getClassLoader().getClass().getName()+"]");
  296.                 //Method method = this.getClass().getClassLoader().getClass().getMethod("loadClass", String.class);
  297.                 Method method = ClassLoader.class.getMethod("loadClass", String.class);
  298.                 //System.out.println("AfterMethod");
  299.                 Object find = method.invoke(this.getClass().getClassLoader(), ClassLoader.class.getName());
  300.                 //System.out.println("Find ["+find+"]");
  301.                 //if(find!=null){
  302.                 //  System.out.println("Find ["+find.getClass().getName()+"]");
  303.                 //}
  304.                 //System.out.println("LOADER ["+loader.getClass().getName()+"]");
  305.                 find = method.invoke(loader, Properties.class.getName());
  306.                 //System.out.println("PropInterface ["+find+"]");
  307.                 //if(find!=null){
  308.                 //  System.out.println("PropInterface ["+find.getClass().getName()+"]");
  309.                 //}
  310.                 Object c = method.invoke(loader, v);
  311.                 if(c==null){
  312.                     throw new Exception("Proprieta' ["+v+"] non trovata");
  313.                 }
  314.                 if(((Class<?>)find).isAssignableFrom(((Class<?>)c))){
  315.                     Constructor<?> constructor = ((Class<?>)c).getConstructor();
  316.                     Properties p = (Properties) constructor.newInstance();
  317.                     Object type = p.get("type");
  318.                     if(type==null){
  319.                         throw new Exception("Proprieta' ["+v+"] con type non definito");
  320.                     }
  321.                     if("openspcoop".equals(type)){
  322.                         opP = p;
  323.                     }
  324.                     else if("className".equals(type)){
  325.                         clP = p;
  326.                     }
  327.                     else if("pdd".equals(type)){
  328.                         pddP = p;
  329.                     }
  330.                     else if("logger".equals(type)){
  331.                         logP = p;
  332.                     }
  333.                     else if("config".equals(type)){
  334.                         configP = p;
  335.                     }
  336.                     else if("msgDiagnostici".equals(type)){
  337.                         msgDiagP = p;
  338.                     }
  339.                     else if("cache".equals(type)){
  340.                         cacheP = p;
  341.                     }
  342.                     else{
  343.                         throw new Exception("Proprieta' ["+v+"] con type non corretto");
  344.                     }
  345.                 }else{
  346.                     throw new Exception("Proprieta' ["+v+"] di tipo non corretto");
  347.                 }
  348.             }catch(Exception e){
  349.                 throw new Exception("Trovata proprieta' per loader ma non sono state fornite correttamente le properties. Errore durante la lookup della properties ["+v+"]: "+e.getMessage(),e);
  350.             }catch(Throwable e){
  351.                 throw new Exception("Trovata proprieta' per loader ma non sono state fornite correttamente le properties. Errore durante la lookup della properties ["+v+"]: "+e.getMessage(),e);
  352.             }
  353.         }
  354.         if(opP!=null && clP!=null && pddP!=null && logP!=null && configP!=null && msgDiagP!=null && cacheP!=null){
  355.             Object [] o = new Object[this.RESULT_LENGTH];
  356.             o[0]=loader;
  357.             o[1]=opP;
  358.             o[2]=clP;
  359.             o[3]=pddP;
  360.             o[4]=logP;
  361.             o[5]=configP;
  362.             o[6]=msgDiagP;
  363.             o[7]=cacheP;
  364.             for (int i = 0; i < o.length; i++) {
  365.                 log.debug("Class ("+tipoRicerca+") ["+i+"]=["+o[i].getClass().getName()+"]");
  366.             }
  367.             log.debug("Loader find ("+tipoRicerca+")");
  368.             return o;
  369.         }
  370.        
  371.         return null;
  372.     }
  373.    
  374.     private void findArchives(Logger log,File fClasspath,List<File> files){
  375.         log.debug("Search libraries in classpath ["+fClasspath.getAbsolutePath()+"] ...");
  376.        
  377.         if(fClasspath.isFile()){
  378.             log.debug("Classpath is file");
  379.             return;
  380.         }
  381.         if(fClasspath.canRead()==false){
  382.             log.debug("Classpath is not readable");
  383.             return;
  384.         }
  385.        
  386.         File [] f = fClasspath.listFiles();
  387.         if(f!=null){
  388.             for (int i = 0; i < f.length; i++) {
  389.                
  390.                 if(f[i].canRead()==false){
  391.                     continue;
  392.                 }
  393.                 if(f[i].isFile()){
  394.                     if(checkFile(f[i])){
  395.                         log.debug("Find archive ["+f[i].getAbsolutePath()+"]");
  396.                         files.add(f[i]);
  397.                     }
  398.                 }
  399.                 else if(f[i].isDirectory()){
  400.                     if(checkDir(f[i])){
  401.                         log.debug("Find dir ["+f[i].getAbsolutePath()+"]");
  402.                         files.add(f[i]);
  403.                     }
  404.                 }
  405.             }
  406.         }
  407.         log.debug("Search libraries in classpath ["+fClasspath.getAbsolutePath()+"]: find "+files.size());
  408.     }
  409.    
  410.     private boolean checkFile(File file)
  411.     {
  412.         if (!file.exists ())
  413.             return false;
  414.         if (!file.canRead())
  415.             return false;
  416.         if (file.isDirectory())
  417.             return false;
  418.         try{
  419.             JarFile archive = JarUtilities.getJar(file, false);
  420.             if(archive==null){
  421.                 return false;
  422.             }
  423.             Manifest manifest = archive.getManifest();
  424.             return checkManifest(manifest);
  425.            
  426.         }catch(Exception e){
  427.             return false;
  428.         }
  429.        
  430.     }
  431.     private boolean checkDir(File dir){
  432.        
  433.         try{
  434.        
  435.             if(!dir.canRead()){
  436.                 return false;
  437.             }
  438.             File [] childs = dir.listFiles();
  439.             if(childs==null || childs.length<=0){
  440.                 return false;
  441.             }
  442.             for (int i = 0; i < childs.length; i++) {
  443.                 if(!childs[i].canRead()){
  444.                     continue;
  445.                 }
  446.                 if(!childs[i].isDirectory()){
  447.                     continue;
  448.                 }
  449.                 if("META-INF".equals(childs[i].getName())==false){
  450.                     continue;
  451.                 }
  452.                 File [] childsInterni = childs[i].listFiles();
  453.                 if(childsInterni==null || childsInterni.length<=0){
  454.                     return false;
  455.                 }
  456.                 for (int j = 0; j < childsInterni.length; j++) {
  457.                     if(!childsInterni[j].canRead()){
  458.                         continue;
  459.                     }
  460.                     if(!childsInterni[j].isFile()){
  461.                         continue;
  462.                     }
  463.                     if("MANIFEST.MF".equals(childsInterni[j].getName())==false){
  464.                         continue;
  465.                     }
  466.                     byte[] m = FileSystemUtilities.readBytesFromFile(childsInterni[j]);
  467.                     if(m==null){
  468.                         return false;
  469.                     }
  470.                     Manifest manifest = new Manifest(new ByteArrayInputStream(m));
  471.                     return checkManifest(manifest);
  472.                 }
  473.             }
  474.            
  475.             return false;
  476.            
  477.         }catch(Exception e){
  478.             return false;
  479.         }
  480.     }
  481.    
  482.     private boolean checkManifest(Manifest manifest){
  483.         if(manifest==null){
  484.             return false;
  485.         }
  486.         if(manifest.getMainAttributes()==null){
  487.             return false;
  488.         }
  489.        
  490.         Attributes attributes = manifest.getMainAttributes();
  491.         if(attributes.size()<2){
  492.             return false;
  493.         }
  494.        
  495.         String attrProduct = attributes.getValue("product-name");
  496.         if(CostantiPdD.OPENSPCOOP2.equals(attrProduct)==false){
  497.             return false;
  498.         }
  499.        
  500.         String attrCopyright = attributes.getValue("copyright");
  501.         if(attrCopyright==null){
  502.             return false;
  503.         }
  504.         if(attrCopyright.contains("Link.it srl (https://link.it). All rights reserved.")==false){
  505.             return false;
  506.         }
  507.         return true;
  508.     }
  509.    
  510.    
  511.    
  512.     private Object[] readFile(File file,Logger log)
  513.     {
  514.         if (!file.exists ()){
  515.             log.debug("File ["+file.getAbsolutePath()+"] not exist");
  516.             return null;
  517.         }
  518.         if (!file.canRead()) {
  519.             log.debug("File ["+file.getAbsolutePath()+"] not readable");
  520.             return null;
  521.         }
  522.         if (file.isDirectory()) {
  523.             log.debug("File ["+file.getAbsolutePath()+"] is directory");
  524.             return null;
  525.         }
  526.         try{
  527.             JarFile archive = JarUtilities.getJar(file, false);
  528.             if(archive==null){
  529.                 log.debug("File ["+file.getAbsolutePath()+"] is not jar archive");
  530.                 return null;
  531.             }
  532.            
  533.             Enumeration<JarEntry> entries = null;
  534.             java.lang.ClassLoader loader = null;
  535.             String parentDirLoader = null;
  536.            
  537.             // Class
  538.             entries = archive.entries();
  539.             while (entries.hasMoreElements()){
  540.                 JarEntry entry = entries.nextElement ();
  541.                 String extension = "";
  542.                 if( entry.getName().contains(".")){
  543.                     extension = entry.getName().substring(entry.getName().lastIndexOf('.')+1, entry.getName().length());
  544.                 }
  545.                 String nome = entry.getName ().replaceAll("/", ".");
  546.                 if(extension!=null){
  547.                     extension = "."+extension;
  548.                     nome = nome.substring(0,nome.length()-extension.length());
  549.                 }
  550.                
  551.                 if(nome.endsWith(".")){
  552.                     // directory
  553.                     continue;
  554.                 }
  555.            
  556.                 try{
  557.                     Method method = this.getClass().getClassLoader().getClass().getMethod("loadClass", String.class);
  558.                     Object find = method.invoke(this.getClass().getClassLoader(), ClassLoader.class.getName());
  559.                     Object c = null;
  560.                     try{
  561.                         c = method.invoke(this.getClass().getClassLoader(), nome);
  562.                     }catch(Throwable e){
  563.                         // ignore
  564.                     }
  565.                     if(c!=null){
  566.                         if(((Class<?>)find).isAssignableFrom(((Class<?>)c))){
  567.                             Constructor<?> constructor = ((Class<?>)c).getConstructor(java.lang.ClassLoader.class);
  568.                             loader = (java.lang.ClassLoader) constructor.newInstance(this.getClass().getClassLoader());
  569.                             parentDirLoader = nome.split("\\.")[0].trim()+".";
  570.                             break;
  571.                         }
  572.                     }
  573.                 }catch(Exception e){
  574.                     // ignore
  575.                 }
  576.             }
  577.             if(loader!=null){
  578.                 log.debug("Loader find (loadClass)");
  579.             }
  580.            
  581.             // Bytes
  582.             if(loader==null){
  583.                 entries = archive.entries();
  584.                 while (entries.hasMoreElements()){
  585.                     JarEntry entry = entries.nextElement ();
  586.                     String extension = "";
  587.                     if( entry.getName().contains(".")){
  588.                         extension = entry.getName().substring(entry.getName().lastIndexOf('.')+1, entry.getName().length());
  589.                     }
  590.                     String nome = entry.getName ().replaceAll("/", ".");
  591.                     if(extension!=null){
  592.                         extension = "."+extension;
  593.                         nome = nome.substring(0,nome.length()-extension.length());
  594.                     }
  595.                    
  596.                     if(nome.endsWith(".")){
  597.                         // directory
  598.                         continue;
  599.                     }
  600.                    
  601.                     byte[] entryBytes = null;
  602.                     try{
  603.                         entryBytes = JarUtilities.getEntry(file, entry.getName());
  604.                     }catch(Throwable e){
  605.                         // ignore
  606.                     }
  607.                     try{
  608.                         ResourceFinder finder = new ResourceFinder(this.getClass().getClassLoader());
  609.                         Object c = null;
  610.                         try{
  611.                             c = finder.loadResource(nome, entryBytes);
  612.                         }catch(Throwable e){
  613.                             // ignore
  614.                         }
  615.                         if(c!=null){
  616.                             Method method = this.getClass().getClassLoader().getClass().getMethod("loadClass", String.class);
  617.                             Object find = method.invoke(this.getClass().getClassLoader(), ClassLoader.class.getName());
  618.                             if(((Class<?>)find).isAssignableFrom(((Class<?>)c))){
  619.                                 Constructor<?> constructor = ((Class<?>)c).getConstructor(java.lang.ClassLoader.class,JarFile.class,String.class, File.class);
  620.                                 parentDirLoader = nome.split("\\.")[0].trim()+".";
  621.                                 loader = (java.lang.ClassLoader) constructor.newInstance(this.getClass().getClassLoader(),archive,parentDirLoader,this.archiveFile);
  622.                                 break;
  623.                             }
  624.                         }
  625.                     }catch(Exception e){
  626.                         // ignore
  627.                     }
  628.                 }
  629.                 if(loader!=null){
  630.                     log.debug("Loader find (ResourceFinder)");
  631.                 }
  632.             }
  633.            
  634.             // Jar
  635.             if(loader==null){
  636.                 entries = archive.entries();
  637.                 while (entries.hasMoreElements()){
  638.                     JarEntry entry = entries.nextElement ();
  639.                     String extension = "";
  640.                     if( entry.getName().contains(".")){
  641.                         extension = entry.getName().substring(entry.getName().lastIndexOf('.')+1, entry.getName().length());
  642.                     }
  643.                     String nome = entry.getName ().replaceAll("/", ".");
  644.                     if(extension!=null){
  645.                         extension = "."+extension;
  646.                         nome = nome.substring(0,nome.length()-extension.length());
  647.                     }
  648.                    
  649.                     if(nome.endsWith(".")){
  650.                         // directory
  651.                         continue;
  652.                     }
  653.                    
  654.                     byte[] entryBytes = null;
  655.                     try{
  656.                         entryBytes = JarUtilities.getEntry(file, entry.getName());
  657.                     }catch(Throwable e){
  658.                         // ignore
  659.                     }
  660.                    
  661.                     if(entryBytes!=null){
  662.                         File tmp = null;
  663.                         try{                            
  664.                             tmp = FileSystemUtilities.createTempFile("PddInterceptor", "PddInterceptor");
  665.                             FileSystemUtilities.writeFile(tmp, entryBytes);
  666.                             if(JarUtilities.isJar(tmp, false)){
  667.                                 if(checkFile(tmp)){
  668.                                     Object [] o = readFile(tmp,log);
  669.                                     if(o!=null){
  670.                                         return o;
  671.                                     }
  672.                                 }
  673.                             }
  674.                         }catch(Throwable e){
  675.                             // ignore
  676.                         }
  677.                         finally{
  678.                             try{
  679.                                 FileSystemUtilities.deleteDir(tmp);
  680.                                 tmp.deleteOnExit();
  681.                             }catch(Exception e){
  682.                                 // ignore
  683.                             }
  684.                         }
  685.                     }          
  686.                 }
  687.             }
  688.            
  689.             // Unzip jar per verifica dir interne
  690.             if(loader==null){
  691.                 File tmp = null;
  692.                 try{            
  693.                     tmp = FileSystemUtilities.createTempFile("PddInterceptor", "PddInterceptor");
  694.                     if(!tmp.delete()) {
  695.                         // ignore
  696.                     }
  697.                     ZipUtilities.unzipFile(file.getAbsolutePath(), tmp.getAbsolutePath());
  698.                     Object [] o = readDir(tmp,log);
  699.                     if(o!=null){
  700.                         return o;
  701.                     }
  702.                 }catch(Throwable e){
  703.                     // ignore
  704.                 }
  705.                 finally{
  706.                     try{
  707.                         FileSystemUtilities.deleteDir(tmp);
  708.                         tmp.deleteOnExit();
  709.                     }catch(Exception e){
  710.                         // ignore
  711.                     }
  712.                 }
  713.             }
  714.            
  715.            
  716.             // Loader
  717.             if(loader!=null){
  718.                                            
  719.                 // Verifico properties
  720.                 entries = archive.entries();
  721.                 Properties opP = null;
  722.                 Properties clP = null;
  723.                 Properties pddP = null;
  724.                 Properties configP = null;
  725.                 Properties logP = null;
  726.                 Properties msgDiagP = null;
  727.                 Properties cacheP = null;
  728.                 while (entries.hasMoreElements()){
  729.                     JarEntry entry = entries.nextElement ();
  730.                     String extension = "";
  731.                     if( entry.getName().contains(".")){
  732.                         extension = entry.getName().substring(entry.getName().lastIndexOf('.')+1, entry.getName().length());
  733.                     }
  734.                     String nome = entry.getName ().replaceAll("/", ".");
  735.                     if(extension!=null){
  736.                         extension = "."+extension;
  737.                         nome = nome.substring(0,nome.length()-extension.length());
  738.                     }
  739.                     if(nome.equals(loader.getClass().getName())==false && !".".equals(extension) && nome.startsWith(parentDirLoader)){
  740.                         Object find = null;
  741.                         Object c = null;
  742.                         try{
  743.                             log.debug("Load ["+nome+"]...");
  744.                             Method method = loader.getClass().getMethod("loadClass", String.class);
  745.                             find = method.invoke(loader, Properties.class.getName());
  746.                             c = method.invoke(loader, nome);
  747.                             log.debug("Load ["+nome+"] OK");
  748.                             if(find==null){
  749.                                 throw new Exception("find null");
  750.                             }
  751.                             if(c==null){
  752.                                 throw new Exception("c null");
  753.                             }
  754.                         }catch(Exception e){
  755.                             log.debug("Load ["+nome+"] ERROR",e);
  756.                         }
  757.                         catch(Throwable e){
  758.                             log.debug("Load ["+nome+"] ERROR",e);
  759.                         }
  760.                         if(find!=null && c!=null){
  761.                             try{
  762.                                 if(((Class<?>)find).isAssignableFrom(((Class<?>)c))){
  763.                                     Constructor<?> constructor = ((Class<?>)c).getConstructor();
  764.                                     Properties properties = (Properties) constructor.newInstance();
  765.                                     Object type = properties.get("type");
  766.                                     if(type==null){
  767.                                         continue;
  768.                                     }
  769.                                     else if("openspcoop".equals(type)){
  770.                                         opP = properties;
  771.                                     }
  772.                                     else if("className".equals(type)){
  773.                                         clP = properties;
  774.                                     }
  775.                                     else if("pdd".equals(type)){
  776.                                         pddP = properties;
  777.                                     }
  778.                                     else if("logger".equals(type)){
  779.                                         logP = properties;
  780.                                     }
  781.                                     else if("config".equals(type)){
  782.                                         configP = properties;
  783.                                     }
  784.                                     else if("msgDiagnostici".equals(type)){
  785.                                         msgDiagP = properties;
  786.                                     }
  787.                                     else if("cache".equals(type)){
  788.                                         cacheP = properties;
  789.                                     }
  790.                                 }
  791.                             }catch(Exception e){
  792.                                 log.debug("Loader instance ["+nome+"] error: "+e.getMessage(),e);
  793.                             }
  794.                             catch(Throwable e){
  795.                                 log.debug("Loader instance ["+nome+"] error: "+e.getMessage(),e);
  796.                             }
  797.                         }
  798.                     }          
  799.                 }
  800.                 if(opP!=null && clP!=null && pddP!=null && logP!=null && configP!=null && msgDiagP!=null && cacheP!=null){
  801.                     Object [] o = new Object[this.RESULT_LENGTH];
  802.                     o[0]=loader;
  803.                     o[1]=opP;
  804.                     o[2]=clP;
  805.                     o[3]=pddP;
  806.                     o[4]=logP;
  807.                     o[5]=configP;
  808.                     o[6]=msgDiagP;
  809.                     o[7]=cacheP;
  810.                     for (int i = 0; i < o.length; i++) {
  811.                         log.debug("Class ["+i+"]=["+o[i].getClass().getName()+"]");
  812.                     }
  813.                     return o;
  814.                 }
  815.             }
  816.            
  817.         } catch (RuntimeException e) {
  818.             return null;
  819.         } catch (Exception e) {
  820.             return null;
  821.         }
  822.        
  823.         return null;
  824.     }
  825.    
  826.        
  827.     private Object[] readDir(File file,Logger log)
  828.     {
  829.         if(file==null) {
  830.             log.debug("File is null");
  831.             return null;
  832.         }
  833.        
  834.         if (!file.exists ()) {
  835.             log.debug("File (DIR) ["+file.getAbsolutePath()+"] not exist");
  836.             return null;
  837.         }
  838.         if (!file.canRead()) {
  839.             log.debug("File (DIR) ["+file.getAbsolutePath()+"] not readable");
  840.             return null;
  841.         }
  842.         if (file.isFile()) {
  843.             log.debug("File (DIR) ["+file.getAbsolutePath()+"] is directory");
  844.             return null;
  845.         }
  846.        
  847.         File[] childs = file.listFiles();
  848.         if(childs!=null && childs.length>0){
  849.            
  850.             java.lang.ClassLoader loader = null;
  851.             String parentDirLoader = null;          
  852.            
  853.             // Chiamate ricorsive
  854.             for (int i = 0; i < childs.length; i++) {
  855.                
  856.                 if(childs[i]!=null) {
  857.                     if(childs[i].isDirectory()){
  858.                        
  859.                         // Check if is war/ear
  860.                         File [] f = childs[i].listFiles();
  861.                         if(f!=null && f.length>0){
  862.                            
  863.                             File [] childsInterni = childs[i].listFiles();
  864.                             File WEB_LIB = null;
  865.                             File WEB_CLASSES = null;
  866.                             File EAR = null;
  867.                            
  868.                             if("WEB-INF".equals(childs[i].getName())){
  869.                                 for (int k = 0; k < childsInterni.length; k++) {
  870.                                     if("lib".equals(childsInterni[k].getName())){
  871.                                         WEB_LIB = childsInterni[k];
  872.                                     }
  873.                                     if("classes".equals(childsInterni[k].getName())){
  874.                                         WEB_CLASSES = childsInterni[k];
  875.                                     }
  876.                                 }
  877.                             }
  878.                             else if("META-INF".equals(childs[i].getName())){
  879.                                 for (int k = 0; k < childsInterni.length; k++) {
  880.                                     if("application.xml".equals(childsInterni[k].getName())){
  881.                                         EAR = childs[i];
  882.                                     }
  883.                                 }
  884.                             }
  885.                            
  886.                             if(WEB_LIB!=null || WEB_CLASSES!=null || EAR!=null){
  887.                                 // dir
  888.                                 if(WEB_LIB!=null){
  889.                                     Object[] o = null;
  890.                                     try{
  891.                                         o = readDir(WEB_LIB,log);
  892.                                     }catch(Throwable e){
  893.                                         // ignore
  894.                                     }
  895.                                     if(o!=null)
  896.                                         return o;  
  897.                                 }
  898.                                
  899.                                 if(WEB_CLASSES!=null){
  900.                                     Object[] o = null;
  901.                                     try{
  902.                                         o = readDir(WEB_CLASSES,log);
  903.                                     }catch(Throwable e){
  904.                                         // ignore
  905.                                     }
  906.                                     if(o!=null)
  907.                                         return o;  
  908.                                 }
  909.                                
  910.                                 if(WEB_LIB!=null || WEB_CLASSES!=null){
  911.                                     Object[] o = null;
  912.                                     try{
  913.                                         o = readDir(childs[i],log);
  914.                                     }catch(Throwable e){
  915.                                         // ignore
  916.                                     }
  917.                                     if(o!=null)
  918.                                         return o;  
  919.                                 }
  920.                                
  921.                                 if(EAR!=null){
  922.                                     Object[] o = null;
  923.                                     try{
  924.                                         o = readDir(EAR,log);
  925.                                     }catch(Throwable e){
  926.                                         // ignore
  927.                                     }
  928.                                     if(o!=null)
  929.                                         return o;  
  930.                                 }
  931.                             }
  932.                            
  933.                             // Provo ad utilizzare direttamente la directory
  934.                             Object[] o = null;
  935.                             try{
  936.                                 o = readDir(childs[i],log);
  937.                             }catch(Throwable e){
  938.                                 // ignore
  939.                             }
  940.                             if(o!=null)
  941.                                 return o;  
  942.                            
  943.                         }
  944.                     }
  945.                 }
  946.             }
  947.            
  948.             List<String> entries = new ArrayList<>();
  949.             List<byte[]> entriesBytes = new ArrayList<byte[]>();
  950.            
  951.             // Inizializzo List
  952.             for (int i = 0; i < childs.length; i++) {
  953.                
  954.                 if(childs[i]!=null) {
  955.                     if(childs[i].isDirectory()){
  956.                        
  957.                         // Check if is war/ear
  958.                         File [] f = childs[i].listFiles();
  959.                         if(f!=null && f.length>0){
  960.                
  961.                             // Utilizzo i files
  962.                             File [] childsEntries = file.listFiles();
  963.                             try{
  964.                                 if(childsEntries!=null){
  965.                                     for (int j = 0; j < childsEntries.length; j++) {
  966.                                         buildEntryNames(childsEntries[j], null, entries,entriesBytes);
  967.                                     }
  968.                                 }
  969.                             }catch(Exception e){
  970.                                 // ignore
  971.                             }
  972.                         }
  973.                     }
  974.                     else{
  975.                         try{
  976.                             entries.add(childs[i].getName());
  977.                             entriesBytes.add(FileSystemUtilities.readBytesFromFile(childs[i]));
  978.                         }catch(Exception e){
  979.                             // ignore
  980.                         }
  981.                     }
  982.                 }
  983.             }
  984.            
  985.            
  986.             // Class
  987.             for (int k = 0; k < entries.size(); k++) {
  988.                    
  989.                 String extension = "";
  990.                 if( entries.get(k).contains(".")){
  991.                     extension = entries.get(k).substring(entries.get(k).lastIndexOf('.')+1, entries.get(k).length());
  992.                 }
  993.                 String nome = entries.get(k).replaceAll("/", ".");
  994.                 if(extension!=null){
  995.                     extension = "."+extension;
  996.                     nome = nome.substring(0,nome.length()-extension.length());
  997.                 }
  998.                
  999.                 try{
  1000.                     Method method = this.getClass().getClassLoader().getClass().getMethod("loadClass", String.class);
  1001.                     Object find = method.invoke(this.getClass().getClassLoader(), ClassLoader.class.getName());
  1002.                     Object c = null;
  1003.                     try{
  1004.                         c = method.invoke(this.getClass().getClassLoader(), nome);
  1005.                     }catch(Throwable e){
  1006.                         // ignore
  1007.                     }
  1008.                     if(((Class<?>)find).isAssignableFrom(((Class<?>)c))){
  1009.                         Constructor<?> constructor = ((Class<?>)c).getConstructor(java.lang.ClassLoader.class);
  1010.                         loader = (java.lang.ClassLoader) constructor.newInstance(this.getClass().getClassLoader());
  1011.                         parentDirLoader = nome.split("\\.")[0].trim()+".";
  1012.                         break;
  1013.                     }
  1014.                 }catch(Throwable e){
  1015.                     // ignore
  1016.                 }
  1017.             }
  1018.             if(loader!=null){
  1019.                 log.debug("Loader DIR find (loadClass)");
  1020.             }
  1021.            
  1022.            
  1023.            
  1024.             if(loader==null){
  1025.                 // Bytes
  1026.                 for (int k = 0; k < entries.size(); k++) {
  1027.                        
  1028.                     String extension = "";
  1029.                     if( entries.get(k).contains(".")){
  1030.                         extension = entries.get(k).substring(entries.get(k).lastIndexOf('.')+1, entries.get(k).length());
  1031.                     }
  1032.                     String nome = entries.get(k).replaceAll("/", ".");
  1033.                     if(extension!=null){
  1034.                         extension = "."+extension;
  1035.                         nome = nome.substring(0,nome.length()-extension.length());
  1036.                     }
  1037.                    
  1038.                     try{
  1039.                         Method method = this.getClass().getClassLoader().getClass().getMethod("loadClass", String.class);
  1040.                         Object find = method.invoke(this.getClass().getClassLoader(), ClassLoader.class.getName());
  1041.                         Object c = null;
  1042.                         try{
  1043.                             ResourceFinder finder = new ResourceFinder(this.getClass().getClassLoader());
  1044.                             c = finder.loadResource(nome, entriesBytes.get(k));
  1045.                         }catch(Throwable e){
  1046.                             // ignore
  1047.                         }
  1048.                         if(c!=null){
  1049.                             if(((Class<?>)find).isAssignableFrom(((Class<?>)c))){
  1050.                                 Constructor<?> constructor = ((Class<?>)c).getConstructor(java.lang.ClassLoader.class,java.util.List.class,java.util.List.class,String.class, File.class);
  1051.                                 parentDirLoader = nome.split("\\.")[0].trim()+".";
  1052.                                 loader = (java.lang.ClassLoader) constructor.newInstance(this.getClass().getClassLoader(),entries,entriesBytes,parentDirLoader,this.archiveFile);
  1053.                                 break;
  1054.                             }
  1055.                         }
  1056.                     }catch(Throwable e){
  1057.                         // ignore
  1058.                     }
  1059.                 }
  1060.                 if(loader!=null){
  1061.                     log.debug("Loader DIR find (ResourceFinder)");
  1062.                 }
  1063.             }
  1064.            
  1065.            
  1066.             if(loader==null){
  1067.                
  1068.                 // Jar
  1069.                 for (int k = 0; k < entries.size(); k++) {
  1070.                        
  1071.                     if(entriesBytes.get(k)!=null){
  1072.                         File tmp = null;
  1073.                         try{
  1074.                             tmp = FileSystemUtilities.createTempFile("PddInterceptor", "PddInterceptor");
  1075.                             FileSystemUtilities.writeFile(tmp, entriesBytes.get(k));
  1076.                             if(JarUtilities.isJar(tmp, false)){
  1077.                                 if(checkFile(tmp)){
  1078.                                     Object [] o = readFile(tmp,log);
  1079.                                     if(o!=null){
  1080.                                         return o;
  1081.                                     }
  1082.                                 }
  1083.                             }
  1084.                         }catch(Throwable e){
  1085.                             // ignore
  1086.                         }
  1087.                         finally{
  1088.                             try{
  1089.                                 FileSystemUtilities.deleteDir(tmp);
  1090.                                 tmp.deleteOnExit();
  1091.                             }catch(Exception e){
  1092.                                 // ignore
  1093.                             }
  1094.                         }
  1095.                     }      

  1096.                 }
  1097.             }
  1098.            
  1099.            
  1100.             // Loader trovato
  1101.             if(loader!=null){
  1102.                                
  1103.                 // Verifico properties
  1104.                 Properties opP = null;
  1105.                 Properties clP = null;
  1106.                 Properties pddP = null;
  1107.                 Properties configP = null;
  1108.                 Properties logP = null;
  1109.                 Properties msgDiagP = null;
  1110.                 Properties cacheP = null;
  1111.                 for (int k = 0; k < entries.size(); k++) {
  1112.                    
  1113.                     String entryName = entries.get(k);
  1114.                     String extension = "";
  1115.                     if( entryName.contains(".")){
  1116.                         extension = entryName.substring(entryName.lastIndexOf('.')+1, entryName.length());
  1117.                     }
  1118.                     String nome = entryName.replaceAll("/", ".");
  1119.                     if(extension!=null){
  1120.                         extension = "."+extension;
  1121.                         nome = nome.substring(0,nome.length()-extension.length());
  1122.                     }
  1123.                     if(nome.equals(loader.getClass().getName())==false && !".".equals(extension) && nome.startsWith(parentDirLoader) ){
  1124.                         Object find = null;
  1125.                         Object c = null;
  1126.                         //Throwable cE = null;
  1127.                         try{
  1128.                             log.debug("Load (DIR) ["+nome+"]...");
  1129.                             Method method = loader.getClass().getMethod("loadClass", String.class);
  1130.                             find = method.invoke(loader, Properties.class.getName());
  1131.                             c = method.invoke(loader, nome);
  1132.                             log.debug("Load (DIR) ["+nome+"] OK");
  1133.                             if(find==null){
  1134.                                 throw new Exception("find null");
  1135.                             }
  1136.                             if(c==null){
  1137.                                 throw new Exception("c null");
  1138.                             }
  1139.                         }catch(Exception e){
  1140.                             log.debug("Loader (DIR) instance ["+nome+"] error: "+e.getMessage(),e);
  1141.                         }
  1142.                         catch(Throwable e){
  1143.                             log.debug("Loader (DIR) instance ["+nome+"] error: "+e.getMessage(),e);
  1144.                         }
  1145.                         if(find!=null && c!=null){
  1146.                             try{
  1147.                                 if(((Class<?>)find).isAssignableFrom(((Class<?>)c))){
  1148.                                     Constructor<?> constructor = ((Class<?>)c).getConstructor();
  1149.                                     Properties properties = (Properties) constructor.newInstance();
  1150.                                     Object type = properties.get("type");
  1151.                                     if(type==null){
  1152.                                         continue;
  1153.                                     }
  1154.                                     else if("openspcoop".equals(type)){
  1155.                                         opP = properties;
  1156.                                     }
  1157.                                     else if("className".equals(type)){
  1158.                                         clP = properties;
  1159.                                     }
  1160.                                     else if("pdd".equals(type)){
  1161.                                         pddP = properties;
  1162.                                     }
  1163.                                     else if("logger".equals(type)){
  1164.                                         logP = properties;
  1165.                                     }
  1166.                                     else if("config".equals(type)){
  1167.                                         configP = properties;
  1168.                                     }
  1169.                                     else if("msgDiagnostici".equals(type)){
  1170.                                         msgDiagP = properties;
  1171.                                     }
  1172.                                     else if("cache".equals(type)){
  1173.                                         cacheP = properties;
  1174.                                     }
  1175.                                 }
  1176.                             }catch(Exception e){
  1177.                                 log.debug("Loader (DIR) instance ["+nome+"] error: "+e.getMessage(),e);
  1178.                             }
  1179.                             catch(Throwable e){
  1180.                                 log.debug("Loader (DIR) instance ["+nome+"] error: "+e.getMessage(),e);
  1181.                             }
  1182.                         }
  1183.                     }          
  1184.                 }
  1185.                 if(opP!=null && clP!=null && pddP!=null && logP!=null && configP!=null && msgDiagP!=null && cacheP!=null){
  1186.                     Object [] o = new Object[this.RESULT_LENGTH];
  1187.                     o[0]=loader;
  1188.                     o[1]=opP;
  1189.                     o[2]=clP;
  1190.                     o[3]=pddP;
  1191.                     o[4]=logP;
  1192.                     o[5]=configP;
  1193.                     o[6]=msgDiagP;
  1194.                     o[7]=cacheP;
  1195.                     for (int i = 0; i < o.length; i++) {
  1196.                         log.debug("Class (DIR) ["+i+"]=["+o[i].getClass().getName()+"]");
  1197.                     }
  1198.                     return o;
  1199.                 }
  1200.             }
  1201.         }
  1202.        
  1203.         return null;
  1204.     }
  1205.    
  1206.     private void buildEntryNames(File file,String prefix,List<String> files,List<byte[]> entriesBytes) throws Exception{
  1207.        
  1208.         String name = null;
  1209.         if(prefix!=null){
  1210.             name = prefix+"."+file.getName();
  1211.         }
  1212.         else{
  1213.             name = file.getName();
  1214.         }
  1215.        
  1216.         if(file.isFile()){
  1217.             files.add(name);
  1218.             entriesBytes.add(FileSystemUtilities.readBytesFromFile(file));
  1219.             return;
  1220.         }
  1221.         else if(file.isDirectory()){
  1222.             File [] childs = file.listFiles();
  1223.             if(childs!=null){
  1224.                 for (int i = 0; i < childs.length; i++) {
  1225.                     buildEntryNames(childs[i],name,files,entriesBytes);
  1226.                 }
  1227.             }
  1228.         }else{
  1229.             return;
  1230.         }
  1231.     }
  1232.    
  1233. }