Example usage for java.nio.file Files newOutputStream

List of usage examples for java.nio.file Files newOutputStream

Introduction

In this page you can find the example usage for java.nio.file Files newOutputStream.

Prototype

public static OutputStream newOutputStream(Path path, OpenOption... options) throws IOException 

Source Link

Document

Opens or creates a file, returning an output stream that may be used to write bytes to the file.

Usage

From source file:org.elasticsearch.xpack.core.ssl.SSLConfigurationReloaderTests.java

/**
 * Tests the reloading of a key config backed by pem files when there is an exception during reloading. An exception is caused by
 * truncating the key file that is being monitored
 *//*from  w  w  w.  ja v  a2  s.  c o m*/
public void testReloadingPEMKeyConfigException() throws Exception {
    Path tempDir = createTempDir();
    Path keyPath = tempDir.resolve("testnode.pem");
    Path certPath = tempDir.resolve("testnode.crt");
    Path clientCertPath = tempDir.resolve("testclient.crt");
    Files.copy(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.pem"),
            keyPath);
    Files.copy(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.crt"),
            certPath);
    Files.copy(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt"),
            clientCertPath);
    MockSecureSettings secureSettings = new MockSecureSettings();
    secureSettings.setString("xpack.ssl.secure_key_passphrase", "testnode");
    Settings settings = Settings.builder().put("xpack.ssl.key", keyPath).put("xpack.ssl.certificate", certPath)
            .putList("xpack.ssl.certificate_authorities", certPath.toString(), clientCertPath.toString())
            .put("path.home", createTempDir()).setSecureSettings(secureSettings).build();
    Environment env = randomBoolean() ? null : TestEnvironment.newEnvironment(settings);
    final SSLService sslService = new SSLService(settings, env);
    final SSLConfiguration config = sslService.sslConfiguration(Settings.EMPTY);
    new SSLConfigurationReloader(settings, env, sslService, resourceWatcherService) {
        @Override
        void reloadSSLContext(SSLConfiguration configuration) {
            fail("reload should not be called! [pem key reload exception]");
        }
    };

    final SSLContext context = sslService.sslContextHolder(config).sslContext();

    // truncate the file
    try (OutputStream os = Files.newOutputStream(keyPath, StandardOpenOption.TRUNCATE_EXISTING)) {
    }

    // we intentionally don't wait here as we rely on concurrency to catch a failure
    assertThat(sslService.sslContextHolder(config).sslContext(), sameInstance(context));
}

From source file:org.elasticsearch.plugins.PluginManagerIT.java

/**
 * @deprecated support for this is not going to stick around, seriously.
 */// w  w w  . j  av a2s  .  c o  m
@Deprecated
public void testAlreadyInstalledNotIsolated() throws Exception {
    String pluginName = "fake-plugin";
    Path pluginDir = createTempDir().resolve(pluginName);
    Files.createDirectories(pluginDir);
    // create a jar file in the plugin
    Path pluginJar = pluginDir.resolve("fake-plugin.jar");
    try (ZipOutputStream out = new JarOutputStream(
            Files.newOutputStream(pluginJar, StandardOpenOption.CREATE))) {
        out.putNextEntry(new ZipEntry("foo.class"));
        out.closeEntry();
    }
    String pluginUrl = createPlugin(pluginDir, "description", "fake desc", "name", pluginName, "version", "1.0",
            "elasticsearch.version", Version.CURRENT.toString(), "java.version",
            System.getProperty("java.specification.version"), "isolated", "false", "jvm", "true", "classname",
            "FakePlugin");

    // install
    ExitStatus status = new PluginManagerCliParser(terminal).execute(args("install " + pluginUrl));
    assertEquals("unexpected exit status: output: " + terminal.getTerminalOutput(), ExitStatus.OK, status);

    // install again
    status = new PluginManagerCliParser(terminal).execute(args("install " + pluginUrl));
    List<String> output = terminal.getTerminalOutput();
    assertEquals("unexpected exit status: output: " + output, ExitStatus.IO_ERROR, status);
    boolean foundExpectedMessage = false;
    for (String line : output) {
        foundExpectedMessage |= line.contains("already exists");
    }
    assertTrue(foundExpectedMessage);
}

From source file:org.elasticsearch.xpack.core.ssl.SSLConfigurationReloaderTests.java

/**
 * Tests the reloading of a truststore when there is an exception during reloading. An exception is caused by truncating the truststore
 * that is being monitored//from   w  w  w  .j av  a 2 s .  c  o  m
 */
public void testTrustStoreReloadException() throws Exception {
    Path tempDir = createTempDir();
    Path trustStorePath = tempDir.resolve("testnode.jks");
    Files.copy(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode.jks"),
            trustStorePath);
    MockSecureSettings secureSettings = new MockSecureSettings();
    secureSettings.setString("xpack.ssl.truststore.secure_password", "testnode");
    Settings settings = Settings.builder().put("xpack.ssl.truststore.path", trustStorePath)
            .put("path.home", createTempDir()).setSecureSettings(secureSettings).build();
    Environment env = randomBoolean() ? null : TestEnvironment.newEnvironment(settings);
    final SSLService sslService = new SSLService(settings, env);
    final SSLConfiguration config = sslService.sslConfiguration(Settings.EMPTY);
    new SSLConfigurationReloader(settings, env, sslService, resourceWatcherService) {
        @Override
        void reloadSSLContext(SSLConfiguration configuration) {
            fail("reload should not be called! [truststore reload exception]");
        }
    };

    final SSLContext context = sslService.sslContextHolder(config).sslContext();

    // truncate the truststore
    try (OutputStream os = Files.newOutputStream(trustStorePath, StandardOpenOption.TRUNCATE_EXISTING)) {
    }

    // we intentionally don't wait here as we rely on concurrency to catch a failure
    assertThat(sslService.sslContextHolder(config).sslContext(), sameInstance(context));
}

From source file:herddb.cli.HerdDBCLI.java

private static void backupTableSpace(final Statement statement, String schema, String file, String suffix,
        final Connection connection, int dumpfetchsize) throws Exception, SQLException {
    List<String> tablesToDump = new ArrayList<>();
    try (ResultSet rs = statement.executeQuery(
            "SELECT table_name" + " FROM " + schema + ".systables" + " WHERE systemtable='false'")) {
        while (rs.next()) {
            String tablename = rs.getString(1).toLowerCase();
            tablesToDump.add(tablename);
        }//from  w ww. ja v  a2s.c  o m
    }
    int dot = file.lastIndexOf('.');
    String ext = "";
    if (dot >= 0) {
        ext = file.substring(dot);
        file = file.substring(0, dot);
    }
    String finalFile = (suffix == null ? file : file + suffix) + ext;
    Path outputfile = Paths.get(finalFile).toAbsolutePath();
    println("Backup tables " + tablesToDump + " from tablespace " + schema + " to " + outputfile);

    try (OutputStream fout = wrapOutputStream(Files.newOutputStream(outputfile, StandardOpenOption.CREATE_NEW),
            ext); SimpleBufferedOutputStream oo = new SimpleBufferedOutputStream(fout, 16 * 1024 * 1024);) {
        HerdDBConnection hcon = connection.unwrap(HerdDBConnection.class);
        HDBConnection hdbconnection = hcon.getConnection();
        BackupUtils.dumpTableSpace(schema, dumpfetchsize, hdbconnection, oo, new ProgressListener() {
            @Override
            public void log(String actionType, String message, Map<String, Object> context) {
                println(message);
            }

        });
    }
    println("Backup finished for tablespace " + schema);
}