Example usage for com.google.common.io Closer register

List of usage examples for com.google.common.io Closer register

Introduction

In this page you can find the example usage for com.google.common.io Closer register.

Prototype


public <C extends Closeable> C register(@Nullable C closeable) 

Source Link

Document

Registers the given closeable to be closed when this Closer is #close closed .

Usage

From source file:gobblin.metastore.FsStateStore.java

/**
 * See {@link StateStore#putAll(String, String, Collection)}.
 *
 * <p>//from  w w  w .ja  v a  2  s.  co m
 *   This implementation does not support putting the state objects into an existing store as
 *   append is to be supported by the Hadoop SequenceFile (HADOOP-7139).
 * </p>
 */
@Override
public void putAll(String storeName, String tableName, Collection<T> states) throws IOException {
    String tmpTableName = this.useTmpFileForPut ? TMP_FILE_PREFIX + tableName : tableName;
    Path tmpTablePath = new Path(new Path(this.storeRootDir, storeName), tmpTableName);

    if (!this.fs.exists(tmpTablePath) && !create(storeName, tmpTableName)) {
        throw new IOException("Failed to create a state file for table " + tmpTableName);
    }

    Closer closer = Closer.create();
    try {
        @SuppressWarnings("deprecation")
        SequenceFile.Writer writer = closer.register(SequenceFile.createWriter(this.fs, this.conf, tmpTablePath,
                Text.class, this.stateClass, SequenceFile.CompressionType.BLOCK, new DefaultCodec()));
        for (T state : states) {
            writer.append(new Text(Strings.nullToEmpty(state.getId())), state);
        }
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }

    if (this.useTmpFileForPut) {
        Path tablePath = new Path(new Path(this.storeRootDir, storeName), tableName);
        HadoopUtils.renamePath(this.fs, tmpTablePath, tablePath);
    }
}

From source file:alluxio.master.journal.ufs.UfsJournalLogWriter.java

public synchronized void close() throws IOException {
    Closer closer = Closer.create();
    if (mJournalOutputStream != null) {
        closer.register(mJournalOutputStream);
    }//w w  w .  j  ava  2 s.  c o  m
    closer.register(mGarbageCollector);
    closer.close();
    mClosed = true;
}

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;
    }/*ww  w .ja v a  2  s .  c o  m*/

    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:gobblin.metastore.FsStateStore.java

@Override
@SuppressWarnings("unchecked")
public List<T> getAll(String storeName, String tableName) throws IOException {
    List<T> states = Lists.newArrayList();

    Path tablePath = new Path(new Path(this.storeRootDir, storeName), tableName);
    if (!this.fs.exists(tablePath)) {
        return states;
    }/*w  w  w  . j a  va2 s . c om*/

    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);
                states.add(state);
                // We need a new object for each read state
                state = this.stateClass.newInstance();
            }
        } 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 states;
}

From source file:org.pantsbuild.tools.jar.Main.java

private void doRun(Closer closer, final File targetJar) throws ExitException {
    JarBuilder jarBuilder = closer.register(new JarBuilder(targetJar, new LoggingListener(targetJar)));

    try {/*from  w w w.ja  v  a  2 s  . c o m*/
        @Nullable
        Manifest mf = getManifest();
        if (mf != null) {
            jarBuilder.useCustomManifest(mf);
        }
    } catch (IOException e) {
        throw new ExitException(1, "Failed to configure custom manifest: %s", e);
    }

    for (Options.FileSource fileSource : options.files) {
        fileSource.addTo(jarBuilder);
    }

    for (File jar : options.jars) {
        jarBuilder.addJar(jar);
    }

    DuplicateHandler duplicateHandler = new DuplicateHandler(options.defaultAction, options.policies);
    try {
        jarBuilder.write(options.compress, duplicateHandler, options.skip);
    } catch (DuplicateEntryException e) {
        throw new ExitException(1, "Refusing to write duplicate entry: %s", e);
    } catch (IOException e) {
        throw new ExitException(1, "Unexpected problem writing target jar %s: %s", targetJar, e);
    }
}

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 {/* w  w  w  .  j a  va2  s .c  o 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: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 {/*from   ww  w  .j av a  2  s  . com*/
        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);
        }
    }
}

From source file:org.apache.gobblin.runtime.JobLauncherTestHelper.java

