public void testAppContextClassHierarchy() { Class<?>[] clazz = ClassUtils.getClassHierarchy(OsgiBundleXmlApplicationContext.class, ClassUtils.ClassSet.ALL_CLASSES); //Closeable.class, Class<?>[] expected = new Class<?>[] { OsgiBundleXmlApplicationContext.class, AbstractDelegatedExecutionApplicationContext.class, AbstractOsgiBundleApplicationContext.class, AbstractRefreshableApplicationContext.class, AbstractApplicationContext.class, DefaultResourceLoader.class, ResourceLoader.class, AutoCloseable.class, DelegatedExecutionOsgiBundleApplicationContext.class, ConfigurableOsgiBundleApplicationContext.class, ConfigurableApplicationContext.class, ApplicationContext.class, Lifecycle.class, Closeable.class, EnvironmentCapable.class, ListableBeanFactory.class, HierarchicalBeanFactory.class, ApplicationEventPublisher.class, ResourcePatternResolver.class, MessageSource.class, BeanFactory.class, DisposableBean.class }; assertTrue(compareArrays(expected, clazz)); }
@After public void destroy() { if (nativeHibernateSessionFactoryBootstrap()) { sf.close(); } else { emf.close(); } for (Closeable closeable : closeables) { try { closeable.close(); } catch (IOException e) { LOGGER.error("Failure", e); } } closeables.clear(); }
@Test public void getSQLCommandsFromFile_closeStream_ioException() throws Exception { // given handler = spy(new DatabaseUpgradeHandler()); // when try { handler.getSQLCommandsFromFile(new File("unknownpath")); fail(); } catch (FileNotFoundException e) { // then assertNotNull(e); verify(handler, times(3)).closeStream(any(Closeable.class)); } }
/** * Test the default functionality of schema support method. In * this case the schema source property is set. * @throws Exception If any errors occur. */ @Test(dataProvider = "schema-source") public void testCheckSchemaSupport2(Object schemaSource) throws Exception { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(true); dbf.setNamespaceAware(true); dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", W3C_XML_SCHEMA_NS_URI); dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaSource); MyErrorHandler eh = MyErrorHandler.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); db.setErrorHandler(eh); db.parse(new File(XML_DIR, "test1.xml")); assertFalse(eh.isErrorOccured()); } finally { if (schemaSource instanceof Closeable) { ((Closeable) schemaSource).close(); } } }
private void handleClose(MsgClose m) throws IOException { int fileID = m.getFileID(); log.info(getName()+": Request close file "+fileID); Closeable stream; synchronized (localIS) { stream = localIS.get(m.getFileID()); } if (stream == null) { // Maybe an OutputStream? synchronized (localOS) { stream = localOS.get(m.getFileID()); } } if (stream == null) { // File descriptor not found log.warning(getName()+": Local file ID "+fileID+" not found"); } else { try { stream.close(); log.fine(getName()+": Closed file "+fileID); } catch (IOException e) { // Exception during skip log.warning(getName()+": Error when closing file ID "+fileID+": "+e.getMessage()); } } }
@Override public void close() { Set<Node> nodes; synchronized (current) { nodes = new HashSet<>(current); } for (Node n : nodes) { for (Closeable c : n.getLookup().lookupAll(Closeable.class)) { try { c.close(); } catch (IOException ex) { LOGGER.log(Level.INFO, null, ex); } } } }
/** * This method verifies that the given {@code metaData} holds sane values to create an {@link IndexService}. * This method tries to update the meta data of the created {@link IndexService} if the given {@code metaDataUpdate} is different from the given {@code metaData}. * This method will throw an exception if the creation or the update fails. * The created {@link IndexService} will not be registered and will be closed immediately. */ public synchronized void verifyIndexMetadata(IndexMetaData metaData, IndexMetaData metaDataUpdate) throws IOException { final List<Closeable> closeables = new ArrayList<>(); try { IndicesFieldDataCache indicesFieldDataCache = new IndicesFieldDataCache(settings, new IndexFieldDataCache.Listener() {}); closeables.add(indicesFieldDataCache); IndicesQueryCache indicesQueryCache = new IndicesQueryCache(settings); closeables.add(indicesQueryCache); // this will also fail if some plugin fails etc. which is nice since we can verify that early final IndexService service = createIndexService("metadata verification", metaData, indicesQueryCache, indicesFieldDataCache, emptyList(), s -> {}); closeables.add(() -> service.close("metadata verification", false)); service.mapperService().merge(metaData, MapperService.MergeReason.MAPPING_RECOVERY, true); if (metaData.equals(metaDataUpdate) == false) { service.updateMetaData(metaDataUpdate); } } finally { IOUtils.close(closeables); } }
/** * Close closable object and wrap {@link IOException} with {@link RuntimeException} * @param closeable closeable object */ public static void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } } }
static void closeQuietly(/*Auto*/Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (RuntimeException rethrown) { throw rethrown; } catch (Exception ignored) { } } }
@Test public void connectionIsMonitored() throws Exception { factory.getProcessor(transport); ArgumentCaptor<TTransport> transportCaptor = ArgumentCaptor.forClass(TTransport.class); ArgumentCaptor<Closeable> handlerCaptor = ArgumentCaptor.forClass(Closeable.class); verify(transportMonitor).monitor(transportCaptor.capture(), handlerCaptor.capture()); assertThat(transportCaptor.getValue(), is(transport)); assertThat(handlerCaptor.getValue(), is(instanceOf(FederatedHMSHandler.class))); }
public static void quietClose(Closeable closeable){ if (closeable != null) { try { closeable.close(); } catch (Throwable e) { } } }
private void tryToClose(@Nullable Closeable c) { try { if (c != null) c.close(); } catch (IOException e) { e.printStackTrace(); } }
public static void close(Closeable closeable) { if (null == closeable) return; try { closeable.close(); } catch (IOException e) { e.printStackTrace(); } }
private ClassLoader storeClassLoader(ClassLoader classLoader) { final ClassLoader previous = currentClassloader.getAndSet(classLoader); if (previous != null && previous instanceof Closeable) { loadersToClose.add(new SoftReference<Closeable>(Cast.cast(Closeable.class, previous))); } return classLoader; }
/** * Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link * java.io.OutputStream} and {@link InputStream} are {@code Closeable}. * * @param closeable The {@link Closeable} to close. */ public static void closeQuietly(Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { // Ignore. } }
private RuntimeRegistratorFtlTemplate(RuntimeBeanEntry runtimeBeanEntry, String name, List<Field> fields, List<MethodDefinition> methods) { // TODO header super(null, runtimeBeanEntry.getPackageName(), name, Collections .emptyList(), Collections.singletonList(Closeable.class .getCanonicalName()), fields, methods); }
/** * Close all the proxy objects which have been opened over the lifetime of * this proxy provider. */ @Override public synchronized void close() throws IOException { for (AddressRpcProxyPair<T> proxy : proxies) { if (proxy.namenode != null) { if (proxy.namenode instanceof Closeable) { ((Closeable)proxy.namenode).close(); } else { RPC.stopProxy(proxy.namenode); } } } }
public static void close(Closeable... closeables) { if (closeables != null) { for (Closeable it : closeables) { if (it != null) { try { it.close(); } catch (Exception e) { e.printStackTrace(); } } } } }
public static PngSizeInfo makeFromResource(IResource resource) throws IOException { PngSizeInfo pngsizeinfo; try { pngsizeinfo = new PngSizeInfo(resource.getInputStream()); } finally { IOUtils.closeQuietly((Closeable)resource); } return pngsizeinfo; }
public static void closeSilently(@Nullable Closeable c) { if (c == null) return; try { c.close(); } catch (Throwable t) { // Do nothing } }
/** * Closes the given {@code Closeable}. Suppresses any IO exceptions. */ public static void closeQuietly(Closeable closeable) { try { if (closeable != null) { closeable.close(); } } catch (IOException e) { Log.w(TAG, "Failed to close resource", e); } }
public static void close(@Nullable Closeable c) { if (c != null && (c instanceof Closeable)) { try { c.close(); } catch (IOException e) { } } }
public static void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException e) { e.printStackTrace(); } } }
public static void closeQuietly(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (RuntimeException rethrown) { throw rethrown; } catch (Exception ignored) { } } }
/** * Close the stream which implements Closeable interface * * @param stream */ private static void closeStream(Closeable stream) { if (null != stream) { try { stream.close(); } catch (IOException e) { stream = null; e.printStackTrace(); } } }
public void close() throws IOException { closed.set(true); executorService.shutdownNow(); if (httpclient instanceof Closeable) { ((Closeable) httpclient).close(); } }
public static void closeQuietly(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (Throwable ignored) { LogUtil.d(ignored.getMessage(), ignored); } } }
public static void closeSilently(Closeable c) { if (c == null) return; try { c.close(); } catch (IOException t) { Log.w(TAG, "close fail ", t); } }
/** * 允许“一口气”关闭多个Closeable的方法 * * @param closeables 多个Closeable对象 */ public static void closeQuietly(final Closeable... closeables) { if (closeables == null) { return; } for (final Closeable closeable : closeables) { closeQuietly(closeable); } }
public static void close(Closeable... closables) { for (Closeable closable : closables) { try { closable.close(); } catch (Exception e) { } } }
/** * For when you don't care if the Closeable you're closing is null, and * you don't care that closing a buffer threw an exception, but * you still care enough that logging might be useful, like in a * "finally" block, after you've already returned to the caller. */ public static void closeThisThingOrMaybeDont(@Nullable Closeable closeable) { if (closeable == null) { log.d("Can't close closable, arg was null."); return; } try { closeable.close(); } catch (IOException e) { log.d("Couldn't close closable, but that's apparently OK. Error was: " + e.getMessage()); } }
private static void closeStream(Closeable stream) { if (stream != null) { try { stream.close(); } catch (IOException e) { } } }
/** * Closes this URLClassLoader, so that it can no longer be used to load * new classes or resources that are defined by this loader. * Classes and resources defined by any of this loader's parents in the * delegation hierarchy are still accessible. Also, any classes or resources * that are already loaded, are still accessible. * <p> * In the case of jar: and file: URLs, it also closes any files * that were opened by it. If another thread is loading a * class when the {@code close} method is invoked, then the result of * that load is undefined. * <p> * The method makes a best effort attempt to close all opened files, * by catching {@link IOException}s internally. Unchecked exceptions * and errors are not caught. Calling close on an already closed * loader has no effect. * <p> * @exception IOException if closing any file opened by this class loader * resulted in an IOException. Any such exceptions are caught internally. * If only one is caught, then it is re-thrown. If more than one exception * is caught, then the second and following exceptions are added * as suppressed exceptions of the first one caught, which is then re-thrown. * * @exception SecurityException if a security manager is set, and it denies * {@link RuntimePermission}{@code ("closeClassLoader")} * * @since 1.7 */ public void close() throws IOException { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkPermission(new RuntimePermission("closeClassLoader")); } List<IOException> errors = ucp.closeLoaders(); // now close any remaining streams. synchronized (closeables) { Set<Closeable> keys = closeables.keySet(); for (Closeable c : keys) { try { c.close(); } catch (IOException ioex) { errors.add(ioex); } } closeables.clear(); } if (errors.isEmpty()) { return; } IOException firstex = errors.remove(0); // Suppress any remaining exceptions for (IOException error: errors) { firstex.addSuppressed(error); } throw firstex; }
public static void a(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (Throwable th) { } } }
/** * 关闭IO * * @param closeables closeables */ public static void closeIO(final Closeable... closeables) { if (closeables == null) { return; } for (Closeable closeable : closeables) { if (closeable != null) { try { closeable.close(); } catch (IOException e) { e.printStackTrace(); } } } }