| | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import com.gitblit.git.GitblitReceivePackFactory; |
| | | import com.gitblit.git.GitblitUploadPackFactory; |
| | | import com.gitblit.git.RepositoryResolver; |
| | | import com.gitblit.manager.IGitblit; |
| | | import com.gitblit.models.UserModel; |
| | | import com.gitblit.transport.ssh.commands.AddKeyCommand; |
| | | import com.gitblit.transport.ssh.commands.CreateRepository; |
| | | import com.gitblit.transport.ssh.commands.DispatchCommand; |
| | | import com.gitblit.transport.ssh.commands.Receive; |
| | | import com.gitblit.transport.ssh.commands.RemoveKeyCommand; |
| | | import com.gitblit.transport.ssh.commands.ReviewCommand; |
| | | import com.gitblit.transport.ssh.commands.SetAccountCommand; |
| | | import com.gitblit.transport.ssh.commands.Upload; |
| | | import com.gitblit.transport.ssh.commands.VersionCommand; |
| | | import com.gitblit.utils.IdGenerator; |
| | | import com.gitblit.utils.WorkQueue; |
| | | import com.google.common.util.concurrent.Atomics; |
| | | |
| | |
| | | * |
| | | */ |
| | | public class SshCommandFactory implements CommandFactory { |
| | | private static final Logger logger = LoggerFactory |
| | | .getLogger(SshCommandFactory.class); |
| | | private static final Logger logger = LoggerFactory.getLogger(SshCommandFactory.class); |
| | | |
| | | private final IGitblit gitblit; |
| | | private final PublicKeyAuthenticator keyAuthenticator; |
| | | private final ScheduledExecutorService startExecutor; |
| | | |
| | | private DispatchCommand dispatcher; |
| | | public SshCommandFactory(IGitblit gitblit, PublicKeyAuthenticator keyAuthenticator, IdGenerator idGenerator) { |
| | | this.gitblit = gitblit; |
| | | this.keyAuthenticator = keyAuthenticator; |
| | | |
| | | public SshCommandFactory( |
| | | WorkQueue workQueue, |
| | | DispatchCommand d) { |
| | | this.dispatcher = d; |
| | | int threads = 2;//cfg.getInt("sshd","commandStartThreads", 2); |
| | | WorkQueue workQueue = new WorkQueue(idGenerator); |
| | | startExecutor = workQueue.createQueue(threads, "SshCommandStart"); |
| | | } |
| | | |
| | | /** |
| | | * Creates the root dispatcher command which builds up the available commands. |
| | | * |
| | | * @param the client |
| | | * @param the command line |
| | | * @return the root dispatcher command |
| | | */ |
| | | protected DispatchCommand createRootDispatcher(SshDaemonClient client, String cmdLine) { |
| | | final UserModel user = client.getUser(); |
| | | |
| | | DispatchCommand gitblitCmd = new DispatchCommand(); |
| | | gitblitCmd.registerCommand(user, VersionCommand.class); |
| | | gitblitCmd.registerCommand(user, AddKeyCommand.class); |
| | | gitblitCmd.registerCommand(user, RemoveKeyCommand.class); |
| | | gitblitCmd.registerCommand(user, ReviewCommand.class); |
| | | |
| | | gitblitCmd.registerCommand(user, CreateRepository.class); |
| | | gitblitCmd.registerCommand(user, SetAccountCommand.class); |
| | | |
| | | DispatchCommand gitCmd = new DispatchCommand(); |
| | | gitCmd.registerCommand(user, Upload.class); |
| | | gitCmd.registerCommand(user, Receive.class); |
| | | |
| | | DispatchCommand root = new DispatchCommand(); |
| | | root.registerDispatcher("gitblit", gitblitCmd); |
| | | root.registerDispatcher("git", gitCmd); |
| | | |
| | | root.setRepositoryResolver(new RepositoryResolver<SshDaemonClient>(gitblit)); |
| | | root.setUploadPackFactory(new GitblitUploadPackFactory<SshDaemonClient>(gitblit)); |
| | | root.setReceivePackFactory(new GitblitReceivePackFactory<SshDaemonClient>(gitblit)); |
| | | root.setAuthenticator(keyAuthenticator); |
| | | |
| | | root.setContext(new SshCommandContext(client, cmdLine)); |
| | | |
| | | return root; |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | private void onStart() throws IOException { |
| | | synchronized (this) { |
| | | SshCommandContext ctx = new SshCommandContext(session.getAttribute(SshDaemonClient.KEY), cmdLine); |
| | | SshDaemonClient client = session.getAttribute(SshDaemonClient.KEY); |
| | | try { |
| | | cmd = dispatcher; |
| | | cmd = createRootDispatcher(client, cmdLine); |
| | | cmd.setArguments(argv); |
| | | cmd.setContext(ctx); |
| | | cmd.setInputStream(in); |
| | | cmd.setOutputStream(out); |
| | | cmd.setErrorStream(err); |
| | |
| | | }); |
| | | cmd.start(env); |
| | | } finally { |
| | | ctx = null; |
| | | client = null; |
| | | } |
| | | } |
| | | } |