|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * The contents of this file are subject to the terms of either the GNU |
| 7 | + * General Public License Version 2 only ("GPL") or the Common Development |
| 8 | + * and Distribution License("CDDL") (collectively, the "License"). You |
| 9 | + * may not use this file except in compliance with the License. You can |
| 10 | + * obtain a copy of the License at |
| 11 | + * http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 | + * or packager/legal/LICENSE.txt. See the License for the specific |
| 13 | + * language governing permissions and limitations under the License. |
| 14 | + * |
| 15 | + * When distributing the software, include this License Header Notice in each |
| 16 | + * file and include the License file at packager/legal/LICENSE.txt. |
| 17 | + * |
| 18 | + * GPL Classpath Exception: |
| 19 | + * Oracle designates this particular file as subject to the "Classpath" |
| 20 | + * exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 | + * file that accompanied this code. |
| 22 | + * |
| 23 | + * Modifications: |
| 24 | + * If applicable, add the following below the License Header, with the fields |
| 25 | + * enclosed by brackets [] replaced by your own identifying information: |
| 26 | + * "Portions Copyright [year] [name of copyright owner]" |
| 27 | + * |
| 28 | + * Contributor(s): |
| 29 | + * If you wish your version of this file to be governed by only the CDDL or |
| 30 | + * only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 | + * elects to include this software in this distribution under the [CDDL or GPL |
| 32 | + * Version 2] license." If you don't indicate a single choice of license, a |
| 33 | + * recipient has the option to distribute your version of this file under |
| 34 | + * either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 | + * its licensees as provided above. However, if you add GPL Version 2 code |
| 36 | + * and therefore, elected the GPL Version 2 license, then the option applies |
| 37 | + * only if the new code is made subject to such option by the copyright |
| 38 | + * holder. |
| 39 | + */ |
| 40 | + |
| 41 | +package org.glassfish.jersey.internal.util.collection; |
| 42 | + |
| 43 | +import java.util.concurrent.Callable; |
| 44 | +import java.util.concurrent.ConcurrentHashMap; |
| 45 | +import java.util.concurrent.ExecutionException; |
| 46 | +import java.util.concurrent.Future; |
| 47 | +import java.util.concurrent.FutureTask; |
| 48 | +import java.util.concurrent.TimeUnit; |
| 49 | +import java.util.concurrent.TimeoutException; |
| 50 | +import java.util.function.Function; |
| 51 | + |
| 52 | +/** |
| 53 | + * Cache implementation that relies on FutureTask. |
| 54 | + * Desired value will only be computed once and computed value stored in the cache. |
| 55 | + * The implementation is based on an example from the "Java Concurrency in Practice" book |
| 56 | + * authored by Brian Goetz and company. |
| 57 | + * |
| 58 | + * @param <K> The type of the key of the cache |
| 59 | + * @param <V> The type of the values in the cache |
| 60 | + * @author Jakub Podlesak (jakub.podlesak at oracle.com) |
| 61 | + */ |
| 62 | +public class Cache<K, V> implements Function<K, V> { |
| 63 | + |
| 64 | + private static final CycleHandler<Object> EMPTY_HANDLER = key -> { }; |
| 65 | + private final CycleHandler<K> cycleHandler; |
| 66 | + private final ConcurrentHashMap<K, OriginThreadAwareFuture> cache = new ConcurrentHashMap<>(); |
| 67 | + private final Function<K, V> computable; |
| 68 | + |
| 69 | + /** |
| 70 | + * Create new cache with given computable to compute values. |
| 71 | + * Detected cycles will be ignored as there is a no-op cycle handler registered by default. |
| 72 | + * |
| 73 | + * @param computable function generated the new value. |
| 74 | + */ |
| 75 | + @SuppressWarnings("unchecked") |
| 76 | + public Cache(Function<K, V> computable) { |
| 77 | + this(computable, (CycleHandler<K>) EMPTY_HANDLER); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Create new cache with given computable and cycle handler. |
| 82 | + * |
| 83 | + * @param computable function generated the new value. |
| 84 | + * @param cycleHandler handler used if the thread cycle is met. |
| 85 | + */ |
| 86 | + public Cache(Function<K, V> computable, CycleHandler<K> cycleHandler) { |
| 87 | + this.computable = computable; |
| 88 | + this.cycleHandler = cycleHandler; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public V apply(final K key) { |
| 93 | + while (true) { |
| 94 | + OriginThreadAwareFuture f = cache.get(key); |
| 95 | + if (f == null) { |
| 96 | + OriginThreadAwareFuture ft = new OriginThreadAwareFuture(key); |
| 97 | + |
| 98 | + f = cache.putIfAbsent(key, ft); |
| 99 | + if (f == null) { |
| 100 | + f = ft; |
| 101 | + ft.run(); |
| 102 | + } |
| 103 | + } else { |
| 104 | + final long tid = f.threadId; |
| 105 | + if ((tid != -1) && (Thread.currentThread().getId() == f.threadId)) { |
| 106 | + cycleHandler.handleCycle(key); |
| 107 | + } |
| 108 | + } |
| 109 | + try { |
| 110 | + return f.get(); |
| 111 | + } catch (InterruptedException ex) { |
| 112 | + throw new RuntimeException(ex); |
| 113 | + } catch (ExecutionException ex) { |
| 114 | + cache.remove(key); // otherwise the exception would be remembered |
| 115 | + Throwable cause = ex.getCause(); |
| 116 | + if (cause == null) { |
| 117 | + throw new RuntimeException(ex); |
| 118 | + } |
| 119 | + if (cause instanceof RuntimeException) { |
| 120 | + throw (RuntimeException) cause; |
| 121 | + } |
| 122 | + throw new RuntimeException(cause); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Empty cache. |
| 129 | + */ |
| 130 | + public void clear() { |
| 131 | + cache.clear(); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Returns true if the key has already been cached. |
| 136 | + * |
| 137 | + * @param key item key. |
| 138 | + * @return true if given key is present in the cache. |
| 139 | + */ |
| 140 | + public boolean containsKey(final K key) { |
| 141 | + return cache.containsKey(key); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Remove item from the cache. |
| 146 | + * |
| 147 | + * @param key item key. |
| 148 | + */ |
| 149 | + public void remove(final K key) { |
| 150 | + cache.remove(key); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Returns the size of the cache |
| 155 | + * |
| 156 | + * @return The number of elements in the cache |
| 157 | + */ |
| 158 | + public int size() { |
| 159 | + return cache.size(); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Should a cycle be detected during computation of a value |
| 164 | + * for given key, this interface allows client code to register |
| 165 | + * a callback that would get invoked in such a case. |
| 166 | + * |
| 167 | + * @param <K> Key type. |
| 168 | + */ |
| 169 | + public interface CycleHandler<K> { |
| 170 | + |
| 171 | + /** |
| 172 | + * Handle cycle that was detected while computing a cache value |
| 173 | + * for given key. This method would typically just throw a runtime exception. |
| 174 | + * |
| 175 | + * @param key instance that caused the cycle. |
| 176 | + */ |
| 177 | + void handleCycle(K key); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Helper class, that remembers the future task origin thread, so that cycles could be detected. |
| 182 | + * If any thread starts computation for given key and the same thread requests the computed value |
| 183 | + * before the computation stops, a cycle is detected and registered cycle handler is called. |
| 184 | + */ |
| 185 | + private class OriginThreadAwareFuture implements Future<V> { |
| 186 | + private final FutureTask<V> future; |
| 187 | + private volatile long threadId; |
| 188 | + |
| 189 | + OriginThreadAwareFuture(K key) { |
| 190 | + this.threadId = Thread.currentThread().getId(); |
| 191 | + Callable<V> eval = () -> { |
| 192 | + try { |
| 193 | + return computable.apply(key); |
| 194 | + } finally { |
| 195 | + threadId = -1; |
| 196 | + } |
| 197 | + }; |
| 198 | + this.future = new FutureTask<>(eval); |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public int hashCode() { |
| 203 | + return future.hashCode(); |
| 204 | + } |
| 205 | + |
| 206 | + @SuppressWarnings("unchecked") |
| 207 | + @Override |
| 208 | + public boolean equals(Object obj) { |
| 209 | + if (obj == null) { |
| 210 | + return false; |
| 211 | + } |
| 212 | + if (getClass() != obj.getClass()) { |
| 213 | + return false; |
| 214 | + } |
| 215 | + |
| 216 | + final OriginThreadAwareFuture other = (OriginThreadAwareFuture) obj; |
| 217 | + if (this.future != other.future && (this.future == null || !this.future.equals(other.future))) { |
| 218 | + return false; |
| 219 | + } |
| 220 | + return true; |
| 221 | + } |
| 222 | + |
| 223 | + @Override |
| 224 | + public boolean cancel(boolean mayInterruptIfRunning) { |
| 225 | + return future.cancel(mayInterruptIfRunning); |
| 226 | + } |
| 227 | + |
| 228 | + @Override |
| 229 | + public boolean isCancelled() { |
| 230 | + return future.isCancelled(); |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public boolean isDone() { |
| 235 | + return future.isDone(); |
| 236 | + } |
| 237 | + |
| 238 | + @Override |
| 239 | + public V get() throws InterruptedException, ExecutionException { |
| 240 | + return future.get(); |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
| 245 | + return future.get(timeout, unit); |
| 246 | + } |
| 247 | + |
| 248 | + public void run() { |
| 249 | + future.run(); |
| 250 | + } |
| 251 | + } |
| 252 | +} |
0 commit comments