PropertiesScriptBuilder.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.core.commons;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.util.Arrays;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import java.util.Map;

  27. import org.openspcoop2.utils.resources.FileSystemUtilities;

  28. /**
  29.  * PropertiesScriptBuilder
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class PropertiesScriptBuilder {

  36.     public static void main(String[] args) throws Exception {
  37.         // Metodo utilizzato dal setup antinstaller
  38.        
  39.         String sourceDir = (String) args[0];
  40.        
  41.         String destDir = (String) args[1];
  42.        
  43.         String distDir = (String) args[2];
  44.        
  45.         String versionePrecedente = null;
  46.         String versioneAttuale = null;
  47.         if(args.length>3){
  48.             versionePrecedente = (String) args[3];
  49.             versioneAttuale = (String) args[4];
  50.         }

  51.         // NOTA: Non far stampare niente, viene usato come meccanismo di check per vedere se l'esecuzione e' andata a buon fine

  52.         //System.out.println("versionePrecedente ["+versionePrecedente+"]");
  53.         //System.out.println("versioneAttuale ["+versioneAttuale+"]");
  54.         //System.out.println("tipoDatabase ["+tipoDatabase+"]");

  55.        
  56.        
  57.         build_Aggiornamento(new File(sourceDir),new File(destDir), new File(distDir),
  58.                 versionePrecedente, versioneAttuale);

  59.     }
  60.        
  61.     private static void build_Aggiornamento(File sourceDir, File destDir,  File distDir,
  62.             String precedenteVersione, String versioneAttuale) throws Exception {

  63.        
  64.         if(sourceDir.exists()==false){
  65.             throw new Exception("Source dir ["+sourceDir.getAbsolutePath()+"] not exists");
  66.         }
  67.         if(sourceDir.canRead()==false){
  68.             throw new Exception("Source dir ["+sourceDir.getAbsolutePath()+"] cannot read");
  69.         }

  70.         if(destDir.exists()==false){
  71.             throw new Exception("Dest dir ["+destDir.getAbsolutePath()+"] not exists");
  72.         }
  73.         if(destDir.canWrite()==false){
  74.             throw new Exception("Dest dir ["+destDir.getAbsolutePath()+"] cannot write");
  75.         }
  76.        
  77.         if(distDir.exists()==false){
  78.             throw new Exception("Dest dir ["+destDir.getAbsolutePath()+"] not exists");
  79.         }
  80.         if(distDir.canWrite()==false){
  81.             throw new Exception("Dest dir ["+destDir.getAbsolutePath()+"] cannot write");
  82.         }
  83.        
  84.         if(precedenteVersione==null){
  85.             throw new Exception("Precedente versione non fornita");
  86.         }
  87.         if(precedenteVersione.contains(".")==false){
  88.             throw new Exception("Precedente versione in un formato non corretto ["+precedenteVersione+"] ('.' not found)");
  89.         }
  90.         int indexOfFirstPoint = precedenteVersione.indexOf(".");
  91.         if(indexOfFirstPoint<=0){
  92.             throw new Exception("Precedente versione in un formato non corretto ["+precedenteVersione+"] ('.' not found with index)");
  93.         }
  94.         String productVersionString = precedenteVersione.substring(0, indexOfFirstPoint);
  95.         int productVersion = -1;
  96.         try{
  97.             productVersion = Integer.parseInt(productVersionString);
  98.         }catch(Exception e){
  99.             throw new Exception("Precedente versione in un formato non corretto ["+precedenteVersione+"] (productVersion:"+productVersionString+"): "+e.getMessage(),e);
  100.         }
  101.         int indexOfSecondPoint = precedenteVersione.indexOf(".",indexOfFirstPoint+1);
  102.         if(indexOfSecondPoint<=0 || indexOfSecondPoint<=indexOfFirstPoint){
  103.             throw new Exception("Precedente versione in un formato non corretto ["+precedenteVersione+"] (second '.' not found)");
  104.         }
  105.         String majorVersionString = precedenteVersione.substring(indexOfFirstPoint+1, indexOfSecondPoint);
  106.         int majorVersion = -1;
  107.         try{
  108.             majorVersion = Integer.parseInt(majorVersionString);
  109.         }catch(Exception e){
  110.             throw new Exception("Precedente versione in un formato non corretto ["+precedenteVersione+"] (majorVersion:"+majorVersionString+"): "+e.getMessage(),e);
  111.         }
  112.         if(precedenteVersione.length()<=indexOfSecondPoint){
  113.             throw new Exception("Precedente versione in un formato non corretto ["+precedenteVersione+"] (length)");
  114.         }
  115.         String minorVersionString = precedenteVersione.substring(indexOfSecondPoint+1,precedenteVersione.length());
  116.         int minorVersion = -1;
  117.         try{
  118.             minorVersion = Integer.parseInt(minorVersionString);
  119.         }catch(Exception e){
  120.             //throw new Exception("Precedente versione in un formato non corretto ["+precedenteVersione+"] (minorVersion:"+minorVersionString+"): "+e.getMessage(),e);
  121.             // Potrebbe essere una BUILD VERSION
  122.             if(minorVersionString.contains("_")){
  123.                 String newMinor = minorVersionString.split("_")[0];
  124.                 try{
  125.                     minorVersion = Integer.parseInt(newMinor);
  126.                 }catch(Exception eInternal){
  127.                     throw new Exception("Precedente versione in un formato non corretto ["+precedenteVersione+"] (minorVersion:"+minorVersionString+" minorVersionBuildNumber:"+newMinor+"): "+eInternal.getMessage(),eInternal);
  128.                 }
  129.             }
  130.             else{
  131.                 throw new Exception("Precedente versione in un formato non corretto ["+precedenteVersione+"] (minorVersion:"+minorVersionString+"): "+e.getMessage(),e);
  132.             }
  133.            
  134.         }
  135.         //System.out.println(productVersion+"."+majorVersion+"."+minorVersion);
  136.        
  137.        
  138.         int tmpMajorVersion = majorVersion;
  139.         int tmpMinorVersion = minorVersion;
  140.        
  141.         Map<String, Boolean> configCreateFileFromDist = new HashMap<String, Boolean>();
  142.         Map<String, ByteArrayOutputStream> configCreateFileDiff = new HashMap<>();
  143.        
  144.        
  145.         while(true){
  146.            
  147.             String actualVersion = productVersion+"."+tmpMajorVersion+"."+tmpMinorVersion;
  148.             String nextVersion = productVersion+"."+tmpMajorVersion+"."+(tmpMinorVersion+1);
  149.             File version = new File(sourceDir,"upgrade_"+actualVersion+"_to_"+nextVersion);
  150.             //System.out.println("CHECK ["+version.getAbsolutePath()+"] ["+version.exists()+"]");
  151.             if(version.exists()){
  152.                 if(version.canRead()==false){
  153.                     throw new Exception("Source version dir ["+version.getAbsolutePath()+"] cannot read");
  154.                 }
  155.                
  156.                 _create_Aggiornamento(version, destDir,
  157.                         configCreateFileFromDist, configCreateFileDiff,
  158.                         nextVersion);
  159.                
  160.                 tmpMinorVersion++;
  161.             }
  162.             else{
  163.                 // check upgrade to major version +1
  164.                 actualVersion = productVersion+"."+tmpMajorVersion+".x";
  165.                 nextVersion = productVersion+"."+(tmpMajorVersion+1)+".0";
  166.                 version = new File(sourceDir,"upgrade_"+actualVersion+"_to_"+nextVersion);
  167.                 //System.out.println("CHECK UPGRADE ["+version.getAbsolutePath()+"] ["+version.exists()+"]");
  168.                 if(version.exists()){
  169.                     if(version.canRead()==false){
  170.                         throw new Exception("Source version dir ["+version.getAbsolutePath()+"] cannot read");
  171.                     }
  172.                    
  173.                     _create_Aggiornamento(version, destDir,
  174.                             configCreateFileFromDist, configCreateFileDiff,
  175.                             nextVersion);
  176.                    
  177.                     tmpMajorVersion++;
  178.                     tmpMinorVersion=0;
  179.                 }
  180.                 else{
  181.                     break;
  182.                 }
  183.             }
  184.            
  185.         }
  186.        
  187.         if(configCreateFileFromDist.size()>0) {
  188.             Iterator<String> it = configCreateFileFromDist.keySet().iterator();
  189.             while (it.hasNext()) {
  190.                 String id = (String) it.next();
  191.                 String fileName = id + ".properties";
  192.                
  193.                 File[] files = distDir.listFiles();
  194.                 if(files!=null && files.length>0) {
  195.                     // Prima esamino i file properties interi poi i diff
  196.                     for(File distFile : files) {
  197.                         if(distFile.getName().equals(fileName)) {
  198.                             File fOut = new File(destDir, fileName);
  199.                             FileSystemUtilities.copy(distFile, fOut);
  200.                             break;
  201.                         }
  202.                     }
  203.                 }
  204.             }
  205.         }
  206.        
  207.         if(configCreateFileDiff.size()>0) {
  208.             Iterator<String> it = configCreateFileDiff.keySet().iterator();
  209.             while (it.hasNext()) {
  210.                 String id = (String) it.next();
  211.                 String fileName = id + ".diff";
  212.                
  213.                 File fOut = new File(destDir, fileName);
  214.                 ByteArrayOutputStream bout = configCreateFileDiff.get(id);
  215.                 bout.flush();
  216.                 bout.close();
  217.                 FileSystemUtilities.writeFile(fOut, bout.toByteArray());

  218.             }
  219.         }
  220.        
  221.     }
  222.    
  223.     private static void _create_Aggiornamento(File sourceDir, File destDir,
  224.             Map<String, Boolean> configCreateFileFromDist , Map<String, ByteArrayOutputStream> configCreateFileDiff ,
  225.             String nextVersion) throws Exception {
  226.                        
  227.         //System.out.println("CHECK.... source ["+sourceDir.getAbsolutePath()+"]");
  228.        
  229.         File[] files = sourceDir.listFiles();
  230.         if(files!=null && files.length>0) {
  231.            
  232.             // Prima esamino i file properties interi poi i diff
  233.             for(File upgradeDir : files) {
  234.                 if(upgradeDir.isDirectory() && (
  235.                         upgradeDir.getName().equals("core") || upgradeDir.getName().equals("protocolli")
  236.                         )) {
  237.                    
  238.                     File[] fileChilds = upgradeDir.listFiles();
  239.                     if(fileChilds!=null && fileChilds.length>0) {
  240.                        
  241.                         Arrays.sort(fileChilds); // sono ordinati per data
  242.                        
  243.                         for(File upgradeFile : fileChilds) {
  244.                             if(upgradeFile.getName().endsWith(".properties")) {
  245.                                 String key = upgradeFile.getName().substring(0, upgradeFile.getName().indexOf(".properties"));
  246.                                 if(configCreateFileFromDist.containsKey(key)==false) {
  247.                                     //System.out.println("ADDD PROPERTIES ["+key+"]");
  248.                                     configCreateFileFromDist.put(key, true);
  249.                                 }
  250.                             }
  251.                         }
  252.                     }
  253.                    
  254.                 }
  255.                
  256.             }
  257.            
  258.             Arrays.sort(files); // sono ordinati per data
  259.             for(File upgradeDir : files) {
  260.                
  261.                 if(upgradeDir.isDirectory() && (
  262.                         upgradeDir.getName().equals("core") || upgradeDir.getName().equals("protocolli")
  263.                         )) {
  264.                    
  265.                     File[] fileChilds = upgradeDir.listFiles();
  266.                     if(fileChilds!=null && fileChilds.length>0) {

  267.                         Arrays.sort(fileChilds); // sono ordinati per data
  268.                        
  269.                         for(File diffFile : fileChilds) {
  270.                
  271.                             if(diffFile.getName().endsWith(".diff")) {
  272.                                
  273.                                 String data= ".YYYY-MM-DD.diff";
  274.                                 //System.out.println("DIFF NAME ["+diffFile.getName()+"]");
  275.                                 String key = diffFile.getName().substring(0, (diffFile.getName().length()-data.length()));
  276.                                 //System.out.println("DIFF KEY ["+key+"]");
  277.                                 if(configCreateFileFromDist.containsKey(key)==false) {
  278.                                    
  279.                                     //System.out.println("ADDD DIFF");
  280.                                    
  281.                                     ByteArrayOutputStream bout = null;
  282.                                     if(configCreateFileDiff.containsKey(key)) {
  283.                                         bout = configCreateFileDiff.get(key);
  284.                                     }
  285.                                     else {
  286.                                         bout = new ByteArrayOutputStream();
  287.                                         configCreateFileDiff.put(key, bout);
  288.                                     }
  289.                                    
  290.                                     _createAggiornamento(diffFile, bout);  
  291.                                 }
  292.                             }
  293.                            
  294.                         }
  295.                     }
  296.                 }
  297.             }
  298.            
  299.         }

  300.     }
  301.    
  302.     private static void _createAggiornamento(File diffFile, ByteArrayOutputStream bout) throws Exception{
  303.        
  304.         byte[] b = FileSystemUtilities.readBytesFromFile(diffFile);
  305.         if(bout.size()>0){
  306.             bout.write("\n\n".getBytes());
  307.         }
  308.         bout.write(b);
  309.     }
  310.    
  311. }