CacheBuilder.java

  1. /*
  2.  *    Copyright 2009-2024 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.mapping;

  17. import java.lang.reflect.Constructor;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Properties;

  22. import org.apache.ibatis.builder.InitializingObject;
  23. import org.apache.ibatis.cache.Cache;
  24. import org.apache.ibatis.cache.CacheException;
  25. import org.apache.ibatis.cache.decorators.BlockingCache;
  26. import org.apache.ibatis.cache.decorators.LoggingCache;
  27. import org.apache.ibatis.cache.decorators.LruCache;
  28. import org.apache.ibatis.cache.decorators.ScheduledCache;
  29. import org.apache.ibatis.cache.decorators.SerializedCache;
  30. import org.apache.ibatis.cache.decorators.SynchronizedCache;
  31. import org.apache.ibatis.cache.impl.PerpetualCache;
  32. import org.apache.ibatis.reflection.MetaObject;
  33. import org.apache.ibatis.reflection.SystemMetaObject;

  34. /**
  35.  * @author Clinton Begin
  36.  */
  37. public class CacheBuilder {
  38.   private final String id;
  39.   private Class<? extends Cache> implementation;
  40.   private final List<Class<? extends Cache>> decorators;
  41.   private Integer size;
  42.   private Long clearInterval;
  43.   private boolean readWrite;
  44.   private Properties properties;
  45.   private boolean blocking;

  46.   public CacheBuilder(String id) {
  47.     this.id = id;
  48.     this.decorators = new ArrayList<>();
  49.   }

  50.   public CacheBuilder implementation(Class<? extends Cache> implementation) {
  51.     this.implementation = implementation;
  52.     return this;
  53.   }

  54.   public CacheBuilder addDecorator(Class<? extends Cache> decorator) {
  55.     if (decorator != null) {
  56.       this.decorators.add(decorator);
  57.     }
  58.     return this;
  59.   }

  60.   public CacheBuilder size(Integer size) {
  61.     this.size = size;
  62.     return this;
  63.   }

  64.   public CacheBuilder clearInterval(Long clearInterval) {
  65.     this.clearInterval = clearInterval;
  66.     return this;
  67.   }

  68.   public CacheBuilder readWrite(boolean readWrite) {
  69.     this.readWrite = readWrite;
  70.     return this;
  71.   }

  72.   public CacheBuilder blocking(boolean blocking) {
  73.     this.blocking = blocking;
  74.     return this;
  75.   }

  76.   public CacheBuilder properties(Properties properties) {
  77.     this.properties = properties;
  78.     return this;
  79.   }

  80.   public Cache build() {
  81.     setDefaultImplementations();
  82.     Cache cache = newBaseCacheInstance(implementation, id);
  83.     setCacheProperties(cache);
  84.     // issue #352, do not apply decorators to custom caches
  85.     if (PerpetualCache.class.equals(cache.getClass())) {
  86.       for (Class<? extends Cache> decorator : decorators) {
  87.         cache = newCacheDecoratorInstance(decorator, cache);
  88.         setCacheProperties(cache);
  89.       }
  90.       cache = setStandardDecorators(cache);
  91.     } else if (!LoggingCache.class.isAssignableFrom(cache.getClass())) {
  92.       cache = new LoggingCache(cache);
  93.     }
  94.     return cache;
  95.   }

  96.   private void setDefaultImplementations() {
  97.     if (implementation == null) {
  98.       implementation = PerpetualCache.class;
  99.       if (decorators.isEmpty()) {
  100.         decorators.add(LruCache.class);
  101.       }
  102.     }
  103.   }

  104.   private Cache setStandardDecorators(Cache cache) {
  105.     try {
  106.       MetaObject metaCache = SystemMetaObject.forObject(cache);
  107.       if (size != null && metaCache.hasSetter("size")) {
  108.         metaCache.setValue("size", size);
  109.       }
  110.       if (clearInterval != null) {
  111.         cache = new ScheduledCache(cache);
  112.         ((ScheduledCache) cache).setClearInterval(clearInterval);
  113.       }
  114.       if (readWrite) {
  115.         cache = new SerializedCache(cache);
  116.       }
  117.       cache = new LoggingCache(cache);
  118.       cache = new SynchronizedCache(cache);
  119.       if (blocking) {
  120.         cache = new BlockingCache(cache);
  121.       }
  122.       return cache;
  123.     } catch (Exception e) {
  124.       throw new CacheException("Error building standard cache decorators.  Cause: " + e, e);
  125.     }
  126.   }

  127.   private void setCacheProperties(Cache cache) {
  128.     if (properties != null) {
  129.       MetaObject metaCache = SystemMetaObject.forObject(cache);
  130.       for (Map.Entry<Object, Object> entry : properties.entrySet()) {
  131.         String name = (String) entry.getKey();
  132.         String value = (String) entry.getValue();
  133.         if (metaCache.hasSetter(name)) {
  134.           Class<?> type = metaCache.getSetterType(name);
  135.           if (String.class == type) {
  136.             metaCache.setValue(name, value);
  137.           } else if (int.class == type || Integer.class == type) {
  138.             metaCache.setValue(name, Integer.valueOf(value));
  139.           } else if (long.class == type || Long.class == type) {
  140.             metaCache.setValue(name, Long.valueOf(value));
  141.           } else if (short.class == type || Short.class == type) {
  142.             metaCache.setValue(name, Short.valueOf(value));
  143.           } else if (byte.class == type || Byte.class == type) {
  144.             metaCache.setValue(name, Byte.valueOf(value));
  145.           } else if (float.class == type || Float.class == type) {
  146.             metaCache.setValue(name, Float.valueOf(value));
  147.           } else if (boolean.class == type || Boolean.class == type) {
  148.             metaCache.setValue(name, Boolean.valueOf(value));
  149.           } else if (double.class == type || Double.class == type) {
  150.             metaCache.setValue(name, Double.valueOf(value));
  151.           } else {
  152.             throw new CacheException("Unsupported property type for cache: '" + name + "' of type " + type);
  153.           }
  154.         }
  155.       }
  156.     }
  157.     if (InitializingObject.class.isAssignableFrom(cache.getClass())) {
  158.       try {
  159.         ((InitializingObject) cache).initialize();
  160.       } catch (Exception e) {
  161.         throw new CacheException(
  162.             "Failed cache initialization for '" + cache.getId() + "' on '" + cache.getClass().getName() + "'", e);
  163.       }
  164.     }
  165.   }

  166.   private Cache newBaseCacheInstance(Class<? extends Cache> cacheClass, String id) {
  167.     Constructor<? extends Cache> cacheConstructor = getBaseCacheConstructor(cacheClass);
  168.     try {
  169.       return cacheConstructor.newInstance(id);
  170.     } catch (Exception e) {
  171.       throw new CacheException("Could not instantiate cache implementation (" + cacheClass + "). Cause: " + e, e);
  172.     }
  173.   }

  174.   private Constructor<? extends Cache> getBaseCacheConstructor(Class<? extends Cache> cacheClass) {
  175.     try {
  176.       return cacheClass.getConstructor(String.class);
  177.     } catch (Exception e) {
  178.       throw new CacheException("Invalid base cache implementation (" + cacheClass + ").  "
  179.           + "Base cache implementations must have a constructor that takes a String id as a parameter.  Cause: " + e,
  180.           e);
  181.     }
  182.   }

  183.   private Cache newCacheDecoratorInstance(Class<? extends Cache> cacheClass, Cache base) {
  184.     Constructor<? extends Cache> cacheConstructor = getCacheDecoratorConstructor(cacheClass);
  185.     try {
  186.       return cacheConstructor.newInstance(base);
  187.     } catch (Exception e) {
  188.       throw new CacheException("Could not instantiate cache decorator (" + cacheClass + "). Cause: " + e, e);
  189.     }
  190.   }

  191.   private Constructor<? extends Cache> getCacheDecoratorConstructor(Class<? extends Cache> cacheClass) {
  192.     try {
  193.       return cacheClass.getConstructor(Cache.class);
  194.     } catch (Exception e) {
  195.       throw new CacheException("Invalid cache decorator (" + cacheClass + ").  "
  196.           + "Cache decorators must have a constructor that takes a Cache instance as a parameter.  Cause: " + e, e);
  197.     }
  198.   }
  199. }