HDDS-1176. Allow persisting X509CertImpl to SCM certificate table. Contributed by Xiaoyu Yao.

This commit is contained in:
Ajay Kumar 2019-02-27 10:40:03 -08:00
parent 29e27faf96
commit fef2e2fc88
1 changed files with 31 additions and 9 deletions

View File

@ -44,16 +44,13 @@ public class CodecRegistry {
* @param <T> Type of the return value. * @param <T> Type of the return value.
* @return the object with the parsed field data * @return the object with the parsed field data
*/ */
public <T> T asObject(byte[] rawData, Class<T> format) throws IOException { public <T> T asObject(byte[] rawData, Class<T> format)
throws IOException {
if (rawData == null) { if (rawData == null) {
return null; return null;
} }
if (valueCodecs.containsKey(format)) { Codec codec = getCodec(format);
return (T) valueCodecs.get(format).fromPersistedFormat(rawData); return (T) codec.fromPersistedFormat(rawData);
} else {
throw new IllegalStateException(
"Codec is not registered for type: " + format);
}
} }
/** /**
@ -66,14 +63,39 @@ public class CodecRegistry {
public <T> byte[] asRawData(T object) throws IOException { public <T> byte[] asRawData(T object) throws IOException {
Preconditions.checkNotNull(object, Preconditions.checkNotNull(object,
"Null value shouldn't be persisted in the database"); "Null value shouldn't be persisted in the database");
Class<T> format = (Class<T>) object.getClass(); Codec<T> codec = getCodec(object);
if (valueCodecs.containsKey(format)) {
Codec<T> codec = (Codec<T>) valueCodecs.get(format);
return codec.toPersistedFormat(object); return codec.toPersistedFormat(object);
}
/**
* Get codec for the typed object including class and subclass.
* @param object typed object.
* @return Codec for the typed object.
* @throws IOException
*/
private <T> Codec getCodec(T object) throws IOException {
Class<T> format = (Class<T>) object.getClass();
return getCodec(format);
}
/**
* Get codec for the typed object including class and subclass.
* @param <T> Type of the typed object.
* @return Codec for the typed object.
* @throws IOException
*/
private <T> Codec getCodec(Class<T> format) throws IOException {
Codec<T> codec;
if (valueCodecs.containsKey(format)) {
codec = (Codec<T>) valueCodecs.get(format);
} else if (valueCodecs.containsKey(format.getSuperclass())) {
codec = (Codec<T>) valueCodecs.get(format.getSuperclass());
} else { } else {
throw new IllegalStateException( throw new IllegalStateException(
"Codec is not registered for type: " + format); "Codec is not registered for type: " + format);
} }
return codec;
} }
/** /**