Example usage for com.google.common.io OutputSupplier OutputSupplier

List of usage examples for com.google.common.io OutputSupplier OutputSupplier

Introduction

In this page you can find the example usage for com.google.common.io OutputSupplier OutputSupplier.

Prototype

OutputSupplier

Source Link

Usage

From source file:com.xebialabs.overthere.ssh.SshTestUtils.java

static File createPrivateKeyFile(String privateKey) {
    try {//w w w.j a  v a 2s .co  m
        final File privateKeyFile = File.createTempFile("private", ".key");
        privateKeyFile.deleteOnExit();
        CharStreams.write(privateKey, new OutputSupplier<Writer>() {
            @Override
            public Writer getOutput() throws IOException {
                return new FileWriter(privateKeyFile);
            }
        });
        return privateKeyFile;
    } catch (IOException exc) {
        throw new RuntimeIOException("Cannot create private key file", exc);
    }
}

From source file:com.notifier.desktop.service.impl.ServiceClientImpl.java

protected boolean sendCommand(ServiceServer.Command command) {
    Socket socket = null;// ww w .  ja va  2s  .  c  o  m
    try {
        socket = getSocket();
        final Socket socketToUse = socket;
        CharStreams.write(command.name(), new OutputSupplier<BufferedWriter>() {
            @Override
            public BufferedWriter getOutput() throws IOException {
                return new BufferedWriter(
                        new OutputStreamWriter(socketToUse.getOutputStream(), ServiceServerImpl.CHARSET));
            }
        });
        return true;
    } catch (ConnectException e) {
        return false;
    } catch (IOException e) {
        logger.warn("Error sending command to service server", e);
        return false;
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                logger.warn("Error closing socket to service server", e);
            }
        }
    }
}

From source file:com.complexible.common.io.IOSuppliers.java

/**
 * Return an {@link OutputSupplier} that will provide the constant value as the output channel
 *
 * @param theObj    the object to serve as output
 * @param <T>       the object type
 * @return          a new OutputSupplier
 *///from  w ww. j  av  a 2  s.  c o m
public static <T> OutputSupplier<T> outputOf(final T theObj) {
    return new OutputSupplier<T>() {
        @Override
        public T getOutput() throws IOException {
            return theObj;
        }
    };
}

From source file:com.complexible.common.io.IOSuppliers.java

/**
 * Return an {@link OutputSupplier} which is an adapter to the provided {@link Supplier} which provides the actual output
 *
 * @param theObj    the supplier to provide the output
 * @param <T>       the output type
 * @return          a new OutputSupplier
 *///from w w  w  .j  a  v  a 2 s. c o m
public static <T> OutputSupplier<T> outputOf(final Supplier<T> theObj) {
    return new OutputSupplier<T>() {
        @Override
        public T getOutput() throws IOException {
            return theObj.get();
        }
    };
}

From source file:org.sonatype.nexus.error.reporting.bundle.SecurityConfigurationXmlAssembler.java

@Override
public Bundle assemble(IssueSubmissionRequest request) throws IssueSubmissionException {
    SecurityConfiguration configuration = (SecurityConfiguration) cloneViaXml(source.getConfiguration());

    try {//from www .  j a v  a  2 s  .c  om
        final ManagedBundle bundle = storageManager.createBundle("security-configuration.xml",
                "application/xml");

        if (configuration != null) {
            configuration.setAnonymousPassword(PASSWORD_MASK);

            SecurityConfigurationXpp3Writer xppWriter = new SecurityConfigurationXpp3Writer();

            Writer writer = new OutputStreamWriter(bundle.getOutputStream());
            try {
                xppWriter.write(writer, configuration);
            } finally {
                IOUtil.close(writer);
            }
        } else {
            ByteStreams.write("Got no security configuration".getBytes("utf-8"),
                    new OutputSupplier<OutputStream>() {
                        @Override
                        public OutputStream getOutput() throws IOException {
                            return bundle.getOutputStream();
                        }
                    });
        }
        return bundle;
    } catch (IOException e) {
        throw new IssueSubmissionException("Could not assemble security-configuration.xml-bundle", e);
    }

}

From source file:org.sonatype.nexus.error.reporting.bundle.SecurityXmlAssembler.java

@Override
public Bundle assemble(IssueSubmissionRequest request) throws IssueSubmissionException {
    OutputStreamWriter out = null;
    try {//from   w w  w  . jav a 2s. c o m
        final ManagedBundle bundle = storageManager.createBundle("security.xml", "application/xml");
        Configuration configuration = (Configuration) cloneViaXml(source.getConfiguration());

        if (configuration != null) {
            for (CUser user : configuration.getUsers()) {
                user.setPassword(PASSWORD_MASK);
                user.setEmail(PASSWORD_MASK);
            }
            SecurityConfigurationXpp3Writer writer = new SecurityConfigurationXpp3Writer();

            out = new OutputStreamWriter(bundle.getOutputStream());
            try {
                writer.write(out, configuration);
            } finally {
                out.close();
            }
        } else {
            ByteStreams.write("Got no security configuration".getBytes("utf-8"),
                    new OutputSupplier<OutputStream>() {
                        @Override
                        public OutputStream getOutput() throws IOException {
                            return bundle.getOutputStream();
                        }
                    });
        }

        return bundle;
    } catch (IOException e) {
        IOUtil.close(out);
        throw new IssueSubmissionException("Could not assemble security.xml: " + e.getMessage(), e);
    }
}

