LCOV - code coverage report
Current view: top level - sshd - SshAutoRegisterModuleGenerator.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 0 33 0.0 %
Date: 2022-11-19 15:00:39 Functions: 0 6 0.0 %

          Line data    Source code
       1             : // Copyright (C) 2012 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.sshd;
      16             : 
      17             : import static com.google.common.base.Preconditions.checkState;
      18             : import static com.google.gerrit.server.plugins.AutoRegisterUtil.calculateBindAnnotation;
      19             : 
      20             : import com.google.common.collect.LinkedListMultimap;
      21             : import com.google.common.collect.ListMultimap;
      22             : import com.google.gerrit.common.Nullable;
      23             : import com.google.gerrit.extensions.annotations.Export;
      24             : import com.google.gerrit.server.plugins.InvalidPluginException;
      25             : import com.google.gerrit.server.plugins.ModuleGenerator;
      26             : import com.google.inject.AbstractModule;
      27             : import com.google.inject.Module;
      28             : import com.google.inject.TypeLiteral;
      29             : import java.lang.annotation.Annotation;
      30             : import java.util.HashMap;
      31             : import java.util.Map;
      32             : import org.apache.sshd.server.command.Command;
      33             : 
      34           0 : class SshAutoRegisterModuleGenerator extends AbstractModule implements ModuleGenerator {
      35           0 :   private final Map<String, Class<Command>> commands = new HashMap<>();
      36           0 :   private final ListMultimap<TypeLiteral<?>, Class<?>> listeners = LinkedListMultimap.create();
      37             :   private CommandName command;
      38             : 
      39             :   @Override
      40             :   protected void configure() {
      41           0 :     bind(Commands.key(command)).toProvider(new DispatchCommandProvider(command));
      42           0 :     for (Map.Entry<String, Class<Command>> e : commands.entrySet()) {
      43           0 :       bind(Commands.key(command, e.getKey())).to(e.getValue());
      44           0 :     }
      45           0 :     for (Map.Entry<TypeLiteral<?>, Class<?>> e : listeners.entries()) {
      46             :       @SuppressWarnings("unchecked")
      47           0 :       TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey();
      48             : 
      49             :       @SuppressWarnings("unchecked")
      50           0 :       Class<Object> impl = (Class<Object>) e.getValue();
      51             : 
      52           0 :       Annotation n = calculateBindAnnotation(impl);
      53           0 :       bind(type).annotatedWith(n).to(impl);
      54           0 :     }
      55           0 :   }
      56             : 
      57             :   @Override
      58             :   public void setPluginName(String name) {
      59           0 :     command = Commands.named(name);
      60           0 :   }
      61             : 
      62             :   @SuppressWarnings("unchecked")
      63             :   @Override
      64             :   public void export(Export export, Class<?> type) throws InvalidPluginException {
      65           0 :     checkState(command != null, "pluginName must be provided");
      66           0 :     if (Command.class.isAssignableFrom(type)) {
      67           0 :       Class<Command> old = commands.get(export.value());
      68           0 :       if (old != null) {
      69           0 :         throw new InvalidPluginException(
      70           0 :             String.format(
      71             :                 "@Export(\"%s\") has duplicate bindings:\n  %s\n  %s",
      72           0 :                 export.value(), old.getName(), type.getName()));
      73             :       }
      74           0 :       commands.put(export.value(), (Class<Command>) type);
      75           0 :     } else {
      76           0 :       throw new InvalidPluginException(
      77           0 :           String.format(
      78             :               "Class %s with @Export(\"%s\") must extend %s or implement %s",
      79           0 :               type.getName(), export.value(), SshCommand.class.getName(), Command.class.getName()));
      80             :     }
      81           0 :   }
      82             : 
      83             :   @Override
      84             :   public void listen(TypeLiteral<?> tl, Class<?> clazz) {
      85           0 :     listeners.put(tl, clazz);
      86           0 :   }
      87             : 
      88             :   @Nullable
      89             :   @Override
      90             :   public Module create() throws InvalidPluginException {
      91           0 :     checkState(command != null, "pluginName must be provided");
      92           0 :     return !commands.isEmpty() ? this : null;
      93             :   }
      94             : }

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