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.taobao.android.builder.tools.jarmerge.JarMergerWithOverride.java

private void addFolder(@NonNull File folder, @NonNull String path) throws IOException, ZipAbortException {
    logger.verbose("addFolder(%1$s, %2$s)", folder, path);
    File[] files = folder.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.isFile()) {
                String entryPath = path + file.getName();
                if (filter == null || filter.checkEntry(entryPath)) {
                    logger.verbose("addFolder(%1$s, %2$s): entry %3$s", folder, path, entryPath);
                    duplicates.put(path + file.getName(), folder.getAbsolutePath());
                    if (duplicates.get(path + file.getName()).size() > 1) {
                        logger.info("[Duplicated]" + path + file.getName() + ":" + folder.getAbsolutePath()
                                + ":" + duplicates.get(path + file.getName()));
                        continue;
                    }//from w ww.j a v  a2 s  .  c o  m

                    // new entry
                    jarOutputStream.putNextEntry(new JarEntry(entryPath));

                    // put the file content
                    Closer localCloser = Closer.create();
                    try {
                        FileInputStream fis = localCloser.register(new FileInputStream(file));
                        int count;
                        while ((count = fis.read(buffer)) != -1) {
                            jarOutputStream.write(buffer, 0, count);
                        }
                    } finally {
                        localCloser.close();
                    }

                    // close the entry
                    jarOutputStream.closeEntry();
                }
            } else if (file.isDirectory()) {
                addFolder(file, path + file.getName() + "/");
            }
        }
    }
}

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

public static NodeStore bootstrapNodeStore(String[] args, Closer closer, String h) throws IOException {
    //TODO add support for other NodeStore flags
    OptionParser parser = new OptionParser();
    OptionSpec<Integer> clusterId = parser.accepts("clusterId", "MongoMK clusterId").withRequiredArg()
            .ofType(Integer.class).defaultsTo(0);
    OptionSpec segmentTar = parser.accepts("segment-tar", "Use oak-segment-tar instead of oak-segment");
    OptionSpec<?> help = parser.acceptsAll(asList("h", "?", "help"), "show help").forHelp();
    OptionSpec<String> nonOption = parser.nonOptions(h);

    OptionSet options = parser.parse(args);
    List<String> nonOptions = nonOption.values(options);

    if (options.has(help)) {
        parser.printHelpOn(System.out);
        System.exit(0);//from w ww .  j a  v a  2 s  .co m
    }

    if (nonOptions.isEmpty()) {
        parser.printHelpOn(System.err);
        System.exit(1);
    }

    String src = nonOptions.get(0);
    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);
        }
        MongoConnection mongo = new MongoConnection(uri.getURI());
        closer.register(asCloseable(mongo));
        DocumentNodeStore store = new DocumentMK.Builder().setMongoDB(mongo.getDB()).setLeaseCheck(false)
                .setClusterId(clusterId.value(options)).getNodeStore();
        closer.register(asCloseable(store));
        return store;
    }

    if (options.has(segmentTar)) {
        return SegmentTarUtils.bootstrapNodeStore(src, closer);
    }

    return SegmentUtils.bootstrapNodeStore(src, closer);
}

From source file:org.apache.gobblin.util.limiter.MultiLimiter.java

@Override
public Closeable acquirePermits(long permits) throws InterruptedException {
    Closer closer = Closer.create();
    for (Limiter limiter : this.underlyingLimiters) {
        Closeable permit = limiter.acquirePermits(permits);
        if (permit == null) {
            try {
                closer.close();//from  w w  w  . j a  va2s.  c  om
            } catch (IOException ioe) {
                throw new RuntimeException("Could not return intermediate permits.");
            }
            return null;
        }
        closer.register(permit);
    }
    return closer;
}

From source file:com.android.tools.lint.Reporter.java

/** Returns a URL to a local copy of the given resource, or null. There is
 * no filename conflict resolution. */
