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:org.apache.gobblin.runtime.JobLauncherTestHelper.java

public void runTest(Properties jobProps) throws Exception {
    String jobName = jobProps.getProperty(ConfigurationKeys.JOB_NAME_KEY);
    String jobId = JobLauncherUtils.newJobId(jobName);
    jobProps.setProperty(ConfigurationKeys.JOB_ID_KEY, jobId);

    JobContext jobContext = null;/*from w w  w  .  j  ava  2  s  . com*/
    Closer closer = Closer.create();
    try {
        JobLauncher jobLauncher = closer
                .register(JobLauncherFactory.newJobLauncher(this.launcherProps, jobProps));
        jobLauncher.launchJob(null);
        jobContext = ((AbstractJobLauncher) jobLauncher).getJobContext();
    } finally {
        closer.close();
    }

    Assert.assertTrue(jobContext.getJobMetricsOptional().isPresent());
    String jobMetricContextTags = jobContext.getJobMetricsOptional().get().getMetricContext().getTags()
            .toString();
    Assert.assertTrue(jobMetricContextTags.contains(ClusterNameTags.CLUSTER_IDENTIFIER_TAG_NAME),
            ClusterNameTags.CLUSTER_IDENTIFIER_TAG_NAME + " tag missing in job metric context tags.");

    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.WRITER_RECORDS_WRITTEN),
                TestExtractor.TOTAL_RECORDS);
    }
}

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

public void runTestWithCancellation(final 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);

    Closer closer = Closer.create();
    try {//  w ww.j a  va2  s.  c o m
        final JobLauncher jobLauncher = closer
                .register(JobLauncherFactory.newJobLauncher(this.launcherProps, jobProps));

        final AtomicBoolean isCancelled = new AtomicBoolean(false);
        // This thread will cancel the job after some time
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);
                    jobLauncher.cancelJob(null);
                    isCancelled.set(true);
                } catch (Exception je) {
                    // Ignored
                }
            }
        });
        thread.start();

        jobLauncher.launchJob(null);
        Assert.assertTrue(isCancelled.get());
    } finally {
        closer.close();
    }

    List<JobState.DatasetState> datasetStateList = this.datasetStateStore.getAll(jobName,
            sanitizeJobNameForDatasetStore(jobId) + ".jst");
    Assert.assertTrue(datasetStateList.isEmpty());
}

From source file:org.spongepowered.api.util.config.ConfigFile.java

/**
 * Write the given data to the file./*from   ww w .  ja  v  a2 s .  co  m*/
 *
 * @param renderedString The data
 * @throws IOException On write error
 */
private void write(String renderedString) throws IOException {
    Closer closer = Closer.create();
    try {
        BufferedWriter bw = closer.register(Files.newWriter(file, CHARSET));
        bw.write(renderedString);
    } finally {
        closer.close();
    }
}

From source file:com.netflix.servo.publish.FileMetricObserver.java

/** {@inheritDoc} */
public void updateImpl(List<Metric> metrics) {
    Preconditions.checkNotNull(metrics);
    File file = new File(dir, fileFormat.format(new Date(clock.now())));
    Closer closer = Closer.create();
    Writer out = null;/*www.  j  a va 2s  .  com*/
    try {
        try {
            LOGGER.debug("writing {} metrics to file {}", metrics.size(), file);
            OutputStream fileOut = new FileOutputStream(file, true);
            if (compress) {
                fileOut = new GZIPOutputStream(fileOut);
            }
            out = closer.register(new OutputStreamWriter(fileOut, "UTF-8"));
            for (Metric m : metrics) {
                out.append(m.getConfig().getName()).append('\t').append(m.getConfig().getTags().toString())
                        .append('\t').append(m.getValue().toString()).append('\n');
            }
        } catch (Throwable t) {
            throw closer.rethrow(t);
        } finally {
            closer.close();
        }
    } catch (IOException e) {
        incrementFailedCount();
        LOGGER.error("failed to write update to file " + file, e);
    }
}

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;
    }/*from w w w .j  av a 2 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.apache.gobblin.runtime.JobLauncherTestHelper.java

public void runTestWithMultipleDatasetsAndFaultyExtractor(Properties jobProps, boolean usePartialCommitPolicy)
        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,
            MultiDatasetTestSourceWithFaultyExtractor.class.getName());
    jobProps.setProperty(ConfigurationKeys.MAX_TASK_RETRIES_KEY, "0");
    if (usePartialCommitPolicy) {
        jobProps.setProperty(ConfigurationKeys.JOB_COMMIT_POLICY_KEY, "partial");
    }/*from   w w w .j a v  a  2  s.  co  m*/

    Closer closer = Closer.create();
    try {
        JobLauncher jobLauncher = closer
                .register(JobLauncherFactory.newJobLauncher(this.launcherProps, jobProps));
        jobLauncher.launchJob(null);
    } catch (JobException je) {
        // JobException is expected
    } finally {
        closer.close();
    }

    if (usePartialCommitPolicy) {
        List<JobState.DatasetState> datasetStateList = this.datasetStateStore.getAll(jobName,
                "Dataset0-current.jst");
        JobState.DatasetState datasetState = datasetStateList.get(0);
        Assert.assertEquals(datasetState.getState(), JobState.RunningState.COMMITTED);
        Assert.assertEquals(datasetState.getTaskCount(), 1);
        TaskState taskState = datasetState.getTaskStates().get(0);
        // BaseDataPublisher will change the state to COMMITTED
        Assert.assertEquals(taskState.getWorkingState(), WorkUnitState.WorkingState.COMMITTED);
    } else {
        // Task 0 should have failed
        Assert.assertTrue(this.datasetStateStore.getAll(jobName, "Dataset0-current.jst").isEmpty());
    }

    for (int i = 1; i < 4; i++) {
        List<JobState.DatasetState> datasetStateList = this.datasetStateStore.getAll(jobName,
                "Dataset" + i + "-current.jst");
        JobState.DatasetState datasetState = datasetStateList.get(0);

        Assert.assertEquals(datasetState.getDatasetUrn(), "Dataset" + i);
        Assert.assertEquals(datasetState.getState(), JobState.RunningState.COMMITTED);
        Assert.assertEquals(datasetState.getCompletedTasks(), 1);
        for (TaskState taskState : datasetState.getTaskStates()) {
            Assert.assertEquals(taskState.getProp(ConfigurationKeys.DATASET_URN_KEY), "Dataset" + i);
            Assert.assertEquals(taskState.getWorkingState(), WorkUnitState.WorkingState.COMMITTED);
        }
    }
}

