Line data Source code
1 : // Copyright (C) 2020 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.logging; 16 : 17 : import com.google.common.base.MoreObjects; 18 : import com.google.common.collect.ImmutableList; 19 : import java.util.ArrayList; 20 : import java.util.List; 21 : 22 : /** 23 : * Thread-safe store for ACL log records. 24 : * 25 : * <p>This class is intended to keep track of user ACL records in {@link LoggingContext}. It needs 26 : * to be thread-safe because it gets shared between threads when the logging context is copied to 27 : * another thread (see {@link LoggingContextAwareRunnable} and {@link LoggingContextAwareCallable}. 28 : * In this case the logging contexts of both threads share the same instance of this class. This is 29 : * important since ACL log records are processed only at the end of a request and user ACL records 30 : * that are created in another thread should not get lost. 31 : */ 32 148 : public class MutableAclLogRecords { 33 148 : private final ArrayList<String> aclLogRecords = new ArrayList<>(); 34 : 35 : public synchronized void add(String record) { 36 1 : aclLogRecords.add(record); 37 1 : } 38 : 39 : public synchronized void set(List<String> records) { 40 0 : aclLogRecords.clear(); 41 0 : aclLogRecords.addAll(records); 42 0 : } 43 : 44 : public synchronized ImmutableList<String> list() { 45 148 : return ImmutableList.copyOf(aclLogRecords); 46 : } 47 : 48 : public boolean isEmpty() { 49 15 : return aclLogRecords.isEmpty(); 50 : } 51 : 52 : @Override 53 : public String toString() { 54 0 : return MoreObjects.toStringHelper(this).add("aclLogRecords", aclLogRecords).toString(); 55 : } 56 : }