protected String addLocalResources(URL url) throws IOException {
    // Attempt to make local copy
    File resourceDir = computeResourceDir();
    if (resourceDir != null) {
        String base = url.getFile();
        base = base.substring(base.lastIndexOf('/') + 1);
        mNameToFile.put(base, new File(url.toExternalForm()));

        File target = new File(resourceDir, base);
        Closer closer = Closer.create();
        try {//  w w  w.ja v  a  2s . c  o  m
            FileOutputStream output = closer.register(new FileOutputStream(target));
            InputStream input = closer.register(url.openStream());
            ByteStreams.copy(input, output);
        } catch (Throwable e) {
            closer.rethrow(e);
        } finally {
            closer.close();
        }
        return resourceDir.getName() + '/' + encodeUrl(base);
    }
    return null;
}

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

@Nullable
public static GarbageCollectableBlobStore bootstrapDataStore(String[] args, Closer closer)
        throws IOException, RepositoryException {
    OptionParser parser = new OptionParser();
    parser.allowsUnrecognizedOptions();//from   ww w  .  jav  a 2  s  .  c o  m

    ArgumentAcceptingOptionSpec<String> s3dsConfig = parser.accepts("s3ds", "S3DataStore config")
            .withRequiredArg().ofType(String.class);
    ArgumentAcceptingOptionSpec<String> fdsConfig = parser.accepts("fds", "FileDataStore config")
            .withRequiredArg().ofType(String.class);

    OptionSet options = parser.parse(args);

    if (!options.has(s3dsConfig) && !options.has(fdsConfig)) {
        return null;
    }

    DataStore delegate;
    if (options.has(s3dsConfig)) {
        SharedS3DataStore s3ds = new SharedS3DataStore();
        String cfgPath = s3dsConfig.value(options);
        Properties props = loadAndTransformProps(cfgPath);
        s3ds.setProperties(props);
        s3ds.init(null);
        delegate = s3ds;
    } else {
        delegate = new OakFileDataStore();
        String cfgPath = fdsConfig.value(options);
        Properties props = loadAndTransformProps(cfgPath);
        populate(delegate, Maps.fromProperties(props), true);
        delegate.init(null);
    }
    DataStoreBlobStore blobStore = new DataStoreBlobStore(delegate);
    closer.register(Utils.asCloseable(blobStore));

    return blobStore;
}

From source file:org.grouplens.lenskit.eval.traintest.TrainTestEvalJob.java

@SuppressWarnings("PMD.AvoidCatchingThrowable")
private void runEvaluation() throws IOException, RecommenderBuildException {
    Closer closer = Closer.create();
    try {//  w w  w . j  a  va2s.  c  o  m
        TableWriter userTable = userOutputSupplier.get();
        if (userTable != null) {
            closer.register(userTable);
        }
        TableWriter predictTable = predictOutputSupplier.get();
        if (predictTable != null) {
            closer.register(predictTable);
        }

        List<Object> outputRow = Lists.newArrayList();

        ExecutionInfo execInfo = buildExecInfo();

        logger.info("Building {}", algorithm.getName());
        StopWatch buildTimer = new StopWatch();
        buildTimer.start();
        RecommenderInstance rec = algorithm.makeTestableRecommender(data, snapshot, execInfo);
        buildTimer.stop();
        logger.info("Built {} in {}", algorithm.getName(), buildTimer);

        logger.info("Measuring {}", algorithm.getName());
        for (ModelMetric metric : modelMetrics) {
            outputRow.addAll(metric.measureAlgorithm(algorithm, data, rec.getRecommender()));
        }

        logger.info("Testing {}", algorithm.getName());
        StopWatch testTimer = new StopWatch();
        testTimer.start();
        List<TestUserMetricAccumulator> evalAccums = new ArrayList<TestUserMetricAccumulator>(
                evaluators.size());

        List<Object> userRow = new ArrayList<Object>();

        UserEventDAO testUsers = data.getTestData().getUserEventDAO();
        for (TestUserMetric eval : evaluators) {
            TestUserMetricAccumulator accum = eval.makeAccumulator(algorithm, data);
            evalAccums.add(accum);
        }

        Cursor<UserHistory<Event>> userProfiles = closer.register(testUsers.streamEventsByUser());
        for (UserHistory<Event> p : userProfiles) {
            assert userRow.isEmpty();
            userRow.add(p.getUserId());

            long uid = p.getUserId();
            LongSet testItems = p.itemSet();

            Supplier<SparseVector> preds = new PredictionSupplier(rec, uid, testItems);
            Supplier<List<ScoredId>> recs = new RecommendationSupplier(rec, uid, testItems);
            Supplier<UserHistory<Event>> hist = new HistorySupplier(rec.getUserEventDAO(), uid);
            Supplier<UserHistory<Event>> testHist = Suppliers.ofInstance(p);

            TestUser test = new TestUser(uid, hist, testHist, preds, recs);

            for (TestUserMetricAccumulator accum : evalAccums) {
                Object[] ures = accum.evaluate(test);
                if (ures != null) {
                    userRow.addAll(Arrays.asList(ures));
                }
            }
            if (userTable != null) {
                try {
                    userTable.writeRow(userRow);
                } catch (IOException e) {
                    throw new RuntimeException("error writing user row", e);
                }
            }
            userRow.clear();

            if (predictTable != null) {
                writePredictions(predictTable, uid, RatingVectorUserHistorySummarizer.makeRatingVector(p),
                        test.getPredictions());
            }
        }
        testTimer.stop();
        logger.info("Tested {} in {}", algorithm.getName(), testTimer);

        writeOutput(buildTimer, testTimer, outputRow, evalAccums);
    } catch (Throwable th) {
        throw closer.rethrow(th, RecommenderBuildException.class);
    } finally {
        closer.close();
    }
}

