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:com.github.autermann.sockets.ssl.SSLUtils.java

public static PrivateKey readKey(InputSupplier<? extends InputStream> in)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    Closer closer = Closer.create();
    try {/*from  w  w  w  . java  2  s.  c o m*/
        Reader reader = closer.register(CharStreams.newReaderSupplier(in, Charsets.UTF_8).getInput());
        return createPrivateKey(new PemReader(reader).readPemObject());
    } catch (IOException e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:com.squareup.wire.java.ProfileLoader.java

private ImmutableList<ProfileFileElement> loadProfileFiles(Multimap<Path, String> pathsToAttempt)
        throws IOException {
    ImmutableList.Builder<ProfileFileElement> result = ImmutableList.builder();
    try (Closer closer = Closer.create()) {
        for (Map.Entry<Path, Collection<String>> entry : pathsToAttempt.asMap().entrySet()) {
            Path base = entry.getKey();
            if (Files.isRegularFile(base)) {
                FileSystem sourceFs = FileSystems.newFileSystem(base, getClass().getClassLoader());
                closer.register(sourceFs);
                base = getOnlyElement(sourceFs.getRootDirectories());
            }//from  ww w.ja  va  2s  .c o m
            for (String path : entry.getValue()) {
                ProfileFileElement element = loadProfileFile(base, path);
                if (element != null)
                    result.add(element);
            }
        }
    }
    return result.build();
}

From source file:gobblin.data.management.copy.hive.HivePartitionFileSet.java

@Override
protected Collection<CopyEntity> generateCopyEntities() throws IOException {

    try (Closer closer = Closer.create()) {
        MultiTimingEvent multiTimer = closer
                .register(new MultiTimingEvent(this.eventSubmitter, "PartitionCopy", true));

        int stepPriority = 0;
        String fileSet = HiveCopyEntityHelper.gson.toJson(this.partition.getValues());

        List<CopyEntity> copyEntities = Lists.newArrayList();

        stepPriority = hiveCopyEntityHelper.addSharedSteps(copyEntities, fileSet, stepPriority);

        multiTimer.nextStage(HiveCopyEntityHelper.Stages.COMPUTE_TARGETS);
        Path targetPath = hiveCopyEntityHelper.getTargetLocation(hiveCopyEntityHelper.getDataset().fs,
                hiveCopyEntityHelper.getTargetFs(), this.partition.getDataLocation(),
                Optional.of(this.partition));
        Partition targetPartition = getTargetPartition(this.partition, targetPath);

        multiTimer.nextStage(HiveCopyEntityHelper.Stages.EXISTING_PARTITION);
        if (this.existingTargetPartition.isPresent()) {
            hiveCopyEntityHelper.getTargetPartitions().remove(this.partition.getValues());
            try {
                checkPartitionCompatibility(targetPartition, this.existingTargetPartition.get());
            } catch (IOException ioe) {
                if (hiveCopyEntityHelper
                        .getExistingEntityPolicy() != HiveCopyEntityHelper.ExistingEntityPolicy.REPLACE_PARTITIONS) {
                    log.error("Source and target partitions are not compatible. Aborting copy of partition "
                            + this.partition, ioe);
                    return Lists.newArrayList();
                }/*  w  w w. j ava 2 s .  c o  m*/
                log.warn("Source and target partitions are not compatible. Will override target partition: "
                        + ioe.getMessage());
                log.debug("Incompatibility details: ", ioe);
                stepPriority = hiveCopyEntityHelper.addPartitionDeregisterSteps(copyEntities, fileSet,
                        stepPriority, hiveCopyEntityHelper.getTargetTable(),
                        this.existingTargetPartition.get());
                this.existingTargetPartition = Optional.absent();
            }
        }

        multiTimer.nextStage(HiveCopyEntityHelper.Stages.PARTITION_SKIP_PREDICATE);
        if (hiveCopyEntityHelper.getFastPartitionSkip().isPresent()
                && hiveCopyEntityHelper.getFastPartitionSkip().get().apply(this)) {
            log.info(String.format("Skipping copy of partition %s due to fast partition skip predicate.",
                    this.partition.getCompleteName()));
            return Lists.newArrayList();
        }

        HiveSpec partitionHiveSpec = new SimpleHiveSpec.Builder<>(targetPath)
                .withTable(HiveMetaStoreUtils.getHiveTable(hiveCopyEntityHelper.getTargetTable().getTTable()))
                .withPartition(
                        Optional.of(HiveMetaStoreUtils.getHivePartition(targetPartition.getTPartition())))
                .build();
        HiveRegisterStep register = new HiveRegisterStep(hiveCopyEntityHelper.getTargetURI(), partitionHiveSpec,
                hiveCopyEntityHelper.getHiveRegProps());
        copyEntities
                .add(new PostPublishStep(fileSet, Maps.<String, String>newHashMap(), register, stepPriority++));

        multiTimer.nextStage(HiveCopyEntityHelper.Stages.CREATE_LOCATIONS);
        HiveLocationDescriptor sourceLocation = HiveLocationDescriptor.forPartition(this.partition,
                hiveCopyEntityHelper.getDataset().fs, this.properties);
        HiveLocationDescriptor desiredTargetLocation = HiveLocationDescriptor.forPartition(targetPartition,
                hiveCopyEntityHelper.getTargetFs(), this.properties);
        Optional<HiveLocationDescriptor> existingTargetLocation = this.existingTargetPartition.isPresent()
                ? Optional.of(HiveLocationDescriptor.forPartition(this.existingTargetPartition.get(),
                        hiveCopyEntityHelper.getTargetFs(), this.properties))
                : Optional.<HiveLocationDescriptor>absent();

        multiTimer.nextStage(HiveCopyEntityHelper.Stages.FULL_PATH_DIFF);
        HiveCopyEntityHelper.DiffPathSet diffPathSet = HiveCopyEntityHelper.fullPathDiff(sourceLocation,
                desiredTargetLocation, existingTargetLocation, Optional.<Partition>absent(), multiTimer,
                hiveCopyEntityHelper);

        multiTimer.nextStage(HiveCopyEntityHelper.Stages.CREATE_DELETE_UNITS);
        if (diffPathSet.pathsToDelete.size() > 0) {
            DeleteFileCommitStep deleteStep = DeleteFileCommitStep.fromPaths(hiveCopyEntityHelper.getTargetFs(),
                    diffPathSet.pathsToDelete, hiveCopyEntityHelper.getDataset().properties);
            copyEntities.add(
                    new PrePublishStep(fileSet, Maps.<String, String>newHashMap(), deleteStep, stepPriority++));
        }

        multiTimer.nextStage(HiveCopyEntityHelper.Stages.CREATE_COPY_UNITS);
        for (CopyableFile.Builder builder : hiveCopyEntityHelper.getCopyableFilesFromPaths(
                diffPathSet.filesToCopy, hiveCopyEntityHelper.getConfiguration(),
                Optional.of(this.partition))) {
            copyEntities.add(builder.fileSet(fileSet).checksum(new byte[0]).build());
        }

        log.info("Created {} copy entities for partition {}", copyEntities.size(),
                this.partition.getCompleteName());

        return copyEntities;
    }
}

From source file:ch.ledcom.tomcat.valves.SessionSerializableCheckerValve.java

/**
 * Check if an object is serializable, emit a warning log if it is not.
 *
 * @param attribute//from   ww w. j  a  v  a 2s . co m
 *            the attribute to check
 * @throws IOException
 */
private void checkSerializable(final Object attribute) throws IOException {
    if (!Serializable.class.isAssignableFrom(attribute.getClass())) {
        log.warn(format("Session attribute [%s] of class [%s] is not " + "serializable.", attribute,
                attribute.getClass()));
    }
    final Closer closer = Closer.create();
    try {
        final ObjectOutputStream out = closer.register(new ObjectOutputStream(ByteStreams.nullOutputStream()));
        out.writeObject(attribute);
    } catch (Throwable t) {
        closer.rethrow(t);
    } finally {
        closer.close();
    }
}

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

@Override
public boolean save() {
    String output = yaml.dump(config.toObject());

    try (Closer closer = Closer.create()) {
        FileOutputStream fis = closer.register(new FileOutputStream(file));
        BufferedOutputStream bis = closer.register(new BufferedOutputStream(fis));
        OutputStreamWriter writer = closer.register(new OutputStreamWriter(bis, "UTF-8"));
        writer.write(output);//from  ww w .  j a  va  2s.c om

        return true;
    } catch (YAMLException | IOException e) {
        log.warn("Failed to write configuration to " + file.getAbsolutePath(), e);
        return false;
    }
}

From source file:gobblin.writer.PartitionedDataWriter.java

public PartitionedDataWriter(DataWriterBuilder<S, D> builder, final State state) throws IOException {
    this.isSpeculativeAttemptSafe = true;
    this.isWatermarkCapable = true;
    this.baseWriterId = builder.getWriterId();
    this.closer = Closer.create();
    this.partitionWriters = CacheBuilder.newBuilder().build(new CacheLoader<GenericRecord, DataWriter<D>>() {
        @Override//from w  w w . java 2s  . c om
        public DataWriter<D> load(final GenericRecord key) throws Exception {
            return PartitionedDataWriter.this.closer.register(
                    new InstrumentedPartitionedDataWriterDecorator<>(createPartitionWriter(key), state, key));
        }
    });

    if (state.contains(ConfigurationKeys.WRITER_PARTITIONER_CLASS)) {
        Preconditions.checkArgument(builder instanceof PartitionAwareDataWriterBuilder,
                String.format("%s was specified but the writer %s does not support partitioning.",
                        ConfigurationKeys.WRITER_PARTITIONER_CLASS, builder.getClass().getCanonicalName()));

        try {
            this.shouldPartition = true;
            this.builder = Optional.of(PartitionAwareDataWriterBuilder.class.cast(builder));
            this.partitioner = Optional.of(WriterPartitioner.class.cast(ConstructorUtils.invokeConstructor(
                    Class.forName(state.getProp(ConfigurationKeys.WRITER_PARTITIONER_CLASS)), state,
                    builder.getBranches(), builder.getBranch())));
            Preconditions.checkArgument(
                    this.builder.get().validatePartitionSchema(this.partitioner.get().partitionSchema()),
                    String.format("Writer %s does not support schema from partitioner %s",
                            builder.getClass().getCanonicalName(),
                            this.partitioner.getClass().getCanonicalName()));
        } catch (ReflectiveOperationException roe) {
            throw new IOException(roe);
        }
    } else {
        this.shouldPartition = false;
        DataWriter<D> dataWriter = builder.build();
        InstrumentedDataWriterDecorator<D> writer = this.closer
                .register(new InstrumentedDataWriterDecorator<>(dataWriter, state));
        this.isSpeculativeAttemptSafe = this.isDataWriterForPartitionSafe(dataWriter);
        this.isWatermarkCapable = this.isDataWriterWatermarkCapable(dataWriter);
        this.partitionWriters.put(NON_PARTITIONED_WRITER_KEY, writer);
        this.partitioner = Optional.absent();
        this.builder = Optional.absent();
    }
}

From source file:org.apache.gobblin.util.limiter.MultiLimiter.java

@Override
public Closeable acquirePermits(long permits) throws InterruptedException {
    Closer closer = Closer.create();
    for (Limiter limiter : this.underlyingLimiters) {
        Closeable permit = limiter.acquirePermits(permits);
        if (permit == null) {
            try {
                closer.close();/*  ww w  .  j  ava 2s . co m*/
            } catch (IOException ioe) {
                throw new RuntimeException("Could not return intermediate permits.");
            }
            return null;
        }
        closer.register(permit);
    }
    return closer;
}

From source file:se.sics.datamodel.util.DMKeyFactory.java

public static Key getDataKey(ByteId dbId, ByteId typeId, ByteId objNrId) throws IOException {
    Closer closer = Closer.create();
    try {//w  w w. jav a  2s . co  m
        ByteArrayOutputStream baos = closer.register(new ByteArrayOutputStream());
        DataOutputStream w = closer.register(new DataOutputStream(baos));

        w.write(dbId.getId());
        w.writeByte(dataKF);
        w.write(typeId.getId());
        w.write(objNrId.getId());
        w.flush();

        return new Key(baos.toByteArray());
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:org.gradle.caching.internal.LocalDirectoryBuildCache.java

@Override
public boolean load(final BuildCacheKey key, final BuildCacheEntryReader reader) throws BuildCacheException {
    return persistentCache.useCache("load build cache entry", new Factory<Boolean>() {
        @Override//ww  w.ja va2 s . co  m
        public Boolean create() {
            File file = getFile(key.getHashCode());
            if (file.isFile()) {
                try {
                    Closer closer = Closer.create();
                    FileInputStream stream = closer.register(new FileInputStream(file));
                    try {
                        reader.readFrom(stream);
                        return true;
                    } finally {
                        closer.close();
                    }
                } catch (IOException ex) {
                    throw new UncheckedIOException(ex);
                }
            }
            return false;
        }
    });
}

From source file:net.myrrix.batch.common.iterator.sequencefile.SequenceFileDirIterator.java

/**
 * Constructor that uses either {@link FileSystem#listStatus(Path)} or
 * {@link FileSystem#globStatus(Path)} to obtain list of files to iterate over
 * (depending on pathType parameter).//from w  ww .j av  a 2s  .c  o  m
 */
public SequenceFileDirIterator(Path path, PathType pathType, PathFilter filter, Comparator<FileStatus> ordering,
        final boolean reuseKeyValueInstances, final Configuration conf) throws IOException {

    FileStatus[] statuses;
    FileSystem fs = path.getFileSystem(conf);
    if (filter == null) {
        statuses = pathType == PathType.GLOB ? fs.globStatus(path) : fs.listStatus(path);
    } else {
        statuses = pathType == PathType.GLOB ? fs.globStatus(path, filter) : fs.listStatus(path, filter);
    }

    if (statuses == null) {
        statuses = NO_STATUSES;
    } else {
        if (ordering == null) {
            // If order does not matter, use a random order
            Collections.shuffle(Arrays.asList(statuses));
        } else {
            Arrays.sort(statuses, ordering);
        }
    }

    closer = Closer.create();

    Iterator<Iterator<Pair<K, V>>> fsIterators = Iterators.transform(Iterators.forArray(statuses),
            new Function<FileStatus, Iterator<Pair<K, V>>>() {
                @Override
                public Iterator<Pair<K, V>> apply(FileStatus from) {
                    try {
                        SequenceFileIterator<K, V> iterator = new SequenceFileIterator<K, V>(from.getPath(),
                                reuseKeyValueInstances, conf);
                        closer.register(iterator);
                        return iterator;
                    } catch (IOException ioe) {
                        throw new IllegalStateException(from.getPath().toString(), ioe);
                    }
                }
            });

    delegate = Iterators.concat(fsIterators);
}