LCOV - code coverage report
Current view: top level - server - RequestCleanup.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 19 22 86.4 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          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;
      16             : 
      17             : import com.google.common.flogger.FluentLogger;
      18             : import com.google.inject.servlet.RequestScoped;
      19             : import java.util.ArrayList;
      20             : import java.util.Iterator;
      21             : import java.util.List;
      22             : 
      23             : /** Registers cleanup activities to be completed when a scope ends. */
      24             : @RequestScoped
      25         138 : public class RequestCleanup implements Runnable {
      26         138 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      27             : 
      28         138 :   private final List<Runnable> cleanup = new ArrayList<>();
      29             :   private boolean ran;
      30             : 
      31             :   /** Register a task to be completed after the request ends. */
      32             :   public void add(Runnable task) {
      33           6 :     synchronized (cleanup) {
      34           6 :       assertNotRan();
      35           6 :       cleanup.add(task);
      36           6 :     }
      37           6 :   }
      38             : 
      39             :   @Override
      40             :   public void run() {
      41         108 :     synchronized (cleanup) {
      42         108 :       assertNotRan();
      43         108 :       ran = true;
      44         108 :       for (Iterator<Runnable> i = cleanup.iterator(); i.hasNext(); ) {
      45             :         try {
      46           1 :           i.next().run();
      47           0 :         } catch (Exception err) {
      48           0 :           logger.atSevere().withCause(err).log("Failed to execute per-request cleanup");
      49           1 :         }
      50           1 :         i.remove();
      51             :       }
      52         108 :     }
      53         108 :   }
      54             : 
      55             :   private void assertNotRan() {
      56         108 :     if (ran) {
      57           0 :       throw new IllegalStateException("Request has already been cleaned up");
      58             :     }
      59         108 :   }
      60             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750