use of org.infinispan.commons.marshall.WrappedByteArray in project indy by Commonjava.
the class TrackingKey2StringMapperTest method testMapper.
@Test
public void testMapper() throws IOException {
TrackingKey2StringMapper mapper = new TrackingKey2StringMapper();
TrackingKey key = new TrackingKey(TEST_ID);
Assert.assertTrue(mapper.isSupportedType(TrackingKey.class));
Assert.assertFalse(mapper.isSupportedType(Object.class));
Assert.assertFalse(mapper.isSupportedType(TrackedContent.class));
Assert.assertTrue(mapper.isSupportedType(WrappedByteArray.class));
Object genKey = mapper.getKeyMapping(TEST_ID);
Assert.assertEquals(key, genKey);
String genId = mapper.getStringMapping(key);
Assert.assertEquals(TEST_ID, genId);
WrappedByteArray array = bytesOfTrackingKey(key);
genId = mapper.getStringMapping(array);
Assert.assertEquals(TEST_ID, genId);
}
use of org.infinispan.commons.marshall.WrappedByteArray in project indy by Commonjava.
the class TrackingKey2StringMapperTest method bytesOfTrackingKey.
private WrappedByteArray bytesOfTrackingKey(TrackingKey key) throws IOException {
byte[] bytes = null;
try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(byteOut)) {
key.writeExternal(stream);
bytes = byteOut.toByteArray();
return new WrappedByteArray(bytes, Arrays.hashCode(bytes));
}
}
use of org.infinispan.commons.marshall.WrappedByteArray in project indy by Commonjava.
the class AbstractIndyKey2StringMapper method getStringMapping.
@Override
public String getStringMapping(Object key) {
Class<T> typeClass = provideKeyClass();
if (typeClass == null) {
return null;
}
Object keyObj = key;
if (keyObj != null) {
if (keyObj.getClass().isAssignableFrom(typeClass)) {
return getStringMappingFromInst(key);
} else if (keyObj instanceof WrappedByteArray) {
try (ObjectInputStream objStream = new ObjectInputStream(new ByteArrayInputStream(((WrappedByteArray) keyObj).getBytes()))) {
T newKey = typeClass.newInstance();
if (newKey instanceof Externalizable) {
Externalizable deSerKey = (Externalizable) newKey;
deSerKey.readExternal(objStream);
return getStringMappingFromInst(newKey);
} else {
keyObj = objStream.readObject();
if (keyObj.getClass().isAssignableFrom(provideKeyClass())) {
return getStringMappingFromInst(keyObj);
}
}
} catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
LOGGER.error("JDBC store error: Cannot deserialize key of WrappedByteArray, error is {}: {}", e.getClass(), e.getMessage());
}
}
}
LOGGER.error("JDBC store error: Not supported key type {}", keyObj == null ? null : keyObj.getClass());
return null;
}
Aggregations