summaryrefslogtreecommitdiffstats
path: root/gerrit-sshd/src/main/java/com/google/gerrit/sshd/DispatchCommandProvider.java
blob: ce1da95fed61ecd7c5447ed5445a6e47a09db885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.sshd;

import com.google.common.collect.Maps;
import com.google.gerrit.extensions.registration.RegistrationHandle;
import com.google.inject.Binding;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;
import com.google.inject.TypeLiteral;

import org.apache.sshd.server.Command;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.concurrent.ConcurrentMap;

/**
 * Creates DispatchCommand using commands registered by {@link CommandModule}.
 */
public class DispatchCommandProvider implements Provider<DispatchCommand> {
  @Inject
  private Injector injector;

  @Inject
  private DispatchCommand.Factory factory;

  private final CommandName parent;
  private volatile ConcurrentMap<String, CommandProvider> map;

  public DispatchCommandProvider(final CommandName cn) {
    this.parent = cn;
  }

  @Override
  public DispatchCommand get() {
    return factory.create(getMap());
  }

  public RegistrationHandle register(final CommandName name,
      final Provider<Command> cmd) {
    final ConcurrentMap<String, CommandProvider> m = getMap();
    final CommandProvider commandProvider = new CommandProvider(cmd, null);
    if (m.putIfAbsent(name.value(), commandProvider) != null) {
      throw new IllegalArgumentException(name.value() + " exists");
    }
    return new RegistrationHandle() {
      @Override
      public void remove() {
        m.remove(name.value(), commandProvider);
      }
    };
  }

  public RegistrationHandle replace(final CommandName name,
      final Provider<Command> cmd) {
    final ConcurrentMap<String, CommandProvider> m = getMap();
    final CommandProvider commandProvider = new CommandProvider(cmd, null);
    m.put(name.value(), commandProvider);
    return new RegistrationHandle() {
      @Override
      public void remove() {
        m.remove(name.value(), commandProvider);
      }
    };
  }

  ConcurrentMap<String, CommandProvider> getMap() {
    if (map == null) {
      synchronized (this) {
        if (map == null) {
          map = createMap();
        }
      }
    }
    return map;
  }

  @SuppressWarnings("unchecked")
  private ConcurrentMap<String, CommandProvider> createMap() {
    ConcurrentMap<String, CommandProvider> m = Maps.newConcurrentMap();
    for (final Binding<?> b : allCommands()) {
      final Annotation annotation = b.getKey().getAnnotation();
      if (annotation instanceof CommandName) {
        final CommandName n = (CommandName) annotation;
        if (!Commands.CMD_ROOT.equals(n) && Commands.isChild(parent, n)) {
          String descr = null;
          if (annotation instanceof Commands.NestedCommandNameImpl) {
            Commands.NestedCommandNameImpl impl =
                ((Commands.NestedCommandNameImpl) annotation);
            descr = impl.descr();
          }
          m.put(n.value(),
              new CommandProvider((Provider<Command>) b.getProvider(), descr));
        }
      }
    }
    return m;
  }

  private static final TypeLiteral<Command> type =
      new TypeLiteral<Command>() {};

  private List<Binding<Command>> allCommands() {
    return injector.findBindingsByType(type);
  }
}