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.common.collect.Maps; 18 : import com.google.gerrit.extensions.registration.RegistrationHandle; 19 : import com.google.inject.Binding; 20 : import com.google.inject.Inject; 21 : import com.google.inject.Injector; 22 : import com.google.inject.Provider; 23 : import com.google.inject.TypeLiteral; 24 : import java.lang.annotation.Annotation; 25 : import java.util.List; 26 : import java.util.concurrent.ConcurrentMap; 27 : import org.apache.sshd.server.command.Command; 28 : 29 : /** Creates DispatchCommand using commands registered by {@link CommandModule}. */ 30 : public class DispatchCommandProvider implements Provider<DispatchCommand> { 31 : @Inject private Injector injector; 32 : 33 : @Inject private DispatchCommand.Factory factory; 34 : 35 : private final CommandName parent; 36 : private volatile ConcurrentMap<String, CommandProvider> map; 37 : 38 17 : public DispatchCommandProvider(CommandName cn) { 39 17 : this.parent = cn; 40 17 : } 41 : 42 : @Override 43 : public DispatchCommand get() { 44 9 : return factory.create(getMap()); 45 : } 46 : 47 : public RegistrationHandle register(CommandName name, Provider<Command> cmd) { 48 0 : final ConcurrentMap<String, CommandProvider> m = getMap(); 49 0 : final CommandProvider commandProvider = new CommandProvider(cmd, null); 50 0 : if (m.putIfAbsent(name.value(), commandProvider) != null) { 51 0 : throw new IllegalArgumentException(name.value() + " exists"); 52 : } 53 0 : return () -> m.remove(name.value(), commandProvider); 54 : } 55 : 56 : public RegistrationHandle replace(CommandName name, Provider<Command> cmd) { 57 0 : final ConcurrentMap<String, CommandProvider> m = getMap(); 58 0 : final CommandProvider commandProvider = new CommandProvider(cmd, null); 59 0 : m.put(name.value(), commandProvider); 60 0 : return () -> m.remove(name.value(), commandProvider); 61 : } 62 : 63 : ConcurrentMap<String, CommandProvider> getMap() { 64 9 : if (map == null) { 65 9 : synchronized (this) { 66 9 : if (map == null) { 67 9 : map = createMap(); 68 : } 69 9 : } 70 : } 71 9 : return map; 72 : } 73 : 74 : @SuppressWarnings("unchecked") 75 : private ConcurrentMap<String, CommandProvider> createMap() { 76 9 : ConcurrentMap<String, CommandProvider> m = Maps.newConcurrentMap(); 77 9 : for (Binding<?> b : allCommands()) { 78 9 : final Annotation annotation = b.getKey().getAnnotation(); 79 9 : if (annotation instanceof CommandName) { 80 9 : final CommandName n = (CommandName) annotation; 81 9 : if (!Commands.CMD_ROOT.equals(n) && Commands.isChild(parent, n)) { 82 9 : String descr = null; 83 9 : if (annotation instanceof Commands.NestedCommandNameImpl) { 84 5 : Commands.NestedCommandNameImpl impl = ((Commands.NestedCommandNameImpl) annotation); 85 5 : descr = impl.descr(); 86 : } 87 9 : m.put(n.value(), new CommandProvider((Provider<Command>) b.getProvider(), descr)); 88 : } 89 : } 90 9 : } 91 9 : return m; 92 : } 93 : 94 17 : private static final TypeLiteral<Command> type = new TypeLiteral<>() {}; 95 : 96 : private List<Binding<Command>> allCommands() { 97 9 : return injector.findBindingsByType(type); 98 : } 99 : }