1 package com.atlassian.cache.ehcache;
2
3 import java.util.SortedMap;
4 import javax.annotation.Nonnull;
5 import javax.annotation.Nullable;
6
7 import com.atlassian.cache.CacheException;
8 import com.atlassian.cache.CacheSettings;
9 import com.atlassian.cache.CacheStatisticsKey;
10 import com.atlassian.cache.CachedReference;
11 import com.atlassian.cache.CachedReferenceListener;
12 import com.atlassian.cache.impl.CachedReferenceListenerSupport;
13 import com.atlassian.cache.impl.LazyCachedReferenceListenerSupport;
14 import com.atlassian.cache.impl.ReferenceKey;
15 import com.atlassian.util.concurrent.Supplier;
16 import net.sf.ehcache.Ehcache;
17 import net.sf.ehcache.Element;
18 import net.sf.ehcache.event.CacheEventListener;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import static com.atlassian.cache.ehcache.DelegatingCacheStatistics.toStatistics;
23
24
25
26
27
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 return (V)delegate.get(ReferenceKey.KEY).getObjectValue();
60 }
61 catch (net.sf.ehcache.CacheException e)
62 {
63 throw new CacheException(e.getCause());
64 }
65 catch (Exception e)
66 {
67 throw new CacheException(e);
68 }
69 }
70
71 @Override
72 public void reset()
73 {
74 try
75 {
76 delegate.remove(ReferenceKey.KEY);
77 }
78 catch (Exception e)
79 {
80 throw new CacheException(e);
81 }
82 }
83
84 @Override
85 public void clear()
86 {
87 reset();
88 }
89
90 public boolean equals(@Nullable final Object other)
91 {
92 if (other instanceof DelegatingCachedReference)
93 {
94 DelegatingCachedReference<?> otherDelegatingReference = (DelegatingCachedReference<?>) other;
95 if (delegate.equals(otherDelegatingReference.delegate))
96 {
97 return true;
98 }
99 }
100 return false;
101 }
102
103 @Override
104 public int hashCode()
105 {
106 return 3 + delegate.hashCode();
107 }
108
109 @Nonnull
110 @Override
111 public SortedMap<CacheStatisticsKey,Supplier<Long>> getStatistics()
112 {
113 return toStatistics(delegate.getStatistics());
114 }
115
116 @Override
117 public void addListener(@Nonnull CachedReferenceListener<V> listener, boolean includeValues)
118 {
119 listenerSupport.add(listener, includeValues);
120 }
121
122 @Override
123 public void removeListener(@Nonnull CachedReferenceListener<V> listener)
124 {
125 listenerSupport.remove(listener);
126 }
127
128 private class DelegatingReferenceCacheEventListener implements CacheEventListener
129 {
130 @Override
131 public void notifyElementRemoved(Ehcache ehcache, Element element) throws net.sf.ehcache.CacheException
132 {
133 listenerSupport.notifyReset((V) element.getObjectValue());
134 }
135
136 @Override
137 public void notifyElementPut(Ehcache ehcache, Element element) throws net.sf.ehcache.CacheException
138 {
139 listenerSupport.notifySet((V) element.getObjectValue());
140 }
141
142 @Override
143 public void notifyElementUpdated(Ehcache ehcache, Element element) throws net.sf.ehcache.CacheException
144 {
145 listenerSupport.notifySet((V) element.getObjectValue());
146 }
147
148 @Override
149 public void notifyElementExpired(Ehcache ehcache, Element element)
150 {
151 listenerSupport.notifyEvict((V) element.getObjectValue());
152 }
153
154 @Override
155 public void notifyElementEvicted(Ehcache ehcache, Element element)
156 {
157 listenerSupport.notifyEvict((V) element.getObjectValue());
158 }
159
160 @Override
161 public void notifyRemoveAll(Ehcache ehcache)
162 {
163 listenerSupport.notifyReset((V) null);
164 }
165
166 @Override
167 public void dispose()
168 {
169
170 }
171
172 public Object clone() throws CloneNotSupportedException
173 {
174 throw new CloneNotSupportedException();
175 }
176 }
177 }