StateMap.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.sdk.state;

  21. import java.sql.PreparedStatement;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;

  26. import org.slf4j.Logger;

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

  35.     /** Tabella Hash contenente le PreparedStatement da eseguire */
  36.     private Map<String,PreparedStatement> tablePstmt;
  37.    
  38.     /** Logger */
  39.     private Logger log = null;
  40.    
  41.     public StateMap(Logger log) {
  42.         this.tablePstmt = new HashMap<String,PreparedStatement>();
  43.         this.log = log;
  44.     }
  45.     public StateMap(HashMap<String, PreparedStatement> preparedStatement, Logger log){
  46.         this.tablePstmt = preparedStatement;
  47.         this.log = log;
  48.     }
  49.    
  50.     public Map<String,PreparedStatement> getPreparedStatement(){
  51.         return this.tablePstmt;
  52.     }
  53.     public void setPreparedStatement(Map<String,PreparedStatement> pstm){
  54.         this.tablePstmt = pstm;
  55.     }
  56.    
  57.     public void put(String key, PreparedStatement pstmt){
  58.         if(this.tablePstmt.containsKey(key)){
  59.             // prima di aggiornare chiudo la preparedStatement
  60.             try{
  61.                 this.tablePstmt.get(key).close();
  62.             }catch(Exception e){
  63.                 this.log.debug("ClosePreparedStatement already exists error: Riscontrato errore durante la chiusura della PreparedStatement ["+key+"]: "+e);
  64.             }
  65.         }
  66.         this.tablePstmt.put(key, pstmt);
  67.     }
  68.    
  69.     public int size(){
  70.         return this.tablePstmt.size();
  71.     }
  72.    
  73.     public List<String> keys(){
  74.         List<String> keys = null;
  75.         if(!this.tablePstmt.isEmpty()) {
  76.             keys = new ArrayList<>();
  77.             for (String key : this.tablePstmt.keySet()) {
  78.                 keys.add(key);
  79.             }
  80.         }
  81.         return keys;
  82.     }
  83.    
  84.     public boolean containsKey(String key){
  85.         return this.tablePstmt.containsKey(key);
  86.     }
  87. }