From source file:org.apache.gobblin.runtime.mapreduce.GobblinOutputCommitter.java

@Override
public void abortJob(JobContext jobContext, JobStatus.State state) throws IOException {
    LOG.info("Aborting Job: " + jobContext.getJobID() + " with state: " + state);

    Configuration conf = jobContext.getConfiguration();

    URI fsUri = URI.create(conf.get(ConfigurationKeys.FS_URI_KEY, ConfigurationKeys.LOCAL_FS_URI));
    FileSystem fs = FileSystem.get(fsUri, conf);

    Path mrJobDir = new Path(conf.get(ConfigurationKeys.MR_JOB_ROOT_DIR_KEY),
            conf.get(ConfigurationKeys.JOB_NAME_KEY));
    Path jobInputDir = new Path(mrJobDir, MRJobLauncher.INPUT_DIR_NAME);

    if (!fs.exists(jobInputDir) || !fs.isDirectory(jobInputDir)) {
        LOG.warn(String.format("%s either does not exist or is not a directory. No data to cleanup.",
                jobInputDir));// w w w  .ja  va2  s  . c  o m
        return;
    }

    // Iterate through all files in the jobInputDir, each file should correspond to a serialized wu or mwu
    try {
        for (FileStatus status : fs.listStatus(jobInputDir, new WorkUnitFilter())) {

            Closer workUnitFileCloser = Closer.create();

            // If the file ends with ".wu" de-serialize it into a WorkUnit
            if (status.getPath().getName().endsWith(AbstractJobLauncher.WORK_UNIT_FILE_EXTENSION)) {
                WorkUnit wu = WorkUnit.createEmpty();
                try {
                    wu.readFields(workUnitFileCloser.register(new DataInputStream(fs.open(status.getPath()))));
                } finally {
                    workUnitFileCloser.close();
                }
                JobLauncherUtils.cleanTaskStagingData(new WorkUnitState(wu), LOG);
            }

            // If the file ends with ".mwu" de-serialize it into a MultiWorkUnit
            if (status.getPath().getName().endsWith(AbstractJobLauncher.MULTI_WORK_UNIT_FILE_EXTENSION)) {
                MultiWorkUnit mwu = MultiWorkUnit.createEmpty();
                try {
                    mwu.readFields(workUnitFileCloser.register(new DataInputStream(fs.open(status.getPath()))));
                } finally {
                    workUnitFileCloser.close();
                }
                for (WorkUnit wu : mwu.getWorkUnits()) {
                    JobLauncherUtils.cleanTaskStagingData(new WorkUnitState(wu), LOG);
                }
            }
        }
    } finally {
        try {
            cleanUpWorkingDirectory(mrJobDir, fs);
        } finally {
            super.abortJob(jobContext, state);
        }
    }
}

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

