co.cask.cdap.logging.serialize.ThrowableProxyArraySerializer Maven / Gradle / Ivy
/*
* Copyright © 2014-2017 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package co.cask.cdap.logging.serialize;
import ch.qos.logback.classic.spi.IThrowableProxy;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericArray;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
/**
* Serializer for ThrowableProxyArray.
*/
final class ThrowableProxyArraySerializer {
private ThrowableProxyArraySerializer() {}
static GenericArray encode(Schema schema, IThrowableProxy[] throwableProxies) {
if (throwableProxies != null) {
Schema tpArraySchema = schema.getTypes().get(1);
GenericArray steArray = new GenericData.Array<>(throwableProxies.length,
tpArraySchema);
for (IThrowableProxy tp : throwableProxies) {
steArray.add(ThrowableProxySerializer.encode(tpArraySchema.getElementType(), tp));
}
return steArray;
}
return null;
}
static IThrowableProxy[] decode(GenericArray datum) {
if (datum != null) {
IThrowableProxy[] throwableProxies = new IThrowableProxy[datum.size()];
int i = 0;
for (GenericRecord aDatum : datum) {
throwableProxies[i++] = ThrowableProxySerializer.decode(aDatum);
}
return throwableProxies;
}
return null;
}
}