Example usage for com.google.common.io Closer create

List of usage examples for com.google.common.io Closer create

Introduction

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

Prototype

public static Closer create() 

Source Link

Document

Creates a new Closer .

Usage

From source file:alluxio.client.block.LocalBlockOutStream.java

/**
 * Creates a new local block output stream.
 *
 * @param blockId the block id//from ww  w.j  a v a  2s.  co m
 * @param blockSize the block size
 * @param workerNetAddress the address of the local worker
 * @param context the file system context
 * @param options the options
 * @throws IOException if an I/O error occurs
 */
public LocalBlockOutStream(long blockId, long blockSize, WorkerNetAddress workerNetAddress,
        FileSystemContext context, OutStreamOptions options) throws IOException {
    super(blockId, blockSize, context);
    if (!NetworkAddressUtils.getClientHostName().equals(workerNetAddress.getHost())) {
        throw new IOException(ExceptionMessage.NO_LOCAL_WORKER.getMessage(workerNetAddress));
    }

    mCloser = Closer.create();
    try {
        mBlockWorkerClient = mCloser.register(context.createBlockWorkerClient(workerNetAddress));
        long initialSize = Configuration.getBytes(PropertyKey.USER_FILE_BUFFER_BYTES);
        String blockPath = mBlockWorkerClient.requestBlockLocation(mBlockId, initialSize,
                options.getWriteTier());
        mReservedBytes += initialSize;
        mWriter = new LocalFileBlockWriter(blockPath);
        mCloser.register(mWriter);
    } catch (IOException e) {
        mCloser.close();
        throw e;
    }
}

From source file:com.facebook.buck.cli.BuildPrehook.java

BuildPrehook(ListeningProcessExecutor processExecutor, Cell cell, BuckEventBus eventBus, BuckConfig buckConfig,
        ImmutableMap<String, String> environment, Iterable<String> arguments) {
    this.processExecutor = processExecutor;
    this.cell = cell;
    this.eventBus = eventBus;
    this.buckConfig = buckConfig;
    this.environment = environment;
    this.arguments = arguments;
    this.closer = Closer.create();
}

From source file:tachyon.job.CommandLineJob.java

@Override
public boolean run() {
    try {// w w  w .j  a va2 s .c om
        String outputPath = getJobConf().getOutputFilePath();
        LOG.info("Exec {} output to {}", mCommand, outputPath);
        Process p = java.lang.Runtime.getRuntime().exec(mCommand);
        String line;
        Closer closer = Closer.create();
        try {
            BufferedReader bri = closer.register(new BufferedReader(new InputStreamReader(p.getInputStream())));
            BufferedReader bre = closer.register(new BufferedReader(new InputStreamReader(p.getErrorStream())));
            File file = new File(outputPath);
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = closer.register(new BufferedWriter(fw));
            while ((line = bri.readLine()) != null) {
                bw.write(line + "\n");
            }
            while ((line = bre.readLine()) != null) {
                bw.write(line + "\n");
            }
            bw.flush();
        } finally {
            closer.close();
        }

        p.waitFor();
        LOG.info("Exec {} output to {} done.", mCommand, outputPath);
    } catch (IOException e) {
        LOG.error(e.getMessage());
        return false;
    } catch (InterruptedException e) {
        LOG.error(e.getMessage());
        return false;
    }

    return true;
}

From source file:gobblin.instrumented.qualitychecker.InstrumentedRowLevelPolicyBase.java

protected InstrumentedRowLevelPolicyBase(State state, Type type, Optional<Class<?>> classTag) {
    super(state, type);
    this.instrumentationEnabled = GobblinMetrics.isEnabled(state);
    this.closer = Closer.create();
    this.metricContext = this.closer
            .register(Instrumented.getMetricContext(state, classTag.or(this.getClass())));

    regenerateMetrics();/*from   ww w.j a v  a 2 s .c  o m*/
}

From source file:com.googlecode.jmxtrans.test.TCPEchoServer.java

public void start() {
    checkState(thread == null, "Server already started");

    thread = new Thread(new Runnable() {
        @Override/*  w w w  .  j a  v  a 2s  .c o m*/
        public void run() {
            Closer closer = Closer.create();
            try {
                try {
                    server = closer.register(new ServerSocket(0));
                    while (true) {
                        processRequests(server);
                    }
                } catch (Throwable t) {
                    throw closer.rethrow(t);
                } finally {
                    closer.close();
                    server = null;
                }
            } catch (IOException ioe) {
                log.error("Exception in TCP echo server", ioe);
            }

        }
    });
    thread.start();
    try {
        synchronized (startSynchro) {
            startSynchro.wait(1000);
        }
    } catch (InterruptedException interrupted) {
        log.error("TCP Echo server seems to take too long to start", interrupted);
    }
}

