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:io.prestosql.memory.ClusterMemoryManager.java

@PreDestroy
public synchronized void destroy() throws IOException {
    try (Closer closer = Closer.create()) {
        for (ClusterMemoryPool pool : pools.values()) {
            closer.register(// w  ww .j  ava2  s  . co m
                    () -> exporter.unexport(generatedNameOf(ClusterMemoryPool.class, pool.getId().toString())));
        }
        closer.register(listenerExecutor::shutdownNow);
    }
}

From source file:org.apache.gobblin.runtime.mapreduce.MRJobLauncher.java

/**
 * Prepare the job input.//  w ww . j  av a  2s .com
 * @throws IOException
 */
private void prepareJobInput(List<WorkUnit> workUnits) throws IOException {
    Closer closer = Closer.create();
    try {
        ParallelRunner parallelRunner = closer
                .register(new ParallelRunner(this.parallelRunnerThreads, this.fs));

        int multiTaskIdSequence = 0;
        // Serialize each work unit into a file named after the task ID
        for (WorkUnit workUnit : workUnits) {

            String workUnitFileName;
            if (workUnit instanceof MultiWorkUnit) {
                workUnitFileName = JobLauncherUtils.newMultiTaskId(this.jobContext.getJobId(),
                        multiTaskIdSequence++) + MULTI_WORK_UNIT_FILE_EXTENSION;
            } else {
                workUnitFileName = workUnit.getProp(ConfigurationKeys.TASK_ID_KEY) + WORK_UNIT_FILE_EXTENSION;
            }
            Path workUnitFile = new Path(this.jobInputPath, workUnitFileName);
            LOG.debug("Writing work unit file " + workUnitFileName);

            parallelRunner.serializeToFile(workUnit, workUnitFile);

            // Append the work unit file path to the job input file
        }
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:net.simon04.guavavfs.VirtualFiles.java

/**
 * Maps a file in to memory as per/* w  w  w . j  a  va  2s.  c om*/
 * {@link FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long)}
 * using the requested {@link MapMode}.
 * <p>
 * <p>Files are mapped from offset 0 to {@code size}.
 * <p>
 * <p>If the mode is {@link MapMode#READ_WRITE} and the file does not exist,
 * it will be created with the requested {@code size}. Thus this method is
 * useful for creating memory mapped files which do not yet exist.
 * <p>
 * <p>This only works for files {@code <=} {@link Integer#MAX_VALUE} bytes.
 *
 * @param file the file to map
 * @param mode the mode to use when mapping {@code file}
 * @return a buffer reflecting {@code file}
 * @throws IOException if an I/O error occurs
 * @see FileChannel#map(MapMode, long, long)
 */
public static MappedByteBuffer map(String file, MapMode mode, long size)
        throws FileNotFoundException, IOException {
    checkNotNull(file);
    checkNotNull(mode);

    Closer closer = Closer.create();
    try {
        RandomAccessFile raf = closer
                .register(new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw"));
        return map(raf, mode, size);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:org.basepom.mojo.duplicatefinder.DuplicateFinderMojo.java

/**
 * Calculates the SHA256 Hash of a class in a file.
 *
 * @param file the archive contains the class
 * @param resourcePath the name of the class
 * @return the MD% Hash as Hex-Value//from  w  w w .j  ava  2  s  .co  m
 * @throws IOException if any error occurs on reading class in archive
 */
private static String getSHA256HexOfElement(final File file, final String resourcePath) throws IOException {
    final Closer closer = Closer.create();
    InputStream in;

    try {
        if (file.isDirectory()) {
            final File resourceFile = new File(file, resourcePath);
            in = closer.register(new BufferedInputStream(new FileInputStream(resourceFile)));
        } else {
            final ZipFile zip = new ZipFile(file);

            closer.register(new Closeable() {
                @Override
                public void close() throws IOException {
                    zip.close();
                }
            });

            final ZipEntry zipEntry = zip.getEntry(resourcePath);

            if (zipEntry == null) {
                throw new IOException(format("Could not find %s in archive %s", resourcePath, file));
            }

            in = zip.getInputStream(zipEntry);
        }

        return SHA_256.newHasher().putBytes(ByteStreams.toByteArray(in)).hash().toString();
    } finally {
        closer.close();
    }
}

From source file:gobblin.runtime.local.LocalJobManager.java

/**
 * Start the job configuration file monitor.
 *
 * <p>//from  w ww. j  a  v a  2 s. c om
 *     The job configuration file monitor currently only supports monitoring
 *     newly added job configuration files.
 * </p>
 */
private void startJobConfigFileMonitor() throws Exception {
    File jobConfigFileDir = new File(this.properties.getProperty(ConfigurationKeys.JOB_CONFIG_FILE_DIR_KEY));
    FileAlterationObserver observer = new FileAlterationObserver(jobConfigFileDir);
    FileAlterationListener listener = new FileAlterationListenerAdaptor() {
        /**
         * Called when a new job configuration file is dropped in.
         */
        @Override
        public void onFileCreate(File file) {
            int pos = file.getName().lastIndexOf(".");
            String fileExtension = pos >= 0 ? file.getName().substring(pos + 1) : "";
            if (!jobConfigFileExtensions.contains(fileExtension)) {
                // Not a job configuration file, ignore.
                return;
            }

            LOG.info("Detected new job configuration file " + file.getAbsolutePath());
            Properties jobProps = new Properties();
            // First add framework configuration properties
            jobProps.putAll(properties);
            // Then load job configuration properties from the new job configuration file
            loadJobConfig(jobProps, file);

            // Schedule the new job
            try {
                boolean runOnce = Boolean
                        .valueOf(jobProps.getProperty(ConfigurationKeys.JOB_RUN_ONCE_KEY, "false"));
                scheduleJob(jobProps, runOnce ? new RunOnceJobListener() : new EmailNotificationJobListener());
            } catch (Throwable t) {
                LOG.error("Failed to schedule new job loaded from job configuration file "
                        + file.getAbsolutePath(), t);
            }
        }

        /**
         * Called when a job configuration file is changed.
         */
        @Override
        public void onFileChange(File file) {
            int pos = file.getName().lastIndexOf(".");
            String fileExtension = pos >= 0 ? file.getName().substring(pos + 1) : "";
            if (!jobConfigFileExtensions.contains(fileExtension)) {
                // Not a job configuration file, ignore.
                return;
            }

            LOG.info("Detected change to job configuration file " + file.getAbsolutePath());
            Properties jobProps = new Properties();
            // First add framework configuration properties
            jobProps.putAll(properties);
            // Then load the updated job configuration properties
            loadJobConfig(jobProps, file);

            String jobName = jobProps.getProperty(ConfigurationKeys.JOB_NAME_KEY);
            try {
                // First unschedule and delete the old job
                unscheduleJob(jobName);
                boolean runOnce = Boolean
                        .valueOf(jobProps.getProperty(ConfigurationKeys.JOB_RUN_ONCE_KEY, "false"));
                // Reschedule the job with the new job configuration
                scheduleJob(jobProps, runOnce ? new RunOnceJobListener() : new EmailNotificationJobListener());
            } catch (Throwable t) {
                LOG.error("Failed to update existing job " + jobName, t);
            }
        }

        private void loadJobConfig(Properties jobProps, File file) {
            Closer closer = Closer.create();
            try {
                Reader propsReader = closer.register(new InputStreamReader(new FileInputStream(file),
                        ConfigurationKeys.DEFAULT_CHARSET_ENCODING));
                jobProps.load(propsReader);
                jobProps.setProperty(ConfigurationKeys.JOB_CONFIG_FILE_PATH_KEY, file.getAbsolutePath());
            } catch (Exception e) {
                LOG.error("Failed to load job configuration from file " + file.getAbsolutePath(), e);
            } finally {
                try {
                    closer.close();
                } catch (IOException e) {
                    LOG.error("unable to close properties file:" + e, e);
                }
            }
        }
    };

    observer.addListener(listener);
    this.fileAlterationMonitor.addObserver(observer);
    this.fileAlterationMonitor.start();
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Copies at most {@code limit} bytes from {@code from} into {@code to},
 * returning the total number of bytes copied. {@code to} is not closed or
 * flushed.// ww  w.ja  v  a 2  s .  co  m
 * 
 * @param from the source to read bytes from
 * @param to the destination to copy bytes read from {@code from} into
 * @param limit the maximum number of bytes to copy
 * @return the total number of bytes copied from {@code from} to {@code to}
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 * @throws IllegalArgumentException if {@code limit} is negative
 */
@ThreadLocalArray(8192)
public static long copy(@NonNull ByteSource from, @NonNull @WillNotClose OutputStream to, long limit)
        throws IOException {
    final Closer closer = Closer.create();
    try {
        return copy(closer.register(from.openStream()), to, limit);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:net.simon04.guavavfs.VirtualFiles.java

private static MappedByteBuffer map(RandomAccessFile raf, MapMode mode, long size) throws IOException {
    Closer closer = Closer.create();
    try {/*from w  w  w . j  a  va  2 s.  c  o  m*/
        FileChannel channel = closer.register(raf.getChannel());
        return channel.map(mode, 0, size);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:io.prestosql.testing.LocalQueryRunner.java

private MaterializedResultWithPlan executeInternal(Session session, @Language("SQL") String sql) {
    lock.readLock().lock();/*from w w w  .  j  ava2  s  .  c o m*/
    try (Closer closer = Closer.create()) {
        AtomicReference<MaterializedResult.Builder> builder = new AtomicReference<>();
        PageConsumerOutputFactory outputFactory = new PageConsumerOutputFactory(types -> {
            builder.compareAndSet(null, MaterializedResult.resultBuilder(session, types));
            return builder.get()::page;
        });

        TaskContext taskContext = TestingTaskContext.builder(notificationExecutor, yieldExecutor, session)
                .setMaxSpillSize(nodeSpillConfig.getMaxSpillPerNode())
                .setQueryMaxSpillSize(nodeSpillConfig.getQueryMaxSpillPerNode()).build();

        Plan plan = createPlan(session, sql, WarningCollector.NOOP);
        List<Driver> drivers = createDrivers(session, plan, outputFactory, taskContext);
        drivers.forEach(closer::register);

        boolean done = false;
        while (!done) {
            boolean processed = false;
            for (Driver driver : drivers) {
                if (alwaysRevokeMemory) {
                    driver.getDriverContext().getOperatorContexts().stream()
                            .filter(operatorContext -> operatorContext.getOperatorStats()
                                    .getRevocableMemoryReservation().getValue() > 0)
                            .forEach(OperatorContext::requestMemoryRevoking);
                }

                if (!driver.isFinished()) {
                    driver.process();
                    processed = true;
                }
            }
            done = !processed;
        }

        verify(builder.get() != null, "Output operator was not created");
        return new MaterializedResultWithPlan(builder.get().build(), plan);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        lock.readLock().unlock();
    }
}

From source file:com.tinspx.util.io.ByteUtils.java

/**
 * Copies at most {@code limit} bytes from {@code from} into {@code to},
 * returning the total number of bytes copied. {@code to} is not closed.
 * /*from   w  w  w  .  j a va2 s.co  m*/
 * @param from the source to read bytes from
 * @param to the destination to copy bytes read from {@code from} into
 * @param limit the maximum number of bytes to copy
 * @return the total number of bytes copied from {@code from} to {@code to}
 * @throws IOException if an IOException occurs
 * @throws NullPointerException if either {@code from} or {@code to} is null
 * @throws IllegalArgumentException if {@code limit} is negative
 */
@ThreadLocalArray(8192)
public static long copy(@NonNull ByteSource from, @NonNull @WillNotClose WritableByteChannel to, long limit)
        throws IOException {
    checkLimit(limit);
    final Closer closer = Closer.create();
    try {
        if (from instanceof ChannelSource) {
            return copy(closer.register(((ChannelSource) from).openChannel()), to, limit);
        } else {
            return copy(closer.register(from.openStream()), to, limit);
        }
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:org.jclouds.vsphere.compute.config.VSphereComputeServiceAdapter.java

@Override
public Image getImage(String imageName) {
    Closer closer = Closer.create();
    VSphereServiceInstance instance = serviceInstance.get();
    closer.register(instance);/*from  www  .j a va2s  .c om*/
    try {
        try {
            return virtualMachineToImage
                    .apply(getVMwareTemplate(imageName, instance.getInstance().getRootFolder()));
        } catch (Throwable t) {
            throw closer.rethrow(t);
        } finally {
            closer.close();
        }
    } catch (IOException e) {
        Throwables.propagateIfPossible(e);
    }
    return null;
}