@Override
public void execute(String... args) throws Exception {
    OptionParser parser = new OptionParser();
    OptionSpec segmentTar = parser.accepts("segment-tar", "Use oak-segment-tar instead of oak-segment");
    OptionSet options = parser.parse(args);

    if (options.nonOptionArguments().isEmpty()) {
        System.out.println("usage: resetclusterid {<path>|<mongo-uri>}");
        System.exit(1);/*from  w  ww.  jav a  2 s.  c  o  m*/
    }

    String source = options.nonOptionArguments().get(0).toString();

    Closer closer = Closer.create();
    try {
        NodeStore store;
        if (args[0].startsWith(MongoURI.MONGODB_PREFIX)) {
            MongoClientURI uri = new MongoClientURI(source);
            MongoClient client = new MongoClient(uri);
            final DocumentNodeStore dns = new DocumentMK.Builder().setMongoDB(client.getDB(uri.getDatabase()))
                    .getNodeStore();
            closer.register(Utils.asCloseable(dns));
            store = dns;
        } else if (options.has(segmentTar)) {
            store = SegmentTarUtils.bootstrapNodeStore(source, closer);
        } else {
            FileStore fs = openFileStore(source);
            closer.register(Utils.asCloseable(fs));
            store = SegmentNodeStore.builder(fs).build();
        }

        deleteClusterId(store);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }

}

From source file:com.surgeplay.visage.master.VisageMaster.java

