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