JCSCachingProvider.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.jcs3.jcache;

  20. import javax.cache.CacheManager;
  21. import javax.cache.configuration.OptionalFeature;
  22. import javax.cache.spi.CachingProvider;
  23. import java.net.URI;
  24. import java.util.Map;
  25. import java.util.Properties;
  26. import java.util.concurrent.ConcurrentHashMap;
  27. import java.util.concurrent.ConcurrentMap;

  28. public class JCSCachingProvider implements CachingProvider
  29. {
  30.     public static final URI DEFAULT_URI = URI.create("jcs://jcache.ccf");

  31.     private final ConcurrentMap<ClassLoader, ConcurrentMap<URI, CacheManager>> cacheManagersByLoader = new ConcurrentHashMap<>();

  32.     @Override
  33.     public CacheManager getCacheManager(final URI inUri, final ClassLoader inClassLoader, final Properties properties)
  34.     {
  35.         final URI uri = inUri != null ? inUri : getDefaultURI();
  36.         final ClassLoader classLoader = inClassLoader != null ? inClassLoader : getDefaultClassLoader();

  37.         ConcurrentMap<URI, CacheManager> managers = cacheManagersByLoader.get(classLoader);
  38.         if (managers == null)
  39.         {
  40.             managers = new ConcurrentHashMap<>();
  41.             final ConcurrentMap<URI, CacheManager> existingManagers = cacheManagersByLoader.putIfAbsent(classLoader, managers);
  42.             if (existingManagers != null)
  43.             {
  44.                 managers = existingManagers;
  45.             }
  46.         }

  47.         CacheManager mgr = managers.get(uri);
  48.         if (mgr == null)
  49.         {
  50.             mgr = new JCSCachingManager(this, uri, classLoader, properties);
  51.             final CacheManager existing = managers.putIfAbsent(uri, mgr);
  52.             if (existing != null)
  53.             {
  54.                 mgr = existing;
  55.             }
  56.         }

  57.         return mgr;
  58.     }

  59.     @Override
  60.     public URI getDefaultURI()
  61.     {
  62.         return DEFAULT_URI;
  63.     }

  64.     @Override
  65.     public void close()
  66.     {
  67.         for (final Map<URI, CacheManager> v : cacheManagersByLoader.values())
  68.         {
  69.             for (final CacheManager m : v.values())
  70.             {
  71.                 m.close();
  72.             }
  73.             v.clear();
  74.         }
  75.         cacheManagersByLoader.clear();
  76.     }

  77.     @Override
  78.     public void close(final ClassLoader classLoader)
  79.     {
  80.         final Map<URI, CacheManager> cacheManagers = cacheManagersByLoader.remove(classLoader);
  81.         if (cacheManagers != null)
  82.         {
  83.             for (final CacheManager mgr : cacheManagers.values())
  84.             {
  85.                 mgr.close();
  86.             }
  87.             cacheManagers.clear();
  88.         }
  89.     }

  90.     @Override
  91.     public void close(final URI uri, final ClassLoader classLoader)
  92.     {
  93.         final Map<URI, CacheManager> cacheManagers = cacheManagersByLoader.remove(classLoader);
  94.         if (cacheManagers != null)
  95.         {
  96.             final CacheManager mgr = cacheManagers.remove(uri);
  97.             if (mgr != null)
  98.             {
  99.                 mgr.close();
  100.             }
  101.         }
  102.     }

  103.     @Override
  104.     public CacheManager getCacheManager(final URI uri, final ClassLoader classLoader)
  105.     {
  106.         return getCacheManager(uri, classLoader, getDefaultProperties());
  107.     }

  108.     @Override
  109.     public CacheManager getCacheManager()
  110.     {
  111.         return getCacheManager(getDefaultURI(), getDefaultClassLoader());
  112.     }

  113.     @Override
  114.     public boolean isSupported(final OptionalFeature optionalFeature)
  115.     {
  116.         return optionalFeature == OptionalFeature.STORE_BY_REFERENCE;
  117.     }

  118.     @Override
  119.     public ClassLoader getDefaultClassLoader()
  120.     {
  121.         return JCSCachingProvider.class.getClassLoader();
  122.     }

  123.     @Override
  124.     public Properties getDefaultProperties()
  125.     {
  126.         return new Properties();
  127.     }

  128.     void remove(final CacheManager mgr)
  129.     {
  130.         final ClassLoader classLoader = mgr.getClassLoader();
  131.         final Map<URI, CacheManager> mgrs = cacheManagersByLoader.get(classLoader);
  132.         if (mgrs != null)
  133.         {
  134.             mgrs.remove(mgr.getURI());
  135.             if (mgrs.isEmpty())
  136.             {
  137.                 cacheManagersByLoader.remove(classLoader);
  138.             }
  139.         }
  140.     }
  141. }