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); }
public TranscoderMarshallerAdapter(Marshaller marshaller) { super(marshaller.mediaType(), MediaType.APPLICATION_OBJECT, MediaType.APPLICATION_UNKNOWN); this.marshaller = marshaller; }
protected Object unmarshall(byte[] source) throws IOException, ClassNotFoundException { return marshaller.objectFromByteBuffer(source); }
@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); }
protected byte[] marshall(Object source) throws IOException, InterruptedException { return marshaller.objectToByteBuffer(source); } }
@Test protected void testSerializingByteArrays() throws Exception { byte[] bytes = new byte[10]; IntStream.range(0, 10).forEach(i -> bytes[i] = (byte) i); byte[] marshalledBytes = marshaller.objectToBuffer(bytes).getBuf(); byte[] unmarsalledBytes = (byte[]) marshaller.objectFromByteBuffer(marshalledBytes); IntStream.range(0, 10).forEach(i -> assertEquals(unmarsalledBytes[i], i)); } }
@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)); } }
@Override public Object fromStorage(Object stored) { try { return stored instanceof byte[] ? marshaller.objectFromByteBuffer((byte[]) stored) : stored; } catch (IOException | ClassNotFoundException e) { throw new CacheException(e); } }
@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; } }
@Test protected void testObjectMarshallingTest() throws Exception { List<ByteBuffer> serializedObjects = new ArrayList<>(testObjects.size()); for (TestObject object : testObjects) { serializedObjects.add(marshaller.objectToBuffer(object)); } assert serializedObjects.size() == testObjects.size(); for (int i = 0; i < testObjects.size(); i++) { byte[] bytes = serializedObjects.get(i).getBuf(); Object testObj = testObjects.get(i); Object unmarshalledObj = marshaller.objectFromByteBuffer(bytes); assert testObj.equals(unmarshalledObj); } }
/** * 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); } }
private Object unmarshall(Object key) { try { return marshaller.objectFromByteBuffer((byte[]) key); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } return key; }
@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; } }
void setEventMarshaller(Optional<Marshaller> eventMarshaller) { eventMarshaller.ifPresent(m -> { TranscoderMarshallerAdapter adapter = new TranscoderMarshallerAdapter(m); if (encoderRegistry.isConversionSupported(MediaType.APPLICATION_OBJECT, m.mediaType())) { log.skippingMarshallerWrapping(m.mediaType().toString()); } else { encoderRegistry.registerTranscoder(adapter); } }); }
@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); }
@Override public Object decodeKey(Object key) throws CodecException { if (key != null) { try { return marshaller.objectFromByteBuffer((byte[]) key); } catch (Exception e) { throw log.keyDecodingFailed(e, this.getName()); } } else { return null; } }
@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; } }
@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 decodeValue(Object value) throws CodecException { if (value != null) { try { return marshaller.objectFromByteBuffer((byte[]) value); } catch (Exception e) { throw log.valueDecodingFailed(e, this.getName()); } } else { return null; } }
@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; } }