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.sshd; 16 : 17 : import com.google.gerrit.lifecycle.LifecycleModule; 18 : import com.google.gerrit.server.account.AccountAttributeLoader; 19 : import com.google.inject.binder.LinkedBindingBuilder; 20 : import org.apache.sshd.server.command.Command; 21 : 22 : /** Module to register commands in the SSH daemon. */ 23 17 : public abstract class CommandModule extends LifecycleModule { 24 : protected boolean slaveMode; 25 : 26 : /** 27 : * Configure a command to be invoked by name. 28 : * 29 : * @param name the name of the command the client will provide in order to call the command. 30 : * @return a binding that must be bound to a non-singleton provider for a {@link Command} object. 31 : */ 32 : protected LinkedBindingBuilder<Command> command(String name) { 33 17 : factory(AccountAttributeLoader.Factory.class); 34 17 : return bind(Commands.key(name)); 35 : } 36 : 37 : /** 38 : * Configure a command to be invoked by name. 39 : * 40 : * @param name the name of the command the client will provide in order to call the command. 41 : * @return a binding that must be bound to a non-singleton provider for a {@link Command} object. 42 : */ 43 : protected LinkedBindingBuilder<Command> command(CommandName name) { 44 17 : return bind(Commands.key(name)); 45 : } 46 : 47 : /** 48 : * Configure a command to be invoked by name. 49 : * 50 : * @param parent context of the parent command, that this command is a subcommand of. 51 : * @param name the name of the command the client will provide in order to call the command. 52 : * @return a binding that must be bound to a non-singleton provider for a {@link Command} object. 53 : */ 54 : protected LinkedBindingBuilder<Command> command(CommandName parent, String name) { 55 17 : return bind(Commands.key(parent, name)); 56 : } 57 : 58 : /** 59 : * Configure a command to be invoked by name. The command is bound to the passed class. 60 : * 61 : * @param parent context of the parent command, that this command is a subcommand of. 62 : * @param clazz class of the command with {@link CommandMetaData} annotation to retrieve the name 63 : * and the description from 64 : */ 65 : protected void command(CommandName parent, Class<? extends BaseCommand> clazz) { 66 17 : CommandMetaData meta = clazz.getAnnotation(CommandMetaData.class); 67 17 : if (meta == null) { 68 0 : throw new IllegalStateException("no CommandMetaData annotation found"); 69 : } 70 17 : if (meta.runsAt().isSupported(slaveMode)) { 71 17 : bind(Commands.key(parent, meta.name(), meta.description())).to(clazz); 72 : } 73 17 : } 74 : 75 : /** 76 : * Alias one command to another. The alias is bound to the passed class. 77 : * 78 : * @param parent context of the parent command, that this command is a subcommand of. 79 : * @param name the name of the command the client will provide in order to call the command. 80 : * @param clazz class of the command with {@link CommandMetaData} annotation to retrieve the 81 : * description from 82 : */ 83 : protected void alias(final CommandName parent, String name, Class<? extends BaseCommand> clazz) { 84 17 : CommandMetaData meta = clazz.getAnnotation(CommandMetaData.class); 85 17 : if (meta == null) { 86 0 : throw new IllegalStateException("no CommandMetaData annotation found"); 87 : } 88 17 : bind(Commands.key(parent, name, meta.description())).to(clazz); 89 17 : } 90 : 91 : /** 92 : * Alias one command to another. 93 : * 94 : * @param from the new command name that when called will actually delegate to {@code to}'s 95 : * implementation. 96 : * @param to name of an already registered command that will perform the action when {@code from} 97 : * is invoked by a client. 98 : */ 99 : protected void alias(String from, String to) { 100 0 : bind(Commands.key(from)).to(Commands.key(to)); 101 0 : } 102 : }