Search in sources :

Example 1 with JCacheManager

use of org.infinispan.jcache.embedded.JCacheManager in project infinispan by infinispan.

the class JCacheLoaderTest method testLoadEntryWithExpiration.

public void testLoadEntryWithExpiration(Method m) {
    final String cacheName = m.getName();
    withCacheManager(new CacheManagerCallable(TestCacheManagerFactory.createCacheManager(false)) {

        @Override
        public void call() {
            ControlledTimeService timeService = new ControlledTimeService();
            TestingUtil.replaceComponent(cm, TimeService.class, timeService, true);
            JCacheManager jCacheManager = createJCacheManager(cm, this);
            InMemoryJCacheLoader<Integer, String> cacheLoader = new InMemoryJCacheLoader<>();
            cacheLoader.store(1, "v1").store(2, "v2");
            MutableConfiguration<Integer, String> cfg = new MutableConfiguration<>();
            final long lifespan = 3000;
            cfg.setReadThrough(true);
            cfg.setExpiryPolicyFactory((Factory<ExpiryPolicy>) () -> new ExpiryPolicy() {

                @Override
                public Duration getExpiryForCreation() {
                    return new Duration(TimeUnit.MILLISECONDS, lifespan);
                }

                @Override
                public Duration getExpiryForAccess() {
                    return null;
                }

                @Override
                public Duration getExpiryForUpdate() {
                    return Duration.ZERO;
                }
            });
            cfg.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
            Cache<Integer, String> cache = jCacheManager.createCache(cacheName, cfg);
            assertEquals("v2", cache.get(2));
            assertEquals("v1", cache.get(1));
            timeService.advance(lifespan + 100);
            DataContainer<Integer, String> dc = cache.unwrap(AdvancedCache.class).getDataContainer();
            assertEquals(null, dc.peek(2));
            assertEquals(null, dc.peek(1));
        }
    });
}
Also used : JCacheManager(org.infinispan.jcache.embedded.JCacheManager) ControlledTimeService(org.infinispan.util.ControlledTimeService) TimeService(org.infinispan.commons.time.TimeService) Factory(javax.cache.configuration.Factory) TestCacheManagerFactory(org.infinispan.test.fwk.TestCacheManagerFactory) InMemoryJCacheLoader(org.infinispan.jcache.util.InMemoryJCacheLoader) Duration(javax.cache.expiry.Duration) MutableConfiguration(javax.cache.configuration.MutableConfiguration) DataContainer(org.infinispan.container.DataContainer) CacheManagerCallable(org.infinispan.test.CacheManagerCallable) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) ControlledTimeService(org.infinispan.util.ControlledTimeService) AdvancedCache(org.infinispan.AdvancedCache) Cache(javax.cache.Cache)

Example 2 with JCacheManager

use of org.infinispan.jcache.embedded.JCacheManager in project infinispan by infinispan.

the class JCacheLoaderTest method testLoadAllWithJCacheLoader.

public void testLoadAllWithJCacheLoader(Method m) {
    final String cacheName = m.getName();
    // GlobalConfigurationBuilder globalBuilder = new GlobalConfigurationBuilder();
    // globalBuilder.asyncTransportExecutor().addProperty("maxThreads", "1");
    withCacheManager(new CacheManagerCallable(TestCacheManagerFactory.createCacheManager(false)) {

        @Override
        public void call() {
            JCacheManager jCacheManager = createJCacheManager(cm, this);
            InMemoryJCacheLoader<Integer, String> cacheLoader = new InMemoryJCacheLoader<>();
            cacheLoader.store(1, "v1").store(2, "v2");
            MutableConfiguration<Integer, String> cfg = new MutableConfiguration<>();
            cfg.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
            Cache<Integer, String> cache = jCacheManager.createCache(cacheName, cfg);
            assertEquals(0, cacheLoader.getLoadCount());
            CompletionListenerFuture future = new CompletionListenerFuture();
            cache.loadAll(Util.asSet(1, 2), true, future);
            futureGet(future);
            assertEquals(2, cacheLoader.getLoadCount());
        }
    });
}
Also used : CacheManagerCallable(org.infinispan.test.CacheManagerCallable) JCacheManager(org.infinispan.jcache.embedded.JCacheManager) InMemoryJCacheLoader(org.infinispan.jcache.util.InMemoryJCacheLoader) CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) MutableConfiguration(javax.cache.configuration.MutableConfiguration) AdvancedCache(org.infinispan.AdvancedCache) Cache(javax.cache.Cache)

Example 3 with JCacheManager

use of org.infinispan.jcache.embedded.JCacheManager in project infinispan by infinispan.

the class JCacheConfigurationTest method testProgrammaticConfigurationHolder.

public void testProgrammaticConfigurationHolder() throws Exception {
    URI uri = JCacheConfigurationTest.class.getClassLoader().getResource("infinispan_uri.xml").toURI();
    Properties properties = getProperties();
    ConfigurationBuilderHolder cbh = new ConfigurationBuilderHolder();
    cbh.getGlobalConfigurationBuilder().addModule(PrivateGlobalConfigurationBuilder.class);
    properties.put(ConfigurationBuilderHolder.class.getName(), cbh);
    withCachingProvider(provider -> {
        try (JCacheManager jCacheManager = new JCacheManager(uri, provider.getClass().getClassLoader(), provider, properties)) {
            DefaultCacheManager defaultCacheManager = jCacheManager.unwrap(DefaultCacheManager.class);
            assertNotNull(defaultCacheManager.getCacheManagerConfiguration().module(PrivateGlobalConfiguration.class));
        }
    });
}
Also used : DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) ConfigurationBuilderHolder(org.infinispan.configuration.parsing.ConfigurationBuilderHolder) PrivateGlobalConfiguration(org.infinispan.configuration.internal.PrivateGlobalConfiguration) JCacheManager(org.infinispan.jcache.embedded.JCacheManager) Properties(java.util.Properties) URI(java.net.URI)