From source file:org.sonatype.nexus.error.reporting.bundle.NexusXmlAssembler.java

@SuppressWarnings("deprecation")
@Override/*www . ja v  a2  s .c  o  m*/
public Bundle assemble(IssueSubmissionRequest request) throws IssueSubmissionException {
    OutputStream out = null;
    try {
        final ManagedBundle bundle = storageManager.createBundle("nexus.xml", "application/xml");
        final Configuration configuration = configHelper.maskPasswords(nexusConfig.getConfigurationModel());

        // No config ?
        if (configuration != null) {
            NexusConfigurationXpp3Writer writer = new NexusConfigurationXpp3Writer();
            out = bundle.getOutputStream();
            writer.write(out, configuration);
            out.close();
        } else {
            ByteStreams.write("Got no configuration from config helper".getBytes("utf-8"),
                    new OutputSupplier<OutputStream>() {
                        @Override
                        public OutputStream getOutput() throws IOException {
                            return bundle.getOutputStream();
                        }
                    });
        }
        return bundle;
    } catch (IOException e) {
        IOUtil.close(out);
        throw new IssueSubmissionException("Could not assemble nexus.xml: " + e.getMessage(), e);
    }
}

From source file:io.druid.indexer.JobHelper.java

public static void setupClasspath(Path distributedClassPath, Job job) throws IOException {
    String classpathProperty = System.getProperty("druid.hadoop.internal.classpath");
    if (classpathProperty == null) {
        classpathProperty = System.getProperty("java.class.path");
    }/*www.j av  a2  s . c o m*/

    String[] jarFiles = classpathProperty.split(File.pathSeparator);

    final Configuration conf = job.getConfiguration();
    final FileSystem fs = distributedClassPath.getFileSystem(conf);

    if (fs instanceof LocalFileSystem) {
        return;
    }

    for (String jarFilePath : jarFiles) {
        File jarFile = new File(jarFilePath);
        if (jarFile.getName().endsWith(".jar")) {
            final Path hdfsPath = new Path(distributedClassPath, jarFile.getName());

            if (!existing.contains(hdfsPath)) {
                if (jarFile.getName().matches(".*SNAPSHOT(-selfcontained)?\\.jar$") || !fs.exists(hdfsPath)) {
                    log.info("Uploading jar to path[%s]", hdfsPath);
                    ByteStreams.copy(Files.newInputStreamSupplier(jarFile), new OutputSupplier<OutputStream>() {
                        @Override
                        public OutputStream getOutput() throws IOException {
                            return fs.create(hdfsPath);
                        }
                    });
                }

                existing.add(hdfsPath);
            }

            DistributedCache.addFileToClassPath(hdfsPath, conf, fs);
        }
    }
}

From source file:com.xebialabs.overthere.itest.ItestsBase1Utils.java

protected static void writeData(final OverthereFile f, byte[] data) {
    try {/*from  www  .ja  v  a 2s. com*/
        write(data, new OutputSupplier<OutputStream>() {
            @Override
            public OutputStream getOutput() throws IOException {
                return f.getOutputStream();
            }
        });
    } catch (IOException exc) {
        throw new RuntimeIOException(format("Cannot write data to file [%s]", f), exc);
    }
}

From source file:com.xebialabs.overthere.itest.ItestsBase5Unix.java

@Test
@Assumption(methods = { "onUnix", "notLocal" })
public void shouldWriteFileToAndReadFileFromSudoUserHomeDirectoryOnUnix() throws IOException {
    // get handle to file in home dir
    final OverthereFile homeDir = connection.getFile(getUnixHomeDirPath());
    final OverthereFile fileInHomeDir = homeDir.getFile("file" + System.currentTimeMillis() + ".dat");
    assertThat(fileInHomeDir.exists(), equalTo(false));

    // write data to file in home dir
    final byte[] contentsWritten = generateRandomBytes(1024);
    write(contentsWritten, new OutputSupplier<OutputStream>() {
        @Override//from  w w w  .j  a v  a2  s . co m
        public OutputStream getOutput() throws IOException {
            return fileInHomeDir.getOutputStream();
        }
    });

    assertThat(fileInHomeDir.exists(), equalTo(true));

    // restrict access to file in home dir
    connection.execute(sysoutHandler(), syserrHandler(),
            CmdLine.build("chmod", "0600", fileInHomeDir.getPath()));

    // read file from home dir
    byte[] contentsRead = readFile(fileInHomeDir);
    assertThat(contentsRead, equalTo(contentsWritten));

    // restrict access to file in home dir
    fileInHomeDir.delete();
    assertThat(fileInHomeDir.exists(), equalTo(false));
}