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.aws.Log4jConfigHelper.java

/**
 * Update the log4j configuration.//from www .j av  a2 s .  c om
 *
 * @param targetClass the target class used to get the original log4j configuration file as a resource
 * @param log4jFileName the custom log4j configuration properties file name
 * @throws IOException if there's something wrong with updating the log4j configuration
 */
public static void updateLog4jConfiguration(Class<?> targetClass, String log4jFileName) throws IOException {
    final Closer closer = Closer.create();
    try {
        final InputStream inputStream = closer.register(targetClass.getResourceAsStream("/" + log4jFileName));
        final Properties originalProperties = new Properties();
        originalProperties.load(inputStream);

        LogManager.resetConfiguration();
        PropertyConfigurator.configure(originalProperties);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:com.complexible.common.io.ByteStreams2.java

public static byte[] gzip(final byte[] theBytes) throws IOException {
    final ByteArrayOutputStream aOut = new ByteArrayOutputStream(theBytes.length);

    final GZIPOutputStream aZipped = new GZIPOutputStream(aOut);
    final ByteArrayInputStream aIn = new ByteArrayInputStream(theBytes);

    Closer aCloser = Closer.create();
    aCloser.register(aZipped);
    aCloser.register(aIn);/*from ww w .  j  a  va  2s. co m*/

    try {
        ByteStreams.copy(aIn, aZipped);
    } finally {
        aCloser.close();
    }

    return aOut.toByteArray();
}

From source file:gobblin.yarn.Log4jConfigurationHelper.java

/**
 * Update the log4j configuration.//  w  w w.  j a  v  a2  s . co  m
 *
 * @param targetClass the target class used to get the original log4j configuration file as a resource
 * @param log4jPath the custom log4j configuration properties file path
 * @throws IOException if there's something wrong with updating the log4j configuration
 */
public static void updateLog4jConfiguration(Class<?> targetClass, String log4jPath) throws IOException {
    Closer closer = Closer.create();
    try {
        InputStream fileInputStream = closer.register(new FileInputStream(log4jPath));
        InputStream inputStream = closer
                .register(targetClass.getResourceAsStream("/" + LOG4J_CONFIGURATION_FILE_NAME));
        Properties customProperties = new Properties();
        customProperties.load(fileInputStream);
        Properties originalProperties = new Properties();
        originalProperties.load(inputStream);

        for (Entry<Object, Object> entry : customProperties.entrySet()) {
            originalProperties.setProperty(entry.getKey().toString(), entry.getValue().toString());
        }

        LogManager.resetConfiguration();
        PropertyConfigurator.configure(originalProperties);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:org.apache.gobblin.util.logs.Log4jConfigurationHelper.java

/**
 * Update the log4j configuration./*ww w  .j ava 2 s  . c  o  m*/
 *
 * @param targetClass the target class used to get the original log4j configuration file as a resource
 * @param log4jPath the custom log4j configuration properties file path
 * @param log4jFileName the custom log4j configuration properties file name
 * @throws IOException if there's something wrong with updating the log4j configuration
 */
public static void updateLog4jConfiguration(Class<?> targetClass, String log4jPath, String log4jFileName)
        throws IOException {
    Closer closer = Closer.create();
    try {
        InputStream fileInputStream = closer.register(new FileInputStream(log4jPath));
        InputStream inputStream = closer.register(targetClass.getResourceAsStream("/" + log4jFileName));
        Properties customProperties = new Properties();
        customProperties.load(fileInputStream);
        Properties originalProperties = new Properties();
        originalProperties.load(inputStream);

        for (Entry<Object, Object> entry : customProperties.entrySet()) {
            originalProperties.setProperty(entry.getKey().toString(), entry.getValue().toString());
        }

        LogManager.resetConfiguration();
        PropertyConfigurator.configure(originalProperties);
    } catch (Throwable t) {
        throw closer.rethrow(t);
    } finally {
        closer.close();
    }
}

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

/**
 * Opens a jar file and registers it with the given {@code closer}.
 *
 * @param closer A closer responsible for closing the opened jar file.
 * @param file A file pointing to a jar.
 * @param verify Whether or not to verify the jar file if it is signed.
 * @return An opened jar file.//www. j a va 2  s.c om
 * @throws IOException if there is a problem opening the given {@code file} as a jar.
 */
static JarFile openJarFile(Closer closer, File file, boolean verify) throws IOException {
    final JarFile jarFile = new JarFile(file, verify);
    closer.register(new Closeable() {
        @Override
        public void close() throws IOException {
            jarFile.close();
        }
    });
    return jarFile;
}

From source file:org.apache.jackrabbit.oak.checkpoint.SegmentCheckpoints.java

static Checkpoints create(File path, Closer closer) throws IOException {
    return new SegmentCheckpoints(closer.register(FileStore.builder(path).build()));
}

From source file:com.google.caliper.util.Util.java

public static ImmutableMap<String, String> loadProperties(ByteSource is) throws IOException {
    Properties props = new Properties();
    Closer closer = Closer.create();
    InputStream in = closer.register(is.openStream());
    try {//from   w  ww .  j a  v  a 2s  . co  m
        props.load(in);
    } finally {
        closer.close();
    }
    return Maps.fromProperties(props);
}

From source file:org.apache.jackrabbit.oak.checkpoint.SegmentTarCheckpoints.java

static Checkpoints create(File path, Closer closer) throws IOException {
    return new SegmentTarCheckpoints(closer.register(fileStoreBuilder(path).build()));
}

From source file:org.impressivecode.depress.mr.astcompare.utils.Utils.java

private static void saveFile(InputStream is, OutputStream os) throws IOException {
    Closer closer = Closer.create();
    try {/*w  ww.  ja v a2 s  .  co m*/
        InputStream in = closer.register(is);
        OutputStream out = closer.register(os);
        ByteStreams.copy(in, out);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}

From source file:org.glowroot.weaving.ClassLoaders.java

public static void defineClassesInBootstrapClassLoader(Collection<LazyDefinedClass> lazyDefinedClasses,
        Instrumentation instrumentation, File generatedJarFile) throws IOException {
    Closer closer = Closer.create();
    try {//from w  w w  .j  a v  a  2 s  . c o m
        FileOutputStream out = closer.register(new FileOutputStream(generatedJarFile));
        JarOutputStream jarOut = closer.register(new JarOutputStream(out));
        generate(lazyDefinedClasses, jarOut);
    } catch (Throwable t) {
        closer.rethrow(t);
    } finally {
        closer.close();
    }
    instrumentation.appendToBootstrapClassLoaderSearch(new JarFile(generatedJarFile));
}