Search in sources :

Example 11 with LineReader

use of com.google.common.io.LineReader in project stashbot by palantir.

the class CommandOutputHandlerFactory method getBranchContainsOutputHandler.

/**
 * Returns an output handler which provides an ImmutableList of branches in the form "refs/heads/foo" when the
 * command prints out a list of the form "XXfoo\n" where X doesn't matter.
 *
 * @return ImmutableList<String> branchNames
 */
public CommandOutputHandler<Object> getBranchContainsOutputHandler() {
    return new CommandOutputHandler<Object>() {

        private ArrayList<String> branches;

        private LineReader lr;

        private boolean processed = false;

        @Override
        public void complete() throws ProcessException {
        }

        @Override
        public void setWatchdog(Watchdog watchdog) {
        }

        @Override
        public Object getOutput() {
            if (processed == false) {
                throw new IllegalStateException("getOutput() called before process()");
            }
            return ImmutableList.copyOf(branches);
        }

        @Override
        public void process(InputStream output) throws ProcessException {
            processed = true;
            if (branches == null) {
                branches = new ArrayList<String>();
                lr = new LineReader(new InputStreamReader(output));
            }
            String branch = "";
            while (branch != null) {
                try {
                    branch = lr.readLine();
                } catch (IOException e) {
                    // dear god, why is this happening?
                    throw new RuntimeException(e);
                }
                // format is "  somebranch\n* branchIamOn\n  someOtherBranch"
                if (branch != null) {
                    branches.add("refs/heads/" + branch.substring(2));
                }
            }
        }
    };
}
Also used : InputStreamReader(java.io.InputStreamReader) Watchdog(com.atlassian.utils.process.Watchdog) InputStream(java.io.InputStream) LineReader(com.google.common.io.LineReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CommandOutputHandler(com.atlassian.stash.scm.CommandOutputHandler)

Example 12 with LineReader

use of com.google.common.io.LineReader in project j2objc by google.

the class SourceBuilder method reindent.

/**
 * Fix line indention, based on brace count.
 */
public String reindent(String code) {
    try {
        // Remove indention from each line.
        StringBuilder sb = new StringBuilder();
        LineReader lr = new LineReader(new StringReader(code));
        String line = lr.readLine();
        while (line != null) {
            sb.append(line.trim());
            line = lr.readLine();
            if (line != null) {
                sb.append('\n');
            }
        }
        String strippedCode = sb.toString();
        // Now indent it again.
        int indent = indention * DEFAULT_INDENTION;
        // reset buffer
        sb.setLength(0);
        lr = new LineReader(new StringReader(strippedCode));
        line = lr.readLine();
        while (line != null) {
            if (line.startsWith("}")) {
                indent -= DEFAULT_INDENTION;
            }
            if (!line.startsWith("#line")) {
                sb.append(pad(indent));
            }
            sb.append(line);
            if (line.endsWith("{")) {
                indent += DEFAULT_INDENTION;
            }
            line = lr.readLine();
            if (line != null) {
                sb.append('\n');
            }
        }
        return sb.toString();
    } catch (IOException e) {
        // Should never happen with string readers.
        throw new AssertionError(e);
    }
}
Also used : LineReader(com.google.common.io.LineReader) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 13 with LineReader

use of com.google.common.io.LineReader in project stash-codesearch-plugin by palantir.

the class GitVersionValidator method validateVersion.

private String validateVersion(GitScm gitScm) {
    GitCommandBuilderFactory gcbf = gitScm.getCommandBuilderFactory();
    CommandOutputHandler<String> coh = new CommandOutputHandler<String>() {

        private final StringBuilder sb = new StringBuilder();

        private boolean finished = false;

        @Override
        public void process(InputStream output) throws ProcessException {
            final LineReader lr = new LineReader(new InputStreamReader(output));
            String line = null;
            try {
                while ((line = lr.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                throw new RuntimeException("Error while determining git version:", e);
            }
        }

        @Override
        public void complete() throws ProcessException {
            finished = true;
        }

        @Override
        public void setWatchdog(Watchdog watchdog) {
        }

        @Override
        public String getOutput() {
            if (!finished) {
                throw new IllegalStateException("Called getOutput() before complete()");
            }
            return sb.toString();
        }
    };
    Command<String> versionCommand = gcbf.builder().version(coh);
    versionCommand.call();
    String versionString = coh.getOutput();
    // version string looks like this: git version 1.7.9.5
    String versionNumber = versionString.split(" ")[2];
    String[] versionParts = versionNumber.split("\\.");
    int min = (versionParts.length <= REQUIRED_GIT_VERSION.length) ? versionParts.length : REQUIRED_GIT_VERSION.length;
    for (int i = 0; i < min; ++i) {
        try {
            int test = Integer.parseInt(versionParts[i]);
            if (test > REQUIRED_GIT_VERSION[i]) {
                return versionNumber;
            } else if (test < REQUIRED_GIT_VERSION[i]) {
                throw new IllegalStateException("Invalid Git Version '" + versionNumber + "', must be equal to or greater than '" + REQUIRED_GIT_VERSION_STRING + "'");
            }
        // else: keep checking next number
        } catch (NumberFormatException e) {
            throw new RuntimeException("Error while determining git version:", e);
        }
    }
    // if we get here, we had a perfect match
    return versionNumber;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) CommandOutputHandler(com.atlassian.stash.scm.CommandOutputHandler) Watchdog(com.atlassian.utils.process.Watchdog) GitCommandBuilderFactory(com.atlassian.stash.scm.git.GitCommandBuilderFactory) LineReader(com.google.common.io.LineReader)

Example 14 with LineReader

use of com.google.common.io.LineReader in project weave by continuuity.

the class EchoServerTestRun method testEchoServer.

@Test
public void testEchoServer() throws InterruptedException, ExecutionException, IOException, URISyntaxException, TimeoutException {
    WeaveRunner runner = YarnTestSuite.getWeaveRunner();
    WeaveController controller = runner.prepare(new EchoServer(), ResourceSpecification.Builder.with().setVirtualCores(1).setMemory(1, ResourceSpecification.SizeUnit.GIGA).setInstances(2).build()).addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))).withApplicationArguments("echo").withArguments("EchoServer", "echo2").start();
    final CountDownLatch running = new CountDownLatch(1);
    controller.addListener(new ServiceListenerAdapter() {

        @Override
        public void running() {
            running.countDown();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    Assert.assertTrue(running.await(30, TimeUnit.SECONDS));
    Iterable<Discoverable> echoServices = controller.discoverService("echo");
    Assert.assertTrue(YarnTestSuite.waitForSize(echoServices, 2, 60));
    for (Discoverable discoverable : echoServices) {
        String msg = "Hello: " + discoverable.getSocketAddress();
        Socket socket = new Socket(discoverable.getSocketAddress().getAddress(), discoverable.getSocketAddress().getPort());
        try {
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true);
            LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8));
            writer.println(msg);
            Assert.assertEquals(msg, reader.readLine());
        } finally {
            socket.close();
        }
    }
    // Increase number of instances
    controller.changeInstances("EchoServer", 3);
    Assert.assertTrue(YarnTestSuite.waitForSize(echoServices, 3, 60));
    echoServices = controller.discoverService("echo2");
    // Decrease number of instances
    controller.changeInstances("EchoServer", 1);
    Assert.assertTrue(YarnTestSuite.waitForSize(echoServices, 1, 60));
    // Increase number of instances again
    controller.changeInstances("EchoServer", 2);
    Assert.assertTrue(YarnTestSuite.waitForSize(echoServices, 2, 60));
    // Make sure still only one app is running
    Iterable<WeaveRunner.LiveInfo> apps = runner.lookupLive();
    Assert.assertTrue(YarnTestSuite.waitForSize(apps, 1, 60));
    // Creates a new runner service to check it can regain control over running app.
    WeaveRunnerService runnerService = YarnTestSuite.createWeaveRunnerService();
    runnerService.startAndWait();
    try {
        Iterable<WeaveController> controllers = runnerService.lookup("EchoServer");
        Assert.assertTrue(YarnTestSuite.waitForSize(controllers, 1, 60));
        for (WeaveController c : controllers) {
            LOG.info("Stopping application: " + c.getRunId());
            c.stop().get(30, TimeUnit.SECONDS);
        }
        Assert.assertTrue(YarnTestSuite.waitForSize(apps, 0, 60));
    } finally {
        runnerService.stopAndWait();
    }
    // Sleep a bit before exiting.
    TimeUnit.SECONDS.sleep(2);
}
Also used : WeaveRunner(com.continuuity.weave.api.WeaveRunner) Discoverable(com.continuuity.weave.discovery.Discoverable) InputStreamReader(java.io.InputStreamReader) ServiceListenerAdapter(com.continuuity.weave.common.ServiceListenerAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) LineReader(com.google.common.io.LineReader) PrinterLogHandler(com.continuuity.weave.api.logging.PrinterLogHandler) WeaveController(com.continuuity.weave.api.WeaveController) OutputStreamWriter(java.io.OutputStreamWriter) WeaveRunnerService(com.continuuity.weave.api.WeaveRunnerService) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 15 with LineReader

