protected byte[] marshall(Object source) throws IOException, InterruptedException { return marshaller.objectToByteBuffer(source); } }
public Schema retrieveSchema(String name) throws IOException, InterruptedException, ClassNotFoundException { if (!knownSchemas.containsKey(name)) { byte[] key = marshaller.objectToByteBuffer(name); byte[] value = (byte[]) cacheManager.getCache(Support.AVRO_METADATA_CACHE_NAME).get(key); if (value==null) throw new IOException(name+" not found in the metadata cache"); Schema schema = (Schema) marshaller.objectFromByteBuffer(value); knownSchemas.put(name, schema); log.info("adding schama "+name+" to metadata cache"); } return knownSchemas.get(name); }
@Override public Object encodeKey(Object key) throws CodecException { if (key != null) { try { return marshaller.objectToByteBuffer(key); } catch (Exception e) { throw log.keyEncodingFailed(e, this.getName()); } } else { return null; } }
@Override public Object encodeValue(Object value) throws CodecException { if (value != null) { try { return marshaller.objectToByteBuffer(value); } catch (Exception e) { throw log.valueEncodingFailed(e, this.getName()); } } else { return null; } }
/** * Clones parameter x of type T with a given Marshaller reference; * * * @return a deep clone of an object parameter x */ @SuppressWarnings("unchecked") public static <T> T cloneWithMarshaller(Marshaller marshaller, T x){ if (marshaller == null) throw new IllegalArgumentException("Cannot use null Marshaller for clone"); byte[] byteBuffer; try { byteBuffer = marshaller.objectToByteBuffer(x); return (T) marshaller.objectFromByteBuffer(byteBuffer); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CacheException(e); } catch (Exception e) { throw new CacheException(e); } }
@Override public Object encodeValue(Object value) throws CodecException { if (value != null) { try { return getMarshaller().objectToByteBuffer(value); } catch (Exception e) { throw log.valueEncodingFailed(e, this.getName()); } } else { return null; } }
public static byte[] getRealKeyStored(String key, RemoteCache rc) throws Exception { Marshaller m = getMarshallerField((RemoteCacheImpl) rc); return m.objectToByteBuffer(key, 64); }
@Override public Object encodeKey(Object key) throws CodecException { if (key != null) { try { return getMarshaller().objectToByteBuffer(key); } catch (Exception e) { throw log.keyEncodingFailed(e, this.getName()); } } else { return null; } }
@Test protected void testRegisterSerializersAreUtilised() throws Exception { TestObject obj = testObjects.get(0); byte[] bytes = marshaller.objectToByteBuffer(obj); assert marshaller.objectFromByteBuffer(bytes).equals(obj); checkCustomSerializerCounters(1, 1); }
/** * Converts a java object to a sequence of bytes applying standard java serialization. * * @param source source the java object to convert. * @param sourceMediaType the MediaType matching application/x-application-object describing the source. * @return byte[] representation of the java object. * @throws EncodingException if the sourceMediaType is not a application/x-java-object or if the conversion is * not supported. */ public static byte[] convertJavaToOctetStream(Object source, MediaType sourceMediaType, Marshaller marshaller) throws IOException, InterruptedException { if (source == null) return null; if (!sourceMediaType.match(MediaType.APPLICATION_OBJECT)) { throw new EncodingException("destination MediaType not conforming to application/x-java-object!"); } Object decoded = decodeObjectContent(source, sourceMediaType); if (decoded instanceof byte[]) return (byte[]) decoded; if (decoded instanceof String && isJavaString(sourceMediaType)) return ((String) decoded).getBytes(StandardCharsets.UTF_8); return marshaller.objectToByteBuffer(source); }
public String getStoredKey(RemoteCache cache, String key) throws IOException, InterruptedException { // 1. marshall the key // 2. encode it with base64 (that's what DefaultTwoWayKey2StringMapper does) // 3. prefix it with 8 (again, done by DefaultTwoWayKey2StringMapper to mark the key as wrapped byte array type) // 4. prefix it with UTF-16 BOM (that is what DefaultTwoWayKey2StringMapper does for non string values) return '\uFEFF' + "8" + Base64.getEncoder().encodeToString(cache.getRemoteCacheManager().getMarshaller().objectToByteBuffer(key)); }
@Test protected void testImmutableCollections() throws Exception { int listSize = 10; TestObject obj = testObjects.get(0); obj.setList(Collections.unmodifiableList(IntStream.range(0, 10).boxed().collect(Collectors.toList()))); byte[] bytes = marshaller.objectToByteBuffer(obj); TestObject unmarshalledObj = (TestObject) marshaller.objectFromByteBuffer(bytes); assert unmarshalledObj.getList().size() == listSize; for (int i = 0; i < listSize; i++) assert unmarshalledObj.getList().get(i) == i; }
@Override public Object transcode(Object content, MediaType contentType, MediaType destinationType) { try { if (destinationType.equals(MediaType.APPLICATION_UNKNOWN) || contentType.equals(MediaType.APPLICATION_UNKNOWN)) { return content; } if (destinationType.match(marshaller.mediaType())) { return marshaller.objectToByteBuffer(content); } if (destinationType.match(MediaType.APPLICATION_OBJECT)) { return marshaller.objectFromByteBuffer((byte[]) content); } } catch (InterruptedException | IOException | ClassNotFoundException e) { throw new CacheException(e); } throw logger.unsupportedContent(content); }
@Override public void call() throws IOException, InterruptedException { cm.getClassWhiteList().addClasses(Person.class); Cache<String, Person> cache = cm.getCache(); Marshaller marshaller = cache.getAdvancedCache().getComponentRegistry().getCacheMarshaller(); Person value = new Person(); cache.put("1", value); // Read using default valueEncoder assertEquals(cache.get("1"), value); // Read unencoded Cache<?, ?> unencodedCache = cache.getAdvancedCache().withEncoding(IdentityEncoder.class); assertEquals(unencodedCache.get(marshaller.objectToByteBuffer("1")), marshaller.objectToByteBuffer(value)); } });
WrappedBytes keyWB = new WrappedByteArray(marshaller.objectToByteBuffer(key));
@Test public void testRestPutEmbeddedMemcachedHotRodGetTest() throws Exception { final String key = "3"; final Object value = "<hey>ho</hey>"; final Marshaller marshaller = cacheFactory.getMarshaller(); // 1. Put with REST byte[] bytes = marshaller.objectToByteBuffer(value); EntityEnclosingMethod put = new PutMethod(cacheFactory.getRestUrl() + "/" + key); put.setRequestEntity(new ByteArrayRequestEntity(bytes, marshaller.mediaType().toString())); HttpClient restClient = cacheFactory.getRestClient(); restClient.executeMethod(put); assertEquals(HttpStatus.SC_OK, put.getStatusCode()); assertEquals("", put.getResponseBodyAsString().trim()); // 2. Get with Embedded (given a marshaller, it can unmarshall the result) assertEquals(value, cacheFactory.getEmbeddedCache().get(key)); // 3. Get with Memcached (given a marshaller, it can unmarshall the result) assertEquals(value, cacheFactory.getMemcachedClient().get(key)); // 4. Get with Hot Rod assertEquals(value, cacheFactory.getHotRodCache().get(key)); } }