View Javadoc

1   package com.atlassian.cache.ehcache;
2   
3   import java.util.SortedMap;
4   
5   import javax.annotation.Nonnull;
6   import javax.annotation.Nullable;
7   
8   import com.atlassian.cache.CacheException;
9   import com.atlassian.cache.CacheSettings;
10  import com.atlassian.cache.CacheStatisticsKey;
11  import com.atlassian.cache.CachedReference;
12  import com.atlassian.cache.CachedReferenceListener;
13  import com.atlassian.cache.impl.CachedReferenceListenerSupport;
14  import com.atlassian.cache.impl.LazyCachedReferenceListenerSupport;
15  import com.atlassian.cache.impl.ReferenceKey;
16  import com.atlassian.util.concurrent.Supplier;
17  
18  import net.sf.ehcache.Ehcache;
19  import net.sf.ehcache.Element;
20  import net.sf.ehcache.event.CacheEventListener;
21  
22  import static com.atlassian.cache.ehcache.DelegatingCacheStatistics.toStatistics;
23  
24  /**
25   * A Lazy Reference that delegates to EhCache.
26   *
27   * @since 2.0
28   */
29  class DelegatingCachedReference<V> extends ManagedCacheSupport implements CachedReference<V>
30  {
31      private final Ehcache delegate;
32      private final CachedReferenceListenerSupport<V> listenerSupport = new LazyCachedReferenceListenerSupport<V>()
33      {
34          @Override
35          protected void init()
36          {
37              delegate.getCacheEventNotificationService().registerListener(new DelegatingReferenceCacheEventListener());
38          }
39      };
40  
41      private DelegatingCachedReference(final Ehcache delegate, CacheSettings settings)
42      {
43          super(delegate, settings);
44          this.delegate = delegate;
45      }
46  
47      static <V> DelegatingCachedReference<V> create(final Ehcache delegate, CacheSettings settings)
48      {
49          return new DelegatingCachedReference<V>(delegate, settings);
50      }
51  
52      @Nonnull
53      @SuppressWarnings("unchecked")
54      @Override
55      public V get()
56      {
57          try
58          {
59              Element element = delegate.get(ReferenceKey.KEY);
60              return element == null ? null : (V) element.getObjectValue();
61          }
62          catch (net.sf.ehcache.CacheException e)
63          {
64              throw new CacheException(e.getCause());
65          }
66          catch (Exception e)
67          {
68              throw new CacheException(e);
69          }
70      }
71  
72      @Override
73      public void reset()
74      {
75          try
76          {
77              delegate.remove(ReferenceKey.KEY);
78          }
79          catch (Exception e)
80          {
81              throw new CacheException(e);
82          }
83      }
84  
85      @Override
86      public void clear()
87      {
88          reset();
89      }
90  
91      public boolean equals(@Nullable final Object other)
92      {
93          if (other instanceof DelegatingCachedReference)
94          {
95              DelegatingCachedReference<?> otherDelegatingReference = (DelegatingCachedReference<?>) other;
96              if (delegate.equals(otherDelegatingReference.delegate))
97              {
98                  return true;
99              }
100         }
101         return false;
102     }
103 
104     @Override
105     public int hashCode()
106     {
107         return 3 + delegate.hashCode();
108     }
109 
110     @Nonnull
111     @Override
112     public SortedMap<CacheStatisticsKey,Supplier<Long>> getStatistics()
113     {
114         return toStatistics(delegate.getStatistics());
115     }
116 
117     @Override
118     public void addListener(@Nonnull CachedReferenceListener<V> listener, boolean includeValues)
119     {
120         listenerSupport.add(listener, includeValues);
121     }
122 
123     @Override
124     public void removeListener(@Nonnull CachedReferenceListener<V> listener)
125     {
126         listenerSupport.remove(listener);
127     }
128 
129     private class DelegatingReferenceCacheEventListener implements CacheEventListener
130     {
131         @Override
132         public void notifyElementRemoved(Ehcache ehcache, Element element) throws net.sf.ehcache.CacheException
133         {
134             listenerSupport.notifyReset((V) element.getObjectValue());
135         }
136 
137         @Override
138         public void notifyElementPut(Ehcache ehcache, Element element) throws net.sf.ehcache.CacheException
139         {
140             listenerSupport.notifySet((V) element.getObjectValue());
141         }
142 
143         @Override
144         public void notifyElementUpdated(Ehcache ehcache, Element element) throws net.sf.ehcache.CacheException
145         {
146             listenerSupport.notifySet((V) element.getObjectValue());
147         }
148 
149         @Override
150         public void notifyElementExpired(Ehcache ehcache, Element element)
151         {
152             listenerSupport.notifyEvict((V) element.getObjectValue());
153         }
154 
155         @Override
156         public void notifyElementEvicted(Ehcache ehcache, Element element)
157         {
158             listenerSupport.notifyEvict((V) element.getObjectValue());
159         }
160 
161         @Override
162         public void notifyRemoveAll(Ehcache ehcache)
163         {
164             listenerSupport.notifyReset((V) null);
165         }
166 
167         @Override
168         public void dispose()
169         {
170             // We don't hold onto any resources so there is nothing to be done.
171         }
172 
173         public Object clone() throws CloneNotSupportedException
174         {
175             throw new CloneNotSupportedException();
176         }
177     }
178 }