Search in sources :

Example 1 with LineReader

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;
}
Also used : Discoverable(com.continuuity.weave.discovery.Discoverable) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) LineReader(com.google.common.io.LineReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter)

Example 2 with LineReader

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);
}
Also used : WeaveRunner(com.continuuity.weave.api.WeaveRunner) Discoverable(com.continuuity.weave.discovery.Discoverable) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) LineReader(com.google.common.io.LineReader) PrinterLogHandler(com.continuuity.weave.api.logging.PrinterLogHandler) WeaveController(com.continuuity.weave.api.WeaveController) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Socket(java.net.Socket) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 3 with LineReader

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();
}
Also used : LineReader(com.google.common.io.LineReader) StringReader(java.io.StringReader) NonNull(com.android.annotations.NonNull)

Example 4 with LineReader

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);
    }
}
Also used : LineReader(com.google.common.io.LineReader) FileReader(java.io.FileReader) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File)

Example 5 with LineReader

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);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) LineReader(com.google.common.io.LineReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) BufferedWriter(java.io.BufferedWriter)

Aggregations

LineReader (com.google.common.io.LineReader)21 InputStreamReader (java.io.InputStreamReader)10 IOException (java.io.IOException)9 StringReader (java.io.StringReader)7 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