LCOV - code coverage report
Current view: top level - server/restapi/config - ListCaches.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 25 27 92.6 %
Date: 2022-11-19 15:00:39 Functions: 5 6 83.3 %

          Line data    Source code
       1             : // Copyright (C) 2014 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.restapi.config;
      16             : 
      17             : import static com.google.common.collect.ImmutableList.toImmutableList;
      18             : import static com.google.gerrit.common.data.GlobalCapability.MAINTAIN_SERVER;
      19             : import static com.google.gerrit.common.data.GlobalCapability.VIEW_CACHES;
      20             : import static com.google.gerrit.server.config.CacheResource.cacheNameOf;
      21             : import static java.nio.charset.StandardCharsets.UTF_8;
      22             : import static java.util.stream.Collectors.joining;
      23             : 
      24             : import com.google.common.cache.Cache;
      25             : import com.google.common.collect.Streams;
      26             : import com.google.gerrit.extensions.annotations.RequiresAnyCapability;
      27             : import com.google.gerrit.extensions.registration.DynamicMap;
      28             : import com.google.gerrit.extensions.registration.Extension;
      29             : import com.google.gerrit.extensions.restapi.BinaryResult;
      30             : import com.google.gerrit.extensions.restapi.Response;
      31             : import com.google.gerrit.extensions.restapi.RestReadView;
      32             : import com.google.gerrit.server.cache.CacheInfo;
      33             : import com.google.gerrit.server.config.ConfigResource;
      34             : import com.google.inject.Inject;
      35             : import java.util.Map;
      36             : import java.util.TreeMap;
      37             : import java.util.stream.Stream;
      38             : import org.kohsuke.args4j.Option;
      39             : 
      40             : @RequiresAnyCapability({VIEW_CACHES, MAINTAIN_SERVER})
      41             : public class ListCaches implements RestReadView<ConfigResource> {
      42             :   private final DynamicMap<Cache<?, ?>> cacheMap;
      43             : 
      44           2 :   public enum OutputFormat {
      45           2 :     LIST,
      46           2 :     TEXT_LIST
      47             :   }
      48             : 
      49             :   @Option(name = "--format", usage = "output format")
      50             :   private OutputFormat format;
      51             : 
      52             :   public ListCaches setFormat(OutputFormat format) {
      53           0 :     this.format = format;
      54           0 :     return this;
      55             :   }
      56             : 
      57             :   @Inject
      58           4 :   public ListCaches(DynamicMap<Cache<?, ?>> cacheMap) {
      59           4 :     this.cacheMap = cacheMap;
      60           4 :   }
      61             : 
      62             :   public Map<String, CacheInfo> getCacheInfos() {
      63           2 :     Map<String, CacheInfo> cacheInfos = new TreeMap<>();
      64           2 :     for (Extension<Cache<?, ?>> e : cacheMap) {
      65           2 :       cacheInfos.put(
      66           2 :           cacheNameOf(e.getPluginName(), e.getExportName()), new CacheInfo(e.getProvider().get()));
      67           2 :     }
      68           2 :     return cacheInfos;
      69             :   }
      70             : 
      71             :   @Override
      72             :   public Response<Object> apply(ConfigResource rsrc) {
      73           2 :     if (format == null) {
      74           2 :       return Response.ok(getCacheInfos());
      75             :     }
      76           1 :     Stream<String> cacheNames =
      77           1 :         Streams.stream(cacheMap)
      78           1 :             .map(e -> cacheNameOf(e.getPluginName(), e.getExportName()))
      79           1 :             .sorted();
      80           1 :     if (OutputFormat.TEXT_LIST.equals(format)) {
      81           1 :       return Response.ok(
      82           1 :           BinaryResult.create(cacheNames.collect(joining("\n")))
      83           1 :               .base64()
      84           1 :               .setContentType("text/plain")
      85           1 :               .setCharacterEncoding(UTF_8));
      86             :     }
      87           1 :     return Response.ok(cacheNames.collect(toImmutableList()));
      88             :   }
      89             : }

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