VersionUtilities.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;

  21. import java.io.InputStream;
  22. import java.util.Properties;

  23. import org.apache.commons.lang.StringUtils;

  24. /**
  25.  * VersionUtilities
  26.  *
  27.  * @author Poli Andrea (apoli@link.it)
  28.  * @author $Author$
  29.  * @version $Rev$, $Date$
  30.  */
  31. public class VersionUtilities {

  32.     private static String PROPERTY_FILE_RESOURCE_NAME = "/govwayVersion.properties";
  33.     public static void setPROPERTY_FILE_RESOURCE_NAME(String PROPERTY_FILE_RESOURCE_NAME) {
  34.         VersionUtilities.PROPERTY_FILE_RESOURCE_NAME = PROPERTY_FILE_RESOURCE_NAME;
  35.     }
  36.    
  37.     private static String BUILD_VERSION_PROPERTY = "buildVersion";
  38.     public static void setBUILD_VERSION_PROPERTY(String bUILD_VERSION_PROPERTY) {
  39.         VersionUtilities.BUILD_VERSION_PROPERTY = bUILD_VERSION_PROPERTY;
  40.     }
  41.    
  42.     private static String VERSION_PROPERTY = "version";
  43.     public static void setVERSION_PROPERTY(String VERSION_PROPERTY) {
  44.         VersionUtilities.VERSION_PROPERTY = VERSION_PROPERTY;
  45.     }
  46.    
  47.     private static String INFO_VERSION_PROPERTY = "infoVersion";
  48.     public static void setINFO_VERSION_PROPERTY(String INFO_VERSION_PROPERTY) {
  49.         VersionUtilities.INFO_VERSION_PROPERTY = INFO_VERSION_PROPERTY;
  50.     }
  51.    
  52.     public static String readVersion() throws UtilsException {
  53.         return _read(VersionUtilities.VERSION_PROPERTY);
  54.     }
  55.     public static String readVersion(InputStream is) throws UtilsException {
  56.         return _read(is, VersionUtilities.VERSION_PROPERTY);
  57.     }
  58.     public static String readVersion(Properties p) {
  59.         return _read(p, VersionUtilities.VERSION_PROPERTY);
  60.     }
  61.    
  62.     public static String readBuildVersion() throws UtilsException {
  63.         return _read(VersionUtilities.BUILD_VERSION_PROPERTY);
  64.     }
  65.     public static String readBuildVersion(InputStream is) throws UtilsException {
  66.         return _read(is, VersionUtilities.BUILD_VERSION_PROPERTY);
  67.     }
  68.     public static String readBuildVersion(Properties p) {
  69.         return _read(p, VersionUtilities.BUILD_VERSION_PROPERTY);
  70.     }
  71.    
  72.     public static IVersionInfo readInfoVersion() throws UtilsException {
  73.         return _instance(_read(VersionUtilities.INFO_VERSION_PROPERTY));
  74.     }
  75.     public static IVersionInfo readInfoVersion(InputStream is) throws UtilsException {
  76.         return _instance(_read(is, VersionUtilities.INFO_VERSION_PROPERTY));
  77.     }
  78.     public static IVersionInfo readInfoVersion(Properties p) throws UtilsException {
  79.         return _instance(_read(p, VersionUtilities.INFO_VERSION_PROPERTY));
  80.     }
  81.     private static IVersionInfo _instance(String className) throws UtilsException {
  82.         if(className!=null && !StringUtils.isEmpty(className)) {
  83.             try {
  84.                 return  (IVersionInfo) Utilities.newInstance(className);
  85.             }catch(Exception e) {
  86.                 throw new UtilsException(e.getMessage(), e);
  87.             }
  88.         }
  89.         return null;
  90.     }
  91.    
  92.     private static String _read(String propertyName) throws UtilsException {
  93.         InputStream is = VersionUtilities.class.getResourceAsStream(VersionUtilities.PROPERTY_FILE_RESOURCE_NAME);
  94.         return _read(is, propertyName);
  95.     }
  96.     private static String _read(InputStream is, String propertyName) throws UtilsException {
  97.         try {
  98.             if(is!=null) {
  99.                 Properties p = new Properties();
  100.                 p.load(is);
  101.                 return _read(p,propertyName);
  102.             }
  103.             return null;
  104.         }catch(Exception e) {
  105.             throw new UtilsException(e.getMessage(),e);
  106.         }
  107.     }
  108.     private static String _read(Properties p, String propertyName) {
  109.         String s = p.getProperty(propertyName);
  110.         if(s!=null) {
  111.             s = s.trim();
  112.         }
  113.         return s;
  114.     }
  115.    
  116. }