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.openjpa;
20  
21  import org.apache.openjpa.conf.OpenJPAConfiguration;
22  import org.apache.openjpa.datacache.DataCacheManagerImpl;
23  import org.apache.openjpa.lib.conf.ObjectValue;
24  
25  import javax.cache.Cache;
26  import javax.cache.CacheManager;
27  import javax.cache.Caching;
28  import javax.cache.configuration.FactoryBuilder;
29  import javax.cache.configuration.MutableConfiguration;
30  import javax.cache.expiry.CreatedExpiryPolicy;
31  import javax.cache.expiry.Duration;
32  import javax.cache.expiry.ExpiryPolicy;
33  import javax.cache.integration.CacheLoader;
34  import javax.cache.integration.CacheWriter;
35  import javax.cache.spi.CachingProvider;
36  import java.net.URI;
37  import java.util.Map;
38  import java.util.Properties;
39  
40  public class OpenJPAJCacheDataCacheManager extends DataCacheManagerImpl
41  {
42      private CachingProvider provider;
43      private CacheManager cacheManager;
44  
45      @Override
46      public void initialize(final OpenJPAConfiguration conf, final ObjectValue dataCache, final ObjectValue queryCache)
47      {
48          super.initialize(conf, dataCache, queryCache);
49          provider = Caching.getCachingProvider();
50  
51          final Properties properties = new Properties();
52          final Map<String, Object> props = conf.toProperties(false);
53          if (props != null)
54          {
55              for (final Map.Entry<?, ?> entry : props.entrySet())
56              {
57                  if (entry.getKey() != null && entry.getValue() != null)
58                  {
59                      properties.setProperty(entry.getKey().toString(), entry.getValue().toString());
60                  }
61              }
62          }
63  
64          final String uri = properties.getProperty("jcache.uri", provider.getDefaultURI().toString());
65          cacheManager = provider.getCacheManager(URI.create(uri), provider.getDefaultClassLoader(), properties);
66      }
67  
68      @Override
69      public void close()
70      {
71          super.close();
72          if (!cacheManager.isClosed())
73          {
74              cacheManager.close();
75          }
76          provider.close();
77      }
78  
79      Cache<Object, Object> getOrCreateCache(final String prefix, final String entity)
80      {
81          final String internalName = prefix + entity;
82          Cache<Object, Object> cache = cacheManager.getCache(internalName);
83          if (cache == null)
84          {
85              final Properties properties = cacheManager.getProperties();
86              final MutableConfiguration<Object, Object> configuration = new MutableConfiguration<Object, Object>()
87                      .setStoreByValue("true".equalsIgnoreCase(properties.getProperty("jcache.store-by-value", "false")));
88  
89              configuration.setReadThrough("true".equals(properties.getProperty("jcache.read-through", "false")));
90              configuration.setWriteThrough("true".equals(properties.getProperty("jcache.write-through", "false")));
91              if (configuration.isReadThrough())
92              {
93                  configuration.setCacheLoaderFactory(new FactoryBuilder.ClassFactory<CacheLoader<Object, Object>>(properties.getProperty("jcache.cache-loader-factory")));
94              }
95              if (configuration.isWriteThrough())
96              {
97                  configuration.setCacheWriterFactory(new FactoryBuilder.ClassFactory<CacheWriter<Object, Object>>(properties.getProperty("jcache.cache-writer-factory")));
98              }
99              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 }