List of usage examples for com.google.common.io Closer create
public static Closer create()
From source file:com.github.sdorra.buildfrontend.AbstractNodeMojo.java
/** * Method description/* w w w . ja v a 2 s . com*/ * * * @param urlString * @param target * * @throws IOException */ private static void download(String urlString, File target) throws IOException { URL url = new URL(urlString); URLConnection connection = url.openConnection(); Closer closer = Closer.create(); try { InputStream input = closer.register(connection.getInputStream()); OutputStream output = closer.register(new FileOutputStream(target)); ByteStreams.copy(input, output); } catch (IOException ex) { throw closer.rethrow(ex); } finally { closer.close(); } }
From source file:org.gradle.caching.internal.LocalDirectoryBuildCacheService.java
@Override public boolean load(final BuildCacheKey key, final BuildCacheEntryReader reader) throws BuildCacheException { return persistentCache.useCache(new Factory<Boolean>() { @Override/*from w w w . j a v a 2 s . c om*/ 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.derquinse.common.io.BytesTransformer.java
/** Transforms a byte source. */ public void transform(ByteSource input, OutputStream output) throws IOException { checkInput(input);//from w ww . j a va 2 s .c o m checkOutput(output); final Closer closer = Closer.create(); try { transform(closer.register(input.openStream()), output); } finally { closer.close(); } }
From source file:org.apache.blur.manager.indexserver.LocalIndexServer.java
public LocalIndexServer(TableDescriptor tableDescriptor, boolean ramDir) throws IOException { _timer = new Timer("Index Importer", true); _bulkTimer = new Timer("Bulk Indexing", true); _closer = Closer.create(); _tableContext = TableContext.create(tableDescriptor); _mergeScheduler = _closer.register(new SharedMergeScheduler(3, 128 * 1000 * 1000)); _searchExecutor = Executors.newCachedThreadPool(); _closer.register(new CloseableExecutorService(_searchExecutor)); _ramDir = ramDir;//from www . j a va 2 s . co m _indexCloser = _closer.register(new BlurIndexCloser()); _closer.register(new Closeable() { @Override public void close() throws IOException { _timer.cancel(); _timer.purge(); } }); getIndexes(_tableContext.getTable()); }
From source file:org.apache.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 && hiveCopyEntityHelper .getExistingEntityPolicy() != HiveCopyEntityHelper.ExistingEntityPolicy.REPLACE_TABLE_AND_PARTITIONS) { log.error("Source and target partitions are not compatible. Aborting copy of partition " + this.partition, ioe); return Lists.newArrayList(); }//from ww w. ja v a 2 s . c om 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))) { CopyableFile fileEntity = builder.fileSet(fileSet).checksum(new byte[0]) .datasetOutputPath(desiredTargetLocation.location.toString()).build(); this.hiveCopyEntityHelper.setCopyableFileDatasets(fileEntity); copyEntities.add(fileEntity); } log.info("Created {} copy entities for partition {}", copyEntities.size(), this.partition.getCompleteName()); return copyEntities; } }
From source file:com.github.fge.jsonschema.core.load.URIManager.java
/** * Get the content at a given URI as a {@link JsonNode} * * @param uri the URI/*from w w w. j a va 2 s . co m*/ * @return the content * @throws NullPointerException provided URI is null * @throws ProcessingException scheme is not registered, failed to get * content, or content is not JSON */ public JsonNode getContent(final URI uri) throws ProcessingException { BUNDLE.checkNotNull(uri, "jsonRef.nullURI"); if (!uri.isAbsolute()) throw new ProcessingException(new ProcessingMessage() .setMessage(BUNDLE.getMessage("refProcessing.uriNotAbsolute")).put("uri", uri)); final String scheme = uri.getScheme(); final URIDownloader downloader = downloaders.get(scheme); if (downloader == null) throw new ProcessingException( new ProcessingMessage().setMessage(BUNDLE.getMessage("refProcessing.unhandledScheme")) .putArgument("scheme", scheme).putArgument("uri", uri)); final Closer closer = Closer.create(); final InputStream in; try { in = closer.register(downloader.fetch(uri)); return reader.fromInputStream(in); } catch (JsonMappingException e) { throw new ProcessingException( new ProcessingMessage().setMessage(e.getOriginalMessage()).put("uri", uri)); } catch (JsonParseException e) { throw new ProcessingException( new ProcessingMessage().setMessage(BUNDLE.getMessage("uriManager.uriNotJson")) .putArgument("uri", uri).put("parsingMessage", e.getOriginalMessage())); } catch (IOException e) { throw new ProcessingException( new ProcessingMessage().setMessage(BUNDLE.getMessage("uriManager.uriIOError")) .putArgument("uri", uri).put("exceptionMessage", e.getMessage())); } finally { try { closer.close(); } catch (IOException ignored) { throw new IllegalStateException(); } } }
From source file:io.prestosql.memory.context.MemoryTrackingContext.java
public void close() { try (Closer closer = Closer.create()) { closer.register(userAggregateMemoryContext::close); closer.register(revocableAggregateMemoryContext::close); closer.register(systemAggregateMemoryContext::close); closer.register(userLocalMemoryContext::close); closer.register(revocableLocalMemoryContext::close); closer.register(systemLocalMemoryContext::close); } catch (IOException e) { throw new RuntimeException("Exception closing memory tracking context", e); }//from w w w . java2 s. co m }
From source file:io.macgyver.core.crypto.KeyStoreManager.java
public void createKeyStoreIfNotPresent() { Closer closer = Closer.create(); try {//from w w w . j a v a 2 s . c o m File keyStoreLocation = getKeyStoreLocation(); if (!keyStoreLocation.exists()) { if (!keyStoreLocation.getParentFile().exists()) { keyStoreLocation.getParentFile().mkdirs(); } KeyStore ks = KeyStore.getInstance("JCEKS"); ks.load(null, getKeyStorePassword()); String keyAlias = "mac0"; ks.setKeyEntry(keyAlias, createAESSecretKey(), getPasswordForKey(keyAlias), null); OutputStream out = new FileOutputStream(keyStoreLocation); closer.register(out); ks.store(out, getKeyStorePassword()); } } catch (GeneralSecurityException e) { throw new ConfigurationException(e); } catch (IOException e) { throw new ConfigurationException(e); } finally { try { closer.close(); } catch (Exception IGNORE) { } } }
From source file:org.parboiled.transform.ClassNodeInitializer.java
public void process(final ParserClassNode classNode) throws IOException { this.classNode = Objects.requireNonNull(classNode, "classNode"); // walk up the parser parent class chain ownerClass = classNode.getParentClass(); Closer closer;//from w w w.ja v a 2 s. c o m ClassReader reader; InputStream in; while (!Object.class.equals(ownerClass)) { annotations.removeAll(CLASS_FLAGS_CLEAR); closer = Closer.create(); try { in = getInputStream(ownerClass); if (in == null) throw new IOException(ownerClass + " not found"); reader = new ClassReader(closer.register(in)); reader.accept(this, ClassReader.SKIP_FRAMES); } finally { closer.close(); } ownerClass = ownerClass.getSuperclass(); } for (final RuleMethod method : classNode.getRuleMethods().values()) { // move all flags from the super methods to their overriding methods if (!method.isSuperMethod()) continue; final String overridingMethodName = method.name.substring(1) + method.desc; final RuleMethod overridingMethod = classNode.getRuleMethods().get(overridingMethodName); method.moveFlagsTo(overridingMethod); } }
From source file:alluxio.network.thrift.SocketTrackingTServerSocket.java
/** * Closes all socket connections that have been accepted by this server socket. *//*from w ww . j a va 2s . co m*/ private void closeClientSockets() throws IOException { // Possible since super constructor can call close(). if (mSockets == null) { return; } Closer closer = Closer.create(); int count = 0; for (Socket s : mSockets) { if (!s.isClosed()) { closer.register(s); count++; } } closer.close(); LOG.info("Closed {} client sockets", count); }