InfoStatistics.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.id.serial;

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

  25. import org.openspcoop2.utils.SemaphoreLock;
  26. import org.openspcoop2.utils.Utilities;

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

  35.     private int errorSerializableAccess = 0;
  36.     private List<Throwable> exceptionOccurs = new ArrayList<Throwable>();
  37.     private List<String> _exceptionOccursDistincts = new ArrayList<>();
  38.     private Map<String, Integer> _occurs = new HashMap<String, Integer>();
  39.    
  40.     public int getErrorSerializableAccess() {
  41.         return this.errorSerializableAccess;
  42.     }
  43.     public List<Throwable> getExceptionOccurs() {
  44.         return this.exceptionOccurs;
  45.     }
  46.     public int getNumber(Throwable e){
  47.         return this._occurs.get(e.getMessage());
  48.     }
  49.    
  50.     private org.openspcoop2.utils.Semaphore semaphore = new org.openspcoop2.utils.Semaphore("InfoStatistics");
  51.     public void addErrorSerializableAccess(Throwable e){
  52.         SemaphoreLock lock = this.semaphore.acquireThrowRuntime("addErrorSerializableAccess");
  53.         try {
  54.             Throwable msgE = Utilities.getInnerNotEmptyMessageException(e);
  55.             String msg = null;
  56.             if(msgE!=null) {
  57.                 msg = msgE.getMessage();
  58.             }
  59.             else {
  60.                 msg = e.toString();
  61.             }
  62.             if(msg==null) {
  63.                 msg = "NullException";
  64.             }
  65.             this.errorSerializableAccess++;
  66.             if(this._exceptionOccursDistincts.contains(msg)==false){
  67.                 this._exceptionOccursDistincts.add(msg);
  68.                 this.exceptionOccurs.add(e);
  69.                 this._occurs.put(msg, 1);
  70.             }
  71.             else{
  72.                 int occur = this._occurs.remove(msg);
  73.                 occur++;
  74.                 this._occurs.put(msg, occur);
  75.             }
  76.         }finally {
  77.             this.semaphore.release(lock, "addErrorSerializableAccess");
  78.         }
  79.     }
  80.    
  81.     public void clear(){
  82.         this.errorSerializableAccess = 0;
  83.         this.exceptionOccurs.clear();
  84.         this._exceptionOccursDistincts.clear();
  85.     }
  86. }