List of usage examples for com.google.common.io Closer create
public static Closer create()
From source file:gobblin.metrics.InnerMetricContext.java
protected InnerMetricContext(MetricContext context, String name, MetricContext parent, List<Tag<?>> tags) throws NameConflictException, ExecutionException { this.name = name; this.closer = Closer.create(); this.parent = Optional.fromNullable(parent); if (this.parent.isPresent()) { this.parent.get().addChildContext(this.name, context); this.metricContext = new ContextWeakReference(context, this); } else {// ww w .j av a2s . c o m this.metricContext = new WeakReference<>(context); } this.tagged = new Tagged(tags); this.tagged.addTag(new Tag<>(MetricContext.METRIC_CONTEXT_ID_TAG_NAME, UUID.randomUUID().toString())); this.tagged.addTag(new Tag<>(MetricContext.METRIC_CONTEXT_NAME_TAG_NAME, name)); }
From source file:alluxio.cli.fs.command.LoadCommand.java
/** * Loads a file or directory in Alluxio space, makes it resident in Alluxio. * * @param filePath The {@link AlluxioURI} path to load into Alluxio * @param local whether to load data to local worker even when the data is already loaded remotely *//*from w w w .j av a 2 s.c o m*/ private void load(AlluxioURI filePath, boolean local) throws AlluxioException, IOException { URIStatus status = mFileSystem.getStatus(filePath); if (status.isFolder()) { List<URIStatus> statuses = mFileSystem.listStatus(filePath); for (URIStatus uriStatus : statuses) { AlluxioURI newPath = new AlluxioURI(uriStatus.getPath()); load(newPath, local); } } else { OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.CACHE_PROMOTE); if (local) { if (!FileSystemContext.INSTANCE.hasLocalWorker()) { System.out .println("When local option is specified," + " there must be a local worker available"); return; } options.setCacheLocationPolicy(new LocalFirstPolicy()); } else if (status.getInAlluxioPercentage() == 100) { // The file has already been fully loaded into Alluxio. System.out.println(filePath + " already in Alluxio fully"); return; } Closer closer = Closer.create(); try { FileInStream in = closer.register(mFileSystem.openFile(filePath, options)); byte[] buf = new byte[8 * Constants.MB]; while (in.read(buf) != -1) { } } catch (Exception e) { throw closer.rethrow(e); } finally { closer.close(); } } System.out.println(filePath + " loaded"); }
From source file:org.apache.gobblin.data.management.retention.DatasetCleaner.java
public DatasetCleaner(FileSystem fs, Properties props) throws IOException { this.closer = Closer.create(); try {//w w w .j a v a 2 s .c om FileSystem optionalRateControlledFs = fs; if (props.contains(DATASET_CLEAN_HDFS_CALLS_PER_SECOND_LIMIT)) { optionalRateControlledFs = this.closer.register(new RateControlledFileSystem(fs, Long.parseLong(props.getProperty(DATASET_CLEAN_HDFS_CALLS_PER_SECOND_LIMIT)))); ((RateControlledFileSystem) optionalRateControlledFs).startRateControl(); } this.datasetFinder = new MultiCleanableDatasetFinder(optionalRateControlledFs, props); } catch (NumberFormatException exception) { throw new IOException(exception); } catch (ExecutionException exception) { throw new IOException(exception); } ExecutorService executor = ScalingThreadPoolExecutor.newScalingThreadPool(0, Integer.parseInt(props.getProperty(MAX_CONCURRENT_DATASETS_CLEANED, DEFAULT_MAX_CONCURRENT_DATASETS_CLEANED)), 100, ExecutorsUtils.newThreadFactory(Optional.of(LOG), Optional.of("Dataset-cleaner-pool-%d"))); this.service = ExecutorsUtils.loggingDecorator(executor); List<Tag<?>> tags = Lists.newArrayList(); tags.addAll(Tag.fromMap(AzkabanTags.getAzkabanTags())); // TODO -- Remove the dependency on gobblin-core after new Gobblin Metrics does not depend on gobblin-core. this.metricContext = this.closer .register(Instrumented.getMetricContext(new State(props), DatasetCleaner.class, tags)); this.isMetricEnabled = GobblinMetrics.isEnabled(props); this.eventSubmitter = new EventSubmitter.Builder(this.metricContext, RetentionEvents.NAMESPACE).build(); this.throwables = Lists.newArrayList(); }
From source file:org.apache.gobblin.runtime.mapreduce.GobblinOutputCommitter.java
@Override public void abortJob(JobContext jobContext, JobStatus.State state) throws IOException { LOG.info("Aborting Job: " + jobContext.getJobID() + " with state: " + state); Configuration conf = jobContext.getConfiguration(); URI fsUri = URI.create(conf.get(ConfigurationKeys.FS_URI_KEY, ConfigurationKeys.LOCAL_FS_URI)); FileSystem fs = FileSystem.get(fsUri, conf); Path mrJobDir = new Path(conf.get(ConfigurationKeys.MR_JOB_ROOT_DIR_KEY), conf.get(ConfigurationKeys.JOB_NAME_KEY)); Path jobInputDir = new Path(mrJobDir, MRJobLauncher.INPUT_DIR_NAME); if (!fs.exists(jobInputDir) || !fs.isDirectory(jobInputDir)) { LOG.warn(String.format("%s either does not exist or is not a directory. No data to cleanup.", jobInputDir));/* w ww .j a v a 2 s . c o m*/ return; } // Iterate through all files in the jobInputDir, each file should correspond to a serialized wu or mwu try { for (FileStatus status : fs.listStatus(jobInputDir, new WorkUnitFilter())) { Closer workUnitFileCloser = Closer.create(); // If the file ends with ".wu" de-serialize it into a WorkUnit if (status.getPath().getName().endsWith(AbstractJobLauncher.WORK_UNIT_FILE_EXTENSION)) { WorkUnit wu = WorkUnit.createEmpty(); try { wu.readFields(workUnitFileCloser.register(new DataInputStream(fs.open(status.getPath())))); } finally { workUnitFileCloser.close(); } JobLauncherUtils.cleanTaskStagingData(new WorkUnitState(wu), LOG); } // If the file ends with ".mwu" de-serialize it into a MultiWorkUnit if (status.getPath().getName().endsWith(AbstractJobLauncher.MULTI_WORK_UNIT_FILE_EXTENSION)) { MultiWorkUnit mwu = MultiWorkUnit.createEmpty(); try { mwu.readFields(workUnitFileCloser.register(new DataInputStream(fs.open(status.getPath())))); } finally { workUnitFileCloser.close(); } for (WorkUnit wu : mwu.getWorkUnits()) { JobLauncherUtils.cleanTaskStagingData(new WorkUnitState(wu), LOG); } } } } finally { try { cleanUpWorkingDirectory(mrJobDir, fs); } finally { super.abortJob(jobContext, state); } } }
From source file:gobblin.source.extractor.extract.google.GoogleDriveFileSystem.java
public GoogleDriveFileSystem() { super(); this.closer = Closer.create(); }
From source file:org.jclouds.vsphere.functions.VirtualMachineToNodeMetadata.java
@Override public NodeMetadata apply(VirtualMachine vm) { Closer closer = Closer.create(); VSphereServiceInstance instance = serviceInstanceSupplier.get(); closer.register(instance);/*from www . j a v a 2 s. c om*/ VirtualMachine freshVm = null; String virtualMachineName = ""; NodeMetadataBuilder nodeMetadataBuilder = new NodeMetadataBuilder(); try { try { String vmMORId = vm.getMOR().get_value(); ManagedEntity[] vms = new InventoryNavigator(instance.getInstance().getRootFolder()) .searchManagedEntities("VirtualMachine"); for (ManagedEntity machine : vms) { if (machine.getMOR().getVal().equals(vmMORId)) { freshVm = (VirtualMachine) machine; break; } } LocationBuilder locationBuilder = new LocationBuilder(); locationBuilder.description(""); locationBuilder.id(""); locationBuilder.scope(LocationScope.HOST); virtualMachineName = freshVm.getName(); VirtualMachinePowerState vmState = freshVm.getRuntime().getPowerState(); NodeMetadata.Status nodeState = toPortableNodeStatus.get(vmState); if (nodeState == null) nodeState = Status.UNRECOGNIZED; nodeMetadataBuilder.name(virtualMachineName).ids(virtualMachineName) .location(locationBuilder.build()).hostname(virtualMachineName); String host = freshVm.getServerConnection().getUrl().getHost(); try { nodeMetadataBuilder.uri(new URI( "https://" + host + ":9443/vsphere-client/vmrc/vmrc.jsp?vm=urn:vmomi:VirtualMachine:" + vmMORId + ":" + freshVm.getSummary().getConfig().getUuid())); } catch (URISyntaxException e) { } Set<String> ipv4Addresses = newHashSet(); Set<String> ipv6Addresses = newHashSet(); if (nodeState == Status.RUNNING && !freshVm.getConfig().isTemplate()) { Predicates2.retry(new Predicate<VirtualMachine>() { @Override public boolean apply(VirtualMachine vm) { try { return !Strings.isNullOrEmpty(vm.getGuest().getIpAddress()); } catch (Exception e) { return false; } } }, 60 * 1000 * 10, 10 * 1000, TimeUnit.MILLISECONDS).apply(freshVm); } if (VSpherePredicate.IsToolsStatusEquals(VirtualMachineToolsStatus.toolsNotInstalled) .apply(freshVm)) logger.trace("<< No VMware tools installed ( " + virtualMachineName + " )"); else if (nodeState == Status.RUNNING && not(VSpherePredicate.isTemplatePredicate).apply(freshVm)) { GuestNicInfo[] nics = freshVm.getGuest().getNet(); if (null != nics) { for (GuestNicInfo nic : nics) { String[] addresses = nic.getIpAddress(); if (null != addresses) { for (String address : addresses) { if (logger.isTraceEnabled()) logger.trace( "<< find IP addresses " + address + " for " + virtualMachineName); if (isInet4Address.apply(address)) { ipv4Addresses.add(address); } else if (isInet6Address.apply(address)) { ipv6Addresses.add(address); } } } } } nodeMetadataBuilder.publicAddresses(filter(ipv4Addresses, not(isPrivateAddress))); nodeMetadataBuilder.privateAddresses(filter(ipv4Addresses, isPrivateAddress)); } CustomFieldValue[] customFieldValues = freshVm.getCustomValue(); if (customFieldValues != null) { for (CustomFieldValue customFieldValue : customFieldValues) { if (customFieldValue.getKey() == customFields.get().get(VSphereConstants.JCLOUDS_TAGS) .getKey()) { nodeMetadataBuilder.tags( COMMA_SPLITTER.split(((CustomFieldStringValue) customFieldValue).getValue())); } else if (customFieldValue.getKey() == customFields.get() .get(VSphereConstants.JCLOUDS_GROUP).getKey()) { nodeMetadataBuilder.group(((CustomFieldStringValue) customFieldValue).getValue()); } } } nodeMetadataBuilder.status(nodeState); return nodeMetadataBuilder.build(); } catch (Throwable t) { logger.error("Got an exception for virtual machine name : " + virtualMachineName); Throwables.propagate(closer.rethrow(t)); } finally { closer.close(); } } catch (IOException e) { return nodeMetadataBuilder.build(); } return nodeMetadataBuilder.build(); }
From source file:org.gradle.api.internal.tasks.cache.LocalDirectoryTaskOutputCache.java
@Override public void store(final TaskCacheKey key, final TaskOutputWriter result) throws IOException { persistentCache.useCache("store task output", new Runnable() { @Override//w ww . java2s . c om public void run() { File file = getFile(key.getHashCode()); try { Closer closer = Closer.create(); OutputStream output = closer.register(new FileOutputStream(file)); try { result.writeTo(output); } catch (Throwable ex) { throw closer.rethrow(ex); } finally { closer.close(); } } catch (IOException ex) { throw new UncheckedIOException(ex); } } }); }
From source file:net.ripe.rpki.commons.crypto.x509cert.X509CertificateParser.java
private void parse() { try {/*from w ww . jav a2 s. c om*/ final Closer closer = Closer.create(); try { final InputStream input = closer.register(new ByteArrayInputStream(encoded)); final CertificateFactory factory = CertificateFactory.getInstance("X.509"); certificate = (X509Certificate) factory.generateCertificate(input); } catch (final CertificateException e) { certificate = null; } catch (final Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } } catch (final IOException e) { certificate = null; } result.rejectIfNull(certificate, CERTIFICATE_PARSED); }
From source file:org.apache.gobblin.cluster.GobblinHelixTask.java
@Override public TaskResult run() { log.info("Actual task {} started.", this.taskId); try (Closer closer = Closer.create()) { closer.register(MDC.putCloseable(ConfigurationKeys.JOB_NAME_KEY, this.jobName)); closer.register(MDC.putCloseable(ConfigurationKeys.JOB_KEY_KEY, this.jobKey)); this.task.run(); log.info("Actual task {} finished.", this.taskId); return new TaskResult(TaskResult.Status.COMPLETED, ""); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); return new TaskResult(TaskResult.Status.CANCELED, ""); } catch (Throwable t) { log.error("GobblinHelixTask " + taskId + " failed due to " + t.getMessage(), t); return new TaskResult(TaskResult.Status.FAILED, Throwables.getStackTraceAsString(t)); }// w w w. java 2 s . co m }
From source file:org.glowroot.agent.live.JvmTool.java
private static <T> T runExternalAttach(long pid, String methodName, InputStreamProcessor<T> processor, @Nullable File glowrootJarFile) throws Exception { List<String> command = buildCommand(pid, methodName, glowrootJarFile); ProcessBuilder processBuilder = new ProcessBuilder(command); Process process = processBuilder.start(); Closer closer = Closer.create(); ErrorStreamReader errorStreamReader; T result = null;// ww w . j av a 2s .com Exception processingException = null; try { InputStream in = closer.register(process.getInputStream()); InputStream err = closer.register(process.getErrorStream()); errorStreamReader = new ErrorStreamReader(err); Thread errorStreamReaderThread = new Thread(errorStreamReader); errorStreamReaderThread.setName("Glowroot-JVM-Tool-Error-Stream-Reader"); errorStreamReaderThread.setDaemon(true); errorStreamReaderThread.start(); try { result = processAndClose(in, processor); } catch (Exception e) { processingException = e; } catch (Throwable t) { processingException = new RuntimeException(t); } errorStreamReaderThread.join(); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } int status = process.waitFor(); if (status == UNAVAILABLE_DUE_TO_RUNNING_IN_JRE_STATUS) { throw new UnavailableDueToRunningInJreException(); } else if (status == UNAVAILABLE_PROBABLY_DUE_TO_DOCKER_PID_ONE_STATUS) { throw new UnavailableDueToDockerAlpinePidOneException(); } else if (status != 0) { logger.error("error occurred while trying to run jvm tool:\n{}\n{}", Joiner.on(' ').join(command), errorStreamReader.getOutput().trim()); throw new IllegalStateException("Error occurred while trying to run jvm tool"); } if (result == null) { throw checkNotNull(processingException); } return result; }