Line data Source code
1 : // Copyright (C) 2009 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.git; 16 : 17 : import com.google.gerrit.server.util.RequestContext; 18 : import com.google.gerrit.server.util.ThreadLocalRequestContext; 19 : import com.google.gerrit.server.util.ThreadLocalRequestScopePropagator; 20 : import com.google.inject.Inject; 21 : import com.google.inject.Key; 22 : import com.google.inject.OutOfScopeException; 23 : import com.google.inject.Provider; 24 : import com.google.inject.Scope; 25 : import java.util.HashMap; 26 : import java.util.Map; 27 : import java.util.concurrent.Callable; 28 : 29 0 : public class PerThreadRequestScope { 30 : public interface Scoper { 31 : <T> Callable<T> scope(Callable<T> callable); 32 : } 33 : 34 : private static class Context { 35 : private final Map<Key<?>, Object> map; 36 : 37 0 : private Context() { 38 0 : map = new HashMap<>(); 39 0 : } 40 : 41 : private <T> T get(Key<T> key, Provider<T> creator) { 42 : @SuppressWarnings("unchecked") 43 0 : T t = (T) map.get(key); 44 0 : if (t == null) { 45 0 : t = creator.get(); 46 0 : map.put(key, t); 47 : } 48 0 : return t; 49 : } 50 : } 51 : 52 : public static class Propagator extends ThreadLocalRequestScopePropagator<Context> { 53 : @Inject 54 : Propagator(ThreadLocalRequestContext local) { 55 0 : super(REQUEST, current, local); 56 0 : } 57 : 58 : @Override 59 : protected Context continuingContext(Context ctx) { 60 0 : return new Context(); 61 : } 62 : 63 : public <T> Callable<T> scope(RequestContext requestContext, Callable<T> callable) { 64 0 : Context ctx = new Context(); 65 0 : Callable<T> wrapped = context(requestContext, cleanup(callable)); 66 0 : return () -> { 67 0 : Context old = current.get(); 68 0 : current.set(ctx); 69 : try { 70 0 : return wrapped.call(); 71 : } finally { 72 0 : current.set(old); 73 : } 74 : }; 75 : } 76 : } 77 : 78 16 : private static final ThreadLocal<Context> current = new ThreadLocal<>(); 79 : 80 : private static Context requireContext() { 81 0 : final Context ctx = current.get(); 82 0 : if (ctx == null) { 83 0 : throw new OutOfScopeException("Not in command/request"); 84 : } 85 0 : return ctx; 86 : } 87 : 88 16 : public static final Scope REQUEST = 89 16 : new Scope() { 90 : @Override 91 : public <T> Provider<T> scope(Key<T> key, Provider<T> creator) { 92 0 : return new Provider<>() { 93 : @Override 94 : public T get() { 95 0 : return requireContext().get(key, creator); 96 : } 97 : 98 : @Override 99 : public String toString() { 100 0 : return String.format("%s[%s]", creator, REQUEST); 101 : } 102 : }; 103 : } 104 : 105 : @Override 106 : public String toString() { 107 0 : return "PerThreadRequestScope.REQUEST"; 108 : } 109 : }; 110 : }