001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.jcs3.jcache.extras.cdi;
020
021import javax.cache.CacheManager;
022import javax.cache.Caching;
023import javax.cache.spi.CachingProvider;
024import javax.enterprise.event.Observes;
025import javax.enterprise.inject.spi.AfterBeanDiscovery;
026import javax.enterprise.inject.spi.Bean;
027import javax.enterprise.inject.spi.BeforeShutdown;
028import javax.enterprise.inject.spi.Extension;
029import javax.enterprise.inject.spi.ProcessBean;
030import java.util.Properties;
031
032// add default CacheProvider and CacheManager
033public class ExtraJCacheExtension implements Extension
034{
035    private static final boolean ACTIVATED = "true".equals(System.getProperty("org.apache.jcs.extra.cdi", "true"));
036
037    private boolean cacheManagerFound;
038    private boolean cacheProviderFound;
039    private CacheManager cacheManager;
040    private CachingProvider cachingProvider;
041
042    public <A> void processBean(final @Observes ProcessBean<A> processBeanEvent)
043    {
044        if (!ACTIVATED)
045        {
046            return;
047        }
048
049        if (cacheManagerFound && cacheProviderFound)
050        {
051            return;
052        }
053
054        final Bean<A> bean = processBeanEvent.getBean();
055        if (CacheManagerBean.class.isInstance(bean) || CacheProviderBean.class.isInstance(bean))
056        {
057            return;
058        }
059
060        if (!cacheManagerFound)
061        {
062            cacheManagerFound = bean.getTypes().contains(CacheManager.class);
063        }
064        if (!cacheProviderFound)
065        {
066            cacheProviderFound = bean.getTypes().contains(CachingProvider.class);
067        }
068    }
069
070    public void addJCacheBeans(final @Observes AfterBeanDiscovery afterBeanDiscovery)
071    {
072        if (!ACTIVATED)
073        {
074            return;
075        }
076
077        if (cacheManagerFound && cacheProviderFound) {
078            return;
079        }
080
081        cachingProvider = Caching.getCachingProvider();
082        if (!cacheManagerFound)
083        {
084            cacheManager = cachingProvider.getCacheManager(
085                    cachingProvider.getDefaultURI(),
086                    cachingProvider.getDefaultClassLoader(),
087                    new Properties());
088            afterBeanDiscovery.addBean(new CacheManagerBean(cacheManager));
089        }
090        if (!cacheProviderFound)
091        {
092            afterBeanDiscovery.addBean(new CacheProviderBean(cachingProvider));
093        }
094    }
095
096    public void destroyIfCreated(final @Observes BeforeShutdown beforeShutdown)
097    {
098        if (cacheManager != null)
099        {
100            cacheManager.close();
101        }
102        if (cachingProvider != null)
103        {
104            cachingProvider.close();
105        }
106    }
107}