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));
}
});
}
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");
}
});
}
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));
}
}
}
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));
}
}
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);
}
});
}
Aggregations