IDSerialGenerator.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.protocol.utils;

  21. import java.sql.Connection;
  22. import java.sql.SQLException;

  23. import javax.sql.DataSource;

  24. import org.slf4j.Logger;
  25. import org.openspcoop2.protocol.sdk.ProtocolException;
  26. import org.openspcoop2.protocol.sdk.state.IState;
  27. import org.openspcoop2.protocol.sdk.state.StateMessage;
  28. import org.openspcoop2.utils.TipiDatabase;
  29. import org.openspcoop2.utils.id.serial.IDSerialGeneratorParameter;
  30. import org.openspcoop2.utils.id.serial.IDSerialGeneratorType;
  31. import org.openspcoop2.utils.jdbc.JDBCUtilities;

  32. /**
  33.  * IDSerialGenerator
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class IDSerialGenerator {

  40.     // ** STATIC INIT *** //

  41.     private static DataSource datasourcePdD = null;
  42.     public static synchronized void init(DataSource ds){
  43.         if(IDSerialGenerator.datasourcePdD==null){
  44.             IDSerialGenerator.datasourcePdD = ds;
  45.         }
  46.     }
  47.     /** public static DataSource getDatasourcePdD() {
  48.             return IDCounterGenerator.datasourcePdD;
  49.         }
  50.         public static void setDatasourcePdD(DataSource datasourcePdD) {
  51.             IDCounterGenerator.datasourcePdD = datasourcePdD;
  52.         }*/
  53.     private static Connection getConnectionPdD() throws SQLException, ProtocolException{
  54.         if(IDSerialGenerator.datasourcePdD==null){
  55.             throw new ProtocolException("IDSerialGenerator non inizializzato");
  56.         }
  57.         return IDSerialGenerator.datasourcePdD.getConnection();
  58.     }
  59.     private static void releaseConnectionPdD(Connection con) throws SQLException{
  60.         JDBCUtilities.closeConnection(IDSerialGenerator.getCheckLogger(), con, IDSerialGenerator.isCheckAutocommit(), IDSerialGenerator.isCheckIsClosed());
  61.     }

  62.     static Logger checkLogger = null;
  63.     static boolean checkIsClosed = true;
  64.     static boolean checkAutocommit = true;
  65.     public static boolean isCheckIsClosed() {
  66.         return checkIsClosed;
  67.     }
  68.     public static void setCheckIsClosed(boolean checkIsClosed) {
  69.         IDSerialGenerator.checkIsClosed = checkIsClosed;
  70.     }
  71.     public static boolean isCheckAutocommit() {
  72.         return checkAutocommit;
  73.     }
  74.     public static void setCheckAutocommit(boolean checkAutocommit) {
  75.         IDSerialGenerator.checkAutocommit = checkAutocommit;
  76.     }
  77.     public static Logger getCheckLogger() {
  78.         return checkLogger;
  79.     }
  80.     public static void setCheckLogger(Logger checkLogger) {
  81.         IDSerialGenerator.checkLogger = checkLogger;
  82.     }
  83.    
  84.     // ** STATIC INIT *** //



  85.     private IState state;
  86.     private Logger log;
  87.     private TipiDatabase tipoDatabase;
  88.     public IDSerialGenerator(Logger log, IState state, TipiDatabase tipoDatabase){
  89.         this.log = log;
  90.         this.state = state;
  91.         this.tipoDatabase = tipoDatabase;
  92.     }

  93.     public long buildIDAsNumber(IDSerialGeneratorParameter param) throws ProtocolException {
  94.         try{
  95.             if(param!=null && IDSerialGeneratorType.ALFANUMERICO.equals(param.getTipo())){
  96.                 throw new ProtocolException("IDSerialGeneratorType["+param.getTipo()+"] prevede anche caratteri alfanumerici");
  97.             }
  98.             return Long.parseLong(this.buildID(param));
  99.         }catch(Exception e){
  100.             throw new ProtocolException(e.getMessage(),e);
  101.         }
  102.     }
  103.     public String buildID(IDSerialGeneratorParameter param) throws ProtocolException {

  104.         // Check connessione
  105.         Connection conDB = null;
  106.         boolean conDBFromDatasource = false;
  107.         try{
  108.             if(this.state instanceof StateMessage){
  109.                 StateMessage stateMsg = (StateMessage) this.state;
  110.                 if(stateMsg.getConnectionDB()!=null && !stateMsg.getConnectionDB().isClosed()){
  111.                     /**System.out.println("PRESA DALLO STATO");*/
  112.                     conDB = stateMsg.getConnectionDB();
  113.                 }
  114.             }
  115.             if(conDB==null){
  116.                 /**System.out.println("PRESA DAL DATASOURCE");*/
  117.                 conDB = IDSerialGenerator.getConnectionPdD();
  118.                 conDBFromDatasource = true;
  119.             }
  120.         }catch(Exception e){
  121.             throw new ProtocolException("Connessione non disponibile: "+e.getMessage(),e);
  122.         }
  123.         checkConnection(conDB);

  124.         String identificativoUnivoco = null;
  125.         try{

  126.             org.openspcoop2.utils.id.serial.IDSerialGenerator generator = new org.openspcoop2.utils.id.serial.IDSerialGenerator();
  127.             identificativoUnivoco = generator.buildID(param, conDB, this.tipoDatabase, this.log);
  128.             return identificativoUnivoco;
  129.         }
  130.         catch(Exception e){
  131.             throw new ProtocolException(e.getMessage(),e);
  132.         }
  133.         finally{

  134.             try{
  135.                 if(conDBFromDatasource)
  136.                     IDSerialGenerator.releaseConnectionPdD(conDB);  
  137.             }catch(Exception e){
  138.                 if(this.log!=null) {
  139.                     this.log.error("Rilascio connessione non riuscito: "+e.getMessage(),e);
  140.                 }
  141.                 /**throw new ProtocolException("Rilascio connessione non riuscito: "+e.getMessage(),e);*/
  142.             }

  143.         }

  144.     }

  145.     private void checkConnection(Connection conDB) throws ProtocolException {
  146.         if(conDB == null){
  147.             throw new ProtocolException("Connessione non disponibile");
  148.         }
  149.     }

  150. }