DatasourceProperties.java

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



package org.openspcoop2.core.monitor.rs.server.config;


import java.util.Properties;

import org.slf4j.Logger;
import org.openspcoop2.utils.LoggerWrapperFactory;
import org.openspcoop2.utils.UtilsException;


/**
* DatasourceProperties
*
* @author Andrea Poli (apoli@link.it)
 * @author $Author$
 * @version $Rev$, $Date$
*/


public class DatasourceProperties {	

	/** Logger utilizzato per errori eventuali. */
	private Logger log = null;



	/* ********  F I E L D S  P R I V A T I  ******** */

	/** Reader delle proprieta' impostate nel file 'rs-api-monitor.datasource.properties' */
	private DatasourceInstanceProperties reader;

	/** Copia Statica */
	private static DatasourceProperties datasourceProperties = null;


	/* ********  C O S T R U T T O R E  ******** */

	/**
	 * Viene chiamato in causa per istanziare il properties reader
	 *
	 * 
	 */
	private DatasourceProperties(String confDir,Logger log) throws Exception {

		if(log!=null)
			this.log = log;
		else
			this.log = LoggerWrapperFactory.getLogger(DatasourceProperties.class);
		
		/* ---- Lettura del cammino del file di configurazione ---- */
		Properties propertiesReader = new Properties();
		java.io.InputStream properties = null;
		try{  
			properties = DatasourceProperties.class.getResourceAsStream("/rs-api-monitor.datasource.properties");
			if(properties==null){
				throw new Exception("File '/rs-api-monitor.datasource.properties' not found");
			}
			propertiesReader.load(properties);
		}catch(Exception e) {
			this.log.error("Riscontrato errore durante la lettura del file 'rs-api-monitor.datasource.properties': \n\n"+e.getMessage());
		    throw new Exception("RS Api MonitorProperties initialize error: "+e.getMessage());
		}finally{
		    try{
				if(properties!=null)
				    properties.close();
		    }catch(Exception er){
		    	// close
		    }
		}

		this.reader = new DatasourceInstanceProperties(propertiesReader, this.log, confDir);
	}


	/**
	 * Il Metodo si occupa di inizializzare il propertiesReader 
	 *
	 * 
	 */
	public static boolean initialize(String confDir,Logger log){

		try {
		    DatasourceProperties.datasourceProperties = new DatasourceProperties(confDir,log);	
		    return true;
		}
		catch(Exception e) {
			log.error("Errore durante l'inizializzazione del BackendProperties: "+e.getMessage(),e);
		    return false;
		}
	}
    
	/**
	 * Ritorna l'istanza di questa classe
	 *
	 * @return Istanza di Properties
	 * 
	 */
	public static DatasourceProperties getInstance() throws UtilsException{
		if(DatasourceProperties.datasourceProperties==null){
			// spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED': l'istanza viene creata allo startup
			synchronized (DatasourceProperties.class) {
				throw new UtilsException("DatasourceProperties non inizializzato");
			}
	    }
	    return DatasourceProperties.datasourceProperties;
	}
    
	public static void updateLocalImplementation(Properties prop){
		DatasourceProperties.datasourceProperties.reader.setLocalObjectImplementation(prop);
	}








	/* ********  M E T O D I  ******** */

	private String readProperty(boolean required,String property) throws UtilsException{
		String tmp = this.reader.getValueConvertEnvProperties(property);
		if(tmp==null){
			if(required){
				throw new UtilsException("Property ["+property+"] not found");
			}
			else{
				return null;
			}
		}else{
			return tmp.trim();
		}
	}

	

	
	/* ----- DB -------- */
	
	public boolean isShowSql() throws UtilsException{
		return Boolean.parseBoolean(this.readProperty(true, "db.showSql"));
	}
	
	
	
	public String getConfigDataSource() throws UtilsException{
		return this.readProperty(true, "db.config.dataSource");
	}
	
	public Properties getConfigDataSourceContext() throws UtilsException{
		return this.reader.readPropertiesConvertEnvProperties("db.config.dataSource.property.");
	}
	
	public String getConfigTipoDatabase() throws UtilsException{
		return this.readProperty(true, "db.config.tipoDatabase");
	}
	
	
	public String getTracceDataSource() throws UtilsException{
		return this.readProperty(true, "db.tracce.dataSource");
	}
	
	public Properties getTracceDataSourceContext() throws UtilsException{
		return this.reader.readPropertiesConvertEnvProperties("db.tracce.dataSource.property.");
	}
	
	public String getTracceTipoDatabase() throws UtilsException{
		return this.readProperty(true, "db.tracce.tipoDatabase");
	}
	
	
	public String getStatisticheDataSource() throws UtilsException{
		return this.readProperty(true, "db.statistiche.dataSource");
	}
	
	public Properties getStatisticheDataSourceContext() throws UtilsException{
		return this.reader.readPropertiesConvertEnvProperties("db.statistiche.dataSource.property.");
	}
	
	public String getStatisticheTipoDatabase() throws UtilsException{
		return this.readProperty(true, "db.statistiche.tipoDatabase");
	}
	


}