Search in sources :

Example 46 with Closeable

use of java.io.Closeable in project bazel by bazelbuild.

the class AndroidResourceMerger method mergeData.

/** Merges all secondary resources with the primary resources. */
static MergedAndroidData mergeData(final ParsedAndroidData primary, final Path primaryManifest, final List<? extends SerializedAndroidData> direct, final List<? extends SerializedAndroidData> transitive, final Path resourcesOut, final Path assetsOut, @Nullable final PngCruncher cruncher, final VariantType type, @Nullable final Path symbolsOut, @Nullable AndroidResourceClassWriter rclassWriter) throws MergingException {
    Stopwatch timer = Stopwatch.createStarted();
    final ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(15));
    try (Closeable closeable = ExecutorServiceCloser.createWith(executorService)) {
        AndroidDataMerger merger = AndroidDataMerger.createWithPathDeduplictor(executorService);
        UnwrittenMergedAndroidData merged = merger.loadAndMerge(transitive, direct, primary, primaryManifest, type != VariantType.LIBRARY);
        logger.fine(String.format("merge finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        timer.reset().start();
        if (symbolsOut != null) {
            AndroidDataSerializer serializer = AndroidDataSerializer.create();
            merged.serializeTo(serializer);
            serializer.flushTo(symbolsOut);
            logger.fine(String.format("serialize merge finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
            timer.reset().start();
        }
        if (rclassWriter != null) {
            merged.writeResourceClass(rclassWriter);
            logger.fine(String.format("write classes finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
            timer.reset().start();
        }
        AndroidDataWriter writer = AndroidDataWriter.createWith(resourcesOut.getParent(), resourcesOut, assetsOut, cruncher, executorService);
        return merged.write(writer);
    } catch (IOException e) {
        throw MergingException.wrapException(e).build();
    } finally {
        logger.fine(String.format("write merge finished in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
    }
}
Also used : Closeable(java.io.Closeable) Stopwatch(com.google.common.base.Stopwatch) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) IOException(java.io.IOException)

Example 47 with Closeable

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

the class AbstractCacheService method registerListenerInternal.

protected String registerListenerInternal(String name, CacheEventListener listener, EventFilter eventFilter, boolean isLocal) {
    EventService eventService = getNodeEngine().getEventService();
    EventRegistration reg;
    if (isLocal) {
        if (eventFilter == null) {
            reg = eventService.registerLocalListener(AbstractCacheService.SERVICE_NAME, name, listener);
        } else {
            reg = eventService.registerLocalListener(AbstractCacheService.SERVICE_NAME, name, eventFilter, listener);
        }
    } else {
        if (eventFilter == null) {
            reg = eventService.registerListener(AbstractCacheService.SERVICE_NAME, name, listener);
        } else {
            reg = eventService.registerListener(AbstractCacheService.SERVICE_NAME, name, eventFilter, listener);
        }
    }
    String id = reg.getId();
    if (listener instanceof Closeable) {
        closeableListeners.put(id, (Closeable) listener);
    } else if (listener instanceof CacheEntryListenerProvider) {
        CacheEntryListener cacheEntryListener = ((CacheEntryListenerProvider) listener).getCacheEntryListener();
        if (cacheEntryListener instanceof Closeable) {
            closeableListeners.put(id, (Closeable) cacheEntryListener);
        }
    }
    return id;
}
Also used : EventRegistration(com.hazelcast.spi.EventRegistration) CacheEntryListener(javax.cache.event.CacheEntryListener) Closeable(java.io.Closeable) EventService(com.hazelcast.spi.EventService)

Example 48 with Closeable

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

the class AbstractCacheService method deleteCacheResources.

private void deleteCacheResources(String name) {
    Set<Closeable> cacheResources;
    ContextMutexFactory.Mutex mutex = cacheResourcesMutexFactory.mutexFor(name);
    try {
        synchronized (mutex) {
            cacheResources = resources.remove(name);
        }
    } finally {
        mutex.close();
    }
    if (cacheResources != null) {
        for (Closeable resource : cacheResources) {
            IOUtil.closeResource(resource);
        }
        cacheResources.clear();
    }
}
Also used : Closeable(java.io.Closeable) ContextMutexFactory(com.hazelcast.util.ContextMutexFactory)

Example 49 with Closeable

use of java.io.Closeable in project h2o-3 by h2oai.

the class MojoReaderBackendFactoryTest method testCreateReaderBackend_URL_Memory.

@Test
public void testCreateReaderBackend_URL_Memory() throws Exception {
    URL dumjo = MojoReaderBackendFactoryTest.class.getResource("dumjo.zip");
    assertNotNull(dumjo);
    MojoReaderBackend r = MojoReaderBackendFactory.createReaderBackend(dumjo, CachingStrategy.MEMORY);
    assertTrue(r instanceof InMemoryMojoReaderBackend);
    try {
        assertTrue(r.exists("binary-file"));
        assertTrue(r.exists("text-file"));
        assertEquals("line1", r.getTextFile("text-file").readLine());
    } finally {
        ((Closeable) r).close();
    }
}
Also used : Closeable(java.io.Closeable) URL(java.net.URL) Test(org.junit.Test)

Example 50 with Closeable

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

the class ClassLocator method tryToGetClassFromRemote.

private Class<?> tryToGetClassFromRemote(String name) throws ClassNotFoundException {
    // we need to acquire a classloading lock before defining a class
    // Java 7+ can use locks with per-class granularity while Java 6 has to use a single lock
    // mutexFactory abstract these differences away
    Closeable classMutex = mutexFactory.getMutexForClass(name);
    try {
        synchronized (classMutex) {
            ClassSource classSource = classSourceMap.get(name);
            if (classSource != null) {
                if (logger.isFineEnabled()) {
                    logger.finest("Class " + name + " is already in a local cache. ");
                }
                return classSource.getClazz();
            }
            byte[] classDef = fetchBytecodeFromRemote(name);
            if (classDef == null) {
                throw new ClassNotFoundException("Failed to load class " + name + " from other members.");
            }
            return defineAndCacheClass(name, classDef);
        }
    } finally {
        IOUtil.closeResource(classMutex);
    }
}
Also used : Closeable(java.io.Closeable)

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