List of usage examples for com.google.common.io Closer create
public static Closer create()
From source file:org.apache.gobblin.writer.FineGrainedWatermarkTracker.java
public FineGrainedWatermarkTracker(Config config) { _watermarksMap = new HashMap<>(); _sweepIntervalMillis = ConfigUtils.getLong(config, WATERMARK_TRACKER_SWEEP_INTERVAL_MS, WATERMARK_TRACKER_SWEEP_INTERVAL_MS_DEFAULT); _stabilityCheckIntervalMillis = ConfigUtils.getLong(config, WATERMARK_TRACKER_STABILITY_CHECK_INTERVAL_MS, WATERMARK_TRACKER_STABILITY_CHECK_INTERVAL_MS_DEFAULT); _watermarkLagThreshold = ConfigUtils.getLong(config, WATERMARK_TRACKER_LAG_THRESHOLD, WATERMARK_TRACKER_LAG_THRESHOLD_DEFAULT); _instrumentationEnabled = GobblinMetrics.isEnabled(config); _closer = Closer.create(); _metricContext = _closer//from w w w . ja v a2s . c o m .register(Instrumented.getMetricContext(ConfigUtils.configToState(config), this.getClass())); regenerateMetrics(); _started = new AtomicBoolean(false); _abort = new AtomicBoolean(false); _sweeper = new Runnable() { @Override public void run() { sweep(); } }; _stabilityChecker = new Runnable() { @Override public void run() { checkStability(); } }; }
From source file:com.metamx.druid.indexing.coordinator.ForkingTaskRunner.java
@Override public ListenableFuture<TaskStatus> run(final Task task) { synchronized (tasks) { if (!tasks.containsKey(task.getId())) { tasks.put(task.getId(), new ForkingTaskRunnerWorkItem(task, exec.submit(new Callable<TaskStatus>() { @Override/*w w w . ja v a 2 s . c o m*/ public TaskStatus call() { final String attemptUUID = UUID.randomUUID().toString(); final File taskDir = new File(config.getBaseTaskDir(), task.getId()); final File attemptDir = new File(taskDir, attemptUUID); final ProcessHolder processHolder; try { final Closer closer = Closer.create(); try { if (!attemptDir.mkdirs()) { throw new IOException( String.format("Could not create directories: %s", attemptDir)); } final File taskFile = new File(attemptDir, "task.json"); final File statusFile = new File(attemptDir, "status.json"); final File logFile = new File(attemptDir, "log"); // time to adjust process holders synchronized (tasks) { final ForkingTaskRunnerWorkItem taskWorkItem = tasks.get(task.getId()); if (taskWorkItem.shutdown) { throw new IllegalStateException("Task has been shut down!"); } if (taskWorkItem == null) { throw new ISE("WTF?! TaskInfo disappeared for task: %s", task.getId()); } if (taskWorkItem.processHolder != null) { throw new ISE("WTF?! TaskInfo already has a process holder for task: %s", task.getId()); } final List<String> command = Lists.newArrayList(); final int childPort = findUnusedPort(); final String childHost = String.format(config.getHostPattern(), childPort); command.add(config.getJavaCommand()); command.add("-cp"); command.add(config.getJavaClasspath()); Iterables.addAll(command, Splitter.on(CharMatcher.WHITESPACE).omitEmptyStrings() .split(config.getJavaOptions())); for (String propName : props.stringPropertyNames()) { for (String allowedPrefix : config.getAllowedPrefixes()) { if (propName.startsWith(allowedPrefix)) { command.add(String.format("-D%s=%s", propName, props.getProperty(propName))); } } } // Override child JVM specific properties for (String propName : props.stringPropertyNames()) { if (propName.startsWith(CHILD_PROPERTY_PREFIX)) { command.add(String.format("-D%s=%s", propName.substring(CHILD_PROPERTY_PREFIX.length()), props.getProperty(propName))); } } String nodeType = task.getNodeType(); if (nodeType != null) { command.add(String.format("-Ddruid.executor.nodeType=%s", nodeType)); } command.add(String.format("-Ddruid.host=%s", childHost)); command.add(String.format("-Ddruid.port=%d", childPort)); command.add(config.getMainClass()); command.add(taskFile.toString()); command.add(statusFile.toString()); jsonMapper.writeValue(taskFile, task); log.info("Running command: %s", Joiner.on(" ").join(command)); taskWorkItem.processHolder = new ProcessHolder( new ProcessBuilder(ImmutableList.copyOf(command)) .redirectErrorStream(true).start(), logFile, childPort); processHolder = taskWorkItem.processHolder; processHolder.registerWithCloser(closer); } log.info("Logging task %s output to: %s", task.getId(), logFile); final InputStream fromProc = processHolder.process.getInputStream(); final OutputStream toLogfile = closer .register(Files.newOutputStreamSupplier(logFile).getOutput()); boolean runFailed = true; ByteStreams.copy(fromProc, toLogfile); final int statusCode = processHolder.process.waitFor(); log.info("Process exited with status[%d] for task: %s", statusCode, task.getId()); if (statusCode == 0) { runFailed = false; } // Upload task logs // XXX: Consider uploading periodically for very long-lived tasks to prevent // XXX: bottlenecks at the end or the possibility of losing a lot of logs all // XXX: at once. taskLogPusher.pushTaskLog(task.getId(), logFile); if (!runFailed) { // Process exited successfully return jsonMapper.readValue(statusFile, TaskStatus.class); } else { // Process exited unsuccessfully return TaskStatus.failure(task.getId()); } } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } } catch (Exception e) { log.info(e, "Exception caught during execution"); throw Throwables.propagate(e); } finally { try { synchronized (tasks) { final ForkingTaskRunnerWorkItem taskWorkItem = tasks.remove(task.getId()); if (taskWorkItem != null && taskWorkItem.processHolder != null) { taskWorkItem.processHolder.process.destroy(); } } log.info("Removing temporary directory: %s", attemptDir); FileUtils.deleteDirectory(attemptDir); } catch (Exception e) { log.error(e, "Suppressing exception caught while cleaning up task"); } } } }))); } return tasks.get(task.getId()).getResult(); } }
From source file:gobblin.filesystem.InstrumentedHDFSFileSystem.java
public InstrumentedHDFSFileSystem() { this.closer = Closer.create(); this.metricContext = new MetricContext.Builder(HDFS_METRIC_CONTEXT_NAME).build(); this.metricContext = this.closer.register(metricContext); this.listStatusTimer = this.metricContext.timer("listStatus"); this.listFilesTimer = this.metricContext.timer("listFiles"); this.globStatusTimer = this.metricContext.timer("globStatus"); this.mkdirTimer = this.metricContext.timer("mkdirs"); this.renameTimer = this.metricContext.timer("rename"); this.deleteTimer = this.metricContext.timer("delete"); this.createTimer = this.metricContext.timer("create"); this.openTimer = this.metricContext.timer("open"); this.setOwnerTimer = this.metricContext.timer("setOwner"); this.getFileStatusTimer = this.metricContext.timer("getFileStatus"); this.setPermissionTimer = this.metricContext.timer("setPermission"); this.setTimesTimer = this.metricContext.timer("setTimes"); this.appendTimer = this.metricContext.timer("append"); this.concatTimer = this.metricContext.timer("concat"); }
From source file:com.android.build.gradle.internal.transforms.MultiStreamJarTransform.java
private static void processFolder(@NonNull JarOutputStream jos, @NonNull String path, @NonNull File folder, @NonNull byte[] buffer) throws IOException { File[] files = folder.listFiles(); if (files != null) { for (File file : files) { if (file.isFile()) { if (file.getName().endsWith(DOT_CLASS)) { // new entry jos.putNextEntry(new JarEntry(path + file.getName())); // put the file content Closer closer = Closer.create(); try { FileInputStream fis = closer.register(new FileInputStream(file)); int count; while ((count = fis.read(buffer)) != -1) { jos.write(buffer, 0, count); }//from w w w .ja v a 2s . co m } finally { closer.close(); } // close the entry jos.closeEntry(); } } else if (file.isDirectory()) { processFolder(jos, path + file.getName() + "/", file, buffer); } } } }
From source file:com.android.builder.files.IncrementalRelativeFileSets.java
/** * Computes the incremental file set that results from comparing a zip file with a possibly * existing cached file. If the cached file does not exist, then the whole zip is reported * as {@link FileStatus#NEW}.//from w w w .j av a2s. c o m * * @param zip the zip file to read, must be a valid, existing zip file * @return the file set * @throws IOException failed to read the zip file */ @NonNull public static ImmutableMap<RelativeFile, FileStatus> fromZip(@NonNull File zip, @NonNull FileCacheByPath cache) throws IOException { Preconditions.checkArgument(zip.isFile(), "!zip.isFile()"); File oldFile = cache.get(zip); if (oldFile == null) { return fromZip(zip, FileStatus.NEW); } Map<RelativeFile, FileStatus> result = Maps.newHashMap(); Closer closer = Closer.create(); try { ZFile newZipReader = closer.register(new ZFile(zip)); ZFile oldZipReader = closer.register(new ZFile(oldFile)); /* * Search for new and modified files. */ for (StoredEntry entry : newZipReader.entries()) { String path = entry.getCentralDirectoryHeader().getName(); if (entry.getType() == StoredEntryType.FILE) { File file = new File(zip, FileUtils.toSystemDependentPath(path)); RelativeFile newRelative = new RelativeFile(zip, file); StoredEntry oldEntry = oldZipReader.get(path); if (oldEntry == null || oldEntry.getType() != StoredEntryType.FILE) { result.put(newRelative, FileStatus.NEW); continue; } if (oldEntry.getCentralDirectoryHeader().getCrc32() != entry.getCentralDirectoryHeader() .getCrc32() || oldEntry.getCentralDirectoryHeader().getUncompressedSize() != entry .getCentralDirectoryHeader().getUncompressedSize()) { result.put(newRelative, FileStatus.CHANGED); } /* * If we get here, then the file exists in both unmodified. */ } } for (StoredEntry entry : oldZipReader.entries()) { String path = entry.getCentralDirectoryHeader().getName(); File file = new File(zip, FileUtils.toSystemDependentPath(path)); RelativeFile oldRelative = new RelativeFile(zip, file); StoredEntry newEntry = newZipReader.get(path); if (newEntry == null || newEntry.getType() != StoredEntryType.FILE) { /* * File does not exist in new. It has been deleted. */ result.put(oldRelative, FileStatus.REMOVED); } } } catch (Throwable t) { throw closer.rethrow(t, IOException.class); } finally { closer.close(); } return ImmutableMap.copyOf(result); }
From source file:org.apache.gobblin.runtime.JobLauncherTestHelper.java
public void runTestWithPullLimit(Properties jobProps, long limit) throws Exception { String jobName = jobProps.getProperty(ConfigurationKeys.JOB_NAME_KEY); String jobId = JobLauncherUtils.newJobId(jobName).toString(); jobProps.setProperty(ConfigurationKeys.JOB_ID_KEY, jobId); Closer closer = Closer.create(); try {/*from ww w .j a v a2 s . co m*/ JobLauncher jobLauncher = closer .register(JobLauncherFactory.newJobLauncher(this.launcherProps, jobProps)); jobLauncher.launchJob(null); } finally { closer.close(); } List<JobState.DatasetState> datasetStateList = this.datasetStateStore.getAll(jobName, sanitizeJobNameForDatasetStore(jobId) + ".jst"); DatasetState datasetState = datasetStateList.get(0); Assert.assertEquals(datasetState.getState(), JobState.RunningState.COMMITTED); Assert.assertEquals(datasetState.getCompletedTasks(), 4); Assert.assertEquals(datasetState.getJobFailures(), 0); for (TaskState taskState : datasetState.getTaskStates()) { Assert.assertEquals(taskState.getWorkingState(), WorkUnitState.WorkingState.COMMITTED); Assert.assertEquals(taskState.getPropAsLong(ConfigurationKeys.EXTRACTOR_ROWS_EXTRACTED), limit); Assert.assertEquals(taskState.getPropAsLong(ConfigurationKeys.WRITER_ROWS_WRITTEN), limit); } }
From source file:net.derquinse.common.util.zip.ZipFileLoader.java
/** * Loads a zip file into memory./* ww w . j av a 2 s. com*/ * @param input Input data. * @return The loaded zip file. * @throws IOException if an I/O error occurs * @throws MaximumSizeExceededException if any of the entries exceeds the maximum size. */ public LoadedZipFile load(ByteSource input) throws IOException { checkInput(input); Closer closer = Closer.create(); try { return load(closer.register(input.openStream())); } finally { closer.close(); } }
From source file:org.apache.gobblin.password.PasswordManager.java
private List<TextEncryptor> getEncryptors(CachedInstanceKey cacheKey) { List<TextEncryptor> encryptors = new ArrayList<>(); int numOfEncryptionKeys = cacheKey.numOfEncryptionKeys; String suffix = ""; int i = 1;//from www . j av a 2 s. c o m if (cacheKey.masterPasswordFile == null || numOfEncryptionKeys < 1) { return encryptors; } Exception exception = null; do { Path currentMasterPasswordFile = new Path(cacheKey.masterPasswordFile + suffix); try (Closer closer = Closer.create()) { if (!fs.exists(currentMasterPasswordFile) || fs.getFileStatus(currentMasterPasswordFile).isDirectory()) { continue; } InputStream in = closer.register(fs.open(currentMasterPasswordFile)); String masterPassword = new LineReader(new InputStreamReader(in, Charsets.UTF_8)).readLine(); TextEncryptor encryptor = useStrongEncryptor ? new StrongTextEncryptor() : new BasicTextEncryptor(); // setPassword() needs to be called via reflection since the TextEncryptor interface doesn't have this method. encryptor.getClass().getMethod("setPassword", String.class).invoke(encryptor, masterPassword); encryptors.add(encryptor); suffix = "." + String.valueOf(i); } catch (FileNotFoundException fnf) { // It is ok for password files not being present LOG.warn("Master password file " + currentMasterPasswordFile + " not found."); } catch (IOException ioe) { exception = ioe; LOG.warn("Master password could not be read from file " + currentMasterPasswordFile); } catch (Exception e) { LOG.warn("Encryptor could not be instantiated."); } } while (i++ < numOfEncryptionKeys); // Throw exception if could not read any existing password file if (encryptors.size() < 1 && exception != null) { throw new RuntimeException("Master Password could not be read from any master password file.", exception); } return encryptors; }
From source file:com.j2swift.util.ProGuardUsageParser.java
public static DeadCodeMap parse(CharSource listing) throws IOException { LineProcessor<DeadCodeMap> processor = new LineProcessor<DeadCodeMap>() { DeadCodeMap.Builder dead = DeadCodeMap.builder(); String lastClass;// w ww. j av a2 s .c om @Override public DeadCodeMap getResult() { return dead.build(); } private void handleClass(String line) { if (line.endsWith(":")) { // Class, but not completely dead; save to read its dead methods lastClass = line.substring(0, line.length() - 1); } else { dead.addDeadClass(line); } } private void handleMethod(String line) throws IOException { Matcher methodMatcher = proGuardMethodPattern.matcher(line); if (!methodMatcher.matches()) { throw new AssertionError("Line doesn't match expected ProGuard format!"); } if (lastClass == null) { throw new IOException("Bad listing format: method not attached to a class"); } String returnType = methodMatcher.group(5); String methodName = methodMatcher.group(6); String arguments = methodMatcher.group(7); String signature = buildMethodSignature(returnType, arguments); dead.addDeadMethod(lastClass, methodName, signature); } private void handleField(String line) throws IOException { String name = line.substring(line.lastIndexOf(" ") + 1); dead.addDeadField(lastClass, name); } @Override public boolean processLine(String line) throws IOException { if (line.startsWith("ProGuard, version") || line.startsWith("Reading ")) { // ignore output header } else if (!line.startsWith(" ")) { handleClass(line); } else if (line.startsWith(" ") && !line.contains("(")) { handleField(line); } else { handleMethod(line); } return true; } }; // TODO(cgdecker): Just use listing.readLines(processor) once guava_jdk5 is upgraded to a newer // version. Closer closer = Closer.create(); try { BufferedReader reader = closer.register(listing.openBufferedStream()); String line; while ((line = reader.readLine()) != null) { processor.processLine(line); } return processor.getResult(); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:org.basepom.mojo.propertyhelper.ValueCache.java
@VisibleForTesting Optional<Map<String, String>> getValues(final AbstractDefinition<?> definition) throws IOException { final Optional<File> definitionFile = definition.getPropertyFile(); // Ephemeral, so return null. if (!definitionFile.isPresent()) { return Optional.absent(); }/*from w ww . ja v a2s .c o m*/ ValueCacheEntry cacheEntry; final File canonicalFile = definitionFile.get().getCanonicalFile(); // Throws an exception if the file must exist and does not. final boolean createFile = IgnoreWarnFailCreate.checkState(definition.getOnMissingFile(), canonicalFile.exists(), definitionFile.get().getCanonicalPath()); cacheEntry = valueFiles.get(canonicalFile); if (cacheEntry != null) { // If there is a cache hit, something either has loaded the file // or another property has already put in a creation order. // Make sure that if this number has a creation order it is obeyed. if (createFile) { cacheEntry.doCreate(); } } else { // Try loading or creating properties. final Properties props = new Properties(); if (!canonicalFile.exists()) { cacheEntry = new ValueCacheEntry(props, false, createFile); // does not exist valueFiles.put(canonicalFile, cacheEntry); } else { if (canonicalFile.isFile() && canonicalFile.canRead()) { final Closer closer = Closer.create(); try { final InputStream stream = closer.register(new FileInputStream(canonicalFile)); props.load(stream); cacheEntry = new ValueCacheEntry(props, true, createFile); valueFiles.put(canonicalFile, cacheEntry); } finally { closer.close(); } } else { throw new IllegalStateException( format("Can not load %s, not a file!", definitionFile.get().getCanonicalPath())); } } } return Optional.of(cacheEntry.getValues()); }