// Copyright 2008 Google Inc.
//
// 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.server.ssh;

import org.apache.sshd.server.CommandFactory;

/** Creates a command implementation based on the client input. */
class GerritCommandFactory implements CommandFactory {
  public Command createCommand(final String commandLine) {
    final int sp1 = commandLine.indexOf(' ');
    String cmd, args;
    if (0 < sp1) {
      cmd = commandLine.substring(0, sp1);
      args = commandLine.substring(sp1 + 1);
    } else {
      cmd = commandLine;
      args = "";
    }

    if ("git".equals(cmd) || "gerrit".equals(cmd)) {
      cmd += "-";
      final int sp2 = args.indexOf(' ');
      if (0 < sp2) {
        cmd += args.substring(0, sp2);
        args = args.substring(sp2 + 1);
      } else {
        cmd += args;
        args = "";
      }
    }

    final AbstractCommand c = create(cmd);
    c.parseArguments(cmd, args);
    return c;
  }

  private AbstractCommand create(final String cmd) {
    if ("git-upload-pack".equals(cmd) || "gerrit-upload-pack".equals(cmd)) {
      return new Upload();
    }

    if ("git-receive-pack".equals(cmd) || "gerrit-receive-pack".equals(cmd)) {
      return new Receive();
    }

    if ("gerrit-flush-group-cache".equals(cmd)) {
      return new AdminFlushGroupCache();
    }

    return new AbstractCommand() {
      @Override
      protected void run(final String[] argv) throws Failure {
        throw new Failure(127, "gerrit: " + getName() + ": not found");
      }
    };
  }
}

@@ -664,16 +664,43 @@
 ctory;%0A%0A
+import java.util.HashMap;%0A%0A
 /** Crea
@@ -813,16 +813,803 @@
 ctory %7B%0A
+  private final HashMap%3CString, Factory%3E commands;%0A%0A  GerritCommandFactory() %7B%0A    commands = new HashMap%3CString, Factory%3E();%0A%0A    commands.put(%22gerrit-upload-pack%22, new Factory() %7B%0A      public AbstractCommand create() %7B%0A        return new Upload();%0A      %7D%0A    %7D);%0A    commands.put(%22gerrit-receive-pack%22, new Factory() %7B%0A      public AbstractCommand create() %7B%0A        return new Receive();%0A      %7D%0A    %7D);%0A    commands.put(%22gerrit-flush-group-cache%22, new Factory() %7B%0A      public AbstractCommand create() %7B%0A        return new AdminFlushGroupCache();%0A      %7D%0A    %7D);%0A%0A    alias(%22gerrit-upload-pack%22, %22git-upload-pack%22);%0A    alias(%22gerrit-receive-pack%22, %22git-receive-pack%22);%0A  %7D%0A%0A  private void alias(final String from, final String to) %7B%0A    commands.put(to, commands.get(from));%0A  %7D%0A%0A
   public
@@ -2334,315 +2334,86 @@
     
-if (%22git-upload-pack%22.equals(cmd) %7C%7C %22gerrit-upload-pack%22.equals(cmd)) %7B%0A      return new Upload();%0A    %7D%0A%0A    if (%22git-receive-pack%22.equals(cmd) %7C%7C %22gerrit-receive-pack%22.equals(cmd)) %7B%0A      return new Receive();%0A    %7D%0A%0A    if (%22gerrit-flush-group-cache%22.equals(cmd)) %7B%0A      return new AdminFlushGroupCach
+final Factory f = commands.get(cmd);%0A    if (f != null) %7B%0A      return f.creat
 e();
@@ -2415,25 +2415,24 @@
 te();%0A    %7D%0A
-%0A
     return n
@@ -2609,24 +2609,24 @@
 %22);%0A      %7D%0A
-
     %7D;%0A  %7D%0A%7D
@@ -2623,11 +2623,85 @@
   %7D;%0A  %7D
+%0A%0A  protected static interface Factory %7B%0A    AbstractCommand create();%0A  %7D
 %0A%7D%0A

Refactor GerritCommandFactory to lookup by hash