From source file:alluxio.client.block.stream.BlockOutStream.java

/**
 * Creates a new remote block output stream.
 *
 * @param blockId the block id//from ww w  .  java2s .co  m
 * @param blockSize the block size
 * @param workerNetAddress the worker network address
 * @param context the file system context
 * @param options the options
 * @throws IOException if an I/O error occurs
 * @return the {@link BlockOutStream} instance created
 */
public static BlockOutStream createRemoteBlockOutStream(long blockId, long blockSize,
        WorkerNetAddress workerNetAddress, FileSystemContext context, OutStreamOptions options)
        throws IOException {
    Closer closer = Closer.create();
    try {
        BlockWorkerClient client = closer.register(context.createBlockWorkerClient(workerNetAddress));

        PacketOutStream outStream = PacketOutStream.createNettyPacketOutStream(context,
                client.getDataServerAddress(), client.getSessionId(), blockId, blockSize,
                options.getWriteTier(), Protocol.RequestType.ALLUXIO_BLOCK);
        closer.register(outStream);
        return new BlockOutStream(outStream, blockId, blockSize, client, options);
    } catch (IOException e) {
        CommonUtils.closeQuietly(closer);
        throw e;
    }
}

From source file:com.android.builder.internal.packaging.zip.ProcessedAndRawByteSources.java

@Override
public void close() throws IOException {
    Closer closer = Closer.create();
    closer.register(mProcessedSource);//w  ww  .  j av  a2s. c om
    closer.register(mRawSource);
    closer.close();
}

From source file:com.sk89q.eduardo.util.config.YamlConfig.java

@Override
public boolean load() {
    try (Closer closer = Closer.create()) {
        FileInputStream fis = closer.register(new FileInputStream(file));
        BufferedInputStream bis = closer.register(new BufferedInputStream(fis));
        UnicodeReader reader = closer.register(new UnicodeReader(bis));
        Object result = yaml.load(reader);

        this.config = ((ConfigObject) result).toConfig();
        return true;
    } catch (FileNotFoundException ignored) {
    } catch (YAMLException | IOException e) {
        log.warn("Failed to read the configuration at " + file.getAbsolutePath(), e);
    }/*ww w .  j av a2  s .  c om*/

    return false;
}

From source file:io.macgyver.core.crypto.KeyStoreManager.java

public synchronized KeyStore getKeyStore() throws GeneralSecurityException {
    if (keyStore != null) {
        return keyStore;
    }/*from   www.  jav a 2s  .  c  o m*/
    KeyStore ks = KeyStore.getInstance("JCEKS");

    Closer c = Closer.create();
    try {
        logger.info("loading keystore from: {}", getKeyStoreLocation());
        BufferedInputStream is = new BufferedInputStream(new FileInputStream(getKeyStoreLocation()));
        c.register(is);
        ks.load(is, getKeyStorePassword());
        keyStore = ks;
    } catch (IOException e) {
        throw new GeneralSecurityException(e);
    } finally {

        try {
            c.close();
        } catch (Exception e) {
            // swallow
        }
    }

    return ks;
}

From source file:alluxio.job.CommandLineJob.java

@Override
public boolean run() {
    try {//from w  w  w  .  j  ava2 s.c  o m
        String outputPath = getJobConf().getOutputFilePath();
        LOG.info("Exec {} output to {}", mCommand, outputPath);
        Process p = java.lang.Runtime.getRuntime().exec(mCommand);
        String line;
        Closer closer = Closer.create();
        try {
            BufferedReader bri = closer.register(new BufferedReader(new InputStreamReader(p.getInputStream())));
            BufferedReader bre = closer.register(new BufferedReader(new InputStreamReader(p.getErrorStream())));
            File file = new File(outputPath);
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = closer.register(new BufferedWriter(fw));
            while ((line = bri.readLine()) != null) {
                bw.write(line + "\n");
            }
            while ((line = bre.readLine()) != null) {
                bw.write(line + "\n");
            }
            bw.flush();
        } finally {
            closer.close();
        }

        p.waitFor();
        LOG.info("Exec {} output to {} done.", mCommand, outputPath);
    } catch (IOException | InterruptedException e) {
        LOG.error(e.getMessage());
        return false;
    }

    return true;
}