View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.spi.config.cache.infinispan;
17  
18  import java.util.HashSet;
19  import java.util.Set;
20  import java.util.concurrent.locks.Lock;
21  import java.util.concurrent.locks.ReentrantReadWriteLock;
22  
23  import org.infinispan.configuration.cache.Configuration;
24  import org.infinispan.manager.EmbeddedCacheManager;
25  
26  import de.smartics.properties.spi.config.cache.Cache;
27  import de.smartics.properties.spi.config.cache.CacheManager;
28  import de.smartics.properties.spi.config.cache.UnawareCache;
29  import de.smartics.properties.spi.config.cache.guava.GuavaCacheManager;
30  import de.smartics.util.lang.StaticAnalysis;
31  
32  /**
33   * The base implementation to connect to Infinispan caches.
34   */
35  public abstract class AbstractInfinispanCompoundKeyCacheManager implements
36      CacheManager
37  { // NOPMD
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    // --- members --------------------------------------------------------------
43  
44    /**
45     * The lock for synchronized access.
46     *
47     * @serial
48     */
49    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
50  
51    /**
52     * The read lock for synchronized access.
53     *
54     * @serial
55     */
56    private final Lock readLock = lock.readLock();
57  
58    /**
59     * The write lock for synchronized access.
60     *
61     * @serial
62     */
63    private final Lock writeLock = lock.writeLock();
64  
65    /**
66     * The infinispan cache manager for property keys.
67     */
68    private final EmbeddedCacheManager manager; // NOPMD
69  
70    /**
71     * The in memory cache manager for configurations / management.
72     */
73    private final GuavaCacheManager configManager;
74  
75    /**
76     * The underlaying cache.
77     */
78    private final org.infinispan.Cache<Object, Object> cache;
79  
80    // ****************************** Initializer *******************************
81  
82    // ****************************** Constructors ******************************
83  
84    /**
85     * Default constructor.
86     *
87     * @param manager the Infinispan cache manager.
88     */
89    public AbstractInfinispanCompoundKeyCacheManager(
90        final CacheManagerProxy cacheManager)
91    {
92      this.manager = cacheManager.getManager();
93      configManager = new GuavaCacheManager();
94  
95      manager.getDefaultCacheConfiguration().jmxStatistics();
96  
97      final String cacheName = cacheManager.getCacheName();
98      this.cache = manager.getCache(cacheName);
99      if (!manager.isRunning(cacheName))
100     {
101       final Configuration cacheConfig = this.cache.getCacheConfiguration();
102       cacheConfig.jmxStatistics();
103       cacheConfig.indexing().indexLocalOnly();
104       cache.start();
105     }
106     // cache.addListener(new CompoundKeyHandlingCacheListener());
107     cache.clear();
108   }
109 
110   // ****************************** Inner Classes *****************************
111 
112   // ********************************* Methods ********************************
113 
114   // --- init -----------------------------------------------------------------
115 
116   // --- get&set --------------------------------------------------------------
117 
118   // --- business -------------------------------------------------------------
119 
120   /**
121    * Removes all keys from all caches.
122    */
123   @Override
124   public final void clearAll()
125   {
126     writeLock.lock();
127     try
128     {
129       internalClearAll();
130     }
131     finally
132     {
133       writeLock.unlock();
134     }
135   }
136 
137   /**
138    * Clears the cache with the given name.
139    *
140    * @param cacheName the name of the cache to be cleared.
141    */
142   @Override
143   public final void clear(final String cacheName)
144   {
145     writeLock.lock();
146     try
147     {
148       internalClear(cacheName);
149     }
150     finally
151     {
152       writeLock.unlock();
153     }
154   }
155 
156   /**
157    * Clears and stops the cache.
158    *
159    * @param cacheName the name of the cache to be cleared and stopped.
160    */
161   @Override
162   public final void discard(final String cacheName)
163   {
164     writeLock.lock();
165     try
166     {
167       if (isManagementCache(cacheName))
168       {
169         configManager.discard(cacheName);
170       }
171       internalClear(cacheName);
172       internalStop(cacheName);
173     }
174     finally
175     {
176       writeLock.unlock();
177     }
178 
179   }
180 
181   /**
182    * Stops all caches, rendering the manager in a stopped state.
183    */
184   @Override
185   public final void stopAll()
186   {
187     writeLock.lock();
188     try
189     {
190       internalStopAll();
191     }
192     finally
193     {
194       writeLock.unlock();
195     }
196   }
197 
198   /**
199    * Stops the cache with the given name.
200    *
201    * @param cacheName the name of the cache to stop.
202    */
203   @Override
204   public final void stop(final String cacheName)
205   {
206     writeLock.lock();
207     try
208     {
209       internalStop(cacheName);
210     }
211     finally
212     {
213       writeLock.unlock();
214     }
215   }
216 
217   /**
218    * Returns the properties cache with the given name. The cache is already
219    * started.
220    *
221    * @param cacheName the name of the cache to fetch.
222    * @return the requested cache already started.
223    */
224   @SuppressWarnings({ StaticAnalysis.RAWTYPES, StaticAnalysis.UNCHECKED })
225   public final UnawareCache getPropertiesCache(final String cacheName)
226   {
227     writeLock.lock();
228     try
229     {
230       final UnawareCache propertiesCache =
231           internalGetPropertiesCache(cacheName);
232       return propertiesCache;
233     }
234     finally
235     {
236       writeLock.unlock();
237     }
238   }
239 
240   /**
241    * Returns the configurations cache with the given name. The cache is already
242    * started.
243    *
244    * @param cacheName the name of the cache to fetch.
245    * @return the requested cache already started.
246    */
247   @SuppressWarnings({ StaticAnalysis.RAWTYPES, StaticAnalysis.UNCHECKED })
248   public final Cache getConfigurationsCache(final String cacheName)
249   {
250     writeLock.lock();
251     try
252     {
253       return configManager.getConfigurationsCache(cacheName);
254     }
255     finally
256     {
257       writeLock.unlock();
258     }
259   }
260 
261   /**
262    * Returns the set of cache names.
263    *
264    * @return the set of cache names.
265    */
266   @SuppressWarnings({ StaticAnalysis.RAWTYPES, StaticAnalysis.UNCHECKED })
267   @Override
268   public final Set<String> getCacheNames()
269   {
270     readLock.lock();
271     try
272     {
273       final Cache infinispanDelegatingCacheCache =
274           configManager.getInfinispanDelegatingCache();
275       final Set<String> cacheNames = infinispanDelegatingCacheCache.keySet();
276 
277       final HashSet allCacheNames = new HashSet<String>();
278       if (cacheNames != null)
279       {
280         allCacheNames.addAll(cacheNames);
281       }
282       final Set inMemoryCacheNames = configManager.getCacheNames();
283       if (inMemoryCacheNames != null)
284       {
285         allCacheNames.addAll(inMemoryCacheNames);
286       }
287       return allCacheNames;
288     }
289     finally
290     {
291       readLock.unlock();
292     }
293   }
294 
295   // --- private -------------------------------------------------------------
296 
297   /**
298    * Removes all keys from all caches.
299    */
300   private void internalClearAll()
301   {
302     configManager.clearAll();
303     cache.clear();
304   }
305 
306   /**
307    * Clears the cache with the given name.
308    *
309    * @param cacheName the name of the cache to be cleared.
310    */
311   private void internalClear(final String cacheName)
312   {
313     if (isManagementCache(cacheName))
314     {
315       configManager.clear(cacheName);
316     }
317     else if (isInfinispanDelegatingCache(cacheName))
318     {
319       internalClearAll();
320     }
321     else
322     {
323       // TODO TK clear only the properties for this cache
324       cache.clear();
325     }
326   }
327 
328   private boolean isInfinispanDelegatingCache(final String cacheName)
329   {
330     return GuavaCacheManager.INFINISPAN_DELEGATING_CACHE_CACHE_NAME
331         .equals(cacheName);
332   }
333 
334   private boolean isManagementCache(final String cacheName)
335   {
336     return cacheName.startsWith("Management");
337   }
338 
339   /**
340    * Stops all caches, rendering the manager in a stopped state.
341    */
342   private void internalStopAll()
343   {
344     configManager.stopAll();
345     cache.stop();
346   }
347 
348   /**
349    * Stops the cache with the given name.
350    *
351    * @param cacheName the name of the cache to stop.
352    */
353   private void internalStop(final String cacheName)
354   {
355     if (isManagementCache(cacheName))
356     {
357       configManager.stop(cacheName);
358     }
359   }
360 
361   /**
362    * Returns the properties cache with the given name. The cache is already
363    * started.
364    *
365    * @param cacheName the name of the cache to fetch.
366    * @return the requested cache already started.
367    */
368   @SuppressWarnings({ StaticAnalysis.RAWTYPES, StaticAnalysis.UNCHECKED })
369   private UnawareCache<?, ?> internalGetPropertiesCache(final String cacheName)
370   {
371     final Cache infinispanCacheCache =
372         configManager.getInfinispanDelegatingCache();
373     InfinispanCompoundKeyDelegatingCache infinispanCache =
374         (InfinispanCompoundKeyDelegatingCache) infinispanCacheCache
375             .get(cacheName);
376 
377     if (infinispanCache == null)
378     {
379       infinispanCache =
380           new InfinispanCompoundKeyDelegatingCache(cacheName, this.cache);
381       infinispanCacheCache.put(cacheName, infinispanCache);
382     }
383     return infinispanCache;
384   }
385 
386   // --- object basics --------------------------------------------------------
387 
388 }