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:com.github.sdorra.buildfrontend.AbstractNodeMojo.java

/**
 * Method description//from   ww  w  .  ja v  a2  s.c  o m
 *
 *
 * @param urlString
 * @param target
 *
 * @throws IOException
 */
private static void download(String urlString, File target) throws IOException {
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    Closer closer = Closer.create();

    try {
        InputStream input = closer.register(connection.getInputStream());
        OutputStream output = closer.register(new FileOutputStream(target));

        ByteStreams.copy(input, output);
    } catch (IOException ex) {
        throw closer.rethrow(ex);
    } finally {
        closer.close();
    }
}

From source file:org.glowroot.agent.central.CentralCollector.java

private static void writeConfigSyncedFile(File file, String agentId) throws IOException {
    Closer closer = Closer.create();
    try {/* w w  w  . j a va  2 s. c o m*/
        PrintWriter out = closer.register(new PrintWriter(file, UTF_8.name()));
        out.println("# this file is created after the agent has pushed its local configuration"
                + " to the central collector");
        out.println("#");
        out.println("# when this file is present (and the agent.id below matches the running"
                + " agent's agent.id), the agent");
        out.println("# will overwrite its local configuration with the agent configuration it"
                + " retrieves from the central");
        out.println("# collector on JVM startup");
        out.println("#");
        out.println("# when this file is not present (or the agent.id below does not match the"
                + " running agent's agent.id),");
        out.println("# the agent will push its local configuration to the central collector on"
                + " JVM startup (overwriting");
        out.println("# any existing remote configuration), after which the agent will"
                + " (re-)create this file using the");
        out.println("# running agent's agent.id");
        out.println("");
        out.println("agent.id=" + agentId);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:org.apache.jackrabbit.oak.plugins.tika.TextExtractorMain.java

private static NodeStore bootStrapNodeStore(String src, boolean segmentTar, BlobStore blobStore, Closer closer)
        throws IOException {
    if (src.startsWith(MongoURI.MONGODB_PREFIX)) {
        MongoClientURI uri = new MongoClientURI(src);
        if (uri.getDatabase() == null) {
            System.err.println("Database missing in MongoDB URI: " + uri.getURI());
            System.exit(1);/*from  www  .j a v  a  2 s.com*/
        }
        MongoConnection mongo = new MongoConnection(uri.getURI());
        closer.register(asCloseable(mongo));
        DocumentNodeStore store = new DocumentMK.Builder().setBlobStore(blobStore).setMongoDB(mongo.getDB())
                .getNodeStore();
        closer.register(asCloseable(store));
        return store;
    }

    if (segmentTar) {
        return SegmentTarUtils.bootstrap(src, blobStore, closer);
    }

    return SegmentUtils.bootstrap(src, blobStore, closer);
}

From source file:com.spotify.styx.StyxScheduler.java

private static DockerRunner createDockerRunner(String id, Environment environment, StateManager stateManager,
        ScheduledExecutorService scheduler, Stats stats) {
    final Config config = environment.config();
    final Closer closer = environment.closer();

    if (isDevMode(config)) {
        LOG.info("Creating LocalDockerRunner");
        return closer.register(DockerRunner.local(scheduler, stateManager));
    } else {/*from   www  . j av a 2 s  .  c  o  m*/
        final KubernetesClient kubernetes = closer.register(getKubernetesClient(config, id));
        return closer.register(DockerRunner.kubernetes(kubernetes, stateManager, stats));
    }
}

From source file:org.apache.jackrabbit.oak.run.SegmentTarUtils.java

static BlobReferenceRetriever newBlobReferenceRetriever(String path, Closer closer) throws IOException {
    return new SegmentBlobReferenceRetriever(closer.register(openFileStore(path, false)));
}

From source file:org.sonatype.nexus.repository.maven.internal.MavenIndexPublisher.java

/**
 * Publishes MI index into {@code target}, sourced from {@code repositories} repositories.
 *//*from  ww w. j  av  a  2 s. co m*/
public static void publishMergedIndex(final Repository target, final List<Repository> repositories)
        throws IOException {
    checkNotNull(target);
    checkNotNull(repositories);
    Closer closer = Closer.create();
    try {
        List<Iterable<Record>> records = new ArrayList<>();
        for (Repository repository : repositories) {
            try {
                ResourceHandler resourceHandler = closer
                        .register(new Maven2WritableResourceHandler(repository));
                IndexReader indexReader = closer.register(new IndexReader(null, resourceHandler));
                ChunkReader chunkReader = closer.register(indexReader.iterator().next());
                records.add(filter(transform(chunkReader, RECORD_EXPANDER::apply),
                        new RecordTypeFilter(Type.ARTIFACT_ADD)));
            } catch (IllegalArgumentException e) {
                throw new IOException(e.getMessage(), e);
            }
        }

        try (Maven2WritableResourceHandler resourceHandler = new Maven2WritableResourceHandler(target)) {
            try (IndexWriter indexWriter = new IndexWriter(resourceHandler, target.getName(), false)) {
                indexWriter.writeChunk(
                        transform(decorate(filter(concat(records), new UniqueFilter()), target.getName()),
                                RECORD_COMPACTOR::apply).iterator());
            }
        }
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:org.glowroot.agent.live.ClasspathCache.java

private static void loadClassNamesFromDirectoryInsideJarFile(File jarFile, String directoryInsideJarFile,
        Location location, Multimap<String, Location> newClassNameLocations) throws IOException {
    Closer closer = Closer.create();
    try {/*from w ww .j  a  va 2  s.c  o  m*/
        InputStream in = closer.register(new FileInputStream(jarFile));
        JarInputStream jarIn = closer.register(new JarInputStream(in));
        loadClassNamesFromJarInputStream(jarIn, directoryInsideJarFile, location, newClassNameLocations);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:org.glowroot.agent.live.ClasspathCache.java

private static void loadClassNamesFromJarFileInsideJarFile(File jarFile, String jarFileInsideJarFile,
        Location location, Multimap<String, Location> newClassNameLocations) throws IOException {
    URI uri;/*from  ww w  . j a v a  2s. c  o  m*/
    try {
        uri = new URI("jar", "file:" + jarFile.getPath() + "!/" + jarFileInsideJarFile, "");
    } catch (URISyntaxException e) {
        // this is a programmatic error
        throw new RuntimeException(e);
    }
    Closer closer = Closer.create();
    try {
        InputStream in = closer.register(uri.toURL().openStream());
        JarInputStream jarIn = closer.register(new JarInputStream(in));
        loadClassNamesFromJarInputStream(jarIn, "", location, newClassNameLocations);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:org.glowroot.agent.live.ClasspathCache.java

private static byte[] getBytesFromJarFileInsideJarFile(String name, File jarFile, String jarFileInsideJarFile)
        throws IOException {
    String path = jarFile.getPath();
    URI uri;/*from   w  w  w.j  av a2  s .  co m*/
    try {
        uri = new URI("jar", "file:" + path + "!/" + jarFileInsideJarFile, "");
    } catch (URISyntaxException e) {
        // this is a programmatic error
        throw new RuntimeException(e);
    }
    Closer closer = Closer.create();
    try {
        InputStream in = closer.register(uri.toURL().openStream());
        JarInputStream jarIn = closer.register(new JarInputStream(in));
        JarEntry jarEntry;
        while ((jarEntry = jarIn.getNextJarEntry()) != null) {
            if (jarEntry.isDirectory()) {
                continue;
            }
            if (jarEntry.getName().equals(name)) {
                return ByteStreams.toByteArray(jarIn);
            }
        }
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
    throw new UnsupportedOperationException();
}

From source file:net.simon04.guavavfs.VirtualFiles.java

private static MappedByteBuffer map(RandomAccessFile raf, MapMode mode, long size) throws IOException {
    Closer closer = Closer.create();
    try {/*from  w w  w .ja  v  a2s .  com*/
        FileChannel channel = closer.register(raf.getChannel());
        return channel.map(mode, 0, size);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}