Example 4 with JCacheManager

use of org.infinispan.jcache.embedded.JCacheManager in project infinispan by infinispan.

the class JCacheConfigurationTest method testJCacheManagerWithRealJarFileSchema.

public void testJCacheManagerWithRealJarFileSchema() throws Exception {
    File sampleJarWithResourceFile = null;
    ClassLoader originalClassLoader = null;
    try {
        // given - creating sample jar file
        String tmpDir = CommonsTestingUtil.tmpDirectory(JCacheConfigurationTest.class);
        File tmpDirFile = new File(tmpDir);
        if (!tmpDirFile.exists()) {
            tmpDirFile.mkdirs();
        }
        String sampleJarWithResourcePathString = Paths.get(tmpDir, "sampleJarWithResource.jar").toString();
        sampleJarWithResourceFile = new File(sampleJarWithResourcePathString);
        String existingResourceToAddInJar = "infinispan_uri.xml";
        String targetPathInJar = "sample-dir-inside-jar/";
        createJar(sampleJarWithResourceFile, existingResourceToAddInJar, targetPathInJar);
        // given - adding jar file into classpath
        String fullTargetPath = targetPathInJar + existingResourceToAddInJar;
        originalClassLoader = Thread.currentThread().getContextClassLoader();
        MyClassLoader myClassLoader = new MyClassLoader(new URL[] {}, originalClassLoader);
        myClassLoader.addURL(new URI("jar:" + sampleJarWithResourceFile.toURI() + "!/").toURL());
        Thread.currentThread().setContextClassLoader(myClassLoader);
        // when
        URI resourceInsideJarUri = new URI("jar:" + sampleJarWithResourceFile.toURI() + "!" + fullTargetPath);
        withCachingProvider(provider -> {
            try (JCacheManager jCacheManager = new JCacheManager(resourceInsideJarUri, provider.getClass().getClassLoader(), provider, getProperties())) {
                // then
                assertNotNull(jCacheManager.getCache("foo"));
            }
        });
    } finally {
        // cleanup
        if (sampleJarWithResourceFile != null && sampleJarWithResourceFile.exists()) {
            sampleJarWithResourceFile.delete();
        }
        if (originalClassLoader != null) {
            Thread.currentThread().setContextClassLoader(originalClassLoader);
        }
    }
}
Also used : JCacheManager(org.infinispan.jcache.embedded.JCacheManager) URLClassLoader(java.net.URLClassLoader) File(java.io.File) URI(java.net.URI)

Example 5 with JCacheManager

use of org.infinispan.jcache.embedded.JCacheManager in project infinispan by infinispan.

the class JCacheConfigurationTest method testProgrammaticGlobalConfiguration.

public void testProgrammaticGlobalConfiguration() throws Exception {
    URI uri = JCacheConfigurationTest.class.getClassLoader().getResource("infinispan_uri.xml").toURI();
    Properties properties = getProperties();
    GlobalConfigurationBuilder global = new GlobalConfigurationBuilder();
    global.addModule(PrivateGlobalConfigurationBuilder.class);
    properties.put(GlobalConfigurationBuilder.class.getName(), global);
    withCachingProvider(provider -> {
        try (JCacheManager jCacheManager = new JCacheManager(uri, provider.getClass().getClassLoader(), provider, properties)) {
            DefaultCacheManager defaultCacheManager = jCacheManager.unwrap(DefaultCacheManager.class);
            assertNotNull(defaultCacheManager.getCacheManagerConfiguration().module(PrivateGlobalConfiguration.class));
        }
    });
}
Also used : PrivateGlobalConfigurationBuilder(org.infinispan.configuration.internal.PrivateGlobalConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) DefaultCacheManager(org.infinispan.manager.DefaultCacheManager) PrivateGlobalConfiguration(org.infinispan.configuration.internal.PrivateGlobalConfiguration) JCacheManager(org.infinispan.jcache.embedded.JCacheManager) Properties(java.util.Properties) URI(java.net.URI)

Aggregations

JCacheManager (org.infinispan.jcache.embedded.JCacheManager)14 URI (java.net.URI)6 MutableConfiguration (javax.cache.configuration.MutableConfiguration)5 Properties (java.util.Properties)4 Cache (javax.cache.Cache)4 AdvancedCache (org.infinispan.AdvancedCache)4 PrivateGlobalConfiguration (org.infinispan.configuration.internal.PrivateGlobalConfiguration)4 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)3 PrivateGlobalConfigurationBuilder (org.infinispan.configuration.internal.PrivateGlobalConfigurationBuilder)3 InMemoryJCacheLoader (org.infinispan.jcache.util.InMemoryJCacheLoader)3 DefaultCacheManager (org.infinispan.manager.DefaultCacheManager)3 CacheManagerCallable (org.infinispan.test.CacheManagerCallable)3 File (java.io.File)2 URLClassLoader (java.net.URLClassLoader)2 ArrayList (java.util.ArrayList)2 CacheManager (javax.cache.CacheManager)2 MutableEntry (javax.cache.processor.MutableEntry)2 Configuration (org.infinispan.configuration.cache.Configuration)2 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)2 ConfigurationBuilderHolder (org.infinispan.configuration.parsing.ConfigurationBuilderHolder)2