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  /**
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  
36  import javax.cache.configuration.CacheEntryListenerConfiguration;
37  import javax.cache.configuration.CompleteConfiguration;
38  import javax.cache.configuration.Configuration;
39  import javax.cache.configuration.Factory;
40  import javax.cache.expiry.EternalExpiryPolicy;
41  import javax.cache.expiry.ExpiryPolicy;
42  import javax.cache.integration.CacheLoader;
43  import javax.cache.integration.CacheWriter;
44  import java.util.Collections;
45  import java.util.HashSet;
46  import java.util.Set;
47  
48  public class JCSConfiguration<K, V> implements CompleteConfiguration<K, V>
49  {
50  
51      /**
52       *
53       */
54      private static final long serialVersionUID = 3322514800838658711L;
55      private final Class<K> keyType;
56      private final Class<V> valueType;
57      private final boolean storeByValue;
58      private final boolean readThrough;
59      private final boolean writeThrough;
60      private final Factory<CacheLoader<K, V>> cacheLoaderFactory;
61      private final Factory<CacheWriter<? super K, ? super V>> cacheWristerFactory;
62      private final Factory<ExpiryPolicy> expiryPolicyFactory;
63      private final Set<CacheEntryListenerConfiguration<K, V>> cacheEntryListenerConfigurations;
64  
65      private volatile boolean statisticsEnabled;
66      private volatile boolean managementEnabled;
67  
68      public JCSConfiguration(final Configuration<K, V> configuration, final Class<K> keyType, final Class<V> valueType)
69      {
70          this.keyType = keyType;
71          this.valueType = valueType;
72          if (configuration instanceof CompleteConfiguration)
73          {
74              final CompleteConfiguration<K, V> cConfiguration = (CompleteConfiguration<K, V>) configuration;
75              storeByValue = configuration.isStoreByValue();
76              readThrough = cConfiguration.isReadThrough();
77              writeThrough = cConfiguration.isWriteThrough();
78              statisticsEnabled = cConfiguration.isStatisticsEnabled();
79              managementEnabled = cConfiguration.isManagementEnabled();
80              cacheLoaderFactory = cConfiguration.getCacheLoaderFactory();
81              cacheWristerFactory = cConfiguration.getCacheWriterFactory();
82              this.expiryPolicyFactory = cConfiguration.getExpiryPolicyFactory();
83              cacheEntryListenerConfigurations = new HashSet<>();
84  
85              final Iterable<CacheEntryListenerConfiguration<K, V>> entryListenerConfigurations = cConfiguration
86                      .getCacheEntryListenerConfigurations();
87              if (entryListenerConfigurations != null)
88              {
89                  for (final CacheEntryListenerConfiguration<K, V> kvCacheEntryListenerConfiguration : entryListenerConfigurations)
90                  {
91                      cacheEntryListenerConfigurations.add(kvCacheEntryListenerConfiguration);
92                  }
93              }
94          }
95          else
96          {
97              expiryPolicyFactory = EternalExpiryPolicy.factoryOf();
98              storeByValue = true;
99              readThrough = false;
100             writeThrough = false;
101             statisticsEnabled = false;
102             managementEnabled = false;
103             cacheLoaderFactory = null;
104             cacheWristerFactory = null;
105             cacheEntryListenerConfigurations = new HashSet<>();
106         }
107     }
108 
109     @Override
110     public Class<K> getKeyType()
111     {
112         return keyType == null ? (Class<K>) Object.class : keyType;
113     }
114 
115     @Override
116     public Class<V> getValueType()
117     {
118         return valueType == null ? (Class<V>) Object.class : valueType;
119     }
120 
121     @Override
122     public boolean isStoreByValue()
123     {
124         return storeByValue;
125     }
126 
127     @Override
128     public boolean isReadThrough()
129     {
130         return readThrough;
131     }
132 
133     @Override
134     public boolean isWriteThrough()
135     {
136         return writeThrough;
137     }
138 
139     @Override
140     public boolean isStatisticsEnabled()
141     {
142         return statisticsEnabled;
143     }
144 
145     @Override
146     public boolean isManagementEnabled()
147     {
148         return managementEnabled;
149     }
150 
151     @Override
152     public Iterable<CacheEntryListenerConfiguration<K, V>> getCacheEntryListenerConfigurations()
153     {
154         return Collections.unmodifiableSet(cacheEntryListenerConfigurations);
155     }
156 
157     @Override
158     public Factory<CacheLoader<K, V>> getCacheLoaderFactory()
159     {
160         return cacheLoaderFactory;
161     }
162 
163     @Override
164     public Factory<CacheWriter<? super K, ? super V>> getCacheWriterFactory()
165     {
166         return cacheWristerFactory;
167     }
168 
169     @Override
170     public Factory<ExpiryPolicy> getExpiryPolicyFactory()
171     {
172         return expiryPolicyFactory;
173     }
174 
175     public synchronized void addListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration)
176     {
177         cacheEntryListenerConfigurations.add(cacheEntryListenerConfiguration);
178     }
179 
180     public synchronized void removeListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration)
181     {
182         cacheEntryListenerConfigurations.remove(cacheEntryListenerConfiguration);
183     }
184 
185     public void statisticsEnabled()
186     {
187         statisticsEnabled = true;
188     }
189 
190     public void managementEnabled()
191     {
192         managementEnabled = true;
193     }
194 
195     public void statisticsDisabled()
196     {
197         statisticsEnabled = false;
198     }
199 
200     public void managementDisabled()
201     {
202         managementEnabled = false;
203     }
204 }