Search in sources :

Example 1 with EncoderRegistry

use of org.infinispan.marshall.core.EncoderRegistry in project infinispan by infinispan.

the class DataConversionTest method testReadUnencoded.

@Test
public void testReadUnencoded() {
    ConfigurationBuilder cfg = new ConfigurationBuilder();
    cfg.memory().storage(StorageType.OFF_HEAP);
    withCacheManager(new CacheManagerCallable(createCacheManager(TestDataSCI.INSTANCE, cfg)) {

        private final EncoderRegistry registry = TestingUtil.extractGlobalComponent(cm, EncoderRegistry.class);

        public Object asStored(Object object) {
            return registry.convert(object, APPLICATION_OBJECT, APPLICATION_PROTOSTREAM);
        }

        @Override
        public void call() {
            cm.getClassAllowList().addClasses(Person.class);
            Cache<String, Person> cache = cm.getCache();
            Person value = new Person();
            cache.put("1", value);
            // Read using default valueEncoder
            assertEquals(cache.get("1"), value);
            // Read unencoded
            Cache<?, ?> unencodedCache = cache.getAdvancedCache().withStorageMediaType();
            assertEquals(unencodedCache.get(asStored("1")), asStored(value));
        }
    });
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) CacheManagerCallable(org.infinispan.test.CacheManagerCallable) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) Person(org.infinispan.test.data.Person) Cache(org.infinispan.Cache) AdvancedCache(org.infinispan.AdvancedCache) Test(org.testng.annotations.Test) AbstractInfinispanTest(org.infinispan.test.AbstractInfinispanTest)

Example 2 with EncoderRegistry

use of org.infinispan.marshall.core.EncoderRegistry in project infinispan by infinispan.

the class DataConversionTest method testTranscoding.

@Test
@SuppressWarnings("unchecked")
public void testTranscoding() {
    ConfigurationBuilder cfg = new ConfigurationBuilder();
    cfg.encoding().key().mediaType(MediaType.APPLICATION_OBJECT_TYPE);
    cfg.encoding().value().mediaType(MediaType.APPLICATION_OBJECT_TYPE);
    withCacheManager(new CacheManagerCallable(createCacheManager(TestDataSCI.INSTANCE, cfg)) {

        @Override
        public void call() {
            Cache<String, Map<String, String>> cache = cm.getCache();
            EncoderRegistry encoderRegistry = TestingUtil.extractGlobalComponent(cm, EncoderRegistry.class);
            encoderRegistry.registerTranscoder(new ObjectXMLTranscoder());
            // Store a map in the cache
            Map<String, String> valueMap = new HashMap<>();
            valueMap.put("BTC", "Bitcoin");
            valueMap.put("ETH", "Ethereum");
            valueMap.put("LTC", "Litecoin");
            cache.put("CoinMap", valueMap);
            assertEquals(valueMap, cache.get("CoinMap"));
            // Obtain the value with a different MediaType
            AdvancedCache<String, String> xmlCache = cache.getAdvancedCache().withMediaType(APPLICATION_OBJECT, APPLICATION_XML);
            assertEquals(xmlCache.get("CoinMap"), "<root><BTC>Bitcoin</BTC><ETH>Ethereum</ETH><LTC>Litecoin</LTC></root>");
            // Reading with same configured MediaType should not change content
            assertEquals(xmlCache.withMediaType(APPLICATION_OBJECT, APPLICATION_OBJECT).get("CoinMap"), valueMap);
            // Writing using XML
            xmlCache.put("AltCoinMap", "<root><CAT>Catcoin</CAT><DOGE>Dogecoin</DOGE></root>");
            // Read using object from undecorated cache
            Map<String, String> map = cache.get("AltCoinMap");
            assertEquals(map.get("CAT"), "Catcoin");
            assertEquals(map.get("DOGE"), "Dogecoin");
        }
    });
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) CacheManagerCallable(org.infinispan.test.CacheManagerCallable) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) AdvancedCache(org.infinispan.AdvancedCache) Map(java.util.Map) HashMap(java.util.HashMap) Cache(org.infinispan.Cache) AdvancedCache(org.infinispan.AdvancedCache) Test(org.testng.annotations.Test) AbstractInfinispanTest(org.infinispan.test.AbstractInfinispanTest)

