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.concurrent.TimeUnit;
19  import java.util.concurrent.locks.Lock;
20  import java.util.concurrent.locks.ReentrantReadWriteLock;
21  import java.util.logging.Logger;
22  
23  import de.smartics.properties.spi.config.cache.CompoundKey;
24  import de.smartics.properties.spi.config.cache.UnawareCache;
25  import de.smartics.util.lang.StaticAnalysis;
26  
27  /**
28   * Implementation of an cache that delegates to an infinispan cache.
29   *
30   * @param <K> the type for the key.
31   * @param <V> the type for the value.
32   */
33  public final class InfinispanCompoundKeyDelegatingCache<K, V> implements
34      UnawareCache<K, V>
35  {
36    // ********************************* Fields *********************************
37  
38    /**
39     * Reference to the logger for this instance.
40     */
41    @SuppressWarnings("unused")
42    private final Logger log = Logger
43        .getLogger(InfinispanCompoundKeyDelegatingCache.class.getName());
44  
45    /**
46     * The lock for synchronized access.
47     *
48     * @serial
49     */
50    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
51  
52    /**
53     * The read lock for synchronized access.
54     *
55     * @serial
56     */
57    private final Lock readLock = lock.readLock();
58  
59    /**
60     * The write lock for synchronized access.
61     *
62     * @serial
63     */
64    private final Lock writeLock = lock.writeLock();
65  
66    /**
67     * The cache to which everything is delegated.
68     */
69    @SuppressWarnings(StaticAnalysis.RAWTYPES)
70    private final org.infinispan.Cache cache;
71  
72    /**
73     * The name of the cache.
74     */
75    private final String cacheName;
76  
77    // --- constants ------------------------------------------------------------
78  
79    // --- members --------------------------------------------------------------
80  
81    // ****************************** Initializer *******************************
82  
83    // ****************************** Constructors ******************************
84  
85    /**
86     * Constructor to create a cache.
87     *
88     * @param cacheName the name of the cache.
89     * @param cache the cach under the hood, that is used to store everything.
90     */
91    public InfinispanCompoundKeyDelegatingCache(final String cacheName,
92        @SuppressWarnings("rawtypes") final org.infinispan.Cache cache)
93    {
94      this.cacheName = cacheName;
95      this.cache = cache;
96    }
97  
98    // ****************************** Inner Classes *****************************
99  
100   // ********************************* Methods ********************************
101 
102   // --- init -----------------------------------------------------------------
103 
104   // --- get&set --------------------------------------------------------------
105 
106   // --- business -------------------------------------------------------------
107 
108   @SuppressWarnings(StaticAnalysis.UNCHECKED)
109   @Override
110   public V get(final Object key)
111   {
112     readLock.lock();
113     try
114     {
115       final CompoundKey compoundKey = new CompoundKey(cacheName, key);
116       final String compoundKeyString = compoundKey.toString();
117       return (V) cache.get(compoundKeyString);
118     }
119     finally
120     {
121       readLock.unlock();
122     }
123   }
124 
125   @SuppressWarnings({ StaticAnalysis.UNCHECKED })
126   @Override
127   public void put(final K key, final V value, final long interval,
128       final TimeUnit unit)
129   {
130     writeLock.lock();
131     try
132     {
133       final CompoundKey compoundKey = new CompoundKey(cacheName, key);
134       final String compoundKeyString = compoundKey.toString();
135       cache.put(compoundKeyString, value, interval, unit);
136     }
137     finally
138     {
139       writeLock.unlock();
140     }
141   }
142 
143   @SuppressWarnings({ StaticAnalysis.UNCHECKED })
144   @Override
145   public void put(final K key, final V value)
146   {
147     writeLock.lock();
148     try
149     {
150       final CompoundKey compoundKey = new CompoundKey(cacheName, key);
151       final String compoundKeyString = compoundKey.toString();
152       cache.put(compoundKeyString, value);
153     }
154     finally
155     {
156       writeLock.unlock();
157     }
158   }
159 
160   @SuppressWarnings({ StaticAnalysis.UNCHECKED })
161   @Override
162   // CHECKSTYLE:OFF
163   public void put(final K key, final V value, final int interval,
164       final TimeUnit unit, final int maxIdleTime,
165       final TimeUnit maxIdleTimeTimeUnit)
166   {
167     writeLock.lock();
168     try
169     {
170       final CompoundKey compoundKey = new CompoundKey(cacheName, key);
171       final String compoundKeyString = compoundKey.toString();
172       cache.put(compoundKeyString, value, interval, unit, maxIdleTime,
173           maxIdleTimeTimeUnit);
174     }
175     finally
176     {
177       writeLock.unlock();
178     }
179   }
180 
181   // CHECKSTYLE:ON
182 
183   @SuppressWarnings({ StaticAnalysis.UNCHECKED })
184   @Override
185   public V remove(final Object key)
186   {
187     writeLock.lock();
188     try
189     {
190       final CompoundKey compoundKey = new CompoundKey(cacheName, key);
191       final String compoundKeyString = compoundKey.toString();
192       return (V) cache.remove(compoundKeyString);
193     }
194     finally
195     {
196       writeLock.unlock();
197     }
198   }
199 
200   // --- object basics --------------------------------------------------------
201 
202 }