@Override
public void run() {
    try {/*from w w w  . j a v a 2  s  .c o  m*/
        Log.setLog(new LogShim(Visage.log));
        long total = Runtime.getRuntime().totalMemory();
        long max = Runtime.getRuntime().maxMemory();
        if (Visage.debug)
            Visage.log.finer("Current heap size: " + humanReadableByteCount(total, false));
        if (Visage.debug)
            Visage.log.finer("Max heap size: " + humanReadableByteCount(max, false));
        if (total < max) {
            Visage.log.warning(
                    "You have set your minimum heap size (Xms) lower than the maximum heap size (Xmx) - this can cause GC thrashing. It is strongly recommended to set them both to the same value.");
        }
        if (max < (1000 * 1000 * 1000)) {
            Visage.log.warning(
                    "The heap size (Xmx) is less than one gigabyte; it is recommended to run Visage with a gigabyte or more. Use -Xms1G and -Xmx1G to do this.");
        }
        Visage.log.info("Setting up Jetty");
        Server server = new Server(
                new InetSocketAddress(config.getString("http.bind"), config.getInt("http.port")));

        List<String> expose = config.getStringList("expose");
        String poweredBy;
        if (expose.contains("server")) {
            if (expose.contains("version")) {
                poweredBy = "Visage v" + Visage.VERSION;
            } else {
                poweredBy = "Visage";
            }
        } else {
            poweredBy = null;
        }

        ResourceHandler resource = new ResourceHandler();
        resource.setResourceBase(config.getString("http.static"));
        resource.setDirectoriesListed(false);
        resource.setWelcomeFiles(new String[] { "index.html" });
        resource.setHandler(new VisageHandler(this));

        if (!"/dev/null".equals(config.getString("log"))) {
            new File(config.getString("log")).getParentFile().mkdirs();
            server.setRequestLog(new AsyncNCSARequestLog(config.getString("log")));
        }
        GzipHandler gzip = new GzipHandler();
        gzip.setHandler(new HeaderHandler("X-Powered-By", poweredBy, resource));
        server.setHandler(gzip);

        String redisHost = config.getString("redis.host");
        int redisPort = config.getInt("redis.port");
        Visage.log.info("Connecting to Redis at " + redisHost + ":" + redisPort);
        resolverNum = config.getInt("redis.resolver-db");
        skinNum = config.getInt("redis.skin-db");
        JedisPoolConfig jpc = new JedisPoolConfig();
        jpc.setMaxIdle(config.getInt("redis.max-idle-connections"));
        jpc.setMaxTotal(config.getInt("redis.max-total-connections"));
        jpc.setMinIdle(config.getInt("redis.min-idle-connections"));
        if (config.hasPath("redis.password")) {
            password = config.getString("redis.password");
        }
        pool = new JedisPool(jpc, redisHost, redisPort);

        Visage.log.info("Connecting to RabbitMQ at " + config.getString("rabbitmq.host") + ":"
                + config.getInt("rabbitmq.port"));
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(config.getString("rabbitmq.host"));
        factory.setPort(config.getInt("rabbitmq.port"));
        factory.setRequestedHeartbeat(10);
        if (config.hasPath("rabbitmq.user")) {
            factory.setUsername(config.getString("rabbitmq.user"));
            factory.setPassword(config.getString("rabbitmq.password"));
        }
        String queue = config.getString("rabbitmq.queue");

        Closer closer = Closer.create();
        steve = ByteStreams.toByteArray(closer.register(ClassLoader.getSystemResourceAsStream("steve.png")));
        alex = ByteStreams.toByteArray(closer.register(ClassLoader.getSystemResourceAsStream("alex.png")));
        closer.close();

        conn = factory.newConnection();
        channel = conn.createChannel();
        if (Visage.debug)
            Visage.log.finer("Setting up queue '" + queue + "'");
        channel.queueDeclare(queue, false, false, true, null);
        channel.basicQos(1);

        if (Visage.debug)
            Visage.log.finer("Setting up reply queue");
        replyQueue = channel.queueDeclare().getQueue();
        consumer = new QueueingConsumer(channel);
        channel.basicConsume(replyQueue, consumer);

        if (config.getBoolean("slave.enable")) {
            Visage.log.info("Starting fallback slave");
            fallback = new VisageSlave(
                    config.getConfig("slave").withValue("rabbitmq", config.getValue("rabbitmq")));
            fallback.start();
        }
        Visage.log.info("Starting Jetty");
        server.start();
        Visage.log.info("Listening for finished jobs");
        try {
            while (run) {
                Delivery delivery = consumer.nextDelivery();
                if (Visage.trace)
                    Visage.log.finest("Got delivery");
                try {
                    String corrId = delivery.getProperties().getCorrelationId();
                    if (queuedJobs.containsKey(corrId)) {
                        if (Visage.trace)
                            Visage.log.finest("Valid");
                        responses.put(corrId, delivery.getBody());
                        Runnable run = queuedJobs.get(corrId);
                        queuedJobs.remove(corrId);
                        if (Visage.trace)
                            Visage.log.finest("Removed from queue");
                        run.run();
                        if (Visage.trace)
                            Visage.log.finest("Ran runnable");
                        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                        if (Visage.trace)
                            Visage.log.finest("Ack'd");
                    } else {
                        Visage.log.warning("Unknown correlation ID?");
                        channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, false);
                    }
                } catch (Exception e) {
                    Visage.log.log(Level.WARNING,
                            "An unexpected error occured while attempting to process a response.", e);
                }
            }
        } catch (InterruptedException e) {
        } catch (Exception e) {
            Visage.log.log(Level.SEVERE, "An unexpected error occured in the master run loop.", e);
            System.exit(2);
        }
        try {
            Visage.log.info("Shutting down master");
            server.stop();
            pool.destroy();
            conn.close(5000);
        } catch (Exception e) {
            Visage.log.log(Level.SEVERE, "A fatal error has occurred while shutting down the master.", e);
        }
    } catch (Exception e) {
        Visage.log.log(Level.SEVERE, "An unexpected error occured while initializing the master.", e);
        System.exit(1);
    }
}

From source file:eu.interedition.text.repository.JdbcStore.java

JdbcStore writeSchema() {
    final Closer closer = Closer.create();
    try {//  www. j  a va  2s.c  o  m
        restore(closer.register(
                new InputStreamReader(getClass().getResourceAsStream("schema.sql"), Charset.forName("UTF-8"))));
        return this;
    } finally {
        try {
            closer.close();
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
    }
}