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 com.google.common.collect.ImmutableSortedMap;
19
20 import net.sf.ehcache.Ehcache;
21 import net.sf.ehcache.Element;
22 import net.sf.ehcache.event.CacheEventListener;
23
24 import static com.atlassian.cache.ehcache.DelegatingCacheStatistics.toStatistics;
25
26
27
28
29
30
31 class DelegatingCachedReference<V> extends ManagedCacheSupport implements CachedReference<V>
32 {
33 private final Ehcache delegate;
34 private final CachedReferenceListenerSupport<V> listenerSupport = new LazyCachedReferenceListenerSupport<V>()
35 {
36 @Override
37 protected void init()
38 {
39 delegate.getCacheEventNotificationService().registerListener(new DelegatingReferenceCacheEventListener());
40 }
41 };
42
43 private DelegatingCachedReference(final Ehcache delegate, CacheSettings settings)
44 {
45 super(delegate, settings);
46 this.delegate = delegate;
47 }
48
49 static <V> DelegatingCachedReference<V> create(final Ehcache delegate, CacheSettings settings)
50 {
51 return new DelegatingCachedReference<V>(delegate, settings);
52 }
53
54 @Nonnull
55 @SuppressWarnings("unchecked")
56 @Override
57 public V get()
58 {
59 try
60 {
61 return (V)delegate.get(ReferenceKey.KEY).getObjectValue();
62 }
63 catch (net.sf.ehcache.CacheException e)
64 {
65 throw new CacheException(e.getCause());
66 }
67 catch (Exception e)
68 {
69 throw new CacheException(e);
70 }
71 }
72
73 @Override
74 public void reset()
75 {
76 try
77 {
78 delegate.remove(ReferenceKey.KEY);
79 }
80 catch (Exception e)
81 {
82 throw new CacheException(e);
83 }
84 }
85
86 @Override
87 public void clear()
88 {
89 reset();
90 }
91
92 public boolean equals(@Nullable final Object other)
93 {
94 if (other instanceof DelegatingCachedReference)
95 {
96 DelegatingCachedReference<?> otherDelegatingReference = (DelegatingCachedReference<?>) other;
97 if (delegate.equals(otherDelegatingReference.delegate))
98 {
99 return true;
100 }
101 }
102 return false;
103 }
104
105 @Override
106 public int hashCode()
107 {
108 return 3 + delegate.hashCode();
109 }
110
111 @Nonnull
112 @Override
113 public SortedMap<CacheStatisticsKey,Supplier<Long>> getStatistics()
114 {
115 if (isStatisticsEnabled())
116 {
117 return toStatistics(delegate.getStatistics());
118 }
119 else
120 {
121 return ImmutableSortedMap.of();
122 }
123 }
124
125 @Override
126 public void addListener(@Nonnull CachedReferenceListener<V> listener, boolean includeValues)
127 {
128 listenerSupport.add(listener, includeValues);
129 }
130
131 @Override
132 public void removeListener(@Nonnull CachedReferenceListener<V> listener)
133 {
134 listenerSupport.remove(listener);
135 }
136
137 private class DelegatingReferenceCacheEventListener implements CacheEventListener
138 {
139 @Override
140 public void notifyElementRemoved(Ehcache ehcache, Element element) throws net.sf.ehcache.CacheException
141 {
142 listenerSupport.notifyReset((V) element.getObjectValue());
143 }
144
145 @Override
146 public void notifyElementPut(Ehcache ehcache, Element element) throws net.sf.ehcache.CacheException
147 {
148 listenerSupport.notifySet((V) element.getObjectValue());
149 }
150
151 @Override
152 public void notifyElementUpdated(Ehcache ehcache, Element element) throws net.sf.ehcache.CacheException
153 {
154 listenerSupport.notifySet((V) element.getObjectValue());
155 }
156
157 @Override
158 public void notifyElementExpired(Ehcache ehcache, Element element)
159 {
160 listenerSupport.notifyEvict((V) element.getObjectValue());
161 }
162
163 @Override
164 public void notifyElementEvicted(Ehcache ehcache, Element element)
165 {
166 listenerSupport.notifyEvict((V) element.getObjectValue());
167 }
168
169 @Override
170 public void notifyRemoveAll(Ehcache ehcache)
171 {
172 listenerSupport.notifyReset((V) null);
173 }
174
175 @Override
176 public void dispose()
177 {
178
179 }
180
181 public Object clone() throws CloneNotSupportedException
182 {
183 throw new CloneNotSupportedException();
184 }
185 }
186 }