JCSConfiguration.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. /**
  20.  *  Copyright 2003-2010 Terracotta, Inc.
  21.  *
  22.  *  Licensed under the Apache License, Version 2.0 (the "License");
  23.  *  you may not use this file except in compliance with the License.
  24.  *  You may obtain a copy of the License at
  25.  *
  26.  *      http://www.apache.org/licenses/LICENSE-2.0
  27.  *
  28.  *  Unless required by applicable law or agreed to in writing, software
  29.  *  distributed under the License is distributed on an "AS IS" BASIS,
  30.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  31.  *  See the License for the specific language governing permissions and
  32.  *  limitations under the License.
  33.  */
  34. package org.apache.commons.jcs3.jcache;

  35. import javax.cache.configuration.CacheEntryListenerConfiguration;
  36. import javax.cache.configuration.CompleteConfiguration;
  37. import javax.cache.configuration.Configuration;
  38. import javax.cache.configuration.Factory;
  39. import javax.cache.expiry.EternalExpiryPolicy;
  40. import javax.cache.expiry.ExpiryPolicy;
  41. import javax.cache.integration.CacheLoader;
  42. import javax.cache.integration.CacheWriter;
  43. import java.util.Collections;
  44. import java.util.HashSet;
  45. import java.util.Set;

  46. public class JCSConfiguration<K, V> implements CompleteConfiguration<K, V>
  47. {

  48.     /**
  49.      *
  50.      */
  51.     private static final long serialVersionUID = 3322514800838658711L;
  52.     private final Class<K> keyType;
  53.     private final Class<V> valueType;
  54.     private final boolean storeByValue;
  55.     private final boolean readThrough;
  56.     private final boolean writeThrough;
  57.     private final Factory<CacheLoader<K, V>> cacheLoaderFactory;
  58.     private final Factory<CacheWriter<? super K, ? super V>> cacheWristerFactory;
  59.     private final Factory<ExpiryPolicy> expiryPolicyFactory;
  60.     private final Set<CacheEntryListenerConfiguration<K, V>> cacheEntryListenerConfigurations;

  61.     private volatile boolean statisticsEnabled;
  62.     private volatile boolean managementEnabled;

  63.     public JCSConfiguration(final Configuration<K, V> configuration, final Class<K> keyType, final Class<V> valueType)
  64.     {
  65.         this.keyType = keyType;
  66.         this.valueType = valueType;
  67.         if (configuration instanceof CompleteConfiguration)
  68.         {
  69.             final CompleteConfiguration<K, V> cConfiguration = (CompleteConfiguration<K, V>) configuration;
  70.             storeByValue = configuration.isStoreByValue();
  71.             readThrough = cConfiguration.isReadThrough();
  72.             writeThrough = cConfiguration.isWriteThrough();
  73.             statisticsEnabled = cConfiguration.isStatisticsEnabled();
  74.             managementEnabled = cConfiguration.isManagementEnabled();
  75.             cacheLoaderFactory = cConfiguration.getCacheLoaderFactory();
  76.             cacheWristerFactory = cConfiguration.getCacheWriterFactory();
  77.             this.expiryPolicyFactory = cConfiguration.getExpiryPolicyFactory();
  78.             cacheEntryListenerConfigurations = new HashSet<>();

  79.             final Iterable<CacheEntryListenerConfiguration<K, V>> entryListenerConfigurations = cConfiguration
  80.                     .getCacheEntryListenerConfigurations();
  81.             if (entryListenerConfigurations != null)
  82.             {
  83.                 for (final CacheEntryListenerConfiguration<K, V> kvCacheEntryListenerConfiguration : entryListenerConfigurations)
  84.                 {
  85.                     cacheEntryListenerConfigurations.add(kvCacheEntryListenerConfiguration);
  86.                 }
  87.             }
  88.         }
  89.         else
  90.         {
  91.             expiryPolicyFactory = EternalExpiryPolicy.factoryOf();
  92.             storeByValue = true;
  93.             readThrough = false;
  94.             writeThrough = false;
  95.             statisticsEnabled = false;
  96.             managementEnabled = false;
  97.             cacheLoaderFactory = null;
  98.             cacheWristerFactory = null;
  99.             cacheEntryListenerConfigurations = new HashSet<>();
  100.         }
  101.     }

  102.     @Override
  103.     public Class<K> getKeyType()
  104.     {
  105.         return keyType == null ? (Class<K>) Object.class : keyType;
  106.     }

  107.     @Override
  108.     public Class<V> getValueType()
  109.     {
  110.         return valueType == null ? (Class<V>) Object.class : valueType;
  111.     }

  112.     @Override
  113.     public boolean isStoreByValue()
  114.     {
  115.         return storeByValue;
  116.     }

  117.     @Override
  118.     public boolean isReadThrough()
  119.     {
  120.         return readThrough;
  121.     }

  122.     @Override
  123.     public boolean isWriteThrough()
  124.     {
  125.         return writeThrough;
  126.     }

  127.     @Override
  128.     public boolean isStatisticsEnabled()
  129.     {
  130.         return statisticsEnabled;
  131.     }

  132.     @Override
  133.     public boolean isManagementEnabled()
  134.     {
  135.         return managementEnabled;
  136.     }

  137.     @Override
  138.     public Iterable<CacheEntryListenerConfiguration<K, V>> getCacheEntryListenerConfigurations()
  139.     {
  140.         return Collections.unmodifiableSet(cacheEntryListenerConfigurations);
  141.     }

  142.     @Override
  143.     public Factory<CacheLoader<K, V>> getCacheLoaderFactory()
  144.     {
  145.         return cacheLoaderFactory;
  146.     }

  147.     @Override
  148.     public Factory<CacheWriter<? super K, ? super V>> getCacheWriterFactory()
  149.     {
  150.         return cacheWristerFactory;
  151.     }

  152.     @Override
  153.     public Factory<ExpiryPolicy> getExpiryPolicyFactory()
  154.     {
  155.         return expiryPolicyFactory;
  156.     }

  157.     public synchronized void addListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration)
  158.     {
  159.         cacheEntryListenerConfigurations.add(cacheEntryListenerConfiguration);
  160.     }

  161.     public synchronized void removeListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration)
  162.     {
  163.         cacheEntryListenerConfigurations.remove(cacheEntryListenerConfiguration);
  164.     }

  165.     public void statisticsEnabled()
  166.     {
  167.         statisticsEnabled = true;
  168.     }

  169.     public void managementEnabled()
  170.     {
  171.         managementEnabled = true;
  172.     }

  173.     public void statisticsDisabled()
  174.     {
  175.         statisticsEnabled = false;
  176.     }

  177.     public void managementDisabled()
  178.     {
  179.         managementEnabled = false;
  180.     }
  181. }