NetBeans headless platform application: read from stdin

"stever" <[email protected]>
Newsgroups gmane.comp.java.netbeans.modules.openide.devel
Message-ID <[email protected]>
If anyone else gets hung up on this, I decided to go with a socket option on an OptionProcessor service provider implementation.  This allows us to define a runtime argument with a socket parameter and telnet in.  


Code:

@ServiceProvider(service = OptionProcessor.class)
public class MyOptionProcessor extends OptionProcessor {
    private Option defaultOption = Option.always();
    private Option scriptOption = Option.defaultArguments();
    private Option socketOption = Option.optionalArgument('s', "socket");

    @Override
    protected Set<Option> getOptions() {
        HashSet set = new HashSet();
        set.add(defaultOption);
        set.add(scriptOption);
        set.add(socketOption);
        return set;
    }

    @Override
    protected void process(Env env, Map<Option, String[]> optionValues) throws CommandException {
        String[] fileArgs = new String[0];
        if (optionValues.containsKey(scriptOption)) {
            fileArgs = optionValues.get(scriptOption);
        }

        int portNumber = 0;
        if (optionValues.containsKey(socketOption)) {
            String strSocket = optionValues.get(socketOption)[0];
            try {
                portNumber = Integer.parseInt(strSocket);
            } catch (NumberFormatException nfe) {
                System.out.println("Bad socket parameter: '" + strSocket + "'. Socket needs to be a decimal number between 1 and 65535.");
            }
        }

        Main main = new Main();
        
        // Read/write using socket instead of stdin/stdout
        if (portNumber > 0) { 
            System.out.println("Listening on port " + Integer.toString(portNumber));
            try (
                ServerSocket server = new ServerSocket(portNumber);
                Socket socket = server.accept();
                ) {

                System.setIn(socket.getInputStream());
                System.setOut(new PrintStream(socket.getOutputStream(), true));
                main.start(fileArgs);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }

        } else { // read from stdin
            main.start(fileArgs);
        }
    }
}


[/code]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.