Search in sources :

Example 1 with IOConsoleOutputStream

use of org.eclipse.ui.console.IOConsoleOutputStream in project ow by vtst.

the class SoyCompilerLaunchConfigurationDelegate method addProcessListeners.

@Override
protected void addProcessListeners(final ILaunchConfiguration config, final Fixture fixture, IProcessListenerAcceptor acceptor) {
    acceptor.acceptTerminationListener(new IProcessTerminationListener() {

        public void terminated(IProcess process, int exitValue) {
            IConsole console = DebugUITools.getConsole(process);
            if (console instanceof IOConsole) {
                IOConsole ioConsole = (IOConsole) console;
                IOConsoleOutputStream stream = ioConsole.newOutputStream();
                if (exitValue == 0) {
                    try {
                        stream.write(messages.getString("soy_compiler_success"));
                        stream.write("\n");
                        stream.flush();
                        for (IFile file : configHelper.getOutputFiles(config)) {
                            try {
                                file.refreshLocal(0, null);
                            } catch (CoreException e) {
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        stream.write(messages.getString("soy_compiler_error"));
                        stream.write("\n");
                        stream.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        //try { fixture.outputFile.refreshLocal(0, null); } catch (CoreException e) {}
        }
    });
    acceptor.acceptPatternMatchListener(new EasyPatternMatchListener() {

        private Pattern pattern = Pattern.compile("Exception [^:]*com.google.template.soy.base.SoySyntaxException: " + "(In file ([^:]*)(:([0-9]+))?(, template [^:]*)?: )?(.*)");

        @Override
        public void connect(TextConsole console) {
            super.connect(console);
            try {
                for (IMarker marker : fixture.inputFile.findMarkers(MARKER_TYPE, true, IResource.DEPTH_ZERO)) {
                    if (MARKER_SOURCE_ID.equals(marker.getAttribute(IMarker.SOURCE_ID))) {
                        marker.delete();
                    }
                }
            } catch (CoreException e) {
            }
        }

        private void processLine(int offset, int length) throws BadLocationException {
            String line = document.get(offset, length);
            Matcher matcher = pattern.matcher(line);
            if (matcher.matches()) {
                String lineNumberAsString = matcher.group(4);
                int lineNumber = (lineNumberAsString == null ? 1 : Integer.parseInt(lineNumberAsString));
                String errorMessage = matcher.group(6);
                IMarker marker;
                try {
                    marker = fixture.inputFile.createMarker(MARKER_TYPE);
                    marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
                    marker.setAttribute(IMarker.MESSAGE, errorMessage);
                    marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_NORMAL);
                    marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
                    marker.setAttribute(IMarker.SOURCE_ID, MARKER_SOURCE_ID);
                } catch (CoreException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void matchFound(PatternMatchEvent matchEvent) {
            try {
                processLine(matchEvent.getOffset(), matchEvent.getLength());
            } catch (BadLocationException e) {
            // This should never arise if the code is correct
            }
        }

        @Override
        public String getPattern() {
            return ".+";
        }
    });
}
Also used : Pattern(java.util.regex.Pattern) IFile(org.eclipse.core.resources.IFile) IOConsoleOutputStream(org.eclipse.ui.console.IOConsoleOutputStream) Matcher(java.util.regex.Matcher) IProcessTerminationListener(net.vtst.eclipse.easyxtext.ui.launching.EasyLaunchConfigurationDelegateUtils.IProcessTerminationListener) IConsole(org.eclipse.ui.console.IConsole) IOException(java.io.IOException) PatternMatchEvent(org.eclipse.ui.console.PatternMatchEvent) CoreException(org.eclipse.core.runtime.CoreException) EasyPatternMatchListener(net.vtst.eclipse.easyxtext.ui.launching.EasyPatternMatchListener) TextConsole(org.eclipse.ui.console.TextConsole) IMarker(org.eclipse.core.resources.IMarker) IProcess(org.eclipse.debug.core.model.IProcess) IOConsole(org.eclipse.ui.console.IOConsole) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with IOConsoleOutputStream

use of org.eclipse.ui.console.IOConsoleOutputStream in project ow by vtst.

the class LessCompilerLaunchConfigurationDelegate method addProcessListeners.

protected void addProcessListeners(ILaunchConfiguration config, final Fixture fixture, IProcessListenerAcceptor acceptor) {
    acceptor.acceptTerminationListener(new IProcessTerminationListener() {

        public void terminated(IProcess process, int exitValue) {
            IConsole console = DebugUITools.getConsole(process);
            if (console instanceof IOConsole) {
                IOConsole ioConsole = (IOConsole) console;
                IOConsoleOutputStream stream = ioConsole.newOutputStream();
                if (exitValue == 0) {
                    try {
                        stream.write(messages.getString("less_compiler_success"));
                        stream.write("\n");
                        stream.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                /*
            FileLink link = new FileLink(destinationFile, null, -1, -1, -1);
            console.defaultStream.hyperlink(link, messages.getString("less_link_to_output"));
            console.defaultStream.println();
            */
                } else {
                    try {
                        stream.write(messages.getString("less_compiler_error"));
                        stream.write("\n");
                        stream.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            try {
                fixture.outputFile.refreshLocal(0, null);
            } catch (CoreException e) {
            }
        }
    });
    acceptor.acceptPatternMatchListener(new EasyPatternMatchListener() {

        private Pattern pattern = Pattern.compile("([0-9]+)( .*)");

        private int numberOfLines = 0;

        private int errorLineNumber = 0;

        private String errorMessage = null;

        @Override
        public void connect(TextConsole console) {
            super.connect(console);
            try {
                for (IMarker marker : fixture.inputFile.findMarkers(MARKER_TYPE, true, IResource.DEPTH_ZERO)) {
                    if (MARKER_SOURCE_ID.equals(marker.getAttribute(IMarker.SOURCE_ID))) {
                        marker.delete();
                    }
                }
            } catch (CoreException e) {
            }
        }

        @Override
        public void disconnect() {
            if (errorLineNumber != 0 && errorMessage != null) {
                try {
                    IMarker marker = fixture.inputFile.createMarker(MARKER_TYPE);
                    marker.setAttribute(IMarker.LINE_NUMBER, this.errorLineNumber);
                    marker.setAttribute(IMarker.MESSAGE, this.errorMessage);
                    marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_NORMAL);
                    marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
                    marker.setAttribute(IMarker.SOURCE_ID, MARKER_SOURCE_ID);
                } catch (CoreException e) {
                    e.printStackTrace();
                }
            }
        }

        private void processLine(int offset, int length) throws BadLocationException {
            String line = document.get(offset, length);
            Matcher matcher = pattern.matcher(line);
            if (matcher.matches()) {
                this.numberOfLines++;
                int lineNumber = Integer.parseInt(matcher.group(1));
                if (this.numberOfLines <= 2)
                    errorLineNumber = lineNumber;
                FileLink link = new FileLink(fixture.inputFile, null, -1, -1, lineNumber);
                console.addHyperlink(link, offset, matcher.group(1).length());
            } else {
                if (this.numberOfLines == 0)
                    errorMessage = line;
            }
        }

        @Override
        public void matchFound(PatternMatchEvent matchEvent) {
            try {
                processLine(matchEvent.getOffset(), matchEvent.getLength());
            } catch (BadLocationException e) {
            // This should never arise if the code is correct
            }
        }

        @Override
        public String getPattern() {
            return ".+";
        }
    });
}
Also used : Pattern(java.util.regex.Pattern) IOConsoleOutputStream(org.eclipse.ui.console.IOConsoleOutputStream) Matcher(java.util.regex.Matcher) IProcessTerminationListener(net.vtst.eclipse.easyxtext.ui.launching.EasyLaunchConfigurationDelegateUtils.IProcessTerminationListener) FileLink(org.eclipse.debug.ui.console.FileLink) IConsole(org.eclipse.ui.console.IConsole) IOException(java.io.IOException) PatternMatchEvent(org.eclipse.ui.console.PatternMatchEvent) CoreException(org.eclipse.core.runtime.CoreException) EasyPatternMatchListener(net.vtst.eclipse.easyxtext.ui.launching.EasyPatternMatchListener) TextConsole(org.eclipse.ui.console.TextConsole) IMarker(org.eclipse.core.resources.IMarker) IProcess(org.eclipse.debug.core.model.IProcess) IOConsole(org.eclipse.ui.console.IOConsole) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with IOConsoleOutputStream

use of org.eclipse.ui.console.IOConsoleOutputStream in project tesb-studio-se by Talend.

the class RuntimeClient method connect.

public void connect(String[] args) throws Exception {
    ClientConfig config = new ClientConfig(args);
    SimpleLogger.setLevel(config.getLevel());
    if (config.getFile() != null) {
        StringBuilder sb = new StringBuilder();
        sb.setLength(0);
        try (Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(config.getFile())))) {
            for (int c = reader.read(); c >= 0; c = reader.read()) {
                sb.append((char) c);
            }
        }
        config.setCommand(sb.toString());
    } else if (config.isBatch()) {
        StringBuilder sb = new StringBuilder();
        sb.setLength(0);
        Reader reader = new BufferedReader(new InputStreamReader(System.in));
        for (int c = reader.read(); c >= 0; c = reader.read()) {
            sb.append((char) c);
        }
        config.setCommand(sb.toString());
    }
    SshClient client = ClientBuilder.builder().build();
    // setupAgent(config.getUser(), config.getKeyFile(), client);
    // client.getProperties().put(FactoryManager.IDLE_TIMEOUT, String.valueOf(config.getIdleTimeout()));
    final Console console = System.console();
    if (console != null) {
        client.setUserInteraction(new UserInteraction() {

            @Override
            public void welcome(ClientSession s, String banner, String lang) {
                System.err.println(banner);
            }

            @Override
            public String[] interactive(ClientSession s, String name, String instruction, String lang, String[] prompt, boolean[] echo) {
                String[] answers = new String[prompt.length];
                try {
                    for (int i = 0; i < prompt.length; i++) {
                        if (echo[i]) {
                            answers[i] = console.readLine(prompt[i] + " ");
                        } else {
                            answers[i] = new String(console.readPassword(prompt[i] + " "));
                        }
                        if (answers[i] == null) {
                            return null;
                        }
                    }
                    return answers;
                } catch (IOError e) {
                    return null;
                }
            }

            @Override
            public boolean isInteractionAllowed(ClientSession session) {
                return true;
            }

            @Override
            public void serverVersionInfo(ClientSession session, List<String> lines) {
            }

            @Override
            public String getUpdatedPassword(ClientSession session, String prompt, String lang) {
                return null;
            }
        });
    }
    client.start();
    ClientSession session = connectWithRetries(client, config);
    if (config.getPassword() != null) {
        session.addPasswordIdentity(config.getPassword());
    }
    session.auth().verify();
    int exitStatus = 0;
    Terminal terminal = TerminalBuilder.terminal();
    Attributes attributes = terminal.enterRawMode();
    IOConsoleOutputStream outputStream = RuntimeConsoleUtil.getOutputStream();
    try {
        ClientChannel channel;
        if (config.getCommand().length() > 0) {
            channel = session.createChannel("exec", config.getCommand() + "\n");
            channel.setIn(new ByteArrayInputStream(new byte[0]));
        } else {
            ChannelShell shell = session.createShellChannel();
            channel = shell;
            channel.setIn(new NoCloseInputStream(inputStream));
            Map<PtyMode, Integer> modes = new HashMap<>();
            // Control chars
            modes.put(PtyMode.VINTR, attributes.getControlChar(ControlChar.VINTR));
            modes.put(PtyMode.VQUIT, attributes.getControlChar(ControlChar.VQUIT));
            modes.put(PtyMode.VERASE, attributes.getControlChar(ControlChar.VERASE));
            modes.put(PtyMode.VKILL, attributes.getControlChar(ControlChar.VKILL));
            modes.put(PtyMode.VEOF, attributes.getControlChar(ControlChar.VEOF));
            modes.put(PtyMode.VEOL, attributes.getControlChar(ControlChar.VEOL));
            modes.put(PtyMode.VEOL2, attributes.getControlChar(ControlChar.VEOL2));
            modes.put(PtyMode.VSTART, attributes.getControlChar(ControlChar.VSTART));
            modes.put(PtyMode.VSTOP, attributes.getControlChar(ControlChar.VSTOP));
            modes.put(PtyMode.VSUSP, attributes.getControlChar(ControlChar.VSUSP));
            modes.put(PtyMode.VDSUSP, attributes.getControlChar(ControlChar.VDSUSP));
            modes.put(PtyMode.VREPRINT, attributes.getControlChar(ControlChar.VREPRINT));
            modes.put(PtyMode.VWERASE, attributes.getControlChar(ControlChar.VWERASE));
            modes.put(PtyMode.VLNEXT, attributes.getControlChar(ControlChar.VLNEXT));
            modes.put(PtyMode.VSTATUS, attributes.getControlChar(ControlChar.VSTATUS));
            modes.put(PtyMode.VDISCARD, attributes.getControlChar(ControlChar.VDISCARD));
            // Input flags
            modes.put(PtyMode.IGNPAR, getFlag(attributes, InputFlag.IGNPAR));
            modes.put(PtyMode.PARMRK, getFlag(attributes, InputFlag.PARMRK));
            modes.put(PtyMode.INPCK, getFlag(attributes, InputFlag.INPCK));
            modes.put(PtyMode.ISTRIP, getFlag(attributes, InputFlag.ISTRIP));
            modes.put(PtyMode.INLCR, getFlag(attributes, InputFlag.INLCR));
            modes.put(PtyMode.IGNCR, getFlag(attributes, InputFlag.IGNCR));
            modes.put(PtyMode.ICRNL, getFlag(attributes, InputFlag.ICRNL));
            modes.put(PtyMode.IXON, getFlag(attributes, InputFlag.IXON));
            modes.put(PtyMode.IXANY, getFlag(attributes, InputFlag.IXANY));
            modes.put(PtyMode.IXOFF, getFlag(attributes, InputFlag.IXOFF));
            // Local flags
            modes.put(PtyMode.ISIG, getFlag(attributes, LocalFlag.ISIG));
            modes.put(PtyMode.ICANON, getFlag(attributes, LocalFlag.ICANON));
            modes.put(PtyMode.ECHO, getFlag(attributes, LocalFlag.ECHO));
            modes.put(PtyMode.ECHOE, getFlag(attributes, LocalFlag.ECHOE));
            modes.put(PtyMode.ECHOK, getFlag(attributes, LocalFlag.ECHOK));
            modes.put(PtyMode.ECHONL, getFlag(attributes, LocalFlag.ECHONL));
            modes.put(PtyMode.NOFLSH, getFlag(attributes, LocalFlag.NOFLSH));
            modes.put(PtyMode.TOSTOP, getFlag(attributes, LocalFlag.TOSTOP));
            modes.put(PtyMode.IEXTEN, getFlag(attributes, LocalFlag.IEXTEN));
            // Output flags
            modes.put(PtyMode.OPOST, getFlag(attributes, OutputFlag.OPOST));
            modes.put(PtyMode.ONLCR, getFlag(attributes, OutputFlag.ONLCR));
            modes.put(PtyMode.OCRNL, getFlag(attributes, OutputFlag.OCRNL));
            modes.put(PtyMode.ONOCR, getFlag(attributes, OutputFlag.ONOCR));
            modes.put(PtyMode.ONLRET, getFlag(attributes, OutputFlag.ONLRET));
            shell.setPtyModes(modes);
            shell.setPtyColumns(terminal.getWidth());
            shell.setPtyLines(terminal.getHeight());
            shell.setAgentForwarding(true);
            String ctype = System.getenv("LC_CTYPE");
            if (ctype == null) {
                ctype = Locale.getDefault().toString() + "." + System.getProperty("input.encoding", Charset.defaultCharset().name());
            }
            shell.setEnv("LC_CTYPE", ctype);
        }
        channel.setOut(outputStream);
        channel.setErr(outputStream);
        channel.open().verify();
        if (channel instanceof PtyCapableChannelSession) {
            registerSignalHandler(terminal, (PtyCapableChannelSession) channel);
        }
        connected = true;
        channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), 0);
        if (channel.getExitStatus() != null) {
            exitStatus = channel.getExitStatus();
        }
        channel.close();
    } finally {
        terminal.setAttributes(attributes);
        client.stop();
        client.close();
        connected = false;
        if (!outputStream.isClosed()) {
            outputStream.close();
        }
    }
}
Also used : SshClient(org.apache.sshd.client.SshClient) IOConsoleOutputStream(org.eclipse.ui.console.IOConsoleOutputStream) HashMap(java.util.HashMap) Attributes(org.jline.terminal.Attributes) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ChannelShell(org.apache.sshd.client.channel.ChannelShell) ClientSession(org.apache.sshd.client.session.ClientSession) Console(java.io.Console) ClientConfig(org.apache.karaf.client.ClientConfig) PtyCapableChannelSession(org.apache.sshd.client.channel.PtyCapableChannelSession) InputStreamReader(java.io.InputStreamReader) PtyMode(org.apache.sshd.common.channel.PtyMode) Terminal(org.jline.terminal.Terminal) FileInputStream(java.io.FileInputStream) ClientChannel(org.apache.sshd.client.channel.ClientChannel) NoCloseInputStream(com.sun.xml.internal.ws.util.NoCloseInputStream) IOError(java.io.IOError) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) UserInteraction(org.apache.sshd.client.auth.keyboard.UserInteraction)

Example 4 with IOConsoleOutputStream

use of org.eclipse.ui.console.IOConsoleOutputStream in project tesb-studio-se by Talend.

the class RuntimeConsoleUtil method getOutputStream.

public static IOConsoleOutputStream getOutputStream() {
    IOConsoleOutputStream outputStream = findConsole().newOutputStream();
    outputStream.setEncoding(System.getProperty("sun.jnu.encoding", "UTF-8"));
    return outputStream;
}
Also used : IOConsoleOutputStream(org.eclipse.ui.console.IOConsoleOutputStream)

Aggregations

IOConsoleOutputStream (org.eclipse.ui.console.IOConsoleOutputStream)4 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 IProcessTerminationListener (net.vtst.eclipse.easyxtext.ui.launching.EasyLaunchConfigurationDelegateUtils.IProcessTerminationListener)2 EasyPatternMatchListener (net.vtst.eclipse.easyxtext.ui.launching.EasyPatternMatchListener)2 IMarker (org.eclipse.core.resources.IMarker)2 CoreException (org.eclipse.core.runtime.CoreException)2 IProcess (org.eclipse.debug.core.model.IProcess)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 IConsole (org.eclipse.ui.console.IConsole)2 IOConsole (org.eclipse.ui.console.IOConsole)2 PatternMatchEvent (org.eclipse.ui.console.PatternMatchEvent)2 TextConsole (org.eclipse.ui.console.TextConsole)2 NoCloseInputStream (com.sun.xml.internal.ws.util.NoCloseInputStream)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Console (java.io.Console)1 FileInputStream (java.io.FileInputStream)1 IOError (java.io.IOError)1