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));
}
}
}
};
}
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);
}
}
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;
}
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);
}
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);
}
Aggregations