public void setupSftpServer(){ SshServer sshd = SshServer.setUpDefaultServer(); sshd.setPort(22); sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser")); List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>(); userAuthFactories.add(new UserAuthNone.Factory()); sshd.setUserAuthFactories(userAuthFactories); sshd.setCommandFactory(new ScpCommandFactory()); List<NamedFactory<Command>> namedFactoryList = new ArrayList<NamedFactory<Command>>(); namedFactoryList.add(new SftpSubsystem.Factory()); sshd.setSubsystemFactories(namedFactoryList); try { sshd.start(); } catch (Exception e) { e.printStackTrace(); } }
@Override public Command createCommand(String command) { if (isSupportedCommand(command)) { return executeSupportedCommand(command); } CommandFactory factory = getDelegateCommandFactory(); if (factory != null) { return factory.createCommand(command); } return createUnsupportedCommand(command); }
@Override public void start(Environment env) throws IOException { if (error != null) { throw error; } super.start(env); }
@Override public void start(Environment env) throws IOException { environment = env; try { CloseableExecutorService executors = getExecutorService(); cmdFuture = executors.submit(() -> { cmdRunner = Thread.currentThread(); this.run(); }); } catch (RuntimeException e) { // e.g., RejectedExecutionException log.error("Failed (" + e.getClass().getSimpleName() + ") to start command=" + command + ": " + e.getMessage(), e); throw new IOException(e); } }
@Override public Session getSession() { return getServerSession(); }
protected void onExit(int exitValue) { onExit(exitValue, ""); }
@Override public String toString() { return getClass().getSimpleName() + "[" + getCommand() + "]"; } }
protected void onExit(int exitValue, String exitMessage) { if (cbCalled) { if (log.isTraceEnabled()) { log.trace("onExit({}) ignore exitValue={}, message={} - already called", this, exitValue, exitMessage); } return; } ExitCallback cb = getExitCallback(); try { if (log.isDebugEnabled()) { log.debug("onExit({}) exiting - value={}, message={}", this, exitValue, exitMessage); } cb.onExit(exitValue, exitMessage); } finally { cbCalled = true; } }
@Override public void destroy() { try { super.destroy(); } finally { if (fileSystem != null) { try { fileSystem.close(); } catch (UnsupportedOperationException | IOException e) { if (log.isDebugEnabled()) { log.debug("destroy({}) - failed ({}) to close file system={}: {}", this, e.getClass().getSimpleName(), fileSystem, e.getMessage()); } } finally { fileSystem = null; } } } } }
@Override public void destroy() { // if thread has not completed, cancel it boolean debugEnabled = log.isDebugEnabled(); if ((cmdFuture != null) && (!cmdFuture.isDone()) && (cmdRunner != Thread.currentThread())) { boolean result = cmdFuture.cancel(true); // TODO consider waiting some reasonable (?) amount of time for cancellation if (debugEnabled) { log.debug("destroy() - cancel pending future=" + result); } } cmdFuture = null; CloseableExecutorService executors = getExecutorService(); if ((executors != null) && (!executors.isShutdown())) { Collection<Runnable> runners = executors.shutdownNow(); if (debugEnabled) { log.debug("destroy() - shutdown executor service - runners count=" + runners.size()); } } this.executorService = null; }
@Override public Command createCommand(String command) { if (isSupportedCommand(command)) { return executeSupportedCommand(command); } CommandFactory factory = getDelegateCommandFactory(); if (factory != null) { return factory.createCommand(command); } return createUnsupportedCommand(command); }
private void initSftp() { sshServer.setCommandFactory(new ScpCommandFactory()); sshServer.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystemFactory())); }
@Override public void start(Environment env) throws IOException { environment = env; try { CloseableExecutorService executors = getExecutorService(); cmdFuture = executors.submit(() -> { cmdRunner = Thread.currentThread(); this.run(); }); } catch (RuntimeException e) { // e.g., RejectedExecutionException log.error("Failed (" + e.getClass().getSimpleName() + ") to start command=" + command + ": " + e.getMessage(), e); throw new IOException(e); } }
@Override public Session getSession() { return getServerSession(); }
protected void onExit(int exitValue) { onExit(exitValue, ""); }
@Override public String toString() { return getClass().getSimpleName() + "[" + getCommand() + "]"; } }
protected void onExit(int exitValue, String exitMessage) { if (cbCalled) { if (log.isTraceEnabled()) { log.trace("onExit({}) ignore exitValue={}, message={} - already called", this, exitValue, exitMessage); } return; } ExitCallback cb = getExitCallback(); try { if (log.isDebugEnabled()) { log.debug("onExit({}) exiting - value={}, message={}", this, exitValue, exitMessage); } cb.onExit(exitValue, exitMessage); } finally { cbCalled = true; } }
@Override public void destroy() { try { super.destroy(); } finally { if (fileSystem != null) { try { fileSystem.close(); } catch (UnsupportedOperationException | IOException e) { if (log.isDebugEnabled()) { log.debug("destroy({}) - failed ({}) to close file system={}: {}", this, e.getClass().getSimpleName(), fileSystem, e.getMessage()); } } finally { fileSystem = null; } } } } }
@Override public void destroy() { // if thread has not completed, cancel it boolean debugEnabled = log.isDebugEnabled(); if ((cmdFuture != null) && (!cmdFuture.isDone()) && (cmdRunner != Thread.currentThread())) { boolean result = cmdFuture.cancel(true); // TODO consider waiting some reasonable (?) amount of time for cancellation if (debugEnabled) { log.debug("destroy() - cancel pending future=" + result); } } cmdFuture = null; CloseableExecutorService executors = getExecutorService(); if ((executors != null) && (!executors.isShutdown())) { Collection<Runnable> runners = executors.shutdownNow(); if (debugEnabled) { log.debug("destroy() - shutdown executor service - runners count=" + runners.size()); } } this.executorService = null; }
@Override public void initialize(int port) { Security.addProvider(new BouncyCastleProvider()); sshd = SshServer.setUpDefaultServer(); sshd.setPort(port); StreamKeyPairProvider fileKeyPairProvider = new StreamKeyPairProvider(); sshd.setKeyPairProvider(fileKeyPairProvider); SftpSubsystem.Factory factory = new SftpSubsystem.Factory(); sshd.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(factory)); sshd.setCommandFactory(new ScpCommandFactory()); sshd.setShellFactory(new ProcessShellFactory()); sshd.setPasswordAuthenticator(new MockPasswordAuthenticator()); }