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 com.google.common.flogger.FluentLogger; 18 : import com.google.gerrit.common.Nullable; 19 : import com.google.gerrit.extensions.registration.DynamicMap; 20 : import com.google.gerrit.server.DynamicOptions; 21 : import com.google.gerrit.server.plugins.Plugin; 22 : import com.google.gerrit.server.plugins.ReloadPluginListener; 23 : import com.google.gerrit.server.plugins.StartPluginListener; 24 : import com.google.inject.Inject; 25 : import com.google.inject.Key; 26 : import com.google.inject.Provider; 27 : import com.google.inject.Singleton; 28 : import org.apache.sshd.server.command.Command; 29 : 30 : @Singleton 31 : class SshPluginStarterCallback implements StartPluginListener, ReloadPluginListener { 32 17 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 33 : 34 : private final DispatchCommandProvider root; 35 : private final DynamicMap<DynamicOptions.DynamicBean> dynamicBeans; 36 : 37 : @Inject 38 : SshPluginStarterCallback( 39 : @CommandName(Commands.ROOT) DispatchCommandProvider root, 40 17 : DynamicMap<DynamicOptions.DynamicBean> dynamicBeans) { 41 17 : this.root = root; 42 17 : this.dynamicBeans = dynamicBeans; 43 17 : } 44 : 45 : @Override 46 : public void onStartPlugin(Plugin plugin) { 47 3 : Provider<Command> cmd = load(plugin); 48 3 : if (cmd != null) { 49 0 : plugin.add(root.register(Commands.named(plugin.getName()), cmd)); 50 : } 51 3 : } 52 : 53 : @Override 54 : public void onReloadPlugin(Plugin oldPlugin, Plugin newPlugin) { 55 0 : Provider<Command> cmd = load(newPlugin); 56 0 : if (cmd != null) { 57 0 : newPlugin.add(root.replace(Commands.named(newPlugin.getName()), cmd)); 58 : } 59 0 : } 60 : 61 : @Nullable 62 : private Provider<Command> load(Plugin plugin) { 63 3 : if (plugin.getSshInjector() != null) { 64 0 : Key<Command> key = Commands.key(plugin.getName()); 65 : try { 66 0 : return plugin.getSshInjector().getProvider(key); 67 0 : } catch (RuntimeException err) { 68 0 : if (!providesDynamicOptions(plugin)) { 69 0 : logger.atWarning().withCause(err).log( 70 : "Plugin %s did not define its top-level command nor any DynamicOptions", 71 0 : plugin.getName()); 72 : } 73 : } 74 : } 75 3 : return null; 76 : } 77 : 78 : private boolean providesDynamicOptions(Plugin plugin) { 79 0 : return dynamicBeans.plugins().contains(plugin.getName()); 80 : } 81 : }