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.openjpa;
020
021import org.apache.openjpa.conf.OpenJPAConfiguration;
022import org.apache.openjpa.datacache.DataCacheManagerImpl;
023import org.apache.openjpa.lib.conf.ObjectValue;
024
025import javax.cache.Cache;
026import javax.cache.CacheManager;
027import javax.cache.Caching;
028import javax.cache.configuration.FactoryBuilder;
029import javax.cache.configuration.MutableConfiguration;
030import javax.cache.expiry.CreatedExpiryPolicy;
031import javax.cache.expiry.Duration;
032import javax.cache.expiry.ExpiryPolicy;
033import javax.cache.integration.CacheLoader;
034import javax.cache.integration.CacheWriter;
035import javax.cache.spi.CachingProvider;
036import java.net.URI;
037import java.util.Map;
038import java.util.Properties;
039
040public class OpenJPAJCacheDataCacheManager extends DataCacheManagerImpl
041{
042    private CachingProvider provider;
043    private CacheManager cacheManager;
044
045    @Override
046    public void initialize(final OpenJPAConfiguration conf, final ObjectValue dataCache, final ObjectValue queryCache)
047    {
048        super.initialize(conf, dataCache, queryCache);
049        provider = Caching.getCachingProvider();
050
051        final Properties properties = new Properties();
052        final Map<String, Object> props = conf.toProperties(false);
053        if (props != null)
054        {
055            for (final Map.Entry<?, ?> entry : props.entrySet())
056            {
057                if (entry.getKey() != null && entry.getValue() != null)
058                {
059                    properties.setProperty(entry.getKey().toString(), entry.getValue().toString());
060                }
061            }
062        }
063
064        final String uri = properties.getProperty("jcache.uri", provider.getDefaultURI().toString());
065        cacheManager = provider.getCacheManager(URI.create(uri), provider.getDefaultClassLoader(), properties);
066    }
067
068    @Override
069    public void close()
070    {
071        super.close();
072        if (!cacheManager.isClosed())
073        {
074            cacheManager.close();
075        }
076        provider.close();
077    }
078
079    Cache<Object, Object> getOrCreateCache(final String prefix, final String entity)
080    {
081        final String internalName = prefix + entity;
082        Cache<Object, Object> cache = cacheManager.getCache(internalName);
083        if (cache == null)
084        {
085            final Properties properties = cacheManager.getProperties();
086            final MutableConfiguration<Object, Object> configuration = new MutableConfiguration<>()
087                    .setStoreByValue("true".equalsIgnoreCase(properties.getProperty("jcache.store-by-value", "false")));
088
089            configuration.setReadThrough("true".equals(properties.getProperty("jcache.read-through", "false")));
090            configuration.setWriteThrough("true".equals(properties.getProperty("jcache.write-through", "false")));
091            if (configuration.isReadThrough())
092            {
093                configuration.setCacheLoaderFactory(new FactoryBuilder.ClassFactory<CacheLoader<Object, Object>>(properties.getProperty("jcache.cache-loader-factory")));
094            }
095            if (configuration.isWriteThrough())
096            {
097                configuration.setCacheWriterFactory(new FactoryBuilder.ClassFactory<CacheWriter<Object, Object>>(properties.getProperty("jcache.cache-writer-factory")));
098            }
099            final String expirtyPolicy = properties.getProperty("jcache.expiry-policy-factory");
100            if (expirtyPolicy != null)
101            {
102                configuration.setExpiryPolicyFactory(new FactoryBuilder.ClassFactory<ExpiryPolicy>(expirtyPolicy));
103            }
104            else
105            {
106                configuration.setExpiryPolicyFactory(new FactoryBuilder.SingletonFactory<ExpiryPolicy>(new CreatedExpiryPolicy(Duration.FIVE_MINUTES)));
107            }
108            configuration.setManagementEnabled("true".equals(properties.getProperty("jcache.management-enabled", "false")));
109            configuration.setStatisticsEnabled("true".equals(properties.getProperty("jcache.statistics-enabled", "false")));
110
111            cache = cacheManager.createCache(internalName, configuration);
112        }
113        return cache;
114    }
115
116    CacheManager getCacheManager()
117    {
118        return cacheManager;
119    }
120}