Example 3 with EncoderRegistry

use of org.infinispan.marshall.core.EncoderRegistry in project infinispan by infinispan.

the class EndpointsCacheFactory method createEmbeddedCache.

private void createEmbeddedCache() {
    GlobalConfigurationBuilder globalBuilder;
    if (cacheMode.isClustered()) {
        globalBuilder = new GlobalConfigurationBuilder();
        globalBuilder.transport().defaultTransport();
    } else {
        globalBuilder = new GlobalConfigurationBuilder().nonClusteredDefault();
    }
    globalBuilder.addModule(PrivateGlobalConfigurationBuilder.class).serverMode(true);
    globalBuilder.defaultCacheName(cacheName);
    if (contextInitializer != null)
        globalBuilder.serialization().addContextInitializer(contextInitializer);
    org.infinispan.configuration.cache.ConfigurationBuilder builder = new org.infinispan.configuration.cache.ConfigurationBuilder();
    builder.clustering().cacheMode(cacheMode).encoding().key().mediaType(MediaType.APPLICATION_OBJECT_TYPE).encoding().value().mediaType(MediaType.APPLICATION_OBJECT_TYPE);
    if (cacheMode.isDistributed() && numOwners != DEFAULT_NUM_OWNERS) {
        builder.clustering().hash().numOwners(numOwners);
    }
    if (cacheMode.isDistributed() && l1Enable) {
        builder.clustering().l1().enable();
    }
    cacheManager = cacheMode.isClustered() ? TestCacheManagerFactory.createClusteredCacheManager(globalBuilder, builder) : TestCacheManagerFactory.createCacheManager(globalBuilder, builder);
    embeddedCache = cacheManager.getCache(cacheName);
    EncoderRegistry encoderRegistry = embeddedCache.getAdvancedCache().getComponentRegistry().getGlobalComponentRegistry().getComponent(EncoderRegistry.class);
    if (marshaller != null) {
        boolean isConversionSupported = encoderRegistry.isConversionSupported(marshaller.mediaType(), APPLICATION_OBJECT);
        if (!isConversionSupported) {
            encoderRegistry.registerTranscoder(new TranscoderMarshallerAdapter(marshaller));
        }
    }
}
Also used : PrivateGlobalConfigurationBuilder(org.infinispan.configuration.internal.PrivateGlobalConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) PrivateGlobalConfigurationBuilder(org.infinispan.configuration.internal.PrivateGlobalConfigurationBuilder) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) ConfigurationBuilder(org.infinispan.client.hotrod.configuration.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) RestServerConfigurationBuilder(org.infinispan.rest.configuration.RestServerConfigurationBuilder) PrivateGlobalConfigurationBuilder(org.infinispan.configuration.internal.PrivateGlobalConfigurationBuilder) TranscoderMarshallerAdapter(org.infinispan.commons.dataconversion.TranscoderMarshallerAdapter) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry)

Example 4 with EncoderRegistry

use of org.infinispan.marshall.core.EncoderRegistry in project infinispan by infinispan.

the class JbossMarshallingModule method cacheManagerStarting.

@Override
public void cacheManagerStarting(GlobalComponentRegistry gcr, GlobalConfiguration globalConfiguration) {
    PERSISTENCE.jbossMarshallingDetected();
    Marshaller userMarshaller = globalConfiguration.serialization().marshaller();
    if (userMarshaller instanceof JBossUserMarshaller) {
        // Core automatically registers a transcoder for the user marshaller
        // Initialize the externalizers from the serialization configuration
        ((JBossUserMarshaller) userMarshaller).initialize(gcr);
    } else {
        // Register a JBoss Marshalling transcoder, ignoring any configured externalizers
        ClassAllowList classAllowList = gcr.getComponent(EmbeddedCacheManager.class).getClassAllowList();
        ClassLoader classLoader = globalConfiguration.classLoader();
        GenericJBossMarshaller jbossMarshaller = new GenericJBossMarshaller(classLoader, classAllowList);
        EncoderRegistry encoderRegistry = gcr.getComponent(EncoderRegistry.class);
        encoderRegistry.registerTranscoder(new JBossMarshallingTranscoder(jbossMarshaller));
    }
}
Also used : GenericJBossMarshaller(org.infinispan.jboss.marshalling.commons.GenericJBossMarshaller) JBossUserMarshaller(org.infinispan.jboss.marshalling.core.JBossUserMarshaller) GenericJBossMarshaller(org.infinispan.jboss.marshalling.commons.GenericJBossMarshaller) Marshaller(org.infinispan.commons.marshall.Marshaller) JBossMarshallingTranscoder(org.infinispan.jboss.marshalling.dataconversion.JBossMarshallingTranscoder) ClassAllowList(org.infinispan.commons.configuration.ClassAllowList) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) JBossUserMarshaller(org.infinispan.jboss.marshalling.core.JBossUserMarshaller)

