Search in sources :

Example 21 with OutputStreamWriter

use of java.io.OutputStreamWriter in project weave by continuuity.

the class YarnWeavePreparer method saveWeaveSpec.

private void saveWeaveSpec(WeaveSpecification spec, final Multimap<String, LocalFile> runnableLocalFiles, Map<String, LocalFile> localFiles) throws IOException {
    // Rewrite LocalFiles inside weaveSpec
    Map<String, RuntimeSpecification> runtimeSpec = Maps.transformEntries(spec.getRunnables(), new Maps.EntryTransformer<String, RuntimeSpecification, RuntimeSpecification>() {

        @Override
        public RuntimeSpecification transformEntry(String key, RuntimeSpecification value) {
            return new DefaultRuntimeSpecification(value.getName(), value.getRunnableSpecification(), value.getResourceSpecification(), runnableLocalFiles.get(key));
        }
    });
    // Serialize into a local temp file.
    LOG.debug("Create and copy {}", Constants.Files.WEAVE_SPEC);
    Location location = createTempLocation(Constants.Files.WEAVE_SPEC);
    Writer writer = new OutputStreamWriter(location.getOutputStream(), Charsets.UTF_8);
    try {
        EventHandlerSpecification eventHandler = spec.getEventHandler();
        if (eventHandler == null) {
            eventHandler = new LogOnlyEventHandler().configure();
        }
        WeaveSpecificationAdapter.create().toJson(new DefaultWeaveSpecification(spec.getName(), runtimeSpec, spec.getOrders(), eventHandler), writer);
    } finally {
        writer.close();
    }
    LOG.debug("Done {}", Constants.Files.WEAVE_SPEC);
    localFiles.put(Constants.Files.WEAVE_SPEC, createLocalFile(Constants.Files.WEAVE_SPEC, location));
}
Also used : DefaultRuntimeSpecification(com.continuuity.weave.internal.DefaultRuntimeSpecification) EventHandlerSpecification(com.continuuity.weave.api.EventHandlerSpecification) DefaultRuntimeSpecification(com.continuuity.weave.internal.DefaultRuntimeSpecification) RuntimeSpecification(com.continuuity.weave.api.RuntimeSpecification) DefaultWeaveSpecification(com.continuuity.weave.internal.DefaultWeaveSpecification) LogOnlyEventHandler(com.continuuity.weave.internal.LogOnlyEventHandler) Maps(com.google.common.collect.Maps) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Location(com.continuuity.weave.filesystem.Location)

Example 22 with OutputStreamWriter

use of java.io.OutputStreamWriter 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 23 with OutputStreamWriter

use of java.io.OutputStreamWriter 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 24 with OutputStreamWriter

use of java.io.OutputStreamWriter in project weave by continuuity.

the class SocketServer method run.

@Override
public void run() {
    try {
        runThread = Thread.currentThread();
        while (running) {
            try {
                Socket socket = serverSocket.accept();
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), Charsets.UTF_8));
                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
                    handleRequest(reader, writer);
                } finally {
                    socket.close();
                }
            } catch (SocketException e) {
                LOG.info("Socket exception: " + e);
            }
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }
}
Also used : SocketException(java.net.SocketException) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) SocketException(java.net.SocketException) PrintWriter(java.io.PrintWriter)

Example 25 with OutputStreamWriter

use of java.io.OutputStreamWriter in project cryptomator by cryptomator.

the class SettingsProvider method save.

private void save(Settings settings) {
    assert settings != null : "method should only be invoked by #scheduleSave, which checks for null";
    final Path settingsPath = getSettingsPath();
    try {
        Files.createDirectories(settingsPath.getParent());
        try (//
        OutputStream out = Files.newOutputStream(settingsPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
            Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
            gson.toJson(settings, writer);
            LOG.info("Settings saved to " + settingsPath);
        }
    } catch (IOException e) {
        LOG.error("Failed to save settings.", e);
    }
}
Also used : Path(java.nio.file.Path) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Aggregations

OutputStreamWriter (java.io.OutputStreamWriter)3616 IOException (java.io.IOException)1453 FileOutputStream (java.io.FileOutputStream)1408 BufferedWriter (java.io.BufferedWriter)1320 Writer (java.io.Writer)939 File (java.io.File)912 PrintWriter (java.io.PrintWriter)589 InputStreamReader (java.io.InputStreamReader)510 OutputStream (java.io.OutputStream)507 BufferedReader (java.io.BufferedReader)426 ByteArrayOutputStream (java.io.ByteArrayOutputStream)373 InputStream (java.io.InputStream)255 URL (java.net.URL)242 HttpURLConnection (java.net.HttpURLConnection)208 FileNotFoundException (java.io.FileNotFoundException)207 Test (org.junit.Test)201 ArrayList (java.util.ArrayList)198 Path (org.apache.hadoop.fs.Path)194 UnsupportedEncodingException (java.io.UnsupportedEncodingException)169 FileInputStream (java.io.FileInputStream)167