CacheResponse.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.cache;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.PrintStream;


  23. /**
  24.  * Classe utilizzata per contenere la risposta ottenuta da una query effettuata su di un registro dei servizi.
  25.  * La risposta puo' essere un oggetto serializzabile, memorizzato nella proprieta' <code>object</code> o un valore di tipo boolean,
  26.  * memorizzato nella proprieta' <code>value</code>.
  27.  * Inoltre la risposta ritornata da una query effettuata sul registro puo' contenere anche un oggetto null.
  28.  * La presenza di un oggetto CacheResponse in cache con la proprieta' <code>object</object> impostata a null
  29.  * indica proprio che esiste una risposta di una query con valore null.  
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class CacheResponse implements java.io.Serializable {

  36.     /**
  37.      * serialVersionUID
  38.      */
  39.     private static final long serialVersionUID = 1L;

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

  41.     /** Object Response. */
  42.     private java.io.Serializable object;
  43.     /** Object Response null */
  44.     private boolean objectNull = false;
  45.     /** Exception Response. */
  46.     private java.io.Serializable exception;


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

  48.     /**
  49.      * Costruttore.
  50.      *
  51.      *
  52.      */
  53.     public CacheResponse(){  
  54.     }
  55.     /**
  56.      * Costruttore.
  57.      *
  58.      * @param o ObjectResponse
  59.      *
  60.      */
  61.     public CacheResponse(java.io.Serializable o){
  62.         this.object = o;
  63.     }






  64.     /* ********  S E T T E R   ******** */

  65.     /**
  66.      * Imposta la proprieta' <code>object</code> (deve implementare l'interfaccia java.io.Serializable)
  67.      *
  68.      * @param o valore della proprieta' <code>object</code>
  69.      *
  70.      */
  71.     public void setObject(java.io.Serializable o){
  72.         this.object = o;
  73.     }
  74.     public void setException(java.io.Serializable exception) {
  75.         this.exception = exception;
  76.     }
  77.     public void setObjectNull(boolean objectNull) {
  78.         this.objectNull = objectNull;
  79.     }



  80.     /* ********  G E T T E R   ******** */

  81.     /**
  82.      * Ritorna la proprieta' <code>object</code>
  83.      *
  84.      * @return valore della proprieta' <code>object</code>
  85.      *
  86.      */
  87.     public java.io.Serializable getObject(){
  88.         return this.object;
  89.     }
  90.     public java.io.Serializable getException() {
  91.         return this.exception;
  92.     }
  93.     public boolean isObjectNull() {
  94.         return this.objectNull;
  95.     }



  96.    
  97.     @Override
  98.     public String toString(){
  99.         if(this.object!=null){
  100.             return "OBJECT: \n"+this.object.toString();
  101.         }
  102.         else if(this.objectNull){
  103.             return "CACHED NULL VALUE";
  104.         }
  105.         else if(this.exception!=null){
  106.             try{
  107.                 ByteArrayOutputStream bout = new ByteArrayOutputStream();
  108.                 PrintStream p = new PrintStream(bout);
  109.                 ((Exception)this.exception).printStackTrace(p);
  110.                 p.flush();
  111.                 p.close();
  112.                 bout.flush();
  113.                 bout.close();
  114.                 return bout.toString();
  115.             }catch(Exception e){
  116.                 return "CACHE EXCEPTION ERROR: "+e.getMessage();
  117.             }
  118.         }
  119.         else{
  120.             return "CACHE OBJECT EMPTY ?";
  121.         }
  122.     }
  123. }