Example 5 with EncoderRegistry

use of org.infinispan.marshall.core.EncoderRegistry in project infinispan by infinispan.

the class DataConversionTest method testObjectEncoder.

@Test
public void testObjectEncoder() {
    GenericJbossMarshallerEncoder encoder = new GenericJbossMarshallerEncoder(org.infinispan.dataconversion.DataConversionTest.class.getClassLoader());
    withCacheManager(new CacheManagerCallable(createCacheManager(new ConfigurationBuilder())) {

        GenericJBossMarshaller marshaller = new GenericJBossMarshaller();

        private byte[] marshall(Object o) {
            try {
                return marshaller.objectToByteBuffer(o);
            } catch (IOException | InterruptedException e) {
                throw new AssertionError("Cannot marshall content", e);
            }
        }

        @Override
        public void call() {
            GlobalComponentRegistry registry = cm.getGlobalComponentRegistry();
            EncoderRegistry encoderRegistry = registry.getComponent(EncoderRegistry.class);
            encoderRegistry.registerEncoder(encoder);
            cm.getClassAllowList().addClasses(Person.class);
            Cache<byte[], byte[]> cache = cm.getCache();
            // Write encoded content to the cache
            Person key1 = new Person("key1");
            Person value1 = new Person("value1");
            byte[] encodedKey1 = marshall(key1);
            byte[] encodedValue1 = marshall(value1);
            cache.put(encodedKey1, encodedValue1);
            // Read encoded content
            assertEquals(cache.get(encodedKey1), encodedValue1);
            // Read with a different valueEncoder
            AdvancedCache<Person, Person> encodingCache = (AdvancedCache<Person, Person>) cache.getAdvancedCache().withEncoding(GenericJbossMarshallerEncoder.class);
            assertEquals(encodingCache.get(key1), value1);
        }
    });
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalComponentRegistry(org.infinispan.factories.GlobalComponentRegistry) GenericJBossMarshaller(org.infinispan.jboss.marshalling.commons.GenericJBossMarshaller) CacheManagerCallable(org.infinispan.test.CacheManagerCallable) EncoderRegistry(org.infinispan.marshall.core.EncoderRegistry) AdvancedCache(org.infinispan.AdvancedCache) Person(org.infinispan.test.data.Person) Cache(org.infinispan.Cache) AdvancedCache(org.infinispan.AdvancedCache) Test(org.testng.annotations.Test)

Aggregations

EncoderRegistry (org.infinispan.marshall.core.EncoderRegistry)12 AdvancedCache (org.infinispan.AdvancedCache)5 Cache (org.infinispan.Cache)3 MediaType (org.infinispan.commons.dataconversion.MediaType)3 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)3 GlobalConfigurationBuilder (org.infinispan.configuration.global.GlobalConfigurationBuilder)3 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)3 CacheManagerCallable (org.infinispan.test.CacheManagerCallable)3 Test (org.testng.annotations.Test)3 Arrays (java.util.Arrays)2 Map (java.util.Map)2 Optional (java.util.Optional)2 ClassAllowList (org.infinispan.commons.configuration.ClassAllowList)2 Transcoder (org.infinispan.commons.dataconversion.Transcoder)2 GenericJBossMarshaller (org.infinispan.jboss.marshalling.commons.GenericJBossMarshaller)2 AbstractInfinispanTest (org.infinispan.test.AbstractInfinispanTest)2 Person (org.infinispan.test.data.Person)2 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1 RemovalListener (com.github.benmanes.caffeine.cache.RemovalListener)1 Closeable (java.io.Closeable)1