public void runTestWithCommitSuccessfulTasksPolicy(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.PUBLISH_DATA_AT_JOB_LEVEL, Boolean.FALSE.toString());
    jobProps.setProperty(ConfigurationKeys.JOB_COMMIT_POLICY_KEY, "successful");
    jobProps.setProperty(ConfigurationKeys.SOURCE_CLASS_KEY, TestSourceWithFaultyExtractor.class.getName());
    jobProps.setProperty(ConfigurationKeys.MAX_TASK_RETRIES_KEY, "0");

    Closer closer = Closer.create();
    try {/*from   w ww  .ja va 2s  .  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");
    JobState jobState = datasetStateList.get(0);

    Assert.assertEquals(jobState.getState(), JobState.RunningState.COMMITTED);
    Assert.assertEquals(jobState.getCompletedTasks(), 4);
    for (TaskState taskState : jobState.getTaskStates()) {
        if (taskState.getTaskId().endsWith("0")) {
            Assert.assertEquals(taskState.getWorkingState(), WorkUnitState.WorkingState.FAILED);
        } else {
            Assert.assertEquals(taskState.getWorkingState(), WorkUnitState.WorkingState.COMMITTED);
            Assert.assertEquals(taskState.getPropAsLong(ConfigurationKeys.WRITER_RECORDS_WRITTEN),
                    TestExtractor.TOTAL_RECORDS);
        }
    }
}

From source file:org.basepom.mojo.propertyhelper.ValueCache.java

public void persist() throws IOException {
    for (final Map.Entry<File, ValueCacheEntry> entries : valueFiles.entrySet()) {
        final ValueCacheEntry entry = entries.getValue();
        if (!entry.isDirty()) {
            continue;
        }//from  w w w. j  a v a  2s  .  c o m
        final File file = entries.getKey();
        if (entry.isExists() || entry.isCreate()) {
            checkNotNull(file, "no file defined, can not persist!");
            final File oldFile = new File(file.getCanonicalPath() + ".bak");

            if (entry.isExists()) {
                checkState(file.exists(), "'%s' should exist!", file.getCanonicalPath());
                // unlink an old file if necessary
                if (oldFile.exists()) {
                    checkState(oldFile.delete(), "Could not delete '%s'", file.getCanonicalPath());
                }
            }

            final File folder = file.getParentFile();
            if (!folder.exists()) {
                checkState(folder.mkdirs(), "Could not create folder '%s'", folder.getCanonicalPath());
            }

            final Closer closer = Closer.create();

            final File newFile = new File(file.getCanonicalPath() + ".new");
            try {
                final OutputStream stream = closer.register(new FileOutputStream(newFile));
                entry.store(stream, "created by property-helper-maven-plugin");
            } finally {
                closer.close();
            }

            if (file.exists()) {
                if (!file.renameTo(oldFile)) {
                    LOG.warn("Could not rename '%s' to '%s'!", file, oldFile);
                }
            }

            if (!file.exists()) {
                if (!newFile.renameTo(file)) {
                    LOG.warn("Could not rename '%s' to '%s'!", newFile, file);
                }
            }
        }
    }
}

From source file:org.apache.gobblin.runtime.JobLauncherTestHelper.java

/**
 * Test when a test with the matching suffix is skipped.
 * @param jobProps job properties// w ww.ja v a 2  s  .  co m
 * @param skippedTaskSuffix the suffix for the task that is skipped
 */
public void runTestWithSkippedTask(Properties jobProps, String skippedTaskSuffix) 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.PUBLISH_DATA_AT_JOB_LEVEL, Boolean.FALSE.toString());
    jobProps.setProperty(ConfigurationKeys.JOB_COMMIT_POLICY_KEY, "successful");
    jobProps.setProperty(ConfigurationKeys.MAX_TASK_RETRIES_KEY, "0");

    Closer closer = Closer.create();
    try {
        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");
    JobState jobState = datasetStateList.get(0);

    Assert.assertEquals(jobState.getState(), JobState.RunningState.COMMITTED);
    // one task is skipped out of 4
    Assert.assertEquals(jobState.getCompletedTasks(), 3);
    for (TaskState taskState : jobState.getTaskStates()) {
        if (taskState.getTaskId().endsWith(skippedTaskSuffix)) {
            Assert.assertEquals(taskState.getWorkingState(), WorkUnitState.WorkingState.PENDING);
        } else {
            Assert.assertEquals(taskState.getWorkingState(), WorkUnitState.WorkingState.COMMITTED);
            Assert.assertEquals(taskState.getPropAsLong(ConfigurationKeys.WRITER_RECORDS_WRITTEN),
                    TestExtractor.TOTAL_RECORDS);
        }
    }
}