Cache.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.OutputStream;
  23. import java.io.Serializable;
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Properties;

  30. import org.openspcoop2.utils.UtilsException;
  31. import org.slf4j.Logger;

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

  40.     public static boolean DEBUG_CACHE = false;
  41.    
  42.     private static Map<String, ICacheImpl> caches = new HashMap<String, ICacheImpl>();
  43.    
  44.     public static void setLog4jSystem() {
  45.         JCS3CacheImpl.setLog4jSystem();
  46.     }
  47.    
  48.     public static boolean initialize(Logger logConsole,Logger logCore,
  49.             String cachePropertiesName,
  50.             String rootDirectory,Properties objectProperties,
  51.             String OPENSPCOOP2_LOCAL_HOME,String OPENSPCOOP2_CACHE_PROPERTIES,String OPENSPCOOP2_CACHE_LOCAL_PATH){
  52.         return JCS3CacheImpl.initialize(logConsole, logCore,
  53.                 cachePropertiesName,
  54.                 rootDirectory, objectProperties,
  55.                 OPENSPCOOP2_LOCAL_HOME, OPENSPCOOP2_CACHE_PROPERTIES, OPENSPCOOP2_CACHE_LOCAL_PATH);
  56.     }
  57.    
  58.     public static String printStatistics(String separatorStat, String separatorCache) throws UtilsException {
  59.         try{
  60.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  61.             printStatistics(bout,separatorStat,separatorCache);
  62.             bout.flush();
  63.             bout.close();
  64.             return bout.toString();
  65.         }catch(Exception e){
  66.             throw new UtilsException(e.getMessage(),e);
  67.         }
  68.     }
  69.     public static void printStatistics(OutputStream out, String separatorStat, String separatorCache) throws UtilsException {
  70.         try{
  71.             if(Cache.caches!=null && !Cache.caches.isEmpty()) {
  72.                 List<String> keys = new ArrayList<>();
  73.                 keys.addAll(Cache.caches.keySet());
  74.                 Collections.sort(keys);
  75.                 for (String key : keys) {
  76.                     try {
  77.                         ICacheImpl cache = Cache.caches.get(key);
  78.                         if(cache!=null) {
  79.                             String s = cache.printStats(separatorStat);
  80.                             out.write(s.getBytes());
  81.                             out.write(separatorCache.getBytes());
  82.                         }
  83.                     }catch(Throwable t) {}
  84.                 }
  85.             }
  86.         }catch(Exception e){
  87.             throw new UtilsException(e.getMessage(),e);
  88.         }
  89.     }
  90.    
  91.    
  92.    
  93.    
  94.     private ICacheImpl cache = null;
  95.    
  96.     public Cache(CacheType cacheType, String name) throws UtilsException{
  97.         switch (cacheType) {
  98.         case JCS:
  99.             this.cache = new JCS3CacheImpl(name);
  100.             break;
  101.         case LimitedHashMap:
  102.             this.cache = new LimitedHashMapCacheImpl(name);
  103.             break;
  104.         case EH:
  105.             this.cache = new EHCacheImpl(name);
  106.             break;
  107.         }
  108.     }
  109.        
  110.     public static boolean DEFAULT_DISABLE_SYNCRONIZED_GET = false;
  111.     public static boolean DISABLE_SYNCRONIZED_GET = true;
  112.     public static void disableSyncronizedGetAsDefault() {
  113.         DEFAULT_DISABLE_SYNCRONIZED_GET = DISABLE_SYNCRONIZED_GET;
  114.     }
  115.     public static boolean isDisableSyncronizedGetAsDefault() {
  116.         return DEFAULT_DISABLE_SYNCRONIZED_GET;
  117.     }
  118.    
  119.     @SuppressWarnings("deprecation")
  120.     @Deprecated
  121.     public void disableSyncronizedGet() throws UtilsException {
  122.         if(this.cache==null) {
  123.             throw new UtilsException("Cache disabled");
  124.         }
  125.         this.cache.disableSyncronizedGet();
  126.     }
  127.     @SuppressWarnings("deprecation")
  128.     @Deprecated
  129.     public boolean isDisableSyncronizedGet() throws UtilsException {
  130.         if(this.cache==null) {
  131.             throw new UtilsException("Cache disabled");
  132.         }
  133.         return this.cache.isDisableSyncronizedGet();
  134.     }
  135.    
  136.     @SuppressWarnings("deprecation")
  137.     @Deprecated
  138.     public void enableDebugSystemOut() throws UtilsException {
  139.         if(this.cache==null) {
  140.             throw new UtilsException("Cache disabled");
  141.         }
  142.         this.cache.enableDebugSystemOut();
  143.     }
  144.     @SuppressWarnings("deprecation")
  145.     @Deprecated
  146.     boolean isEnableDebugSystemOut() throws UtilsException {
  147.         if(this.cache==null) {
  148.             throw new UtilsException("Cache disabled");
  149.         }
  150.         return this.cache.isEnableDebugSystemOut();
  151.     }
  152.    

  153.     public int getCacheSize() {
  154.         if(this.cache==null) {
  155.             throw new RuntimeException("Cache not initialized");
  156.         }
  157.         return this.cache.getCacheSize();
  158.     }
  159.     public void setCacheSize(int cacheSize) {
  160.         if(this.cache==null) {
  161.             throw new RuntimeException("Cache not initialized");
  162.         }
  163.         this.cache.setCacheSize(cacheSize);
  164.     }

  165.     public CacheAlgorithm getCacheAlgoritm() {
  166.         if(this.cache==null) {
  167.             throw new RuntimeException("Cache not initialized");
  168.         }
  169.         return this.cache.getCacheAlgoritm();
  170.     }
  171.     public void setCacheAlgoritm(CacheAlgorithm cacheAlgoritm) {
  172.         if(this.cache==null) {
  173.             throw new RuntimeException("Cache not initialized");
  174.         }
  175.         this.cache.setCacheAlgoritm(cacheAlgoritm);
  176.     }

  177.     public long getItemIdleTime() throws UtilsException {
  178.         if(this.cache==null) {
  179.             throw new RuntimeException("Cache not initialized");
  180.         }
  181.         return this.cache.getItemIdleTime();
  182.     }
  183.     public void setItemIdleTime(long itemIdleTimeCache) throws UtilsException {
  184.         if(this.cache==null) {
  185.             throw new RuntimeException("Cache not initialized");
  186.         }
  187.         this.cache.setItemIdleTime(itemIdleTimeCache);
  188.     }

  189.     public long getItemLifeTime() throws UtilsException {
  190.         if(this.cache==null) {
  191.             throw new RuntimeException("Cache not initialized");
  192.         }
  193.         return this.cache.getItemLifeTime();
  194.     }
  195.     public boolean isEternal() throws UtilsException {
  196.         if(this.cache==null) {
  197.             throw new RuntimeException("Cache not initialized");
  198.         }
  199.         return this.cache.isEternal();
  200.     }
  201.     public void setItemLifeTime(long itemLifeTimeCache) throws UtilsException {
  202.         if(this.cache==null) {
  203.             throw new RuntimeException("Cache not initialized");
  204.         }
  205.         this.cache.setItemLifeTime(itemLifeTimeCache);
  206.     }
  207.    
  208.     public void build() throws UtilsException{
  209.         if(this.cache==null) {
  210.             throw new RuntimeException("Cache not initialized");
  211.         }
  212.         this.cache.build();
  213.         Cache.caches.put(this.cache.getName(), this.cache);
  214.     }

  215.    
  216.    
  217.    
  218.     public void clear() throws UtilsException{
  219.         if(this.cache!=null){
  220.             try{
  221.                 this.cache.clear();
  222.             }catch(Exception e){
  223.                 throw new UtilsException(e.getMessage(),e);
  224.             }
  225.         }
  226.     }

  227.     public Object get(String key){
  228.         if(this.cache==null) {
  229.             throw new RuntimeException("Cache not initialized");
  230.         }
  231.         if(DEBUG_CACHE) {
  232.             System.out.println("GET @"+this.cache.getName()+" ["+key+"]");
  233.         }
  234.         return this.cache.get(key);
  235.     }
  236.    
  237.     public void remove(String key) throws UtilsException{
  238.         if(this.cache==null) {
  239.             throw new RuntimeException("Cache not initialized");
  240.         }
  241.         try{
  242.             this.cache.remove(key);
  243.         }catch(Exception e){
  244.             throw new UtilsException(e.getMessage(),e);
  245.         }  
  246.     }
  247.    
  248.     public void put(String key,org.openspcoop2.utils.cache.CacheResponse value) throws UtilsException{
  249.         if(this.cache==null) {
  250.             throw new RuntimeException("Cache not initialized");
  251.         }
  252.         try{
  253.             this.cache.put(key, value);
  254.         }catch(Exception e){
  255.             throw new UtilsException(e.getMessage(),e);
  256.         }
  257.     }
  258.     public void put(String key,Serializable value) throws UtilsException{
  259.         if(this.cache==null) {
  260.             throw new RuntimeException("Cache not initialized");
  261.         }
  262.         try{
  263.             this.cache.put(key, value);
  264.         }catch(Exception e){
  265.             throw new UtilsException(e.getMessage(),e);
  266.         }
  267.     }
  268.    
  269.     public int getItemCount()  throws UtilsException {
  270.         if(this.cache==null) {
  271.             throw new RuntimeException("Cache not initialized");
  272.         }
  273.         return this.cache.getItemCount();
  274.     }
  275.    
  276.     public List<String> keys() throws UtilsException {
  277.         if(this.cache==null) {
  278.             throw new RuntimeException("Cache not initialized");
  279.         }
  280.         return this.cache.keys();
  281.     }
  282.    
  283.     public String printStats(String separator) throws UtilsException {
  284.         if(this.cache==null) {
  285.             throw new RuntimeException("Cache not initialized");
  286.         }
  287.         return this.cache.printStats(separator);
  288.     }
  289.     public void printStats(OutputStream out, String separator) throws UtilsException {
  290.         if(this.cache==null) {
  291.             throw new RuntimeException("Cache not initialized");
  292.         }
  293.         this.cache.printStats(out, separator);
  294.     }
  295.        
  296.     @Override
  297.     public String toString(){
  298.         if(this.cache==null) {
  299.             //throw new RuntimeException("Cache not initialized");
  300.             return "NonDisponibile";
  301.         }
  302.         return this.cache.toString();
  303.     }
  304.     public String printKeys(String separator) throws UtilsException{
  305.         if(this.cache==null) {
  306.             throw new RuntimeException("Cache not initialized");
  307.         }
  308.         return this.cache.printKeys(separator);
  309.     }


  310. }