VaultDatabaseProperties.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.vault.cli;

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

  23. import org.openspcoop2.core.commons.CoreException;

  24. /**
  25. * VaultDatabaseProperties
  26. *
  27. * @author Poli Andrea (apoli@link.it)
  28. * @author $Author$
  29. * @version $Rev$, $Date$
  30. */
  31. public class VaultDatabaseProperties {
  32.    
  33.     private static VaultDatabaseProperties staticInstance = null;
  34.     private static synchronized void init() throws CoreException{
  35.         if(VaultDatabaseProperties.staticInstance == null){
  36.             VaultDatabaseProperties.staticInstance = new VaultDatabaseProperties();
  37.         }
  38.     }
  39.     public static VaultDatabaseProperties getInstance() throws CoreException{
  40.         if(VaultDatabaseProperties.staticInstance == null){
  41.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
  42.             synchronized (VaultDatabaseProperties.class) {
  43.                 VaultDatabaseProperties.init();
  44.             }
  45.         }
  46.         return VaultDatabaseProperties.staticInstance;
  47.     }
  48.    
  49.    
  50.    
  51.    
  52.     private static final String PROPERTIES_FILE = "/govway_vault.cli.database.properties";
  53.    
  54.     private String tipoDatabase = null;
  55.     private String driver = null;
  56.     private String connectionUrl = null;
  57.     private String username = null;
  58.     private String password = null;
  59.    
  60.    
  61.     private VaultDatabaseProperties() throws CoreException {

  62.         Properties props = new Properties();
  63.         try {
  64.             InputStream is = VaultDatabaseProperties.class.getResourceAsStream(VaultDatabaseProperties.PROPERTIES_FILE);
  65.             props.load(is);
  66.         } catch(Exception e) {
  67.             throw new CoreException("Errore durante l'init delle properties", e);
  68.         }
  69.        
  70.         // PROPERTIES
  71.                
  72.         this.tipoDatabase = this.getProperty(props, "tipoDatabase", true);
  73.        
  74.         this.driver = this.getProperty(props, "driver", true);
  75.         this.connectionUrl = this.getProperty(props, "connection-url", true);
  76.         this.username = this.getProperty(props, "username", true);
  77.         this.password = this.getProperty(props, "password", true);

  78.     }
  79.    
  80.     private String getProperty(Properties props,String name,boolean required) throws CoreException{
  81.         String tmp = props.getProperty(name);
  82.         if(tmp==null){
  83.             if(required){
  84.                 throw new CoreException("Property '"+name+"' not found");
  85.             }
  86.             else{
  87.                 return null;
  88.             }
  89.         }
  90.         else{
  91.             return tmp.trim();
  92.         }
  93.     }

  94.     public String getTipoDatabase() {
  95.         return this.tipoDatabase;
  96.     }
  97.     public String getDriver() {
  98.         return this.driver;
  99.     }
  100.     public String getConnectionUrl() {
  101.         return this.connectionUrl;
  102.     }
  103.     public String getUsername() {
  104.         return this.username;
  105.     }
  106.     public String getPassword() {
  107.         return this.password;
  108.     }
  109. }