1

I am writing an SFTP service using Apache SSHD. The server will be installed on Windows using Apache commons daemon procrun. I'd like to handle service start/stop using static methods start() and stop() similar to how Tomcat does it. However I don't understand how this works. I think my confusion comes for understanding how procrun starts and stops a service. From what I understand, procrun launches a new java process with args "start"/"stop"? How can a new instance of a Java program handle control for one that is already running?

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.server.Command;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.config.keys.AuthorizedKeysAuthenticator;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class SftpServer {

    private static final Logger logger = LogManager.getLogger(SftpServer.class);

    public static void main(String[] args){
        if (args.length == 1) {
            if (args[0] == "start") {
                try{
                    start();
                } catch (IOException e) {
                    logger.error(e);
                }
            } else if (args[0] == "stop") {
                stop();
            } else {
                System.err.println("Unkown command. Use start/stop to conrol the service.");
            }
        } else {
            System.err.println("Use start/stop to control the service.");
        }
    }

    public static void start() throws IOException {
        SshServer sshd = SshServer.setUpDefaultServer();
        sshd.setPublickeyAuthenticator(new AuthorizedKeysAuthenticator(new File("conf/users.keys")));
        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("conf/hostkey.ser")));
        sshd.setPort(22);
        List<NamedFactory<Command>> subSystemFactories = new ArrayList<>();
        subSystemFactories.add(new SftpSubsystemFactory());
        sshd.setSubsystemFactories(subSystemFactories);
        sshd.start();
        while(true){
        }
    }

    public static void stop(){
        // What to do here?
    }
}
Ben Foltz
  • 48
  • 6

0 Answers0