List of usage examples for com.google.common.io Closer create
public static Closer create()
From source file:gobblin.metastore.FsStateStore.java
@Override @SuppressWarnings("unchecked") public T get(String storeName, String tableName, String stateId) throws IOException { Path tablePath = new Path(new Path(this.storeRootDir, storeName), tableName); if (!this.fs.exists(tablePath)) { return null; }/*from ww w . j a v a 2s . com*/ Closer closer = Closer.create(); try { @SuppressWarnings("deprecation") SequenceFile.Reader reader = closer.register(new SequenceFile.Reader(this.fs, tablePath, this.conf)); try { Text key = new Text(); T state = this.stateClass.newInstance(); while (reader.next(key)) { state = (T) reader.getCurrentValue(state); if (key.toString().equals(stateId)) { return state; } } } catch (Exception e) { throw new IOException( "failure retrieving state from storeName " + storeName + " tableName " + tableName, e); } } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } return null; }
From source file:org.apache.gobblin.metastore.FsStateStore.java
@Override @SuppressWarnings("unchecked") public T get(String storeName, String tableName, String stateId) throws IOException { Path tablePath = new Path(new Path(this.storeRootDir, storeName), tableName); if (!this.fs.exists(tablePath)) { return null; }// w ww .j a v a2 s .c o m Closer closer = Closer.create(); try { @SuppressWarnings("deprecation") GobblinSequenceFileReader reader = closer .register(new GobblinSequenceFileReader(this.fs, tablePath, this.conf)); try { Text key = new Text(); T state = this.stateClass.newInstance(); while (reader.next(key)) { state = (T) reader.getCurrentValue(state); if (key.toString().equals(stateId)) { return state; } } } catch (Exception e) { throw new IOException( "failure retrieving state from storeName " + storeName + " tableName " + tableName, e); } } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } return null; }
From source file:org.mapfish.print.servlet.MapPrinterServlet.java
protected String getSpecFromPostBody(HttpServletRequest httpServletRequest) throws IOException { if (httpServletRequest.getParameter("spec") != null) { return httpServletRequest.getParameter("spec"); }/*from w w w. j a va 2 s . c o m*/ Closer closer = Closer.create(); try { final InputStreamReader reader = closer .register(new InputStreamReader(httpServletRequest.getInputStream(), getEncoding())); BufferedReader bufferedReader = closer.register(new BufferedReader(reader)); final String spec = CharStreams.toString(bufferedReader); return spec; } finally { closer.close(); } }
From source file:com.tinspx.util.io.ByteUtils.java
/** * Used instead of {@link #toByteArray(ByteSource)} when the size of the * {@code ByteSource} is known and {@link ByteSource#size() size()} should * not be called on {@code source}./*from ww w . ja va 2 s . co m*/ */ @ThreadLocalArray(8192) public static byte[] toByteArray(ByteSource source, int expectedSize) throws IOException { final Closer closer = Closer.create(); try { return toByteArray(closer.register(source.openStream()), expectedSize); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:org.apache.gobblin.runtime.Task.java
/** * Instantiate a new {@link Task}./*from ww w .j av a2 s . c o m*/ * * @param context a {@link TaskContext} containing all necessary information to construct and run a {@link Task} * @param taskStateTracker a {@link TaskStateTracker} for tracking task state * @param taskExecutor a {@link TaskExecutor} for executing the {@link Task} and its {@link Fork}s * @param countDownLatch an optional {@link java.util.concurrent.CountDownLatch} used to signal the task completion */ public Task(TaskContext context, TaskStateTracker taskStateTracker, TaskExecutor taskExecutor, Optional<CountDownLatch> countDownLatch) { this.taskContext = context; this.taskState = context.getTaskState(); this.jobId = this.taskState.getJobId(); this.taskId = this.taskState.getTaskId(); this.taskKey = this.taskState.getTaskKey(); this.taskStateTracker = taskStateTracker; this.taskExecutor = taskExecutor; this.countDownLatch = countDownLatch; this.closer = Closer.create(); this.closer.register(this.taskState.getTaskBrokerNullable()); this.extractor = closer .register(new InstrumentedExtractorDecorator<>(this.taskState, this.taskContext.getExtractor())); this.recordStreamProcessors = this.taskContext.getRecordStreamProcessors(); // add record stream processors to closer if they are closeable for (RecordStreamProcessor r : recordStreamProcessors) { if (r instanceof Closeable) { this.closer.register((Closeable) r); } } List<Converter<?, ?, ?, ?>> converters = this.taskContext.getConverters(); this.converter = closer.register(new MultiConverter(converters)); // can't have both record stream processors and converter lists configured try { Preconditions.checkState(this.recordStreamProcessors.isEmpty() || converters.isEmpty(), "Converters cannot be specified when RecordStreamProcessors are specified"); } catch (IllegalStateException e) { try { closer.close(); } catch (Throwable t) { LOG.error("Failed to close all open resources", t); } throw new TaskInstantiationException( "Converters cannot be specified when RecordStreamProcessors are specified"); } try { this.rowChecker = closer.register(this.taskContext.getRowLevelPolicyChecker()); } catch (Exception e) { try { closer.close(); } catch (Throwable t) { LOG.error("Failed to close all open resources", t); } throw new RuntimeException("Failed to instantiate row checker.", e); } this.taskMode = getExecutionModel(this.taskState); this.recordsPulled = new AtomicLong(0); this.lastRecordPulledTimestampMillis = 0; this.shutdownRequested = new AtomicBoolean(false); this.shutdownLatch = new CountDownLatch(1); // Setup Streaming constructs this.watermarkingStrategy = "FineGrain"; // TODO: Configure if (isStreamingTask()) { Extractor underlyingExtractor = this.taskContext.getRawSourceExtractor(); if (!(underlyingExtractor instanceof StreamingExtractor)) { LOG.error( "Extractor {} is not an instance of StreamingExtractor but the task is configured to run in continuous mode", underlyingExtractor.getClass().getName()); throw new TaskInstantiationException("Extraction " + underlyingExtractor.getClass().getName() + " is not an instance of StreamingExtractor but the task is configured to run in continuous mode"); } this.watermarkStorage = Optional.of(taskContext.getWatermarkStorage()); Config config; try { config = ConfigUtils.propertiesToConfig(taskState.getProperties()); } catch (Exception e) { LOG.warn("Failed to deserialize taskState into Config.. continuing with an empty config", e); config = ConfigFactory.empty(); } long commitIntervalMillis = ConfigUtils.getLong(config, TaskConfigurationKeys.STREAMING_WATERMARK_COMMIT_INTERVAL_MILLIS, TaskConfigurationKeys.DEFAULT_STREAMING_WATERMARK_COMMIT_INTERVAL_MILLIS); if (watermarkingStrategy.equals("FineGrain")) { // TODO: Configure this.watermarkTracker = Optional.of(this.closer.register(new FineGrainedWatermarkTracker(config))); this.watermarkManager = Optional.of((WatermarkManager) this.closer .register(new TrackerBasedWatermarkManager(this.watermarkStorage.get(), this.watermarkTracker.get(), commitIntervalMillis, Optional.of(this.LOG)))); } else { // writer-based watermarking this.watermarkManager = Optional .of((WatermarkManager) this.closer.register(new MultiWriterWatermarkManager( this.watermarkStorage.get(), commitIntervalMillis, Optional.of(this.LOG)))); this.watermarkTracker = Optional.absent(); } } else { this.watermarkManager = Optional.absent(); this.watermarkTracker = Optional.absent(); this.watermarkStorage = Optional.absent(); } }
From source file:com.android.repository.io.impl.FileOpImpl.java
@Override public void saveProperties(@NonNull File file, @NonNull Properties props, @NonNull String comments) throws IOException { Closer closer = Closer.create(); try {/*from w ww . j a v a 2s .c o m*/ OutputStream fos = closer.register(newFileOutputStream(file)); props.store(fos, comments); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:gobblin.writer.AsyncWriterManager.java
protected AsyncWriterManager(Config config, long commitTimeoutMillis, long commitStepWaitTimeMillis, double failureAllowanceRatio, boolean retriesEnabled, int numRetries, int minRetryIntervalMillis, int maxOutstandingWrites, AsyncDataWriter asyncDataWriter, Optional<Logger> loggerOptional) { Preconditions.checkArgument(commitTimeoutMillis > 0, "Commit timeout must be greater than 0"); Preconditions.checkArgument(commitStepWaitTimeMillis > 0, "Commit step wait time must be greater than 0"); Preconditions.checkArgument(commitStepWaitTimeMillis < commitTimeoutMillis, "Commit step wait time must be less " + "than commit timeout"); Preconditions.checkArgument((failureAllowanceRatio <= 1.0 && failureAllowanceRatio >= 0), "Failure Allowance must be a ratio between 0 and 1"); Preconditions.checkArgument(maxOutstandingWrites > 0, "Max outstanding writes must be greater than 0"); Preconditions.checkNotNull(asyncDataWriter, "Async Data Writer cannot be null"); this.log = loggerOptional.isPresent() ? loggerOptional.get() : LoggerFactory.getLogger(AsyncWriterManager.class); this.closer = Closer.create(); State state = ConfigUtils.configToState(config); this.instrumentationEnabled = GobblinMetrics.isEnabled(state); this.metricContext = this.closer.register(Instrumented.getMetricContext(state, asyncDataWriter.getClass())); regenerateMetrics();//from w w w. java 2 s . co m this.commitTimeoutMillis = commitTimeoutMillis; this.commitStepWaitTimeMillis = commitStepWaitTimeMillis; this.failureAllowanceRatio = failureAllowanceRatio; this.minRetryIntervalMillis = minRetryIntervalMillis; if (retriesEnabled) { this.numRetries = numRetries; this.retryQueue = Optional.of(new LinkedBlockingQueue<Attempt>()); this.retryThreadPool = Optional.of(new ScheduledThreadPoolExecutor(1, ExecutorsUtils .newDaemonThreadFactory(Optional.of(this.log), Optional.of("AsyncWriteManagerRetry-%d")))); this.retryThreadPool.get().execute(new RetryRunner()); } else { this.numRetries = 0; this.retryQueue = Optional.absent(); this.retryThreadPool = Optional.absent(); } this.maxOutstandingWrites = maxOutstandingWrites; this.writePermits = new Semaphore(maxOutstandingWrites); this.asyncDataWriter = asyncDataWriter; this.closer.register(asyncDataWriter); }
From source file:se.sics.datamodel.util.DMKeyFactory.java
private static byte[] serializeLexico(long val) throws IOException { Closer closer = Closer.create(); try {/*w w w . j av a 2 s . com*/ ByteArrayOutputStream baos = closer.register(new ByteArrayOutputStream()); DataOutputStream w = closer.register(new DataOutputStream(baos)); byte sign = (val < 0 ? (byte) 0 : (byte) 1); long absVal = Math.abs(val); byte[] iVal = Longs.toByteArray(absVal); w.write(INDEXVAL_LONG); w.write(sign); w.write(iVal); w.flush(); return baos.toByteArray(); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:alluxio.worker.block.UnderFileSystemBlockReader.java
/** * Closes the block reader. After this, this block reader should not be used anymore. * This is recommended to be called after the client finishes reading the block. It is usually * triggered when the client unlocks the block. * * @throws IOException if any I/O errors occur */// ww w. j a va 2s . c o m @Override public void close() throws IOException { if (mClosed) { return; } try { // This aborts the block if the block is not fully read. updateBlockWriter(mBlockMeta.getBlockSize()); Closer closer = Closer.create(); if (mBlockWriter != null) { closer.register(mBlockWriter); } if (mUnderFileSystemInputStream != null) { closer.register(mUnderFileSystemInputStream); } closer.close(); } finally { mClosed = true; } }
From source file:org.apache.gobblin.runtime.JobLauncherTestHelper.java
public void runTestWithMultipleDatasets(Properties jobProps) throws Exception { String jobName = jobProps.getProperty(ConfigurationKeys.JOB_NAME_KEY); String jobId = JobLauncherUtils.newJobId(jobName).toString(); jobProps.setProperty(ConfigurationKeys.JOB_ID_KEY, jobId); jobProps.setProperty(ConfigurationKeys.SOURCE_CLASS_KEY, MultiDatasetTestSource.class.getName()); Closer closer = Closer.create(); try {//w w w . j a v a 2s . c o m JobLauncher jobLauncher = closer .register(JobLauncherFactory.newJobLauncher(this.launcherProps, jobProps)); jobLauncher.launchJob(null); } finally { closer.close(); } for (int i = 0; i < 4; i++) { List<JobState.DatasetState> datasetStateList = this.datasetStateStore.getAll(jobName, "Dataset" + i + "-current.jst"); DatasetState datasetState = datasetStateList.get(0); Assert.assertEquals(datasetState.getDatasetUrn(), "Dataset" + i); Assert.assertEquals(datasetState.getState(), JobState.RunningState.COMMITTED); Assert.assertEquals(datasetState.getCompletedTasks(), 1); Assert.assertEquals(datasetState.getJobFailures(), 0); for (TaskState taskState : datasetState.getTaskStates()) { Assert.assertEquals(taskState.getProp(ConfigurationKeys.DATASET_URN_KEY), "Dataset" + i); Assert.assertEquals(taskState.getWorkingState(), WorkUnitState.WorkingState.COMMITTED); Assert.assertEquals(taskState.getPropAsLong(ConfigurationKeys.WRITER_RECORDS_WRITTEN), TestExtractor.TOTAL_RECORDS); } } }