Example usage for java.lang AutoCloseable close

List of usage examples for java.lang AutoCloseable close

Introduction

In this page you can find the example usage for java.lang AutoCloseable close.

Prototype

void close() throws Exception;

Source Link

Document

Closes this resource, relinquishing any underlying resources.

Usage

From source file:com.splicemachine.mrio.api.core.SMRecordReaderImpl.java

@Override
public void close() throws IOException {
    IOException lastThrown = null;
    if (LOG.isDebugEnabled())
        SpliceLogUtils.debug(LOG, "close");
    if (localTxn != null) {
        try {/*from w w  w. java  2 s  . c  o m*/
            localTxn.commit();
        } catch (IOException ioe) {
            try {
                localTxn.rollback();
            } catch (Exception e) {
                ioe.addSuppressed(e);
            }
            lastThrown = ioe;
        }
    }
    if (activationHolder != null) {
        //activationHolder.close();
    }

    for (AutoCloseable c : closeables) {
        if (c != null) {
            try {
                c.close();
            } catch (Exception e) {
                if (lastThrown != null)
                    lastThrown.addSuppressed(e);
                else
                    lastThrown = e instanceof IOException ? (IOException) e : new IOException(e);
            }
        }
    }
    if (lastThrown != null) {
        throw lastThrown;
    }
}

From source file:io.github.mywarp.mywarp.bukkit.MyWarpPlugin.java

@Override
public void onDisable() {
    unregister();/*from ww w  .j av a  2  s .  com*/

    //close any registered Closables
    for (AutoCloseable closeable : closeables) {
        try {
            closeable.close();
        } catch (Exception e) {
            log.warn("Failed to close " + closeable.getClass().getCanonicalName(), e);
        }
    }
}

From source file:org.apache.drill.exec.service.ServiceEngine.java

private void submit(Executor p, final String name, final AutoCloseable c) {
    p.execute(new Runnable() {
        @Override//w w  w.ja  v a2  s.c  o  m
        public void run() {
            Stopwatch watch = Stopwatch.createStarted();
            try {
                c.close();
            } catch (Exception e) {
                logger.warn("Failure while closing {}.", name, e);
            }
            long elapsed = watch.elapsed(MILLISECONDS);
            if (elapsed > 500) {
                logger.info("closed " + name + " in " + elapsed + " ms");
            }
        }
    });
}

From source file:org.onos.yangtools.yang2sources.plugin.YangToSourcesProcessor.java

private ContextHolder processYang() throws MojoExecutionException {
    YangParserImpl parser = new YangParserImpl();
    List<Closeable> closeables = new ArrayList<>();
    log.info(Util.message("Inspecting %s", LOG_PREFIX, yangFilesRootDir));
    try {//from   w  w w.j  ava2s.com
        /*
         * Collect all files which affect YANG context. This includes all
         * files in current project and optionally any jars/files in the
         * dependencies.
         */
        final Collection<File> yangFilesInProject = Util.listFiles(yangFilesRootDir, excludedFiles, log);
        final Collection<File> allFiles = new ArrayList<>(yangFilesInProject);
        if (inspectDependencies) {
            allFiles.addAll(Util.findYangFilesInDependencies(log, project));
        }

        if (allFiles.isEmpty()) {
            log.info(Util.message("No input files found", LOG_PREFIX));
            return null;
        }

        /*
         * Check if any of the listed files changed. If no changes occurred,
         * simply return null, which indicates and of execution.
         */
        boolean noChange = true;
        for (final File f : allFiles) {
            if (buildContext.hasDelta(f)) {
                log.debug(Util.message("buildContext %s indicates %s changed, forcing regeneration", LOG_PREFIX,
                        buildContext, f));
                noChange = false;
            }
        }

        if (noChange) {
            log.info(Util.message("None of %s input files changed", LOG_PREFIX, allFiles.size()));
            return null;
        }

        final List<InputStream> yangsInProject = new ArrayList<>();
        for (final File f : yangFilesInProject) {
            yangsInProject
                    .add(new NamedFileInputStream(f, META_INF_YANG_STRING + File.separator + f.getName()));
        }

        List<InputStream> all = new ArrayList<>(yangsInProject);
        closeables.addAll(yangsInProject);
        Map<InputStream, Module> allYangModules;

        /**
         * Set contains all modules generated from input sources. Number of
         * modules may differ from number of sources due to submodules
         * (parsed submodule's data are added to its parent module). Set
         * cannot contains null values.
         */
        Set<Module> projectYangModules;
        try {
            if (inspectDependencies) {
                YangsInZipsResult dependentYangResult = Util.findYangFilesInDependenciesAsStream(log, project);
                Closeable dependentYangResult1 = dependentYangResult;
                closeables.add(dependentYangResult1);
                all.addAll(dependentYangResult.getYangStreams());
            }

            allYangModules = parser.parseYangModelsFromStreamsMapped(all);

            projectYangModules = new HashSet<>();
            for (InputStream inProject : yangsInProject) {
                Module module = allYangModules.get(inProject);
                if (module != null) {
                    projectYangModules.add(module);
                }
            }

        } finally {
            for (AutoCloseable closeable : closeables) {
                closeable.close();
            }
        }

        Set<Module> parsedAllYangModules = new HashSet<>(allYangModules.values());
        SchemaContext resolveSchemaContext = parser.resolveSchemaContext(parsedAllYangModules);
        log.info(Util.message("%s files parsed from %s", LOG_PREFIX, Util.YANG_SUFFIX.toUpperCase(),
                yangsInProject));
        return new ContextHolder(resolveSchemaContext, projectYangModules);

        // MojoExecutionException is thrown since execution cannot continue
    } catch (Exception e) {
        String message = Util.message("Unable to parse %s files from %s", LOG_PREFIX, Util.YANG_SUFFIX,
                yangFilesRootDir);
        log.error(message, e);
        throw new MojoExecutionException(message, e);
    }
}