use of com.google.common.io.LineReader in project weave by continuuity.

the class ResourceReportTestRun method testRunnablesGetAllowedResourcesInEnv.

@Test
public void testRunnablesGetAllowedResourcesInEnv() throws InterruptedException, IOException, TimeoutException, ExecutionException {
    WeaveRunner runner = YarnTestSuite.getWeaveRunner();
    ResourceSpecification resourceSpec = ResourceSpecification.Builder.with().setVirtualCores(1).setMemory(2048, ResourceSpecification.SizeUnit.MEGA).setInstances(1).build();
    WeaveController controller = runner.prepare(new EnvironmentEchoServer(), resourceSpec).addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))).withApplicationArguments("envecho").withArguments("EnvironmentEchoServer", "echo2").start();
    final CountDownLatch running = new CountDownLatch(1);
    controller.addListener(new ServiceListenerAdapter() {

        @Override
        public void running() {
            running.countDown();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    Assert.assertTrue(running.await(30, TimeUnit.SECONDS));
    Iterable<Discoverable> envEchoServices = controller.discoverService("envecho");
    Assert.assertTrue(YarnTestSuite.waitForSize(envEchoServices, 1, 30));
    // TODO: check virtual cores once yarn adds the ability
    Map<String, String> expectedValues = Maps.newHashMap();
    expectedValues.put(EnvKeys.YARN_CONTAINER_MEMORY_MB, "2048");
    expectedValues.put(EnvKeys.WEAVE_INSTANCE_COUNT, "1");
    // check environment of the runnable.
    Discoverable discoverable = envEchoServices.iterator().next();
    for (Map.Entry<String, String> expected : expectedValues.entrySet()) {
        Socket socket = new Socket(discoverable.getSocketAddress().getHostName(), discoverable.getSocketAddress().getPort());
        try {
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8), true);
            LineReader reader = new LineReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8));
            writer.println(expected.getKey());
            Assert.assertEquals(expected.getValue(), reader.readLine());
        } finally {
            socket.close();
        }
    }
    controller.stop().get(30, TimeUnit.SECONDS);
    // Sleep a bit before exiting.
    TimeUnit.SECONDS.sleep(2);
}
Also used : WeaveRunner(com.continuuity.weave.api.WeaveRunner) Discoverable(com.continuuity.weave.discovery.Discoverable) InputStreamReader(java.io.InputStreamReader) ServiceListenerAdapter(com.continuuity.weave.common.ServiceListenerAdapter) ResourceSpecification(com.continuuity.weave.api.ResourceSpecification) CountDownLatch(java.util.concurrent.CountDownLatch) LineReader(com.google.common.io.LineReader) PrinterLogHandler(com.continuuity.weave.api.logging.PrinterLogHandler) WeaveController(com.continuuity.weave.api.WeaveController) OutputStreamWriter(java.io.OutputStreamWriter) Map(java.util.Map) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

LineReader (com.google.common.io.LineReader)20 InputStreamReader (java.io.InputStreamReader)10 IOException (java.io.IOException)8 StringReader (java.io.StringReader)6 ArrayList (java.util.ArrayList)6 OutputStreamWriter (java.io.OutputStreamWriter)5 Socket (java.net.Socket)5 Discoverable (com.continuuity.weave.discovery.Discoverable)4 BufferedReader (java.io.BufferedReader)4 FileReader (java.io.FileReader)4 InputStream (java.io.InputStream)4 PrintWriter (java.io.PrintWriter)4 CommandOutputHandler (com.atlassian.stash.scm.CommandOutputHandler)3 Watchdog (com.atlassian.utils.process.Watchdog)3 WeaveController (com.continuuity.weave.api.WeaveController)3 WeaveRunner (com.continuuity.weave.api.WeaveRunner)3 PrinterLogHandler (com.continuuity.weave.api.logging.PrinterLogHandler)3 Matcher (java.util.regex.Matcher)3 Test (org.junit.Test)3 ServiceListenerAdapter (com.continuuity.weave.common.ServiceListenerAdapter)2