ExtraJCacheExtension.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.extras.cdi;

  20. import javax.cache.CacheManager;
  21. import javax.cache.Caching;
  22. import javax.cache.spi.CachingProvider;
  23. import javax.enterprise.event.Observes;
  24. import javax.enterprise.inject.spi.AfterBeanDiscovery;
  25. import javax.enterprise.inject.spi.Bean;
  26. import javax.enterprise.inject.spi.BeforeShutdown;
  27. import javax.enterprise.inject.spi.Extension;
  28. import javax.enterprise.inject.spi.ProcessBean;
  29. import java.util.Properties;

  30. // add default CacheProvider and CacheManager
  31. public class ExtraJCacheExtension implements Extension
  32. {
  33.     private static final boolean ACTIVATED = "true".equals(System.getProperty("org.apache.jcs.extra.cdi", "true"));

  34.     private boolean cacheManagerFound;
  35.     private boolean cacheProviderFound;
  36.     private CacheManager cacheManager;
  37.     private CachingProvider cachingProvider;

  38.     public <A> void processBean(final @Observes ProcessBean<A> processBeanEvent)
  39.     {
  40.         if (!ACTIVATED)
  41.         {
  42.             return;
  43.         }

  44.         if (cacheManagerFound && cacheProviderFound)
  45.         {
  46.             return;
  47.         }

  48.         final Bean<A> bean = processBeanEvent.getBean();
  49.         if (CacheManagerBean.class.isInstance(bean) || CacheProviderBean.class.isInstance(bean))
  50.         {
  51.             return;
  52.         }

  53.         if (!cacheManagerFound)
  54.         {
  55.             cacheManagerFound = bean.getTypes().contains(CacheManager.class);
  56.         }
  57.         if (!cacheProviderFound)
  58.         {
  59.             cacheProviderFound = bean.getTypes().contains(CachingProvider.class);
  60.         }
  61.     }

  62.     public void addJCacheBeans(final @Observes AfterBeanDiscovery afterBeanDiscovery)
  63.     {
  64.         if (!ACTIVATED)
  65.         {
  66.             return;
  67.         }

  68.         if (cacheManagerFound && cacheProviderFound) {
  69.             return;
  70.         }

  71.         cachingProvider = Caching.getCachingProvider();
  72.         if (!cacheManagerFound)
  73.         {
  74.             cacheManager = cachingProvider.getCacheManager(
  75.                     cachingProvider.getDefaultURI(),
  76.                     cachingProvider.getDefaultClassLoader(),
  77.                     new Properties());
  78.             afterBeanDiscovery.addBean(new CacheManagerBean(cacheManager));
  79.         }
  80.         if (!cacheProviderFound)
  81.         {
  82.             afterBeanDiscovery.addBean(new CacheProviderBean(cachingProvider));
  83.         }
  84.     }

  85.     public void destroyIfCreated(final @Observes BeforeShutdown beforeShutdown)
  86.     {
  87.         if (cacheManager != null)
  88.         {
  89.             cacheManager.close();
  90.         }
  91.         if (cachingProvider != null)
  92.         {
  93.             cachingProvider.close();
  94.         }
  95.     }
  96. }