View Javadoc
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.jcs.jcache.extras.cdi;
20  
21  import javax.cache.CacheManager;
22  import javax.cache.Caching;
23  import javax.cache.spi.CachingProvider;
24  import javax.enterprise.event.Observes;
25  import javax.enterprise.inject.spi.AfterBeanDiscovery;
26  import javax.enterprise.inject.spi.Bean;
27  import javax.enterprise.inject.spi.BeforeShutdown;
28  import javax.enterprise.inject.spi.Extension;
29  import javax.enterprise.inject.spi.ProcessBean;
30  import java.util.Properties;
31  
32  // add default CacheProvider and CacheManager
33  public class ExtraJCacheExtension implements Extension
34  {
35      private static final boolean ACTIVATED = "true".equals(System.getProperty("org.apache.jcs.extra.cdi", "true"));
36  
37      private boolean cacheManagerFound = false;
38      private boolean cacheProviderFound = false;
39      private CacheManager cacheManager;
40      private CachingProvider cachingProvider;
41  
42      public <A> void processBean(final @Observes ProcessBean<A> processBeanEvent)
43      {
44          if (!ACTIVATED)
45          {
46              return;
47          }
48  
49          if (cacheManagerFound && cacheProviderFound)
50          {
51              return;
52          }
53  
54          final Bean<A> bean = processBeanEvent.getBean();
55          if (CacheManagerBean.class.isInstance(bean) || CacheProviderBean.class.isInstance(bean))
56          {
57              return;
58          }
59  
60          if (!cacheManagerFound)
61          {
62              cacheManagerFound = bean.getTypes().contains(CacheManager.class);
63          }
64          if (!cacheProviderFound)
65          {
66              cacheProviderFound = bean.getTypes().contains(CachingProvider.class);
67          }
68      }
69  
70      public void addJCacheBeans(final @Observes AfterBeanDiscovery afterBeanDiscovery)
71      {
72          if (!ACTIVATED)
73          {
74              return;
75          }
76  
77          if (cacheManagerFound && cacheProviderFound) {
78              return;
79          }
80  
81          cachingProvider = Caching.getCachingProvider();
82          if (!cacheManagerFound)
83          {
84              cacheManager = cachingProvider.getCacheManager(
85                      cachingProvider.getDefaultURI(),
86                      cachingProvider.getDefaultClassLoader(),
87                      new Properties());
88              afterBeanDiscovery.addBean(new CacheManagerBean(cacheManager));
89          }
90          if (!cacheProviderFound)
91          {
92              afterBeanDiscovery.addBean(new CacheProviderBean(cachingProvider));
93          }
94      }
95  
96      public void destroyIfCreated(final @Observes BeforeShutdown beforeShutdown)
97      {
98          if (cacheManager != null)
99          {
100             cacheManager.close();
101         }
102         if (cachingProvider != null)
103         {
104             cachingProvider.close();
105         }
106     }
107 }