LCOV - code coverage report
Current view: top level - server/plugins - UniversalServerPluginProvider.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 22 27 81.5 %
Date: 2022-11-19 15:00:39 Functions: 8 9 88.9 %

          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.plugins;
      16             : 
      17             : import com.google.common.flogger.FluentLogger;
      18             : import com.google.gerrit.extensions.registration.PluginName;
      19             : import com.google.gerrit.server.plugincontext.PluginSetContext;
      20             : import com.google.inject.Inject;
      21             : import com.google.inject.Singleton;
      22             : import java.nio.file.Path;
      23             : import java.util.ArrayList;
      24             : import java.util.List;
      25             : import org.eclipse.jgit.internal.storage.file.FileSnapshot;
      26             : 
      27             : @Singleton
      28             : class UniversalServerPluginProvider implements ServerPluginProvider {
      29         149 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      30             : 
      31             :   private final PluginSetContext<ServerPluginProvider> serverPluginProviders;
      32             : 
      33             :   @Inject
      34         149 :   UniversalServerPluginProvider(PluginSetContext<ServerPluginProvider> sf) {
      35         149 :     this.serverPluginProviders = sf;
      36         149 :   }
      37             : 
      38             :   @Override
      39             :   public ServerPlugin get(Path srcPath, FileSnapshot snapshot, PluginDescription pluginDescription)
      40             :       throws InvalidPluginException {
      41           1 :     return providerOf(srcPath).get(srcPath, snapshot, pluginDescription);
      42             :   }
      43             : 
      44             :   @Override
      45             :   public String getPluginName(Path srcPath) {
      46           1 :     return providerOf(srcPath).getPluginName(srcPath);
      47             :   }
      48             : 
      49             :   @Override
      50             :   public boolean handles(Path srcPath) {
      51           3 :     List<ServerPluginProvider> providers = providersForHandlingPlugin(srcPath);
      52           3 :     switch (providers.size()) {
      53             :       case 1:
      54           1 :         return true;
      55             :       case 0:
      56           3 :         return false;
      57             :       default:
      58           0 :         throw new MultipleProvidersForPluginException(srcPath, providers);
      59             :     }
      60             :   }
      61             : 
      62             :   @Override
      63             :   public String getProviderPluginName() {
      64           0 :     return PluginName.GERRIT;
      65             :   }
      66             : 
      67             :   private ServerPluginProvider providerOf(Path srcPath) {
      68           1 :     List<ServerPluginProvider> providers = providersForHandlingPlugin(srcPath);
      69           1 :     switch (providers.size()) {
      70             :       case 1:
      71           1 :         return providers.get(0);
      72             :       case 0:
      73           0 :         throw new IllegalArgumentException(
      74             :             "No ServerPluginProvider found/loaded to handle plugin file "
      75           0 :                 + srcPath.toAbsolutePath());
      76             :       default:
      77           0 :         throw new MultipleProvidersForPluginException(srcPath, providers);
      78             :     }
      79             :   }
      80             : 
      81             :   private List<ServerPluginProvider> providersForHandlingPlugin(Path srcPath) {
      82           3 :     List<ServerPluginProvider> providers = new ArrayList<>();
      83           3 :     serverPluginProviders.runEach(
      84             :         serverPluginProvider -> {
      85           3 :           boolean handles = serverPluginProvider.handles(srcPath);
      86           3 :           logger.atFine().log(
      87             :               "File %s handled by %s ? => %s",
      88           3 :               srcPath, serverPluginProvider.getProviderPluginName(), handles);
      89           3 :           if (handles) {
      90           1 :             providers.add(serverPluginProvider);
      91             :           }
      92           3 :         });
      93           3 :     return providers;
      94             :   }
      95             : }

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