Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 437382149b347be8e9a7c4471862cc444ca239b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*******************************************************************************
 *  Copyright (c) 2012, 2016 SSI Schaefer and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *      SSI Schaefer
 *******************************************************************************/
package org.eclipse.debug.internal.core.groups.observer;

import java.util.concurrent.CountDownLatch;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.IStreamListener;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.core.model.IStreamMonitor;
import org.eclipse.debug.core.model.IStreamsProxy;

/**
 * The {@code StreamObserver} observes a given {@linkplain IStreamsProxy output stream} instance and
 * notifies a {@linkplain CountDownLatch synchronization object} when a given string appears in the
 * output.
 */
public class StreamObserver implements Runnable {
    private final String stringPattern;
    private final IProcess process;
    private final CountDownLatch countDownLatch;
    private final IProgressMonitor pMonitor;

    public StreamObserver(IProgressMonitor monitor, IProcess process, String pattern,
            CountDownLatch countDownLatch) {
        this.process = process;
        this.pMonitor = monitor;
        this.stringPattern = pattern;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        // append wild card if not provided
        StringBuilder patternBuilder = new StringBuilder();
        if (!stringPattern.startsWith(".*")) { //$NON-NLS-1$
            patternBuilder.append(".*"); //$NON-NLS-1$
        }
        patternBuilder.append(stringPattern);
        if (!stringPattern.endsWith(".*")) { //$NON-NLS-1$
            patternBuilder.append(".*"); //$NON-NLS-1$
        }
        // create pattern and start listening to the output
        final Pattern pattern = Pattern.compile(patternBuilder.toString(), Pattern.MULTILINE);
        final IStreamMonitor outputStreamMonitor = process.getStreamsProxy()
                .getOutputStreamMonitor();
        outputStreamMonitor.addListener(new IStreamListener() {
            @Override
            public void streamAppended(String text, IStreamMonitor monitor) {
                if (countDownLatch.getCount() == 0) {
                    outputStreamMonitor.removeListener(this);
                    return;
                }

                Matcher matcher = pattern.matcher(text);
                if (!matcher.find() && !pMonitor.isCanceled()) {
                    return;
                }
                countDownLatch.countDown();
            }
        });
    }
}

Back to the top