From source file:org.apache.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;
    }//from  ww  w . j  a  v a  2  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);
                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:com.taobao.android.builder.tools.jarmerge.JarMergerWithOverride.java

public void addJar(@NonNull File file, boolean removeEntryTimestamp) throws IOException {
    logger.verbose("addJar(%1$s)", file);
    init();/*from w ww  .  j a  v  a  2  s  .com*/

    Closer localCloser = Closer.create();
    try {
        FileInputStream fis = localCloser.register(new FileInputStream(file));
        ZipInputStream zis = localCloser.register(new ZipInputStream(fis));

        // loop on the entries of the jar file package and put them in the final jar
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory()) {
                continue;
            }

            String name = entry.getName();
            if (filter != null && !filter.checkEntry(entry.getName())) {
                continue;
            }
            JarEntry newEntry;

            // Preserve the STORED method of the input entry.
            if (entry.getMethod() == JarEntry.STORED) {
                newEntry = new JarEntry(entry);
            } else {
                // Create a new entry so that the compressed len is recomputed.
                newEntry = new JarEntry(name);
            }
            if (removeEntryTimestamp) {
                newEntry.setTime(0);
            }

            // add the entry to the jar archive
            logger.verbose("addJar(%1$s): entry %2$s", file, name);
            duplicates.put(name, file.getAbsolutePath());
            if (duplicates.get(name).size() > 1) {
                logger.info("[Duplicated]" + name + ":" + file.getAbsolutePath() + ":" + duplicates.get(name));
                continue;
            }

            jarOutputStream.putNextEntry(newEntry);

            // read the content of the entry from the input stream, and write it into the archive.
            int count;
            while ((count = zis.read(buffer)) != -1) {
                jarOutputStream.write(buffer, 0, count);
            }

            // close the entries for this file
            jarOutputStream.closeEntry();
            zis.closeEntry();
        }
    } catch (ZipAbortException e) {
        throw new IOException("check exception", e);
    } finally {
        localCloser.close();
    }
}

From source file:net.myrrix.client.translating.TranslatingClientRecommender.java

private File copyAndTranslateToTempFile(Reader reader) throws IOException {
    File tempFile = File.createTempFile("myrrix-", ".csv.gz");
    tempFile.deleteOnExit();/*from w  ww  . jav  a2  s  . c  o m*/
    log.debug("Translating ingest input to {}", tempFile);
    Closer closer = Closer.create();
    try {
        BufferedReader buffered = closer.register(IOUtils.buffer(reader));
        Writer out = closer.register(IOUtils.buildGZIPWriter(tempFile));
        CharSequence line;
        while ((line = buffered.readLine()) != null) {
            Iterator<String> it = COMMA_SPLIT.split(line).iterator();
            String userIDString = it.next();
            String itemIDString = it.next();
            long longUserID = translateUser(userIDString);
            long longItemID = translateItem(itemIDString);
            String translatedLine;
            if (it.hasNext()) {
                String valueString = it.next();
                translatedLine = COMMA_JOIN.join(longUserID, longItemID, valueString);
            } else {
                translatedLine = COMMA_JOIN.join(longUserID, longItemID);
            }
            out.write(translatedLine);
            out.write('\n');
        }
    } finally {
        closer.close();
    }
    log.debug("Done translating ingest input to {}", tempFile);
    return tempFile;
}

From source file:com.facebook.buck.step.fs.ZipDirectoryWithMaxDeflateStep.java

@Override
public int execute(ExecutionContext context) {
    File inputDirectory = new File(inputDirectoryPath);
    Preconditions.checkState(inputDirectory.exists() && inputDirectory.isDirectory());

    Closer closer = Closer.create();
    try {/*w  w w.  j  a  v  a2s . c o  m*/
        ImmutableMap.Builder<File, ZipEntry> zipEntriesBuilder = ImmutableMap.builder();
        addDirectoryToZipEntryList(inputDirectory, "", zipEntriesBuilder);
        ImmutableMap<File, ZipEntry> zipEntries = zipEntriesBuilder.build();

        if (!zipEntries.isEmpty()) {
            ZipOutputStream outputStream = closer.register(
                    new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outputZipPath))));

            for (Map.Entry<File, ZipEntry> zipEntry : zipEntries.entrySet()) {
                outputStream.putNextEntry(zipEntry.getValue());
                ByteStreams.copy(Files.newInputStreamSupplier(zipEntry.getKey()), outputStream);
                outputStream.closeEntry();
            }
        }
    } catch (IOException e) {
        e.printStackTrace(context.getStdErr());
        return 1;
    } finally {
        try {
            closer.close();
        } catch (IOException e) {
            Throwables.propagate(e);
        }
    }
    return 0;
}