From source file:org.apache.drill.test.ClusterFixture.java

private Exception safeClose(AutoCloseable item, Exception ex) {
    try {//from w ww.jav a2 s  . com
        if (item != null) {
            item.close();
        }
    } catch (Exception e) {
        ex = ex == null ? e : ex;
    }
    return ex;
}

From source file:com.streamsets.pipeline.stage.origin.jdbc.JdbcSource.java

private void closeQuietly(AutoCloseable c) {
    if (c != null) {
        try {//w w w  .  j  a  v  a 2 s.com
            c.close();
        } catch (Exception ex) {
            LOG.debug("Error while closing: {}", ex.toString(), ex);
        }
    }
}

From source file:com.emc.ecs.sync.EcsSync.java

private void safeClose(AutoCloseable closeable) {
    try {/*from  w  ww. j av a 2s  .c o m*/
        if (closeable != null)
            closeable.close();
    } catch (Throwable t) {
        log.warn("could not close " + closeable.getClass().getSimpleName(), t);
    }
}

From source file:com.splicemachine.derby.impl.sql.execute.operations.SpliceBaseOperation.java

@Override
public void close() throws StandardException {
    try {/* w w  w.java 2  s.  c om*/
        if (LOG_CLOSE.isTraceEnabled())
            LOG_CLOSE.trace(String.format("closing operation %s", this));
        if (remoteQueryClient != null) {
            remoteQueryClient.close();
        }
        if (closeables != null) {
            for (AutoCloseable closeable : closeables) {
                closeable.close();
            }
            closeables = null;
        }
        clearCurrentRow();
        for (SpliceOperation op : getSubOperations())
            op.close();

        /* If this is the top ResultSet then we must
           * close all of the open subqueries for the
         * entire query.
         */
        if (isTopResultSet) {

            LanguageConnectionContext lcc = getActivation().getLanguageConnectionContext();

            int staLength = (subqueryTrackingArray == null) ? 0 : subqueryTrackingArray.length;

            for (int index = 0; index < staLength; index++) {
                if (subqueryTrackingArray[index] == null) {
                    continue;
                }
                if (subqueryTrackingArray[index].isClosed()) {
                    continue;
                }
                subqueryTrackingArray[index].close();
            }
        }
        isOpen = false;
        operationContext = null;
    } catch (Exception e) {
        throw Exceptions.parseException(e);
    }
}

From source file:com.streamsets.pipeline.lib.jdbc.JdbcUtil.java

public void closeQuietly(AutoCloseable c) {
    try {//from w  w w.  ja  va  2  s  .  c o  m
        if (null != c) {
            c.close();
        }
    } catch (Exception ignored) {
    }
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlPage.java

/**
 * Clean up this page.//from   w w w . j  av  a 2  s .c o m
 */
@Override
public void cleanUp() {
    //To avoid endless recursion caused by window.close() in onUnload
    if (cleaning_) {
        return;
    }
    cleaning_ = true;
    super.cleanUp();
    executeEventHandlersIfNeeded(Event.TYPE_UNLOAD);
    deregisterFramesIfNeeded();
    cleaning_ = false;
    if (autoCloseableList_ != null) {
        for (final AutoCloseable closeable : new ArrayList<>(autoCloseableList_)) {
            try {
                closeable.close();
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}