Search in sources :

Example 1 with WrappedByteArray

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);
}
Also used : WrappedByteArray(org.infinispan.commons.marshall.WrappedByteArray) TrackedContent(org.commonjava.indy.folo.model.TrackedContent) TrackingKey2StringMapper(org.commonjava.indy.folo.data.idxmodel.TrackingKey2StringMapper) TrackingKey(org.commonjava.indy.folo.model.TrackingKey) Test(org.junit.Test)

Example 2 with WrappedByteArray

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));
    }
}
Also used : WrappedByteArray(org.infinispan.commons.marshall.WrappedByteArray) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 3 with WrappedByteArray

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;
}
Also used : WrappedByteArray(org.infinispan.commons.marshall.WrappedByteArray) ByteArrayInputStream(java.io.ByteArrayInputStream) Externalizable(java.io.Externalizable) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

WrappedByteArray (org.infinispan.commons.marshall.WrappedByteArray)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Externalizable (java.io.Externalizable)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 TrackingKey2StringMapper (org.commonjava.indy.folo.data.idxmodel.TrackingKey2StringMapper)1 TrackedContent (org.commonjava.indy.folo.model.TrackedContent)1 TrackingKey (org.commonjava.indy.folo.model.TrackingKey)1 Test (org.junit.Test)1