use of com.google.common.io.LineReader in project weave by continuuity.
the class FailureRestartTestRun method getInstances.
private Set<Integer> getInstances(Iterable<Discoverable> discoverables) throws IOException {
Set<Integer> instances = Sets.newHashSet();
for (Discoverable discoverable : discoverables) {
InetSocketAddress socketAddress = discoverable.getSocketAddress();
Socket socket = new Socket(socketAddress.getAddress(), socketAddress.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));
String msg = "Failure";
writer.println(msg);
String line = reader.readLine();
Assert.assertTrue(line.endsWith(msg));
instances.add(Integer.parseInt(line.substring(0, line.length() - msg.length())));
} finally {
socket.close();
}
}
return instances;
}
use of com.google.common.io.LineReader in project weave by continuuity.
the class LocalFileTestRun method testLocalFile.
@Test
public void testLocalFile() throws Exception {
String header = Files.readFirstLine(new File(getClass().getClassLoader().getResource("header.txt").toURI()), Charsets.UTF_8);
WeaveRunner runner = YarnTestSuite.getWeaveRunner();
if (runner instanceof YarnWeaveRunnerService) {
((YarnWeaveRunnerService) runner).setJVMOptions("-verbose:gc -Xloggc:gc.log -XX:+PrintGCDetails");
}
WeaveController controller = runner.prepare(new LocalFileApplication()).withApplicationArguments("local").withArguments("LocalFileSocketServer", "local2").addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true))).start();
if (runner instanceof YarnWeaveRunnerService) {
((YarnWeaveRunnerService) runner).setJVMOptions("");
}
Iterable<Discoverable> discoverables = controller.discoverService("local");
Assert.assertTrue(YarnTestSuite.waitForSize(discoverables, 1, 60));
InetSocketAddress socketAddress = discoverables.iterator().next().getSocketAddress();
Socket socket = new Socket(socketAddress.getAddress(), socketAddress.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));
String msg = "Local file test";
writer.println(msg);
Assert.assertEquals(header, reader.readLine());
Assert.assertEquals(msg, reader.readLine());
} finally {
socket.close();
}
controller.stopAndWait();
Assert.assertTrue(YarnTestSuite.waitForSize(discoverables, 0, 60));
TimeUnit.SECONDS.sleep(2);
}
use of com.google.common.io.LineReader in project buck by facebook.
the class Actions method blame.
@NonNull
public String blame(@NonNull XmlDocument xmlDocument) throws IOException, SAXException, ParserConfigurationException {
ImmutableMultimap<Integer, Record> resultingSourceMapping = getResultingSourceMapping(xmlDocument);
LineReader lineReader = new LineReader(new StringReader(xmlDocument.prettyPrint()));
StringBuilder actualMappings = new StringBuilder();
String line;
int count = 0;
while ((line = lineReader.readLine()) != null) {
actualMappings.append(count + 1).append(line).append("\n");
if (resultingSourceMapping.containsKey(count)) {
for (Record record : resultingSourceMapping.get(count)) {
actualMappings.append(count + 1).append("-->").append(record.getActionLocation().toString()).append("\n");
}
}
count++;
}
return actualMappings.toString();
}
use of com.google.common.io.LineReader in project vscrawler by virjar.
the class LoadNextBatchSeedEvent method initSeeds.
@Override
public Collection<Seed> initSeeds(VSCrawlerContext vsCrawlerContext) {
Properties properties = VSCrawlerContext.vsCrawlerConfigFileWatcher.loadedProperties();
String seedFilePath = PathResolver.resolveAbsolutePath(properties.getProperty(String.format(VSCrawlerConstant.VSCRAWLER_INIT_SEED_FILE, vsCrawlerContext.getCrawlerName())));
if (StringUtils.isBlank(seedFilePath) || !new File(seedFilePath).exists()) {
if (StringUtils.isNotBlank(seedFilePath)) {
log.warn("can not find file:{}", seedFilePath);
}
seedFilePath = PathResolver.resolveAbsolutePath(filePath);
}
if (StringUtils.isEmpty(seedFilePath) || !new File(seedFilePath).exists()) {
if (StringUtils.isNotBlank(seedFilePath)) {
log.warn("can not find file:{}", seedFilePath);
}
return Collections.emptyList();
}
vsCrawlerContext.getAutoEventRegistry().registerEvent(LoadNextBatchSeedEvent.class);
Collection<Seed> seeds = null;
try {
fileReader = new FileReader(new File(PathResolver.resolveAbsolutePath(seedFilePath)));
lineReader = new LineReader(fileReader);
seeds = readBatch();
return seeds;
} catch (IOException e) {
log.error("error when load init seed resource");
return Collections.emptyList();
} finally {
closeOrReadNextBatch(seeds, vsCrawlerContext);
}
}
use of com.google.common.io.LineReader in project cdap by caskdata.
the class CommandPortService method serve.
/**
* Starts accepting incoming request. This method would block until this service is stopped.
*
* @throws IOException If any I/O error occurs on the socket connection.
*/
private void serve() throws IOException {
while (isRunning()) {
try {
Socket socket = serverSocket.accept();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
try {
// Read the client command and dispatch
String command = new LineReader(new InputStreamReader(socket.getInputStream(), "UTF-8")).readLine();
CommandHandler handler = handlers.get(command);
if (handler != null) {
try {
handler.handle(writer);
} catch (Throwable t) {
LOG.error(String.format("Exception thrown from CommandHandler for command %s", command), t);
}
}
} finally {
writer.flush();
socket.close();
}
} catch (Throwable th) {
// NOTE: catch any exception to keep the main service running
// Trigger by serverSocket.close() through the call from stop().
LOG.debug(th.getMessage(), th);
}
}
}
Aggregations