Search in sources :

Example 51 with Closeable

use of java.io.Closeable in project hazelcast by hazelcast.

the class EventServiceImpl method close.

@Override
public void close(EventRegistration eventRegistration) {
    Registration registration = (Registration) eventRegistration;
    Object listener = registration.getListener();
    if (!(listener instanceof Closeable)) {
        return;
    }
    try {
        ((Closeable) listener).close();
    } catch (IOException e) {
        EmptyStatement.ignore(e);
    }
}
Also used : EventRegistration(com.hazelcast.spi.EventRegistration) Closeable(java.io.Closeable) IOException(java.io.IOException)

Example 52 with Closeable

use of java.io.Closeable in project fresco by facebook.

the class StatefulProducerRunnableTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    mException = new ConcurrentModificationException();
    mSuccessMap = new HashMap<>();
    mSuccessMap.put("state", "success");
    mFailureMap = new HashMap<>();
    mFailureMap.put("state", "failure");
    mCancellationMap = new HashMap<>();
    mCancellationMap.put("state", "cancelled");
    mStatefulProducerRunnable = new StatefulProducerRunnable<Closeable>(mConsumer, mProducerListener, PRODUCER_NAME, REQUEST_ID) {

        @Override
        protected void disposeResult(Closeable result) {
            try {
                result.close();
            } catch (IOException ioe) {
                throw new RuntimeException("unexpected IOException", ioe);
            }
        }

        @Override
        protected Closeable getResult() throws Exception {
            return mResultSupplier.get();
        }

        @Override
        protected Map<String, String> getExtraMapOnCancellation() {
            return mCancellationMap;
        }

        @Override
        protected Map<String, String> getExtraMapOnFailure(Exception exception) {
            return mFailureMap;
        }

        @Override
        protected Map<String, String> getExtraMapOnSuccess(Closeable result) {
            return mSuccessMap;
        }
    };
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) Closeable(java.io.Closeable) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) IOException(java.io.IOException) ConcurrentModificationException(java.util.ConcurrentModificationException)

Example 53 with Closeable

use of java.io.Closeable in project guava by google.

the class FinalizableReferenceQueueClassLoaderUnloadingTest method doTestUnloadableInStaticFieldIfClosed.

// If you have a FinalizableReferenceQueue that is a static field of one of the classes of your
// app (like the FrqUser class above), then the app's ClassLoader will never be gc'd. The reason
// is that we attempt to run a thread in a separate ClassLoader that will detect when the FRQ
// is no longer referenced, meaning that the app's ClassLoader has been gc'd, and when that
// happens. But the thread's supposedly separate ClassLoader actually has a reference to the app's
// ClasLoader via its AccessControlContext. It does not seem to be possible to make a
// URLClassLoader without capturing this reference, and it probably would not be desirable for
// security reasons anyway. Therefore, the FRQ.close() method provides a way to stop the thread
// explicitly. This test checks that calling that method does allow an app's ClassLoader to be
// gc'd even if there is a still a FinalizableReferenceQueue in a static field. (Setting the field
// to null would also work, but only if there are no references to the FRQ anywhere else.)
private WeakReference<ClassLoader> doTestUnloadableInStaticFieldIfClosed() throws Exception {
    final URLClassLoader myLoader = (URLClassLoader) getClass().getClassLoader();
    final URL[] urls = myLoader.getURLs();
    URLClassLoader sepLoader = new URLClassLoader(urls, myLoader.getParent());
    Class<?> frqC = FinalizableReferenceQueue.class;
    Class<?> sepFrqC = sepLoader.loadClass(frqC.getName());
    assertNotSame(frqC, sepFrqC);
    Class<?> sepFrqSystemLoaderC = sepLoader.loadClass(FinalizableReferenceQueue.SystemLoader.class.getName());
    Field disabled = sepFrqSystemLoaderC.getDeclaredField("disabled");
    disabled.setAccessible(true);
    disabled.set(null, true);
    Class<?> frqUserC = FrqUser.class;
    Class<?> sepFrqUserC = sepLoader.loadClass(frqUserC.getName());
    assertNotSame(frqUserC, sepFrqUserC);
    assertSame(sepLoader, sepFrqUserC.getClassLoader());
    Callable<?> sepFrqUser = (Callable<?>) sepFrqUserC.newInstance();
    WeakReference<?> finalizableWeakReference = (WeakReference<?>) sepFrqUser.call();
    GcFinalization.awaitClear(finalizableWeakReference);
    Field sepFrqUserFinalizedF = sepFrqUserC.getField("finalized");
    Semaphore finalizeCount = (Semaphore) sepFrqUserFinalizedF.get(null);
    boolean finalized = finalizeCount.tryAcquire(5, TimeUnit.SECONDS);
    assertTrue(finalized);
    Field sepFrqUserFrqF = sepFrqUserC.getField("frq");
    Closeable frq = (Closeable) sepFrqUserFrqF.get(null);
    frq.close();
    return new WeakReference<ClassLoader>(sepLoader);
}
Also used : Closeable(java.io.Closeable) Semaphore(java.util.concurrent.Semaphore) URL(java.net.URL) Callable(java.util.concurrent.Callable) Field(java.lang.reflect.Field) URLClassLoader(java.net.URLClassLoader) WeakReference(java.lang.ref.WeakReference)

Example 54 with Closeable

use of java.io.Closeable in project hazelcast by hazelcast.

the class IOUtilTest method testCloseResource.

@Test
public void testCloseResource() throws Exception {
    Closeable closeable = mock(Closeable.class);
    closeResource(closeable);
    verify(closeable).close();
    verifyNoMoreInteractions(closeable);
}
Also used : Closeable(java.io.Closeable) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 55 with Closeable

use of java.io.Closeable in project OpenAM by OpenRock.

the class SimpleTaskExecutorTest method testExecute.

@Test
public void testExecute() throws Exception {
    // given
    Task task = mock(Task.class);
    Closeable connection = mock(Closeable.class);
    when(connectionFactory.create()).thenReturn(connection);
    when(connectionFactory.isValid(connection)).thenReturn(true);
    executor.start();
    // when
    executor.execute(null, task);
    // then
    verify(task).execute(connection, adapter);
}
Also used : Task(org.forgerock.openam.sm.datalayer.api.Task) Closeable(java.io.Closeable) Test(org.testng.annotations.Test)

Aggregations

Closeable (java.io.Closeable)216 IOException (java.io.IOException)88 Test (org.junit.Test)56 ArrayList (java.util.ArrayList)29 File (java.io.File)26 HashMap (java.util.HashMap)12 VirtualFile (org.jboss.vfs.VirtualFile)12 URL (java.net.URL)9 Path (java.nio.file.Path)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Map (java.util.Map)7 ISE (io.druid.java.util.common.ISE)6 InputStream (java.io.InputStream)6 MountHandle (org.jboss.as.server.deployment.module.MountHandle)6 ResourceRoot (org.jboss.as.server.deployment.module.ResourceRoot)6 ProgramController (co.cask.cdap.app.runtime.ProgramController)5 ProgramType (co.cask.cdap.proto.ProgramType)4 ProgramId (co.cask.cdap.proto.id.ProgramId)4 Pair (io.druid.java.util.common.Pair)4 FileOutputStream (java.io.FileOutputStream)4